0

这是一个非常奇怪的问题。我使用从苹果示例复制的方法创建纹理。它在苹果的示例中运行良好,但在我的项目中却不行。没有显示纹理,只有 glcolor4f 定义的颜色。我用 glistexture 和 glgeterror 来检查,他们说那里没有错。这仅在我第一次加载纹理时发生。如果我释放纹理并重新加载它,它可以使用相同的代码。有没有其他方法可以检查 OpenGL 的错误?

这是我用来加载纹理的代码:

- (FETexture *)loadTextureWithPath:(NSString*)name{
NSURL    *url = nil;
CGImageSourceRef    src;
CGImageRef    image;
CGContextRef    context = nil;
CGColorSpaceRef    colorSpace;

GLuint textureId;
GLuint pboId;
GLubyte *data;

url = [NSURL fileURLWithPath: name];
src = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);

if (!src) {
    NSLog(@"No image");
    return nil;
}

image = CGImageSourceCreateImageAtIndex(src, 0, NULL);
CFRelease(src);

GLuint width = (GLint)CGImageGetWidth(image);
GLuint height = (GLint)CGImageGetHeight(image);

data = (GLubyte*) calloc(width * height * 4, sizeof(GLubyte));

colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
CGColorSpaceRelease(colorSpace);

// Core Graphics referential is upside-down compared to OpenGL referential
// Flip the Core Graphics context here
// An alternative is to use flipped OpenGL texture coordinates when drawing textures
CGContextTranslateCTM(context, 0.0, height);
CGContextScaleCTM(context, 1.0, -1.0);

// Set the blend mode to copy before drawing since the previous contents of memory aren't used. This avoids unnecessary blending.
CGContextSetBlendMode(context, kCGBlendModeCopy);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);

CGContextRelease(context);
CGImageRelease(image);

glGenTextures(1, &textureId);
glGenBuffers(1, &pboId);

// Bind the texture
glBindTexture(GL_TEXTURE_2D, textureId);

// Bind the PBO
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboId);

// Upload the texture data to the PBO
glBufferData(GL_PIXEL_UNPACK_BUFFER, width * height * 4 * sizeof(GLubyte), data, GL_STATIC_DRAW);

// Setup texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);

// OpenGL likes the GL_BGRA + GL_UNSIGNED_INT_8_8_8_8_REV combination
// Use offset instead of pointer to indictate that we want to use data copied from a PBO
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
             GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);

// We can delete the application copy of the texture data now
free(data);

glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

FETexture *texture = [FETexture new];

texture.textureID = textureId;
texture.bufferID = pboId;
texture.width = width;
texture.height = height;

return texture;
4

1 回答 1

0

您没有显示抽奖站点的任何相关代码。也许你没有在绘制时绑定这个纹理。

您可以在https://developer.apple.com/downloads/index.action的“Xcode 图形工具”中使用 OpenGL Profiler来调试 GL 错误和状态。

于 2013-04-23T21:38:55.890 回答