PDA

View Full Version : Lightning - Help Me!!!

lsdi
05-06-2002, 08:43 PM
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 );
}

05-06-2002, 11:13 PM
Are you sure for the normals?

MPech
05-06-2002, 11:17 PM
I do agree, there is nothing wrong with your code here, it may be a problem of normals.

How do you build them ?

lsdi
05-07-2002, 06:54 PM
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;
}

blood.angel
05-07-2002, 07:54 PM

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

lsdi
05-07-2002, 08:07 PM
same problem http://www.opengl.org/discussion_boards/ubb/frown.gif

Miguel_dup1
05-07-2002, 08:59 PM
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...

Miguel_dup1
05-07-2002, 09:02 PM
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...

lsdi
05-07-2002, 09:04 PM
How to average the normals?

blood.angel
05-07-2002, 09:30 PM
Are you sure you have all your triangles ordered the right way? Either all clockwise or anticlockwise?

Miguel_dup1
05-07-2002, 09:53 PM
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 < numOfObjects; obj_index++ )
{
currObj = &(m_Objects[obj_index]);

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

for( int face_index = 0; face_index < currObj->numOfFaces; face_index++ )
{
temp_polygons[0] = currObj->m_Vertices[currObj->m_Faces[face_index].VertexIndex[0]];
temp_polygons[1] = currObj->m_Vertices[currObj->m_Faces[face_index].VertexIndex[1]];
temp_polygons[2] = currObj->m_Vertices[currObj->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 < currObj->numOfVertices; vertex_index++ )
{
memset(&newNormal, 0, sizeof(vector3));
//>--------------------------------------------------------------------<//
// Computing average perpendicular vectors //
//>--------------------------------------------------------------------<//
//
for( int face_index = 0; face_index < currObj->numOfFaces; face_index++ )
{
if( currObj->m_Faces[face_index].VertexIndex[0] == vertex_index &#0124; &#0124;
currObj->m_Faces[face_index].VertexIndex[1] == vertex_index &#0124; &#0124;
currObj->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 > 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->m_Normals[vertex_index] = Normal3v( &newNormal );

shared_vertices = 0;
}

delete [] pTempNormals;
}

return true;
}

Have fun. http://www.opengl.org/discussion_boards/ubb/smile.gif

Miguel_dup1
05-07-2002, 10:08 PM
Yeah, blood angel brought up a good point...
Are you drawing you polygons properly?

lsdi
05-08-2002, 09:10 AM
Yeah, Its all ok with polygons draw