OpenGL纹理混合和平移

发布于 2024-09-19 18:35:35 字数 2339 浏览 6 评论 0原文

我有两个纹理映射到一个表面,一个是棋盘(地板上的图案),一个是光照贴图,两者都混合并呈现得很好。我遇到的问题是在混合之前使光照贴图纹理进行转换。我需要这样做,这样我就可以模拟手电筒扫描地板......

代码:

    //Floor - Checkerboard
  float fF = 3.0; //Floor Factor

  //Checkerboard texture
  glActiveTexture(GL_TEXTURE0);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, textures[0]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);//turn on mipmapping
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  //Spotlight, modulate on checkerboard
  glActiveTexture(GL_TEXTURE1);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, textures[4]);
  **glPushMatrix();
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glTranslatef(5.0, 5.0, 0.0);
    glMatrixMode(GL_MODELVIEW);
  glPopMatrix();**
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

  float vals[4] = {0.0, 0.0, 0.0, 0.5};

  glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, vals);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
  glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); 
  glTexEnvf(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
  glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
  glTexEnvf(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE);
  glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
  glTexEnvf(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_CONSTANT);
  glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);

  float spot_x = 1;
  float spot_y = 1;

  glBegin(GL_QUADS); 
  glNormal3d(0, 1, 0);
  glMultiTexCoord2f(GL_TEXTURE0, 0.0, fF); 
  glMultiTexCoord2f(GL_TEXTURE1, 0.0, spot_y); 
  glVertex3fv(d);
  glMultiTexCoord2f(GL_TEXTURE0, fF, fF); 
  glMultiTexCoord2f(GL_TEXTURE1, spot_x, spot_y); 
  glVertex3fv(c);
  glMultiTexCoord2f(GL_TEXTURE0, fF, 0.0); 
  glMultiTexCoord2f(GL_TEXTURE1, spot_x, 0.0); 
  glVertex3fv(e);
  glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0); 
  glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0); 
  glVertex3fv(g);

  glEnd();

我尝试将六行代码放在**的几个位置中,认为也许OpenGL状态机在某个地方需要它具体,但似乎没有什么能给我想要的效果。

以前有人这样做过吗?我在我的 OpenGL 书籍或网上都找不到任何有关翻译和混合纹理的信息。我可能正在寻找错误的东西,但这两天我一直在用头撞墙。任何帮助都会很棒。

谢谢

I've got two textures mapping to a surface, one is a checkerboard (the pattern on the floor) and one is a lightmap, both of which are blending and presenting just fine. What I'm having trouble with is making the lightmap texture translate prior to blending. I need to do this so I can simulate a flashlight scanning across the floor....

Code:

    //Floor - Checkerboard
  float fF = 3.0; //Floor Factor

  //Checkerboard texture
  glActiveTexture(GL_TEXTURE0);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, textures[0]);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);//turn on mipmapping
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  //Spotlight, modulate on checkerboard
  glActiveTexture(GL_TEXTURE1);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, textures[4]);
  **glPushMatrix();
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glTranslatef(5.0, 5.0, 0.0);
    glMatrixMode(GL_MODELVIEW);
  glPopMatrix();**
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

  float vals[4] = {0.0, 0.0, 0.0, 0.5};

  glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, vals);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
  glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); 
  glTexEnvf(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
  glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
  glTexEnvf(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE);
  glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
  glTexEnvf(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_CONSTANT);
  glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);

  float spot_x = 1;
  float spot_y = 1;

  glBegin(GL_QUADS); 
  glNormal3d(0, 1, 0);
  glMultiTexCoord2f(GL_TEXTURE0, 0.0, fF); 
  glMultiTexCoord2f(GL_TEXTURE1, 0.0, spot_y); 
  glVertex3fv(d);
  glMultiTexCoord2f(GL_TEXTURE0, fF, fF); 
  glMultiTexCoord2f(GL_TEXTURE1, spot_x, spot_y); 
  glVertex3fv(c);
  glMultiTexCoord2f(GL_TEXTURE0, fF, 0.0); 
  glMultiTexCoord2f(GL_TEXTURE1, spot_x, 0.0); 
  glVertex3fv(e);
  glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0); 
  glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0); 
  glVertex3fv(g);

  glEnd();

I've tried putting the six lines of code inside the **'s in several locations, thinking maybe the OpenGL state machine needed it somewhere specific, but nothing seems to give me the effect I want.

Anyone done this before? I can't find any information in either of my OpenGL books, or online about translating AND blending textures. I might be looking for the wrong thing, but I've been beating my head against a wall for two days now. Any help at all would be awesome.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2024-09-26 18:35:35

为了完整起见,问题在于纹理坐标落在 [0,1] 区间内,并且我按整数值进行翻译,因此看起来好像根本没有翻译。

For completeness, the problem is that texture coordinates fall in the [0,1] interval, and I was translating by integer values, so it appeared as though it wasn't translating at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文