【问题标题】:Draw texture on touch in openGL iphone在openGL iphone中绘制触摸纹理
【发布时间】:2011-08-03 13:00:11
【问题描述】:

最近开始使用 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];

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

【问题讨论】:

    标签: iphone opengl-es-2.0


    【解决方案1】:

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

    你试过了吗:

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    清除 z-buffer 然后渲染飞船?

    【讨论】:

    • 由于我是openGL的新手,请详细说明您在说什么。我该如何进行?我所做的是正确的方法吗???我正在尝试像 photobooth 一样进行图像变形
    • @Dimple OpenGL 带有一个可选的深度缓冲区,用于在绘制 3D 场景时检测可见性(前景对象的 z 值小于背景对象,因此应用程序知道要绘制哪些像素)。当只渲染像照片这样的 2D 纹理时,Z 缓冲区可能会适得其反,最终会阻止渲染新的纹理(深度缓冲区冲突)。禁用 z 缓冲区可能会有所帮助。但我不确定这是否是你的问题。也许发布一个更完整的示例会有所帮助。
    猜你喜欢
    • 2014-08-06
    • 2017-01-05
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多