在openGL iphone中触摸纹理

时间:2011-08-03 13:00:11

标签: iphone opengl-es-2.0

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

  

{

[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,缓冲区);

// 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];

我不知道我在哪里做错了。我是新手。

1 个答案:

答案 0 :(得分:0)

如果您仍在代码中渲染背景纹理,然后在其上方渲染太空飞船,则太空飞船可能无法通过深度缓冲测试。

你试过了吗?

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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