22

我的问题是在 GLSL 着色器中可以访问多个纹理。这就是我正在做的事情:

着色器:

uniform sampler2D sampler0;
uniform sampler2D sampler1;
uniform float blend;
void main( void )
{
    vec2 coords = gl_TexCoord[0];
    vec4 col = texture2D(sampler0, coords);
    vec4 col2 = texture2D(sampler1, coords);
    if (blend > 0.5){
        gl_FragColor = col;
    } else {
        gl_FragColor = col2;
    }
};

因此,我只是根据统一变量在两个颜色值之间进行选择。很简单(这是一个测试),但是当 blend <= 0.5 时,我不是预期的行为,而是全黑。

OpenGL代码:

m_sampler0location = m_shader.FindUniform("sampler0");
m_sampler1location = m_shader.FindUniform("sampler1");
m_blendlocation = m_shader.FindUniform("blend");

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
m_extensions.glUniform1iARB(m_sampler0location, 0);
glBindTexture(GL_TEXTURE_2D, Texture0.Handle);  
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
m_extensions.glUniform1iARB(m_sampler1location, 1);
glBindTexture(GL_TEXTURE_2D, Texture1.Handle);
glBegin(GL_QUADS);
    //lower left
    glTexCoord2f(0, 0);
    glVertex2f(-1.0, -1.0);
    //upper left
    glTexCoord2f(0, maxCoords0.t);
    glVertex2f(-1.0, 1.0);
    //upper right
    glTexCoord2f(maxCoords0.s, maxCoords0.t);
    glVertex2f(1.0, 1.0);
    //lower right
    glTexCoord2f(maxCoords0.s, 0);
    glVertex2f(1.0, -1.0);
glEnd()

着色器在这一切之前被编译和绑定。该过程中的所有健全性检查都表明一切正常。 正如我所说,col着色器程序中的值反映了纹理中的片段;的值为col2黑色。显示的纹理是最后一个活动纹理 - 如果我将最后一个更改glBindTexture为 bind Texture0.Handle,则纹理会更改。根据 Bahbar 的回复修复。

事实上,场景渲染为全黑,即使我添加了类似gl_FragColor.r = blend;着色器最后一行的内容。但是,如果我注释掉调用glActiveTexture(GL_TEXTURE1);,着色器会再次工作,并且相同的纹理会出现在 sampler0 和 sampler1 中。

这是怎么回事?有问题的行glActiveTexture(GL_TEXTURE1);, 似乎工作得很好,随后的glGetIntegerv(GL_ACTIVE_TEXTURE, &anint). 为什么它会如此可怕地破坏一切?我已经尝试升级我的显示驱动程序。

4

5 回答 5

23

这是一个基本的 GLUT 示例(在 OS X 上编写,根据需要进行调整),它生成两个棋盘纹理,加载带有两个采样器的着色器,并通过对每个采样器着色(一个红色,一个蓝色)和混合来组合它们。看看这是否适合你:

#include <stdio.h>
#include <stdlib.h>

#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

#define kTextureDim 64

GLuint t1;
GLuint t2;

/* adapted from the red book */
GLuint makeCheckTex() {
    GLubyte image[kTextureDim][kTextureDim][4]; // RGBA storage

    for (int i = 0; i < kTextureDim; i++) {
        for (int j = 0; j < kTextureDim; j++) {
            int c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0))*255;
            image[i][j][0]  = (GLubyte)c;
            image[i][j][1]  = (GLubyte)c;
            image[i][j][2]  = (GLubyte)c;
            image[i][j][3]  = (GLubyte)255;
        }
    }

    GLuint texName;
    glGenTextures(1, &texName);    
    glBindTexture(GL_TEXTURE_2D, texName);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTextureDim, kTextureDim, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

    return texName;
}

