2011-08-03 103 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