Using single channel texture (OpenGL 2)?

前提是你 提交于 2020-02-25 04:32:08

问题


Short storry:

when I render anything using texture loaded like this

glTexImage2D   ( GL_TEXTURE_2D, 0, GL_R8, width,  height, 0, GL_RED,   GL_UNSIGNED_BYTE, pixels );

I get only black


Long storry:

I can get RGBA texture with alpha channel (e.g. text with transparent backgorund using this code):

This code works:

// === load
#define GL_ABGR 0x8000
SDL_Surface * surf = SDL_LoadBMP( "common_resources/dejvu_sans_mono_RGBA.bmp" );
glGenTextures  ( 1, &itex );
glBindTexture  ( GL_TEXTURE_2D, itex );
glTexImage2D   ( GL_TEXTURE_2D, 0, GL_RGBA, surf->w,  surf->h, 0, GL_ABGR,   GL_UNSIGNED_BYTE, surf->pixels );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// ....
// === render
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, itex );
glColor3f(1.0f,1.0f,1.0f);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
drawString ( caption,             xmin, ymin+12, 6 );

renders like

But I'm trying to use just one channel (8-bit; grayscale) images / textures instead of RGBA. These I cannot get to render neither with nor without transparancy. Whatever I do I get only black image.

This doesn't

// === load
#define GL_ABGR 0x8000
SDL_Surface * surf = SDL_LoadBMP( "common_resources/dejvu_sans_mono_Alpha.bmp" );
glGenTextures  ( 1, &itex );
glBindTexture  ( GL_TEXTURE_2D, itex );
glTexImage2D   ( GL_TEXTURE_2D, 0, GL_R8, surf->w,  surf->h, 0, GL_RED,   GL_UNSIGNED_BYTE, surf->pixels );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// ....
// === render
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, itex );
glColor3f(1.0f,1.0f,1.0f);
//glEnable(GL_BLEND);
//glEnable(GL_ALPHA_TEST);
//glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
drawString ( caption,             xmin, ymin+12, 6 );

renders like

Notes:

  1. I know that I should somehow use glTexEnv according to e.g. here but my main problem is that apparently the monochrome texture does not render at all
  2. I tried also other GL_LUMINANCE and GL_INTENSITY instead of GL_RED in glTexImage2D with no difference
  3. there are other questions like here and here but mostly with OpenGL>3.0 and fragment shaders

Also, is it possible that my graphics card or driver does not support this ? I'm on ubuntu 16.04

GL_VENDOR:     Intel Open Source Technology Center
GL_RENDERER:   Mesa DRI Intel(R) HD Graphics 530 (Skylake GT2) 
GL_VERSION:    3.0 Mesa 11.2.0

for completeness - although it is not importaint the drawString looks like this:

drawString ( caption,             xmin, ymin+12, 6 ){
  const int nchars = 95;
  float persprite = 1.0f/nchars;
  glBegin(GL_QUADS);
  for(int i=0; i<65536; i++){
    if( str[i] == 0 ) break; // 0-terminated string
    int isprite = str[i] - 33; // 33 is offset of meaningfull ASCII characters
    float offset  = isprite*persprite+(persprite*0.57);
    float xi = i*sz + x;
    glTexCoord2f( offset          , 1.0f ); glVertex3f( xi,    y,    3.0f );
    glTexCoord2f( offset+persprite, 1.0f ); glVertex3f( xi+sz, y,    3.0f );
    glTexCoord2f( offset+persprite, 0.0f ); glVertex3f( xi+sz, y+sz*2, 3.0f );
    glTexCoord2f( offset          , 0.0f ); glVertex3f( xi,    y+sz*2, 3.0f );
  }
  glEnd();

}


回答1:


I want to try to help you. In my projects I am using this arguments for generating textures from grayscale source images:

glTexImage2D(GL_TEXTURE_2D, 0, 1, width, height, 0, GL_RED,
             GL_UNSIGNED_BYTE, pixels);

As written in documentation, third argument - number of color components (1 in our case). Need to check integer value of GL_R8 or replace it explicitly.

GL_RED means that you place luminances in red channel (not in each red, green, blue channels as for grayscale image).



来源:https://stackoverflow.com/questions/39943456/using-single-channel-texture-opengl-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!