2011-08-03 74 views
0

最近开始使用OpenGL,我设法在屏幕和帧缓冲区上绘制背景图像。一切正常。当我创建新纹理并将其添加到帧缓冲区时出现问题。在openGL中绘制纹理触摸iphone

{

[EAGLContext setCurrentContext:context]; 

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
glViewport(0, 0, backingWidth, backingHeight); 
    // Clear screen 
glClear(GL_COLOR_BUFFER_BIT); 

// Render player ship 

    [playerShip drawAtPoint:CGPointMake(160, 240)]; 
// glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
// [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 

如果我去掉上面两行

// glBindRenderbufferOES(GL_RENDERBUFFER_OES,viewRenderbuffer);

// [context presentRenderbuffer:GL_RENDERBUFFER_OES];

我不得到新的纹理,我正在通过下面的代码绘制:

NSInteger的myDataLength = 20 * 20 * 4; GLubyte * buffer =(GLubyte *)malloc(myDataLength); glReadPixels(location.x-10,location.y-10,20,20,GL_RGBA,GL_UNSIGNED_BYTE,buffer);

// gl renders "upside down" so swap top to bottom into new array. 
// there's gotta be a better way, but this works. 
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); 
for(int y = 0; y < 20; y++) 
{ 
    for(int x = 0; x < 20 * 4; x++) 
    { 
     buffer2[(19 - y) * 20 * 4 + x] = buffer[y * 4 * 20 + x]; 
    } 
} 

// make data provider with data. 
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); 

// prep the ingredients 
int bitsPerComponent = 8; 
int bitsPerPixel = 32; 
int bytesPerRow = 4 * 20; 
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 

// make the cgimage 
CGImageRef imageRef = CGImageCreate(20, 20, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 

// then make the uiimage from that 
UIImage *myImage = [UIImage imageWithCGImage:imageRef]; 
Texture2D *texture = [[Texture2D alloc] initWithImage:myImage]; 
UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil); 

    glEnable(GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
glLoadIdentity(); 
[texture drawAtPoint:location]; 
glPopMatrix(); 

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 

glPushMatrix(); 
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
[context presentRenderbuffer:GL_RENDERBUFFER_OES]; 

我不知道我在做错误。我是新来的。

回答

0

如果您仍然在您的代码中渲染背景纹理,然后在其上渲染太空船,则太空船可能会使深度缓冲区测试失败。

您尝试过:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

清除z缓冲区,然后渲染宇宙飞船?

+0

由于我是openGL的新手,请详细说明你在说什么。我应该如何继续?我所做的是正确的方式继续?我正在尝试图像扭曲,就像photobooth – DivineDesert

+0

@Dimple OpenGL带有一个可选的深度缓冲区,用于在绘制3D场景时检测可见性(前景对象的z值比背景对象小,因此应用程序知道要绘制哪个像素)。当只渲染2D纹理(如照片)时,Z缓冲区可能会产生反效果,并最终阻止渲染新的纹理(深度缓冲区拼图)。禁用z缓冲区可能有帮助。但我不确定这是否是您的问题。也许发布更完整的示例可以提供帮助。 – KPK