OpenGL纹理没有显示(使用cocoa)

时间:2013-04-23 21:09:43

标签: objective-c cocoa opengl textures

这是一个非常奇怪的问题。我使用从apple的样本中复制的方法创建纹理。它适用于苹果的样品,但不适用于我的项目。纹理没有显示,只有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;

1 个答案:

答案 0 :(得分:0)

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

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