# Lightning - Help Me!!!

This code its how I put lights in my terrain engine, the problem is that the terrain looks like a chess-board, b&w quads.

My calculation of normals its ok.

lights() its in REDRAW function too

void lightsgl(void)
{
lights();
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}

void lights(void)
{
float ambience[4] = {0.3f, 0.3f, 0.3f, 1.0};
float diffuse[4] = {0.5f, 0.5f, 0.5f, 1.0};
float g_LightPosition[4] = {0, 1, 0, 1};
glLightfv( GL_LIGHT0, GL_AMBIENT, ambience );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
glLightfv( GL_LIGHT0, GL_POSITION, g_LightPosition );
}

Are you sure for the normals?

I do agree, there is nothing wrong with your code here, it may be a problem of normals.

How do you build them ?

My normal calcs: (based on triangles)

{
vNormal.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y));
vNormal.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z));
vNormal.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x));
return vNormal;
}

{
vVector.x = vPoint1.x - vPoint2.x;
vVector.y = vPoint1.y - vPoint2.y;
vVector.z = vPoint1.z - vPoint2.z;
return vVector;
}

{
return (float)sqrt( (vNormal.x * vNormal.x) +
(vNormal.y * vNormal.y) +
(vNormal.z * vNormal.z) );
}

{
float magnitude = Magnitude(vNormal);
vNormal.x /= magnitude;
vNormal.y /= magnitude;
vNormal.z /= magnitude;
return vNormal;
}

{
vNormal = Normalize(vNormal);
return vNormal;
}

I had a similar problem where quads lined up together had that checkerboard effect in a light. Even though my normals were correct.

same problem

Do the dark sqaures in you terrain look like they never get lit up even if you move around your terrain? Are there only two shades of color, like a white and a gray? Or there is a bit of a gray scale going on?
If you have a gray scale color going on, I would say you are simply computing the normals and not the average normals which gives the effect of smoothness…

Well, I was just looking at your normalization code, when you are not computing average normals which gives the effect of smothness… This is prolly what you need to do…

How to average the normals?

Are you sure you have all your triangles ordered the right way? Either all clockwise or anticlockwise?

Below is a piece of code I use for computing average normals for my model loader… It looks challenging, but it is not so bad actually.

bool C3ds::NormalizeVectors()
{
if( numOfObjects < 1 ) return false;

``````vector3 temp_polygons[3];
int shared_vertices = 0;

tObjInfo *currObj;

for( unsigned int obj_index = 0; obj_index &lt; numOfObjects; obj_index++ )
{
currObj	= &(m_Objects[obj_index]);

currObj-&gt;m_Normals		= new vector3[currObj-&gt;numOfVertices];
vector3 *pTempNormals	= new vector3[currObj-&gt;numOfFaces];

for( int face_index = 0; face_index &lt; currObj-&gt;numOfFaces; face_index++ )
{
temp_polygons[0] = currObj-&gt;m_Vertices[currObj-&gt;m_Faces[face_index].VertexIndex[0]];
temp_polygons[1] = currObj-&gt;m_Vertices[currObj-&gt;m_Faces[face_index].VertexIndex[1]];
temp_polygons[2] = currObj-&gt;m_Vertices[currObj-&gt;m_Faces[face_index].VertexIndex[2]];

// Finding the perpendicular vector //
CrossProduct3v( temp_polygons, &pTempNormals[face_index] );
}

vector3 newNormal;

for( int vertex_index = 0; vertex_index &lt; currObj-&gt;numOfVertices; vertex_index++ )
{
memset(&newNormal, 0, sizeof(vector3));
//&gt;--------------------------------------------------------------------&lt;//
//	Computing average perpendicular vectors								//
//&gt;--------------------------------------------------------------------&lt;//
//
for( int face_index = 0; face_index &lt; currObj-&gt;numOfFaces; face_index++ )
{
if( currObj-&gt;m_Faces[face_index].VertexIndex[0] == vertex_index | |
currObj-&gt;m_Faces[face_index].VertexIndex[1] == vertex_index | |
currObj-&gt;m_Faces[face_index].VertexIndex[2] == vertex_index )
{
newNormal.V[0] += pTempNormals[face_index].V[0];
newNormal.V[1] += pTempNormals[face_index].V[1];
newNormal.V[2] += pTempNormals[face_index].V[2];

shared_vertices++;
}
}

if( shared_vertices &gt; 0 )
shared_vertices = shared_vertices;

// Averaging the perpendicular vectors //
newNormal.V[0] /= ((float)shared_vertices);
newNormal.V[1] /= ((float)shared_vertices);
newNormal.V[2] /= ((float)shared_vertices);

// Normalizing the vector //
currObj-&gt;m_Normals[vertex_index] = Normal3v( &newNormal );

shared_vertices = 0;
}

delete [] pTempNormals;
}

return true;
``````

}

Have fun.

Yeah, blood angel brought up a good point…
Are you drawing you polygons properly?

Yeah, Its all ok with polygons draw