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);
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?