OpenGL重复调用glTexImage2D和alpha混合

时间:2012-12-30 16:05:33

标签: opengl alphablending glteximage2d visual-artifacts

这更多是出于好奇而不是出于任何实际目的:OpenGL规范中是否有任何内容表明多次调用glTexImage2D(例如,每帧一次)是非法的?我的意思是非法的,因为'它可能产生错误的结果',而不仅仅是效率低下(假设我不关心不使用glTexSubImage2D的性能影响)。

我问的原因是我在绘制重叠的纹理映射基元时发现了一些非常奇怪的瑕疵,这些基元使用部分透明的纹理,每个帧使用glTexImage2D加载一次(参见附图) ):几秒钟后(即几百帧),屏幕上出现小的矩形黑色斑块(它们实际上是在连续帧之间的黑色和正常之间翻转)。

Artifacts

我附上了我能编写的最简单的示例代码,显示了问题。

#include <stdio.h>

#ifndef __APPLE__
# include <SDL/SDL.h>
# include <SDL/SDL_opengl.h>
#else
# include <SDL.h>
# include <SDL_opengl.h>
#endif

/* some constants and variables that several functions use */
const int width = 640;
const int height = 480;
#define texSize 64

GLuint vbo;
GLuint tex;

/* forward declaration, creates a random texture; uses glTexSubImage2D if 
   update is non-zero (otherwise glTexImage2D) */
void createTexture(GLuint label, int update);

int init() 
{
  /* SDL initialization */
  if (SDL_Init(SDL_INIT_VIDEO) < 0)
    return 0;

  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  if (!SDL_SetVideoMode(width, height, 0, SDL_OPENGL)) {
    fprintf(stderr, "Couldn't initialize OpenGL");
    return 0;
  }

  /* OpenGL initialization */
  glClearColor(0, 0, 0, 0);
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

  glViewport(0, 0, width, height);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, width, height, 0, -1, 1);
  glMatrixMode(GL_MODELVIEW);

  /* creating the VBO and the textures */
  glGenBuffers(1, &vbo);
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
  glBufferData(GL_ARRAY_BUFFER, 1024, 0, GL_DYNAMIC_DRAW);

  glGenTextures(1, &tex);
  createTexture(tex, 0);

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);

  return 1;
}

/* draw a triangle at the specified point */
void drawTriangle(GLfloat x, GLfloat y)
{
  GLfloat coords1[12] = {0, 0, 0, 0, /**/200, 0, 1, 0, /**/200, 150, 1, 1};

  glLoadIdentity();
  glTranslatef(x, y, 0);

  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(coords1), coords1);
  glVertexPointer(2, GL_FLOAT, 4*sizeof(GLfloat), (void*)0);
  glTexCoordPointer(2, GL_FLOAT, 4*sizeof(GLfloat),
    (char*)0 + 2*sizeof(GLfloat));

  glDrawArrays(GL_TRIANGLES, 0, 3);
}

void render()
{
  glClear(GL_COLOR_BUFFER_BIT);

  drawTriangle(250, 50);

  createTexture(tex, 0);
  drawTriangle(260, 120);

  SDL_GL_SwapBuffers();
}

void cleanup()
{
  glDeleteTextures(1, &tex);
  glDeleteBuffers(1, &vbo);

  SDL_Quit();
}

int main(int argc, char* argv[])
{
  SDL_Event event;

  if (!init()) return 1;

  while (1) {
    while (SDL_PollEvent(&event))
      if (event.type == SDL_QUIT)
        return 0;

    render();
  }

  cleanup();

  return 0;
}

void createTexture(GLuint label, int update)
{
  GLubyte data[texSize*texSize*4];
  GLubyte* p;
  int i, j;

  glBindTexture(GL_TEXTURE_2D, label);

  for (i = 0; i < texSize; ++i) {
    for (j = 0; j < texSize; ++j) {
      p = data + (i + j*texSize)*4;
      p[0] = ((i % 8) > 4?255:0);
      p[1] = ((j % 8) > 4?255:0);
      p[2] = ((i % 8) > 4?255:0);
      p[3] = 255 - i*3;
    }
  }

  if (!update)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texSize, texSize, 0, GL_RGBA,
      GL_UNSIGNED_BYTE, data);
  else
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texSize, texSize, GL_RGBA,
      GL_UNSIGNED_BYTE, data);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

注意:

  1. 我正在使用SDL,但我在wxWidgets中看到了相同的情况,因此它不是与SDL相关的问题。

  2. 如果我为每一帧使用glTexSubImage2D(在update = 1中使用createTexture),则工件会消失。

  3. 如果我禁用了混合,则不再有任何工件。

  4. 我一直在2010年末的MacBook Air上对此进行测试,不过我怀疑这是特别相关的。

1 个答案:

答案 0 :(得分:1)

这显然是一个OpenGL实现错误(只是在循环中调用glTexImage2D不应该导致这种情况发生)。