Skip to main content
GameDev.net gamedev.net
🔒 Locked

Terrain Normals

Started by guts Jul 14, 2003 at 8:04 AM 3 replies 450+ views
Original Post
guts
guts
Hi! I''m currently calculating vertices, texcoords and indices this way:
	bool SwitchSides = true;
	for(int x=0;x<=size-2;x+=step)
	{
		if(SwitchSides)
		{
			for(int y=size-1;y>=0;y-=step)
			{
				mpVertices[mNumVer].x = x;
				mpVertices[mNumVer].y =(int)GetHeight(x, y); 
				mpVertices[mNumVer].z =y;                
				mpTexcoords[mNumVer].x = mpVertices[x1].x / (float)size;
				mpTexcoords[mNumVer].y = mpVertices[x1].z / (float)size;
				mNumVer++;

				mpVertices[mNumVer].x = x + step;  
				mpVertices[mNumVer].y = (int)GetHeight(x + step, y);
				mpVertices[mNumVer].z = (int)y; 
				mpTexcoords[mNumVer].x = mpVertices[x1].x / (float)size; 
				mpTexcoords[mNumVer].y = mpVertices[x1].z / (float)size;
				mNumVer++;                                 
			}
		} 
		else 
		{
			for(int y=0;y<=size-1;y+=step)
			{
				mpVertices[mNumVer].x = x + step;  
				mpVertices[mNumVer].y =(int)GetHeight(x + step, y);
				mpVertices[mNumVer].z =(int) y;                          
				mpTexcoords[mNumVer].x = mpVertices[x1].x / (float)size; 
				mpTexcoords[mNumVer].y = mpVertices[x1].z / (float)size;  
				mNumVer++;

				mpVertices[mNumVer].x = (int)x;  
				mpVertices[mNumVer].y = (int)GetHeight(x, y);
				mpVertices[mNumVer].z = (int)y; 
				mpTexcoords[mNumVer].x = mpVertices[x1].x / (float)size; 
				mpTexcoords[mNumVer].y = mpVertices[x1].z / (float)size;
				mNumVer++;
			}
		}
		SwitchSides = !SwitchSides;
	}


	int idx = 0;
	mNumInd = mNumVer;
	mpIndices = new int[mNumInd];

	for (idx=0; idx < mNumInd; idx++)
		mpIndices[idx] = idx;
[/CODE]

My problem is that I don''t have an idea how to calculate normals for triangle strips.
Maybe this sounds kind of impertinent, but would somebody please post the source code that calculates these normals (I tried it for a long time and now I''m really sick of this...)

thanks in advance ...
guts   
Zorodius
Zorodius
The normal of a triangle is the cross product of two of its sides taken as vectors from a common vertex. (more info)

"Last time, I asked: ''What does mathematics mean to you?'' And some people answered: ''The manipulation of numbers, the manipulation of structures.'' And if I had asked what music means to you, would you have answered: ''The manipulation of notes?''" - S. Lang
You don't need to vote for the "lesser of two evils"! Learn about Instant Runoff Voting, the simple cure for a broken democracy!
guts
guts
Yes of course ...
But I want to calulate normals for each vertex...
edotorpedo
edotorpedo

Well, if you''re not required to calculate these normals each frame, here''s what I do (a rectangular grid provided):


CVector n;

n = position[i].getNormal(position[i + numVertices], position[i + 1]);

n.add(position[i].getNormal(position[i + 1], position[i - numVertices]));

n.add(position[i].getNormal(position[i - numVertices], position[i - 1]));

n.add(position[i].getNormal(position[i - 1], position[i + numVertices]));

n.normalize();
n.mirror();



Basically what I''m doing is averaging the normals created by up-right, right-down, down-left, left-up vertices. The getNormal() function returns the normal on position, if it''s connected with its parameter vertices in one triangle.

Edo
Edo
Sander
Sander
Calculate all the triangle normals. Then for each vertex, take the average of all the triangle normals that vertex is touching and normalize the result.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!

Topic Locked

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

Sign in to reply to this topic.