GLSL - 两种纹理(不混合)

时间:2012-12-01 15:38:46

标签: c++ opengl glsl

我正在使用GLSL进行纹理处理。 如何用GLSL处理两个纹理? 一个人建议我在我的GLSL中做两个samplers2D ...... 但GLSL如何知道哪些采样器2D应该使用? (我不是在谈论混合纹理......)

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

openGL 3.3


编辑:

我有这个:

uniform Sampler2D texture1;
uniform Sampler2D texture2;

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

1 个答案:

答案 0 :(得分:2)

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

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