OpenGL Shaders and Depth

Started by
1 comment, last by Arjan B 12 years, 9 months ago
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 ^^.

unledopf.png

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);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(400, 400);
glutCreateWindow("GLSL Testing");
glClearColor(0, 0, 0, 1);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

glutDisplayFunc(DisplayFunction);
glutReshapeFunc(ReshapeFunction);
glutIdleFunc(IdleFunction);
glutKeyboardFunc(KeyboardFunction);

// initialize glew
glewInit();

// set up shaders
SetShaders();

glutMainLoop();

CleanUp();

return 0;
}


Rendering function:

void DisplayFunction() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

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

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

glutSwapBuffers();
}


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?
Advertisement
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:
http://db-in.com/blo...-opengl-es-2-x/

Hope that helps,
-Matt
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:

glFrontFace(GL_CW);
glutSolidTeapot(1);
glFrontFace(GL_CCW);


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?

This topic is closed to new replies.

Advertisement