PDA

View Full Version : Lights and normals



TheBlob
12-27-2001, 02:52 AM
Hi!
My code renders a very beautiful landscape via a heightfield. Itīs done with
GL_TRIANGLE_STRIP. Works fine except the f**** lighting. And canīt get the normals to work properly. Is there anything to consider if Iīm using GL_TRIANGLE_STRIP instead of GL_TRIANGLE ??




for(int y=int(y_start); y<int(y_end)-1; y++)
{


glBegin(GL_TRIANGLE_STRIP);


for(int x=int(x_start); x<int(x_end)-1; x++)
{



//FIRST TRIANGLE......

glColor3f(heightfield[(y*field_dim)+x].color[0],heightfield[(y*field_dim)+x].color[1],heightfield[(y*field_dim)+x].color[2]);

glNormal3f(heightfield[(y*field_dim)+x].normal.x, heightfield[(y*field_dim)+x].normal.y, heightfield[(y*field_dim)+x].normal.z);

glTexCoord2f(heightfield[(y*field_dim)+x].texture_coo.x,heightfield[(y*field_dim)+x].texture_coo.y);
glVertex3f(heightfield[(y*field_dim)+x].point.x,heightfield[(y*field_dim)+x].point.y,heightfield[(y*field_dim)+x].point.z);


//SECOND TRIANGLE......

glColor3f(heightfield[((y+1)*field_dim)+x+1].color[0],heightfield[y+1)*field_dim)+x+1].color[1],heightfield[y+1)*field_dim)+x+1].color[2]);

glNormal3f(heightfield[y+1)*field_dim)+x+1].normal.x, heightfield[y+1)*field_dim)+x+1].normal.y, heightfield[y+1)*field_dim)+x+1].normal.z);

glTexCoord2f(heightfield[y+1)*field_dim)+x+1].texture_coo.x,heightfield[y+1)*field_dim)+x+1].texture_coo.y);
glVertex3f(heightfield[y+1)*field_dim)+x+1].point.x,heightfield[y+1)*field_dim)+x+1].point.y,heightfield[y+1)*field_dim)+x+1].point.z);

}




glEnd();
}



I donīt know how to set the normals there???

[This message has been edited by TheBlob (edited 12-27-2001).]

Bob
12-27-2001, 02:59 AM
Things that you don't mention.

Do you enable color material (if you don't, using both glColor and glNormal is useless)?
Do you enable lighting?
Do you enable the light you want to use?
Do you setup the light properly?
Do you calculate the normals properly?
What happens? Do you see anything at all? Are the triangle strips unlit?

TheBlob
12-27-2001, 03:07 AM
Well thanks so far.
The trianglestrip is rendered and colored correctly, so glColorMaterial is enabled.
The thing is, that the triangles arenīt correctly shaded.
I modifeis my code so i can now move the light through the world and if the light position is 0.0f,0.0f,0.0f nothing is shaded, but when i move the light it seems for me that lighting is turned on!? But still it looks very wrong, but thats maybe because i calculate the normals wrong....

Bob
12-27-2001, 03:26 AM
If the light looks like it's off when located at (0,0,0), it can be that you set a directional light. Do you set the fourth coordinate to zero also? Then you have a vector that points nowhere. It can also be that the light is below the terrain, and the light is shining on the back face.

Can also be normals, post the code you use to calculate them.

TheBlob
12-27-2001, 04:49 AM
Here is my code to calculate the face-normals.



for(y=0; y<field_dim; y++)
{

for(int x=0; x<field_dim; x++)
{

temp_normal[y*(field_dim)+x][0] = P3D_Calculate_Normal(heightfield[y*field_dim+x].point,
heightfield[(y+1)*field_dim+x].point,
heightfield[(y+1)*field_dim+x+1].point);

temp_normal[y*(field_dim)+x][1] = P3D_Calculate_Normal(heightfield[y*field_dim+x].point,
heightfield[(y)*field_dim+x+1].point,
heightfield[(y+1)*field_dim+x+1].point);

}


}


And hereīs the code for P3D_Calculate_Normal




// Points p1, p2, & p3 specified in counter clock-wise order
CL_VEKTOR P3D_Calculate_Normal(CL_VEKTOR param_v1, CL_VEKTOR param_v2, CL_VEKTOR param_v3)
{

CL_VEKTOR out;
CL_VEKTOR v1,v2;


// Calculate two vectors from the three points
v1.x = param_v1.x - param_v2.x;
v1.y = param_v1.y - param_v2.y;
v1.z = param_v1.z - param_v2.z;

v2.x = param_v2.x - param_v3.x;
v2.y = param_v2.y - param_v3.y;
v2.z = param_v2.z - param_v3.z;

// Take the cross product of the two vectors to get
// the normal vector which will be stored in out
out.x = v1.y*v2.z - v1.z*v2.y;
out.y = v1.z*v2.x - v1.x*v2.z;
out.z = v1.x*v2.y - v1.y*v2.x;
//out = v1.CrossProduct(v2);

// Normalize the vector (shorten length to one)
out.Normalize();

return out;
}


Well maybe itīs hard to figure out a mistake, but i would really appreciate any help.
I also compute Vertexnormals, but this is really weird and doesnīt work at all.

chowe6685
12-27-2001, 06:53 PM
I only glanced at your code, but when you calculate normals you generate three parts, the x the y and the z via the cross product, thats fine. But the y part needs to be the opposite, that is
out.x = v1.y*v2.z - v1.z*v2.y; out.y = -(v1.z*v2.x - v1.x*v2.z); out.z = v1.x*v2.y - v1.y*v2.x;
that might help