我正在使用可可(我在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。
有什么建议吗?
感谢,
凯尔
它被定义为'GLuint纹理[1]'(我更新了我的文章以包含标题定义)。我在调用glBindTexture()后也检查'glIsTexture()',它仍然返回false。它不会在'glBegin()'和'glEnd()'之间调用,它会在定时器启动之前在初始化方法中调用。纹理是一个256x256 PNG文件。 – Kyle 2010-01-16 07:47:46
这实际上是错误的。 glIsTexture将为任何已绑定的纹理名称返回true。它不一定是“准备就绪”(在OpenGL中称为complete)。另外,我很惊讶iPhone的纹理必须是方形的。 2的力量,是的...但方形?我很确定GL ES规范要求说32x16纹理才能工作。 – Bahbar 2010-01-16 08:36:27
@Bahbar:http://www.opengl.org/sdk/docs/man/xhtml/glIsTexture.xml – 2010-01-16 11:19:13