2010-01-16 104 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