2013-12-15 5 views
1

У меня есть кусок кода, который загружает текстуры, используя glTexImage2D так:OpenGL ES 2.0 - Загрузить PNG с GLKit вместо OpenGL API

// 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

Код после 'GLKTextureLoader' вызова выглядит подозреваемого. Что произойдет, если вы отбросите все это, кроме 'glBindTexture (texture.target, texture.name)'? – rickster

+0

@rickster Я удалил строки после GLKTextureLoader и добавил вызов glBindTexture в glkView: drawInRect: метод, который, кажется, решает проблему – rraallvv

ответ

1

pathForResource: @ "Blob.png" OfType: Nil

Должно быть pathForResource: @ "клякса" OfType: @ "PNG"

И большую часть времени вы должны использовать ноль вместо этого, если Nil

+0

Я внес изменения, но все равно получаю черное изображение – rraallvv

+0

Это то, что я могу сказать , Остальная часть opengl находится вне меня на моем iPhone на данный момент. Сожалею. – uchuugaka

1

Это, как я ее решил:

Это идет в код OpenGL инициализации

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

И это идет в методе визуализации, в этом случае glkView:drawInRect:

Смежные вопросы