Open GL - 如何从像素缓冲区加载纹理

时间:2017-07-18 14:15:23

标签: c++ opengl textures

我想画四边形纹理。我想从像素的1D无符号char缓冲区绑定我的纹理,如下所示:

enter image description here

每个像素由2个字节组成,这2个字节是16位灰度值。

我实际上尝试过很多东西,但我在四边形上看不到任何东西:

void RawImageDrawer::initializeGL() {
  glShadeModel(GL_SMOOTH);
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
  glClearDepth(1.0f); 
  glEnable(GL_DEPTH_TEST); 
  glEnable(GL_TEXTURE_2D);
  glDepthFunc((GL_LEQUAL));
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void RawImageDrawer::resizeGL(int width, int height) {
  if (height == 0)
    height = 1;
  glViewport(0, 0, width, height);
  glMatrixMode(GL_MODELVIEW);*/
  glLoadIdentity();
}

void RawImageDrawer::loadTexture() {
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(
    GL_TEXTURE_2D, 
    0, 
    GL_RED, 
    1024, 
    1024, 
    0, 
    GL_LUMINANCE, 
    GL_UNSIGNED_BYTE, 
    buffer //unsigned char* described up there
  );
}

void RawImageDrawer::paintGL() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity(); 
  glTranslatef(0.0f, 0.0f, 0.0f); 

  glBindTexture(GL_TEXTURE_2D, texture);

  glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex3f(-0.5f, -0.5f, 0);
    glTexCoord2i(0,1); glVertex3f(0.5f, -0.5f, 0);
    glTexCoord2i(1,1); glVertex3f(0.5f, 0.5f, 0);
    glTexCoord2i(1,0); glVertex3f(-0.5f, 0.5f, 0);
  glEnd();
}

我也尝试将TEXTURE_2D改为TEXTURE_1D,没有改变任何东西..

1 个答案:

答案 0 :(得分:0)

由于您的缓冲区是16位灰度,因此您应指定GL_UNSIGNED_SHORT而不是GL_UNSIGNED_BYTE。确保您的值扩展到整个[0,65535]范围,因为对于16位数据,65535是完整的光度值。

同样GL_RED永远不是internalFormat glTexImage2D参数的有效值。您应该使用GL_R16作为internalFormatGL_RED作为format参数。