PDA

View Full Version : Triangle Strips & Specifying normals

roguespear
01-20-2005, 05:55 AM
Hiya guys i am trying to draw a surface covered in snow using triangle strips. With a specified height for each of the points i am trying to calculate the normals for each of the points so lighting in my scene works.

My code for drawing the triangle strips is:
void drawSnow()
{
glDisable(GL_TEXTURE_2D);
int X =0, Z = 0;
int arrayXPos = 0, arrayZPos = 0;
Vector3 temp[4], norm[3];
// Set the colour to a shade of white
glColor3f(0.7, 0.7, 0.8);

for(X =0; X < 1024; X += STEP_SIZE)
{
glBegin(GL_TRIANGLE_STRIP);

arrayZPos = 0;//(1024/STEP_SIZE);

for(Z = 0; Z < 1024; Z += STEP_SIZE)
{
temp[0].x = X;
temp[0].y = snowHeight[arrayXPos][arrayZPos];
temp[0].z = Z;

temp[1].x = X + STEP_SIZE;
temp[1].y = snowHeight[arrayXPos+1][arrayZPos];
temp[1].z = Z;

temp[2].x = X ;
temp[2].y = snowHeight[arrayXPos][arrayZPos+1];
temp[2].z = Z + STEP_SIZE;

norm[0] = temp[3].vector(temp[0],temp[1]);
norm[1] = temp[3].vector(temp[1],temp[2]);
norm[2] = temp[3].vector(temp[2],temp[0]);

temp[3] = temp[3].normal(norm);

glNormal3f(temp[3].x,temp[3].y,temp[3].z);
glVertex3i(temp[0].x, temp[0].y, temp[0].z);

temp[2].x = X + STEP_SIZE;
temp[2].y = snowHeight[arrayXPos+1][arrayZPos+1];
temp[2].z = Z + STEP_SIZE;

norm[0] = temp[3].vector(temp[0],temp[1]);
norm[1] = temp[3].vector(temp[1],temp[2]);
norm[2] = temp[3].vector(temp[2],temp[0]);

temp[3] = temp[3].normal(norm);

glNormal3f(temp[3].x,temp[3].y,temp[3].z);
glVertex3i(temp[1].x, temp[1].y, temp[1].z);

arrayZPos++;
}
arrayXPos++;
glEnd();
}

glEnable(GL_TEXTURE_2D);
} snowHeight is the 2D array that holds the height for each of the points. And the code that calculates the normals is:

Vector3 Vector3::normal(Vector3 polygonPoints[])
{
Vector3 Vec1 = vector(polygonPoints[2],polygonPoints[0]);
Vector3 Vec2 = vector(polygonPoints[1],polygonPoints[0]);

Vector3 normal = crossProduct(Vec1,Vec2);

normal = normalise(normal);

return normal;
}

Vector3 Vector3::crossProduct(Vector3 Vec1, Vector3 Vec2)
{
Vector3 normal;

normal.x = ((Vec1.y * Vec2.z) - (Vec1.z * Vec2.y));
normal.y = ((Vec1.z * Vec2.x) - (Vec1.x * Vec2.z));
normal.z = ((Vec1.x * Vec2.y) - (Vec1.y * Vec2.x));

return normal;
} When i run the code the normals do not match with the next triangle strip. You can tell where each strip stops and starts. Could someone please help me as this isnt even the main part of what i am trying to acheive but need lighting to work before i continue.

Tom

CrazyButcher
01-21-2005, 06:19 AM
precalculate all normals before rendering.
to get smooth normals every vertex must have a normalized sum of all triangle-normals that belong to it.

like you run thru all triangles and compute normals for them, and "add" it to all 3 vertices of that triangle.
then after that you run thru all vertices and normalize their normals.

this will give you smooth normals.

it doesnt work at "draw/runtime" because the moment you calculate a normal for the vertex it doesnt know what other triangles will be part of it, hence it cant be smooth with those others. thats why you need to do it before.

to speed up your rendering you could look into using vertex arrays, and store your values (position,normal) in arrays before rendering. Cause unless your snow doesnt change every frame, you only need to compute your terrain once.