Advertisement

problem on computing normals

Started by July 30, 2003 05:00 PM
2 comments, last by lilKen 21 years, 7 months ago
greetings , For a few days now I'm bashing my head to the wall with this aparently simple thing, per vertex normals... Context: I'm currently playing with heightmaps rendered through a vertex array.The geometry is arranged in a strip of triangles .The algorithm is quite simple , derived from gametutorials.com's heightmapping tutorials to fit terragen's 257*257 byte raw file format exporter:

#define MAP_SIZE_XY 257
#define STEP_SIZE 8

//excerpt from loader:

for ( X = 0; X < MAP_SIZE_XY-1; X += STEP_SIZE )
	{
		if(bSwitchSides)
		{	
			for ( Y = MAP_SIZE_XY-1-STEP_SIZE ; Y > 0; Y -= STEP_SIZE )
			{
				x = X +vPos.X;							
				y = iHeight(X, Y );
				z = Y;							
			pVerts[uiNrVerts].X=x;
			pVerts[uiNrVerts].Y=y;
			pVerts[uiNrVerts].Z=z;
			pTexVerts[uiNrVerts].x=(float)x/(float)MAP_SIZE_XY;
			pTexVerts[uiNrVerts].y=(float)z/(float)MAP_SIZE_XY;
			pIndices[uiNrVerts]=uiNrVerts;
			uiNrVerts++;
				x = X + STEP_SIZE +vPos.X; 
				y = y = iHeight(X + STEP_SIZE, Y ); 
				z = Y;
			pVerts[uiNrVerts].X=x;
			pVerts[uiNrVerts].Y=y;
			pVerts[uiNrVerts].Z=z;
			pTexVerts[uiNrVerts].x=(float)x/(float)MAP_SIZE_XY;
			pTexVerts[uiNrVerts].y=(float)z/(float)MAP_SIZE_XY;
			pIndices[uiNrVerts]=uiNrVerts;	
			uiNrVerts++;
			}
		}
		else
		{	
			for (Y = STEP_SIZE; Y < MAP_SIZE_XY-1; Y += STEP_SIZE ) 
			{
				x = X + STEP_SIZE +vPos.X; 
				y = iHeight(X + STEP_SIZE, Y ); 
				z = Y;
			pVerts[uiNrVerts].X=x;
			pVerts[uiNrVerts].Y=y;
			pVerts[uiNrVerts].Z=z;
			pTexVerts[uiNrVerts].x=(float)x/(float)MAP_SIZE_XY;
			pTexVerts[uiNrVerts].y=(float)z/(float)MAP_SIZE_XY;
			pIndices[uiNrVerts]=uiNrVerts;
			uiNrVerts++;
				x = X +vPos.X;							
				y = iHeight(X, Y );	
				z = Y;							
			pVerts[uiNrVerts].X=x;
			pVerts[uiNrVerts].Y=y;
			pVerts[uiNrVerts].Z=z;
			pTexVerts[uiNrVerts].x=(float)x/(float)MAP_SIZE_XY;
			pTexVerts[uiNrVerts].y=(float)z/(float)MAP_SIZE_XY;
			pIndices[uiNrVerts]=uiNrVerts;
			uiNrVerts++;
			}
		}
		
		bSwitchSides = !bSwitchSides;
	}
//now comes the "per face normal" computations based on the existing vertex array


	CVECTOR vVector1,vVector2;
	CVECTOR *pTempNormals=new CVECTOR[uiNrVerts];
	CVECTOR *pTempNormalsPerVertex=new CVECTOR[uiNrVerts];

	bool bSwitchSides=false;
	for(int i=0;i<uiNrVerts-2;i++)
	{
		if(bSwitchSides)
		{
		
			vVector1=pVerts[i]-pVerts[i+1];
			vVector2=pVerts[i+1]-pVerts[i+2];
			pTempNormals[i]=vVector1.vCrossProduct(vVector2);
			pTempNormals[i].vNormalize();
			pNormals[i]=pTempNormals[i];
		}
		else
		{
			vVector1=pVerts[i-1]-pVerts[i];
			vVector2=pVerts[i]-pVerts[i+1];
			pTempNormals[i]=vVector1.vCrossProduct(vVector2);
			pTempNormals[i].vNormalize();
			pNormals[i]=pTempNormals[i];
		}
		bSwitchSides=!bSwitchSides;
	}
//the next part should get the per vertex normals by averaging all the normals of the //faces sharing the same vertice

	for(int i=0;i<uiNrVerts;i++)
	{
		pTempNormalsPerVertex[i]=pNormals[i-2]+pNormals[i-1]+pNormals[i]+pNormals[i-3+(int)(MAP_SIZE_XY/STEP_SIZE)]+pNormals[i-2+(int)(MAP_SIZE_XY/STEP_SIZE)]+pNormals[i-1+(int)(MAP_SIZE_XY/STEP_SIZE)];
		pTempNormalsPerVertex[i].vDivideVectorByScalar(6);
		pTempNormalsPerVertex[i].vNormalize();
	}
	pNormals=pTempNormalsPerVertex;
}

Now this obviously doesnt work the way it should . I could post some screenshots eventualy or even a set of binaries if needed.Thanks in advance [edited by - lilken on July 30, 2003 6:05:26 PM]
---sorry for not using proper english , I'm from latin roots
Not that this answers your question, but what''s the point of dividing the vector by a scalar (6) if you are just gonna normalize it on the next line?
Advertisement
at first I didnt normalize the normal , and still kept it there because of a minus to that scalar that I might need to change the direction of the normal.
---sorry for not using proper english , I'm from latin roots
So what goes wrong then?

This topic is closed to new replies.

Advertisement