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