如何正确绑定2个纹理

时间:2013-02-16 19:16:48

标签: c++ opengl textures glsl

我是OpenGL的新手,试图在2个纹理之间获得色差。 这是我的初始化功能:

glGenTextures(1, &TEX0);
glBindTexture(GL_TEXTURE_2D, TEX0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);


glGenTextures(1, &TEX1);
glBindTexture(GL_TEXTURE_2D, TEX1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, MODE, GL_UNSIGNED_BYTE, BUFFER0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, MODE, GL_UNSIGNED_BYTE, BUFFER1);

这是我的显示功能:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);

glUseProgram(shader);

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, TEX0);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, TEX1);

glUniform1i(glGetUniformLocation(shader, "tex0"), 0);
glUniform1i(glGetUniformLocation(shader, "tex1"), 1);

glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-1);
glTexCoord2f(0,1); glVertex2f(-1,+1);
glTexCoord2f(1,1); glVertex2f(+1,+1);
glTexCoord2f(1,0); glVertex2f(+1,-1);
glEnd();

glDisable(GL_TEXTURE_2D);
glUseProgram(0);
glFlush();
glutSwapBuffers();

这是我的片段着色器:

uniform sampler2D tex0;
uniform sampler2D tex1;

void main()
{
    vec4 otex = texture2D(oframe, gl_TexCoord[0].st);
    vec4 ntex = texture2D(nframe, gl_TexCoord[0].st);
    gl_FragColor = vec4(abs(otex.rgb - ntex.rgb), 1.0);
}

所以问题是第一个激活的纹理总是黑色的。怎么解决这个? 有很多类似的主题,但没有一个对我有帮助。谢谢。

1 个答案:

答案 0 :(得分:5)

第一个错误:OpenGL是一个状态机。 glTexImage的调用会影响当前选择性纹理单元中当前绑定的纹理。你必须这样写:

glGenTextures(1, &tex0);
glBindTexture(GL_TEXTURE_2D, tex0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, MODE, GL_UNSIGNED_BYTE, BUFFER0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glGenTextures(1, &tex1);
glBindTexture(GL_TEXTURE_2D, tex1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, MODE, GL_UNSIGNED_BYTE, BUFFER1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

如果glTexImage在glTexEnv的glTexParameter之前或之后出现并不重要,它可以正常工作。由于您使用的是片段着色器,因此glTexEnv是不必要的。 glTexEnv设置的所有内容都已被片段着色器中表达式的多功能性所取代。