我正在使用可可(我在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