2013-12-15 4 views
1

나는이 같은 glTexImage2D를 사용하여 텍스처로드 코드 조각이 :이와 코드를 대체 는 OpenGL ES 2.0 - 대신의 OpenGL API의 GLKit와 PNG를로드

// to test texturing 
CGImageRef imageRef = [[UIImage imageNamed:@"Blob.png"] CGImage]; 
int width = CGImageGetWidth(imageRef); 
int height = CGImageGetHeight(imageRef); 
GLubyte* textureData = (GLubyte *)malloc(width * height * 4); // if 4 components per pixel (RGBA) 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(textureData, width, height, 
              bitsPerComponent, bytesPerRow, colorSpace, 
              kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 

CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextRelease(context); 

glActiveTexture(GL_TEXTURE0); 
glGenTextures(1, &texId); 
glBindTexture(GL_TEXTURE_2D, texId); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); 

glBindTexture(GL_TEXTURE_2D, 0); 

이 코드는 오류를 발생 아무튼를 대신에 검은 색 이미지를 보여줍니다.

NSError *error; 
GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Blob.png" ofType:Nil] 
                   options:Nil 
                   error:&error]; assert(!error); 

//bind the texture to texture unit 0 
glActiveTexture(GL_TEXTURE0 + 0); 
glBindTexture(texture.target, texture.name); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glEnable(texture.target); 

glBindTexture(GL_TEXTURE_2D, 0); 

무엇이 문제 일 수 있습니까?

내가 포트에 원하는 프로젝트는 https://github.com/glman74/simpleFBO

+0

코드에

NSError *error; GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Blob.png" ofType:Nil] options:Nil error:&error]; 

가 그리고 이것은 렌더링 방법에가는 OpenGL을 초기화 코드는 의심을 보인다. 'glBindTexture (texture.target, texture.name)'을 제외하고 그걸 모두 버리면 어떻게됩니까? – rickster

+0

@rickster GLKTextureLoader 뒤에 줄을 제거하고 glkView : drawInRect : 메서드에서 glBindTexture에 대한 호출을 추가하여 문제를 해결할 수 있습니다. – rraallvv

답변

1

pathForResource에 있습니다 : @ "물방울"ofType :

이 pathForResource이어야한다 무기 호

"Blob.png"ofType @ "PNG"

@

그리고 대부분의 시간을 당신은 무기 호

+0

변경 사항을 적용했지만 여전히 검은 색 이미지가 나타납니다. – rraallvv

+0

이것이 내가 말할 수있는 범위입니다 . OpenGL 부분의 나머지 부분은 지금 내 iPhone에서 나와 있습니다. 죄송합니다. – uchuugaka

1

이 내가 그것을 해결 한 방법입니다 대신하는 경우 nil을 사용해야합니다

이 간다 은`GLKTextureLoader` 호출 후이 경우 glkView:drawInRect:

glBindTexture(GL_TEXTURE_2D, 0);