gpt4 book ai didi

cocoa OpenGL 纹理创建

转载 作者:行者123 更新时间:2023-12-03 16:21:51 30 4
gpt4 key购买 nike

我正在使用 Cocoa 开发我的第一个 OpenGL 应用程序(我在 iPhone 上使用过 OpenGL ES),但我在从图像文件加载纹理时遇到问题。这是我的纹理加载代码:

@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。

有什么建议吗?

谢谢

凯尔

最佳答案

glGenTextures(1, &textures[texIndex] );

您的纹理定义是什么?

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

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

另外:

检查纹理是否为正方形且尺寸是否为 2 的幂。

尽管没有在任何地方强调这一点,但 iPhone 的 OpenGL ES 实现要求它们这样做。

关于 cocoa OpenGL 纹理创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2075784/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com