Jump to content
  • Advertisement
Sign in to follow this  
Arjan B

OpenGL Shaders and Depth

This topic is 2563 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

Hi forum!

I've been trying to get the hang of using shaders and have been doing this with OpenGL.
However, things aren't looking the way I want them too ^^.


It seems like there is something going wrong with calculating the depth of each to-be-drawn pixel.
All I'm trying to do yet is draw everything in red with diffuse lighting.

Vertex shader:

varying vec3 normal;
varying vec4 pos;

void main() {
normal = normalize(gl_NormalMatrix * gl_Normal);
pos = gl_ModelViewMatrix * gl_Vertex;
gl_Position = ftransform();

Fragment shader:

varying vec3 normal;
varying vec4 pos;

void main() {
vec4 color = vec4(1.0, 0.0, 0.0, 1.0);
vec4 lightPos = gl_LightSource[0].position;
vec4 lightVec = normalize(lightPos - pos);

vec4 diffuse = color * max(0.0, dot(normal, lightVec.xyz));

gl_FragColor = diffuse;

Set up code:

int main(int argc, char **argv) {
// initialize glut
glutInit(&argc, argv);
glutInitWindowSize(400, 400);
glutCreateWindow("GLSL Testing");
glClearColor(0, 0, 0, 1);


// initialize glew

// set up shaders



return 0;

Rendering function:

void DisplayFunction() {

float lightPos[] = {-2, 3, 20, 1};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

glTranslatef(0, 0, 5);
glRotatef(30, 1, 1, 0);

gluLookAt(0, 0, 0, 0, 0, 1, 0, 1, 0);


When I comment out glEnable(GL_CULL_FACE), I get nothing but a black screen. Also, I thought setting the light positions z-value to -20 would light the side of the teapot we're looking at.
These two things together make me think the problem might lie with calculating the normals.

Does anybody see what causes the errors you can see in the image?

Share this post

Link to post
Share on other sites
Try changing this line in your vertex shader:
pos = gl_ModelViewMatrix * gl_Vertex;
to this either this:
pos = gl_ModelViewProjectionMatrix * gl_Vertex
or this:
pos = ftransform();

If that works(pretty sure it will) it means that the problem was that with your position data you were sending your lighting calculations in your fragment shader weren't taking into account the Projection matrix and therefore were not looking correct. If you need a better explanation about mode+view+projection matrices please read this:

Hope that helps,
-Matt Edited by Ryu.Tenchi

Share this post

Link to post
Share on other sites
Thank you for your response!
But pos is meant to be the position in eye coordinates. gl_Position is set with ftransform() as you suggested.

I have been making quite a few changes, but I think the one that fixed the problem is this one:


For some reason, the order for front faces seems reversed in glutSolidTeapot().

One thing I'm still confused about though, what is the relation between a fragment and the vertex from which it receives those varyings?
The position I calculate is that of the vertex right? So how does per pixel lighting work if I don't have the position of the fragment?

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!