仅绘制纹理OpenGL ES iPhone的一部分

时间:2010-06-11 12:47:27

标签: iphone opengl-es

..Continued on from my previous question

我有一个320 * 480 RGB565帧缓冲,我希望在iPhone上使用OpenGL ES 1.0绘制。

- (void)setupView
{   
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, (int[4]){0, 0, 480, 320});

    glEnable(GL_TEXTURE_2D);
}

// Updates the OpenGL view when the timer fires
- (void)drawView
{
  // Make sure that you are drawing to the current context 
  [EAGLContext setCurrentContext:context];

  //Get the 320*480 buffer
  const int8_t * frameBuf = [source getNextBuffer];

  //Create enough storage for a 512x512 power of 2 texture
  int8_t lBuf[2*512*512];

  memcpy (lBuf, frameBuf, 320*480*2);

  //Upload the texture
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, lBuf);

  //Draw it
  glDrawTexiOES(0, 0, 1, 480, 320);

  [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

如果我在512 * 512中生成原始纹理,则输出会被错误地裁剪,但除此之外看起来不错。然而,使用320 * 480的require输出大小,一切都会变形并搞砸。

我很确定这是我将帧缓冲区复制到新的512 * 512缓冲区的方式。我试过这个例程

int8_t lBuf[512][512][2];
const char * frameDataP = frameData;
for (int ii = 0; ii < 480; ++ii) {
    memcpy(lBuf[ii], frameDataP, 320);
    frameDataP += 320;
}

哪个更好,但宽度似乎被拉伸,高度混乱。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:1)

您是以纵向还是横向模式绘制此背景? glDrawTexiOES参数为(x, y, z, width, height),因为您的代码的每个其他部分似乎都将这些数字引用为可能导致“裁剪错误”问题的320x480 - 尝试切换{{1剪裁矩形中的{}和320以及480调用中的宽度/高度。 (可能部分是我的错,因为没有在最后一个帖子中发布参数。抱歉!)

但是你必须使用第二个FB复制例程。 glDrawTex命令不起作用的原因是因为它将获取320x480x2缓冲区(307200字节)并将其作为单个连续块转储到512x512x2缓冲区(524288字节)中 - 它不足以复制320字节,添加192块“死空间”,然后恢复。

答案 1 :(得分:0)

看起来分配的缓冲区不够大。如果每个颜色分量有8位(1字节),则分配的字节太少。这可以解释为什么图像仅对图像的一部分显示正确。 我想以下几行:

 //Create enough storage for a 512x512 power of 2 texture
 int8_t lBuf[2*512*512];
 memcpy (lBuf, frameBuf, 320*480*2);

需要更改为:

 //Create enough storage for a 512x512 power of 2 texture
 int8_t lBuf[3*512*512];
 memcpy (lBuf, frameBuf, 320*480*3);
相关问题