2010-01-16 109 views
3

我正在使用可可(我在iPhone上使用过OpenGL ES)的第一个OpenGL应用程序,并且无法从图像文件中加载纹理。这里是我的纹理加载代码:可可OpenGL纹理创建

@interface MyOpenGLView : NSOpenGLView 
{ 
GLenum texFormat[ 1 ]; // Format of texture (GL_RGB, GL_RGBA) 
NSSize texSize[ 1 ];  // Width and height 

GLuint textures[1];  // Storage for one texture 
} 

- (BOOL) loadBitmap:(NSString *)filename intoIndex:(int)texIndex 
{ 
BOOL success = FALSE; 
NSBitmapImageRep *theImage; 
int bitsPPixel, bytesPRow; 
unsigned char *theImageData; 

NSData* imgData = [NSData dataWithContentsOfFile:filename options:NSUncachedRead error:nil]; 

theImage = [NSBitmapImageRep imageRepWithData:imgData]; 

if(theImage != nil) 
{ 
    bitsPPixel = [theImage bitsPerPixel]; 
    bytesPRow = [theImage bytesPerRow]; 
    if(bitsPPixel == 24)  // No alpha channel 
     texFormat[texIndex] = GL_RGB; 
    else if(bitsPPixel == 32) // There is an alpha channel 
     texFormat[texIndex] = GL_RGBA; 
    texSize[texIndex].width = [theImage pixelsWide]; 
    texSize[texIndex].height = [theImage pixelsHigh]; 

    if(theImageData != NULL) 
    { 
     NSLog(@"Good so far..."); 
     success = TRUE; 

     // Create the texture 

     glGenTextures(1, &textures[texIndex]); 

     NSLog(@"tex: %i", textures[texIndex]); 

     NSLog(@"%i", glIsTexture(textures[texIndex])); 

     glPixelStorei(GL_UNPACK_ROW_LENGTH, [theImage pixelsWide]); 

     glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

     // Typical texture generation using data from the bitmap 
     glBindTexture(GL_TEXTURE_2D, textures[texIndex]); 

     NSLog(@"%i", glIsTexture(textures[texIndex])); 

     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texSize[texIndex].width, texSize[texIndex].height, 0, texFormat[texIndex], GL_UNSIGNED_BYTE, [theImage bitmapData]); 

     NSLog(@"%i", glIsTexture(textures[texIndex])); 
    } 
} 


return success; 
} 

看来,glGenTextures()函数并不实际创建纹理因为textures[0]保持为0。另外,登录glIsTexture(textures[texIndex])始终返回false。

有什么建议吗?

感谢,

凯尔

回答

0

好吧,我想通了。事实证明,我在设置我的上下文之前试图加载纹理。一旦我在初始化方法的末尾加载纹理,它就可以正常工作。

感谢您的答案。

Kyle

1
glGenTextures(1, &textures[texIndex]); 

你是什么textures定义是什么?

glIsTexture仅当纹理已经准备就绪时返回true。 glGenTextures返回的名称,但尚未通过调用glBindTexture与纹理关联的名称不是纹理的名称。

检查glGenTextures是否在glBegin和glEnd之间意外执行 - 这是唯一的官方失败原因。

另外:

检查纹理呈正方形,其尺寸是2

电源虽然没有强调任何地方不够iPhone的OpenGL ES实现需要他们是这样的。

+0

它被定义为'GLuint纹理[1]'(我更新了我的文章以包含标题定义)。我在调用glBindTexture()后也检查'glIsTexture()',它仍然返回false。它不会在'glBegin()'和'glEnd()'之间调用,它会在定时器启动之前在初始化方法中调用。纹理是一个256x256 PNG文件。 – Kyle 2010-01-16 07:47:46

+0

这实际上是错误的。 glIsTexture将为任何已绑定的纹理名称返回true。它不一定是“准备就绪”(在OpenGL中称为complete)。另外,我很惊讶iPhone的纹理必须是方形的。 2的力量,是的...但方形?我很确定GL ES规范要求说32x16纹理才能工作。 – Bahbar 2010-01-16 08:36:27

+0

@Bahbar:http://www.opengl.org/sdk/docs/man/xhtml/glIsTexture.xml – 2010-01-16 11:19:13