首页 > 解决方案 > 如何使用纹理单元(它们似乎不起作用)

问题描述

我正在尝试使用一个颜色和另一个的 alpha 组合两个纹理,为此我使用 glTexEnvf() 将纹理的两个方面存储在不同的纹理单元中后组合它们。但它不起作用,我认为它是由 GL_TEXTURE1 没有按预期工作引起的,

我知道问题不在于纹理本身,因为我已经对其进行了测试,并且它们正常显示。这当前将 GL_TEXTURE0 的非 alpha 部分显示为黑色,因为似乎没有从 GL_TEXTURE1 中提取颜色数据(我也尝试过 GL_TEXTURE0+1 和所有这些的 ARB 版本,尽管 ARB 拒绝显示任何内容)

我也确定它们的尺寸完全相同,以防万一这可能是问题所在。

void mask(){
glActiveTexture(GL_TEXTURE0);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, images[0].textures[images[0].active_frame]);

  glActiveTexture(GL_TEXTURE1);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, images[1].textures[images[1].active_frame]);

  printf("%d\n", images[0].active_frame);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
  glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE1);
  glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);

  glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE0);
  glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
  glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
  glBegin(GL_QUADS);
  glTexCoord2f(1.0,1.0);
  glVertex2f((images[0].center.x)+(images[0].width),(images[0].center.y)+(images[0].height));
  glTexCoord2f(1.0,0.0);
  glVertex2f((images[0].center.x)+(images[0].width),(images[0].center.y)-(images[0].height));
  glTexCoord2f(0.0,0.0);
  glVertex2f((images[0].center.x)-(images[0].width),(images[0].center.y)-(images[0].height));
  glTexCoord2f(0.0,1.0);
  glVertex2f((images[0].center.x)-(images[0].width),(images[0].center.y)+(images[0].height));
  glEnd();
}

这是我的初始化代码(删除了不相关的东西,例如用于获取纹理图像的 devIL 初始化):

  glutInit(&argc, argv); 
  //finish initialising openGL
  glutInitDisplayMode(GLUT_RGBA);
  //create the window (game mode makes it full screen, there is a fullscreen function
  // but for some reason this was not working)
  glutEnterGameMode();
  //set the background colour
  glClearColor(55/360.0f, 171/360.0f, 200/306.0f, 0.0f);
  //enable textures and transparency for the QUADS
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_ALPHA_TEST);
  glAlphaFunc(GL_GREATER, 0.0f);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

对于上下文,我有一个存储有关纹理信息的结构,因为我有动画(我只是通过纹理运行):

typedef struct image {
  int num_of_frames;
  int active_frame;
  int anim_called;
  int* textures;
  cntr center;
  //the width and height are scaled for the screen
  float width;
  float height;

} image;

它应该只在 GL_TEXTURE0 没有 alpha 的地方显示 GL_TEXTURE1。目前它在 GL_TEXTURE0 没有 alpha 的地方显示为黑色

我刚刚发现其他纹理(在 GL_TEXTURE0 上)现在也没有颜色,(GL_TEXTURE1 仍然根本不显示)这是我用来正常显示图像的代码:

void drawimage(image* img){
  //set the blending mode to allow the transparency of the image to effect the QUAD
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  //Binds the current frame of the current image (must be done before glBegin)
  glBindTexture( GL_TEXTURE_2D, img->textures[img->active_frame] );
  //draws the textured QUAD
  glBegin(GL_QUADS);
  glColor4f(1.0f, 1.0f, 10.f, 1.0f);
  glTexCoord2f(1.0,1.0);
  glVertex2f((img->center.x)+(img->width),(img->center.y)+(img->height));
  glTexCoord2f(1.0,0.0);
  glVertex2f((img->center.x)+(img->width),(img->center.y)-(img->height));
  glTexCoord2f(0.0,0.0);
  glVertex2f((img->center.x)-(img->width),(img->center.y)-(img->height));
  glTexCoord2f(0.0,1.0);
  glVertex2f((img->center.x)-(img->width),(img->center.y)+(img->height));
  glEnd();
}

我有一种解决这个问题的感觉,我需要“重置”glTexEnvf,但我不知道如何。

标签: copenglraspberry-pi3freeglutopengl-compat

解决方案


问题是我没有纹理单元 1 的纹理坐标,正如 derhass 所指出的那样。


推荐阅读