Sign in to follow this  
Arjan B

OpenGL Shaders and Depth

Recommended Posts

Arjan B    1136
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 ^^.

[img]http://img692.imageshack.us/img692/926/unledopf.png[/img]

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:
[code]
varying vec3 normal;
varying vec4 pos;

void main() {
normal = normalize(gl_NormalMatrix * gl_Normal);
pos = gl_ModelViewMatrix * gl_Vertex;
gl_Position = ftransform();
}
[/code]

Fragment shader:
[code]
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;
}
[/code]

Set up code:
[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;
}
[/code]

Rendering function:
[code]
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();
}
[/code]

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:
[code]pos = gl_ModelViewMatrix * gl_Vertex;[/code]
to this either this:
[code]pos = gl_ModelViewProjectionMatrix * gl_Vertex[/code]
or this:
[code]pos = ftransform();[/code]


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:
[url="http://db-in.com/blog/2011/04/cameras-on-opengl-es-2-x/"]http://db-in.com/blo...-opengl-es-2-x/[/url]

Hope that helps,
-Matt Edited by Ryu.Tenchi

Share this post


Link to post
Share on other sites
Arjan B    1136
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:
[code]
glFrontFace(GL_CW);
glutSolidTeapot(1);
glFrontFace(GL_CCW);
[/code]

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

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