2013-04-23 92 views
0

这是一个非常奇怪的问题。我使用从苹果示例复制的方法创建纹理。它在苹果样本中工作正常,但不在矿山项目中。纹理不显示,只有glcolor4f定义的颜色。我用glistexture和glgeterror来检查,他们说没有错。这只发生在我第一次加载纹理。如果我释放纹理并重新加载,它可以使用相同的代码。有没有其他的方法来检查OpenGL的错误?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; 

回答

0

你没有表现出任何的抽奖网站的相关代码。也许你在抽签时没有约束这个纹理。

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

+0

真的非常感谢您的回答。我不显示绘制代码是因为数据架构非常复杂。但是当我加载和绘制时,我会打印纹理名称,它也是一样的。我已经删除了一堆代码,根本没有任何改变。今天,我把这个项目带到另一个mac,它的工作正常......为什么......好吧,我将使用调试工具来检查它是否是xcode或我的hacintosh问题。真的非常感谢您的帮助。 – user1904689 2013-04-24 02:02:43