void loadShader() {
#define STRINGIFY(A) #A

    const GLchar* source = STRINGIFY(

                                     uniform sampler2D tex1;
                                     uniform sampler2D tex2;

                                     void main() {
                                         vec4 s1 = texture2D(tex1, gl_TexCoord[0].st);
                                         vec4 s2 = texture2D(tex2, gl_TexCoord[0].st + vec2(0.0625, 0.0625));
                                         gl_FragColor = mix(vec4(1, s1.g, s1.b, 0.5), vec4(s2.r, s2.g, 1, 0.5), 0.5);
                                     }

                                     );

    GLuint program = glCreateProgram();
    GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(shader, 1, &source, NULL);
    glCompileShader(shader);

    GLint logLength;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
    if (logLength > 0) {
        GLchar* log = (GLchar*)malloc(logLength);
        glGetShaderInfoLog(shader, logLength, &logLength, log);
        printf("Shader compile log:\n%s\n", log);
        free(log);
    }

    glAttachShader(program, shader);  
    glLinkProgram(program);

    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
    if (logLength > 0) {
        GLchar* log = (GLchar*)malloc(logLength);
        glGetProgramInfoLog(program, logLength, &logLength, log);
        printf("Program link log:\n%s\n", log);
        free(log);
    }

    GLuint t1Location = glGetUniformLocation(program, "tex1");
    GLuint t2Location = glGetUniformLocation(program, "tex2");

    glUniform1i(t1Location, 0);
    glUniform1i(t2Location, 1);

    glUseProgram(program);
}


void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);

    t1 = makeCheckTex();
    t2 = makeCheckTex();

    loadShader();
}


void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, t1);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, t2);

    glBegin(GL_QUADS);
    //lower left
    glTexCoord2f(0, 0);
    glVertex2f(-1.0, -1.0);
    //upper left
    glTexCoord2f(0, 1.0);
    glVertex2f(-1.0, 1.0);
    //upper right
    glTexCoord2f(1.0, 1.0);
    glVertex2f(1.0, 1.0);
    //lower right
    glTexCoord2f(1.0, 0);
    glVertex2f(1.0, -1.0);
    glEnd();

    glutSwapBuffers();
}


void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2, 2, -2, 2, -2, 2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


int main(int argc, char **argv)
{
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);

    glutInitWindowSize(512, 512);
    glutInitWindowPosition(0, 0);

    glutCreateWindow("GLSL Texture Blending");

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);

    init();

    glutMainLoop();
    return 0;
}

希望结果看起来像这样(您可以注释掉glUseProgram调用以查看在没有着色器的情况下绘制的第一个纹理):替代文字

于 2010-08-27T15:06:44.907 回答
12

只是为了帮助其他可能对使用多种纹理感兴趣并且在几天寻找答案后会感到绝望的人。我发现你需要打电话

glUseProgram(program);

GLuint t1Location = glGetUniformLocation(program, "tex1");
GLuint t2Location = glGetUniformLocation(program, "tex2");

glUniform1i(t1Location, 0);
glUniform1i(t2Location, 1);

为了让它工作(glUseProgram 是我在网上找到的 99% 的示例代码的最后一条指令)。现在它可能只是我的情况,不会影响地球上的任何其他人,但以防万一我认为我会分享。

于 2014-03-28T12:23:31.300 回答
9

回复很晚,但是对于遇到此问题的任何人-我遇到了同样的问题,经过短暂的摆弄后,我意识到打电话可以glActiveTexture(GL_TEXTURE0)解决问题。如果活动纹理单元未“重置”为零,则似乎有些东西会混淆。这可能是系统相关的行为;我正在使用 Mesa 8.0.4 运行 64 位 Archlinux。

于 2012-08-22T01:02:19.410 回答
2

在编译着色器进行测试时,我发现了两个错误:

  1. coords应该分配st4-component 的部分gl_TexCoord,例如

    vec2 coords = gl_TexCoord[0].st;
    
  2. 着色器不应以分号结尾。

您是否在主程序中检查着色器正确编译的任何地方?您可能想查看GL_COMPILE_STATUSviaglGetShaderglGetShaderInfoLog

于 2010-08-26T17:04:32.553 回答
0

听起来你的glActiveTexture电话打不通。你确定你正确设置了函数指针吗?

glGetIntegerv(GL_ACTIVE_TEXTURE, &anint)调用glActiveTexture(GL_TEXTURE1) 后调用验证。

glEnable(GL_TEXTURE_2D)没什么用。着色器本身指定要使用的纹理单元,以及要“启用”每个单元的哪个目标。


编辑添加:

嗯,你的新情况很奇怪。你甚至不能显示红色的事实很奇怪,特别是(好吧,你是否将 alpha 设置为 1 只是为了确保?)。

也就是说,您应该glActiveTexture在完成设置纹理单元 1 之后(即在glBindTexture调用之后)恢复 GL_TEXTURE0。

于 2010-08-25T19:27:57.107 回答