【问题标题】:GLSL - Two textures (Not mixed)GLSL - 两种纹理(不混合)
【发布时间】:2012-12-01 15:38:46
【问题描述】:

我正在使用 GLSL 处理纹理。 如何使用 GLSL 处理两个纹理? 一个人推荐我在我的 GLSL 中做两个 samplers2D ...... 但是 GLSL 怎么知道应该使用哪个 samplers2D 呢? (我不是在谈论混合纹理......)

我听说我应该使用 glBindTexture。 我怎样才能做到这一点?使用 glBindTexture?谁有这方面的例子?

openGL 3.3


编辑:

我有这个:

uniform Sampler2D texture1;
uniform Sampler2D texture2;

我需要绘制两个对象,使用纹理,那么GLSL如何知道他应该根据我要绘制的对象使用texture1还是texture2。这是我的问题。

【问题讨论】:

  • @NicolBolas 你知道他们为什么在这些例子中使用GL_TEXTURE0 + x 而不是GL_TEXTURE0GL_TEXTURE2 等吗?
  • @user1118321 因为GL_TEXTURE# 最多只能达到 31,而 actual 限制可能要高得多。您应该始终使用GL_TEXTURE0 + x
  • 我现在很困惑,请告诉我,我不需要混合纹理,我需要将这些纹理用于不同的对象。金字塔的纹理#1,盒子的纹理#2,等等……我可以绑定它,人们怎么说我!那个 opengl wiki 有很大帮助,但它没有显示 frag/vert 着色器代码....
  • @Spamdark:这是一个完全不同的问题。那只是用一种纹理渲染一个对象,然后用不同的纹理渲染另一个对象。如果你可以用一个纹理渲染一个对象,那么你可以用两个来渲染。

标签: c++ opengl glsl


【解决方案1】:

您需要将每个纹理绑定到不同的纹理单元,然后使用多纹理坐标。它看起来像这样(假设你已经有了纹理):

glActiveTexture (GL_TEXTURE0);  // First texture is going into texture unit 0
glBindTexture (GL_TEXTURE_2D, tex0);
glEnable (GL_TEXTURE_2D);

glActiveTexture (GL_TEXTURE1);  // Second texture is going into texture unit 1
glBindTexture (GL_TEXTURE2D, tex1);
glEnable (GL_TEXTURE_2D);

glUseProgram (yourGLSLProgramID);
glUniform1i (sampler1Location, 0); // tell glsl that sampler 1 is sampling the texture in texture unit 0
glUniform1i (sampler2Location, 1); // tell glsl that sampler 2 is sampling the texture in texture unit 1
///... set the rest of your uniforms...

glBegin (GL_QUADS);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
    glVertexCoord2f(0.0, 0.0);

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
    glVertexCoord2f(width, 0.0);

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
    glVertexCoord2f(width, height);

    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
    glVertexCoord2f(0.0, height);
glEnd();

【讨论】:

  • 谢谢,但我需要使用 openGL 3.3。
  • 好的,所以唯一的区别是不使用glBegin()/glEnd()。相反,您会为各种纹理单元使用一组坐标。
  • 有关如何使用glTexCoordPointer() 而不是glMultiTexCoord() 的信息,请参阅this answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多