PDA

View Full Version : Lighting help



Bambowski
12-07-2014, 07:22 PM
Hello all,

I am currently working on a small dungeon crawling game using OpenGL and written in C.

I have run into a bit of a speed bump in that I cannot seem to get my lighting effects working with the correct normals.

When I define the normals I have calculated with each surface, the lighting effects behave unpredictably (sometimes remaining dark even with light sources nearby, sometimes not reacting to ambient lighting at all, etc...). However, when I use the coordinates (0, 0, 1) for each normal vector, the lighting seems to behave naturally regardless of whether or not (0, 0, 1) are theoretically the correct coordinates. I know that the normals that I have calculated myself are correct in theory as I have double checked them with a vector calculator. Would anyone happen to know what might be causing this?

This is my draw function-


GLvoid DungeonConstruction(GLvoid)
{
GLfloat x_m, y_m, z_m, u_m, v_m;
GLfloat xtrans, ztrans, ytrans;
GLfloat sceneroty;
int totalWalls;
//camera variables
xtrans = -xpos;
ztrans = -zpos;
ytrans = -mov1-0.5;
sceneroty = 360.0 - yrot;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_NORMALIZE);


glRotatef(lookupdown, 1.0, 0, 0);
glRotatef(sceneroty, 0, 1.0, 0);

glTranslatef(xtrans, ytrans, ztrans);
glBindTexture(GL_TEXTURE_2D,texture[0]);

//here are function calls to shapes (normals are defined in each polygon's respective function.)

}



This my initGL function-

GLvoid GLStuff(GLsizei Width, GLsizei Height)
{
LoadGLTextures();
glEnable(GL_TEXTURE_2D);

glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);


glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_LIGHT1);
}

I can provide more specific bits of code on request.


Thanks in advance!

GClements
12-07-2014, 08:36 PM
How coarse is the geometry? Vertex lighting works best with smooth surfaces tessellated into many small faces. It doesn't work so well if the distance between a point light source and a face isn't significantly larger than the face (because you're under-sampling the intensity function), or if you're using per-vertex lighting (smooth shading) and the angle between adjacent faces is large (so adjacent normals point in substantially different directions).

Also, if you're calculating normals from the geometry, make sure that they all point the same way (typically outward for a closed surface). If the direction is inconsistent, the results will be garbage. In practice, this means that your faces must have consistent orientation (i.e. either always clockwise or always anti-clockwise, when facing perpendicular to the face from a given side).