Skip to main content
gamedev.net
🔒 Locked

Errors in computing surface normals

Started by wizard341 Nov 22, 2004 at 9:42 PM 0 replies 650+ views
Original Post
wizard341
wizard341
I've been struggling with this problem for a while now, hopefully someone can come help me out. I want to compute a surface normal for a triangle (for backface culling implemented in software), and to my knowledge my equations are correct, but the direction of the normals im getting is aqward. I have a vertex class, which isnt really important save for these functions...

Vector Vector::Normalize3PointSurface(Vector v1, Vector v2, Vector v3)
{
	Vector tempVector = v2 - v1;
	Vector tempVector2 = v3 - v1;
	Vector tempVector3 = Vector::crossProduct(tempVector, tempVector2);

	return tempVector3;

}

float Vector::dotProduct(const Vector &vec1, const Vector &vec2)
{
	return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
}
Vector Vector::crossProduct(const Vector &vec1, const Vector &vec2)
{
	return Vector( (vec1.y * vec2.z) - (vec1.z * vec2.y), (vec1.z * vec2.x) - (vec1.x * vec2.z), (vec1.x * vec2.y) - (vec1.y * vec2.x)); 
}
Now I have a pyramid, defined as such

Vector LowerRight(100.0f,100.0f,-10.0f);
Vector LowerLeft(-100.0f,100.0f,-10.0f);
Vector LowerRight2(100.0f,100.0f,-210.0f);
Vector LowerLeft2(-100.0f,100.0f,-210.0f);

Vector PyramidMiddle(0.0f,-100.0f,-110.0f);

Vector VertexPoints2[5] = {PyramidMiddle, LowerLeft, LowerRight, LowerRight2, LowerLeft2};
int SurfaceList2[4][3] = { {1,2,0}, {2,3,0}, {3,4,0}, {4,1,0} };
						//front face, right face, back face, left face

//structures to hold some data in the future 
Vector TriFaceNormals[4];
float TriFaceMagnitudes[4];
float TriFaceOrientations[4];

Vector ViewVector(0.0f,0.0f,-1.0f);

And then finally I draw the object:

void DrawPyramid()
{
	//calculate surface normals and normalize them
	for(int i = 0; i < 4; i++)
	{
		TriFaceNormals = Vector::Normalize3PointSurface(
			VertexPoints[SurfaceList[0]],
			VertexPoints[SurfaceList[1]],
			VertexPoints[SurfaceList[2]]);

		TriFaceNormals.normalize();
		TriFaceOrientations = Vector::dotProduct(VertexPoints[SurfaceList[0]], ViewVector);
	}

	
	glBegin(GL_TRIANGLES);
	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			glVertex3f(
			VertexPoints2[SurfaceList2[j]].x,
			VertexPoints2[SurfaceList2[j]].y,
			VertexPoints2[SurfaceList2[j]].z);
		}
	}
	glEnd();
}
The object draws perfectly fine, but the problem is when i step through in in debugging - the 'TriFaceOrientions' give me the values of 10, 210, 210, 10. Interestingly enough, If i change the z values way back when I define the object, those same values appear here as my 'TriFaceOrientations'. Something is definatly wrong here anyone have any ideas? Thanks in advance for your help!
lack o comments
lack o comments
I could be wrong about this. My math is pretty weak. But... are you sure you are calculating your cross-products correctly.

Shouldn't it be: (Uy*Vz)-(Vy*Uz),(-Ux*Vz)+(Vx*Uz),(Ux*Vy)-(Vx*Uy)

EDIT: Nevrmind. I see. (good lord am I bad at this)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.