Beginner GLSL Problems
I am trying to learn GLSL but I am coming across some really weird things:
First, if I create a trivial vertex/fragment shader it works fine:
[vertex]
main(void) {
gl_Position = ftransform();
}
[fragment]
main (void) {
gl_FragColor = vec4(0.4,0.4,0.8,1.0);
}
However, once I try and introduce texturing some weird things happen. If I modify the above shaders to the following:
[vertex]
main(void) {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
[fragment]
uniform sampler2D colorMap;
main (void) {
gl_FragColor = vec4(0.4,0.4,0.8,1.0);
//gl_FragColor = texture2D( colorMap, gl_TexCoord[0].st);
}
The above shaders render the object with the texture, even though the fragment shader just assigns each fragment a constant color. In fact, it seems as though if I set gl_TexCoord[0] in the vertex shader, it doesnt matter what I do in the fragment shader, it just textures the scene. Furthermore, it also doesn't matter what I set gl_TexCoord[0] to in the vertex shader, it will still just render the scene the same way. For instance, the following shaders do the same as the above shader:
[vertex]
main(void) {
gl_TexCoord[0] = vec4(0.0,0.0,0.0,0.0);
gl_Position = ftransform();
}
[fragment]
uniform sampler2D colorMap;
main (void) {
//gl_FragColor = vec4(0.4,0.4,0.8,1.0);
gl_FragColor = texture2D( colorMap, gl_TexCoord[0].st);
}
Does anyone know what is going on? It looks like my shaders are messing up and the fixed function pipeline takes over or something but I dont know. Any kind of help would be appreciated.
Thanks,
ghempton
How are you going about rendering the polygons? What I mean by this is you should do this
Setup textures
Call Shader
Render with glDraw*
Shutdown textures
Shutdown Shader
BTW you don't need glEnable(GL_TEXTURE_2D) with the shaders. Also how is your setup code for the shaders? Did you assign the texture to the correct unit? And give it the name reference colorMap?
Setup textures
Call Shader
Render with glDraw*
Shutdown textures
Shutdown Shader
BTW you don't need glEnable(GL_TEXTURE_2D) with the shaders. Also how is your setup code for the shaders? Did you assign the texture to the correct unit? And give it the name reference colorMap?
I am actually just trying to do this with a cylinder right now. I am using gluCylinder.
As far as setting up the shaders is concerned, I am fairly certain that I am compiling linking and then using the program correctly. I am also making sure that the active texture unit is GL_TEXTURE0. However, I am not sure how to connect texture unit 0 with a name reference. How do you go about that?
Ahh, I wasn't sure about GL_ENABLE(GL_TEXTURE_2D). Now that I am not enabling that, the cylinder renders completely white.
As far as setting up the shaders is concerned, I am fairly certain that I am compiling linking and then using the program correctly. I am also making sure that the active texture unit is GL_TEXTURE0. However, I am not sure how to connect texture unit 0 with a name reference. How do you go about that?
Ahh, I wasn't sure about GL_ENABLE(GL_TEXTURE_2D). Now that I am not enabling that, the cylinder renders completely white.
Ok, I realized that I forgot to bind colorMap to the texture (originally I assumed that it did this for me).
I do the following:
GLint samplerLoc = glGetUniformLocation(p, "colorMap");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glUniform1i(samplerLoc, texture[0]);
The cylinder now renders completely black.
I do the following:
GLint samplerLoc = glGetUniformLocation(p, "colorMap");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glUniform1i(samplerLoc, texture[0]);
The cylinder now renders completely black.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement