Sign in to follow this  
ghempton

Beginner GLSL Problems

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this