本文介绍了使用单通道纹理(OpenGL 2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短仓库

使用此类载入的纹理呈现任何内容

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 );

我只有黑色

长存储:

我可以使用Alpha通道获得RGBA纹理代码):

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

此代码的工作原理:

// === 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 );

呈现像

但我试图只使用一个通道(8位;灰度)图像/纹理而不是RGBA。这些我不能既没有透明也没有。无论我做什么,我只得到黑色图像。

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.

这不

// === 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 );

呈现像

注意:


  1. 我知道我应该使用 glTexEnv ,但我的主要问题是,显然单色纹理根本不呈现

  2. 我也试过其他 GL_LUMINANCE GL_INTENSITY 而不是 GL_RED glTexImage2D 还有其他问题,例如和,但主要使用OpenGL> 3.0和片段着色器

  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

此外,我的显卡或驱动程序是否可能不支持?我在ubuntu 16.04

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






完整性 - 虽然不是importaint,但 drawString 看起来像这样:


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();

}

推荐答案

我想尝试帮助你。在我的项目中,我使用这个参数从灰度源图像生成纹理:

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);

如,第三个参数 - 颜色组件的数量(在我们的示例中为1)。需要检查 GL_R8 的整数值或明确替换。

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 表示您将亮度放在红色通道中(而不是像灰度图像一样在每个红色,绿色和蓝色通道中)。

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

这篇关于使用单通道纹理(OpenGL 2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:14