Jump to content
  • Advertisement
Sign in to follow this  
Assembler015

OpenGL GLSL strangeness ... or I'm just dumb

This topic is 4635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello - I have been playing around with GLSL and I'm having some troubles getting a vertex/fragment shader working. Here's what I'm doing... I have a model and a light in a fixed position. I simply trying to apply an unrealistic shading to the model. The non-working vertex shader:

varying float intensity;

void main ()
{
    vec3  light_position = vec3(0, 0, 100);
    vec3  normal;
    vec3  light_vector;
    float angle_from_light;

    normal       = normalize(gl_Normal);
    light_vector = normalize(light_position-vec3(gl_Vertex));
  
    angle_from_light = max(dot(normal, light_vector), 0.0);

    intensity = angle_from_light;

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position    = ftransform();
}


And, the non-working fragment shader:

uniform sampler2D text;

varying float intensity;

void main ()
{
    vec4 color;

    color = texture2D(text, gl_TexCoord[0].st);

    color.x *= intensity;
    color.y *= intensity;
    color.z *= intensity;

    gl_FragColor = color;
}


And finally, the opengl code (which renders a shaded model when I use the built in opengl lighting functionality)...
/* Simplified source for posting readability */
glUseProgramObjectARB(shaderprog);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sometexture);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_TRIANGLES);
   while(There's triangles to be rendered...)
   {
       glNormal3f(some_normal_x, some_normal_y, some_normal_z);
       glTexCoord2d(some_tex_x, some_tex_y);
       glVertex3f(some_position_x, some_position_y, some_position_z);
    }
glEnd();

glUseProgramObjectARB(0);

When I run the program, I see the sphere (the model being rendered) completely black at the starting edge, and completely bright at the ending edge. After a lot of confusion and failed attempts to correct this behavior, I changed my vertex shader to read:
varying float intensity;

void main ()
{
    intensity = 1.0;

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position    = ftransform();
}


Based on my understanding, I expected the intensity value in the fragment shader to always be 1.0 as well, which would make the model appear as if no shading was applied. However, the model rendered *exactly* the same as it did before I made this change. Is anyone willing to point out where I went stupid? Thanks for your time, --Andrew

Share this post


Link to post
Share on other sites
Advertisement
There's probably an error in the fragment or vertex shader that causes it not to compile and the program just continues to use the fixed-function pipeline as always. That "vec3(gl_Vertex)" looks suspicious(you could just use gl_Vertex.xyz) but I'm not really sure. Use glGetInfoLogARB after you compile them, to see the errors or warnings that have been produced.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
There's probably an error in the fragment or vertex shader that causes it not to compile and the program just continues to use the fixed-function pipeline as always. That "vec3(gl_Vertex)" looks suspicious(you could just use gl_Vertex.xyz) but I'm not really sure. Use glGetInfoLogARB after you compile them, to see the errors or warnings that have been produced.


I took your advice and made 3 calls to glGetInfoLogARB -- after compiling the vertex shader, after compiling the fragment shader, and after linking the shaders to a program.

The first two calls return nothing (no length is set by the function call and nothing in the character arrays passed). The third call -- after the link step -- returns the string:

'Link successful. The GLSL vertex shader will run in sofware. The GLSL fragment shader will run in hardware.'


I was expecting the vertex shader to run in hardware as well, but I'm going to guess that it doesn't matter if the link was successful (right?).


Thanks for the advice...the more information the better :-)
--Andrew

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!