Sign in to follow this  
Swatter555

2D Tiling in D3D: Incorrect dynamic buffer usage

Recommended Posts

I am creating a lit 2D tiling map in D3D. Basically, my basic tile is a 6 vert square that is 16x16. I fill the buffer with 2 for loops, each screen is 58x38 squares. The vb is dynamic, I discard and refill the buffer on map scroll. The problem is, the normal data is not correct. I have debugged it, and the processes and most of the data seems correct, except the normal data just will not change. I am calculating the face normals of the triangles, then averaging them. I thinking that I must be doing something fundementally wrong, because I cant find the error in the code. Forgive me for the data dump, here is some code. Any help would be very valuable :)
/////////////////
//VERTEX_SQUARE//
/////////////////

//The structure I use the keep height data

class VERTEX_SQUARE
{
public:
	//Vertex height data
	float v0,v1,v2,v3,v4,v5;

	//Vertice vectors
	//Vertex 0
	D3DVECTOR vec0;
	D3DVECTOR vec1;
	D3DVECTOR vec2;
	D3DVECTOR vec3;
	D3DVECTOR vec4;
	D3DVECTOR vec5;

	//Normal faces
	D3DVECTOR triangle0;
	D3DVECTOR triangle1;

	//Vertex normal values stored in vector
	D3DVECTOR normals0;
	D3DVECTOR normals1;
	D3DVECTOR normals2;
	D3DVECTOR normals3;
	D3DVECTOR normals4;
	D3DVECTOR normals5;

	//Surrounding square normals
	D3DVECTOR nw,n,ne;
	D3DVECTOR w,e;
	D3DVECTOR sw,s,se;
};
/////////////////
//VERTEX_SQUARE//
/////////////////


////////////////////
//Init_vb_main_map//
////////////////////

int RENDER_CLASS::Init_vb_main_map()
{
	//Setup D3D_VERTEX pointer
	D3D_VERTEX* Vertex = NULL;
	int offset_x    = 0;
	int offset_y    = 0;
	int x_          = -512;
	int y_          =  384;
	int wrld_x      = g_Dx_Obj.pos_x_axis; //Starting map position,used for height array
	int wrld_y      = g_Dx_Obj.pos_y_axis; //Starting map position,used for height array
	
	//Lock the vertex buffer
	vb_main_map->Lock(0,0 * sizeof(D3D_VERTEX),(void**)&Vertex,D3DLOCK_DISCARD);

	//Fill map buffer
	for(int y = 0; y < 12768; y+=336)
	{
		for(int x = 0; x < 336; x+=6)
		{
			//Compute the face normals for the triangles
			 //Triangle 0
			 Terrain_Data.Height_Map[wrld_x][wrld_y].triangle0 =
		     g_Dx_Obj.ComputeFaceNormals(Terrain_Data.Height_Map[wrld_x][wrld_y].vec0,
				                         Terrain_Data.Height_Map[wrld_x][wrld_y].vec1,
				                         Terrain_Data.Height_Map[wrld_x][wrld_y].vec2);

			 //Triangle 1
			 Terrain_Data.Height_Map[wrld_x][wrld_y].triangle1 =
		     g_Dx_Obj.ComputeFaceNormals(Terrain_Data.Height_Map[wrld_x][wrld_y].vec3,
				                         Terrain_Data.Height_Map[wrld_x][wrld_y].vec4,
				                         Terrain_Data.Height_Map[wrld_x][wrld_y].vec5);


			 //Find the average normal from surrounding squares
			//Vertex  0
			Terrain_Data.Height_Map[wrld_x][wrld_y].normals0 =
			g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].w,
				                           Terrain_Data.Height_Map[wrld_x][wrld_y].sw,
										   Terrain_Data.Height_Map[wrld_x][wrld_y].s);


			//Vertex  1
			Terrain_Data.Height_Map[wrld_x][wrld_y].normals1 =
			g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].nw,
				                           Terrain_Data.Height_Map[wrld_x][wrld_y].n,
										   Terrain_Data.Height_Map[wrld_x][wrld_y].w);

			//Vertex  2
			Terrain_Data.Height_Map[wrld_x][wrld_y].normals2 =
			g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].n,
				                           Terrain_Data.Height_Map[wrld_x][wrld_y].ne,
										   Terrain_Data.Height_Map[wrld_x][wrld_y].e);


			//Vertex  3
			Terrain_Data.Height_Map[wrld_x][wrld_y].normals3 =
			g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].w,
				                           Terrain_Data.Height_Map[wrld_x][wrld_y].sw,
										   Terrain_Data.Height_Map[wrld_x][wrld_y].s);

			

			//Vertex  4
			Terrain_Data.Height_Map[wrld_x][wrld_y].normals4 =
			g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].n,
				                           Terrain_Data.Height_Map[wrld_x][wrld_y].ne,
										   Terrain_Data.Height_Map[wrld_x][wrld_y].e);

			
			//Vertex  5
			Terrain_Data.Height_Map[wrld_x][wrld_y].normals5 =
			g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].w,
				                           Terrain_Data.Height_Map[wrld_x][wrld_y].sw,
										   Terrain_Data.Height_Map[wrld_x][wrld_y].s);




			//Update vertex 0, triangle 0
			Vertex[y+x+0].x  = (0 + offset_x + x_);
			Vertex[y+x+0].y  = (-16 + offset_y + y_);
			Vertex[y+x+0].z  = Terrain_Data.Height_Map[wrld_x][wrld_y].v0;
			Vertex[y+x+0].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals0.x;
			Vertex[y+x+0].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals0.y;
			Vertex[y+x+0].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals0.z;
			Vertex[y+x+0].u  = 0;
			Vertex[y+x+0].v  = 1;

			//Put vector data in height map
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec0.x = Vertex[y+x+0].x;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec0.y = Vertex[y+x+0].y;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec0.z = Vertex[y+x+0].z;
			//----------------------------------------------------------------

			//Update vertex 1, triangle 0
			Vertex[y+x+1].x  = (0 + offset_x + x_);
			Vertex[y+x+1].y  = (0 + offset_y + y_);
			Vertex[y+x+1].z  = Terrain_Data.Height_Map[wrld_x][wrld_y].v1;
			Vertex[y+x+1].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals1.x;
			Vertex[y+x+1].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals1.y;
			Vertex[y+x+1].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals1.z;
			Vertex[y+x+1].u  = 0;
			Vertex[y+x+1].v  = 0;

			//Put vector data in height map
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec1.x = Vertex[y+x+1].x;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec1.y = Vertex[y+x+1].y;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec1.z = Vertex[y+x+1].z;
			//----------------------------------------------------------------

			//Update vertex 2, triangle 0
			Vertex[y+x+2].x  = (16 + offset_x + x_);
			Vertex[y+x+2].y  = (0 + offset_y + y_);
			Vertex[y+x+2].z  = Terrain_Data.Height_Map[wrld_x][wrld_y].v2;
			Vertex[y+x+2].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals2.x;
			Vertex[y+x+2].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals2.y;
			Vertex[y+x+2].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals2.z;
			Vertex[y+x+2].u  = 1;
			Vertex[y+x+2].v  = 0;

			//Put vector data in height map
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec2.x = Vertex[y+x+2].x;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec2.y = Vertex[y+x+2].y;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec2.z = Vertex[y+x+2].z;
			//----------------------------------------------------------------

			//Update vertex 3, triangle 1
			Vertex[y+x+3].x  = (0 + offset_x + x_);
			Vertex[y+x+3].y  = (-16 + offset_y + y_);
			Vertex[y+x+3].z  = Terrain_Data.Height_Map[wrld_x][wrld_y].v3;
			Vertex[y+x+3].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals3.x;
			Vertex[y+x+3].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals3.y;
			Vertex[y+x+3].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals3.z;
			Vertex[y+x+3].u  = 0;
			Vertex[y+x+3].v  = 1;

			//Put vector data in height map
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec3.x = Vertex[y+x+3].x;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec3.y = Vertex[y+x+3].y;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec3.z = Vertex[y+x+3].z;
			//----------------------------------------------------------------

			//Update vertex 4, triangle 1
			Vertex[y+x+4].x  = (16 + offset_x + x_);
			Vertex[y+x+4].y  = (0 + offset_y + y_);
			Vertex[y+x+4].z  = Terrain_Data.Height_Map[wrld_x][wrld_y].v4;
			Vertex[y+x+4].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals4.x;
			Vertex[y+x+4].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals4.y;
			Vertex[y+x+4].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals4.z;
			Vertex[y+x+4].u  = 1;
			Vertex[y+x+4].v  = 0;

			//Put vector data in height map
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec4.x = Vertex[y+x+4].x;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec4.y = Vertex[y+x+4].y;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec4.z = Vertex[y+x+4].z;
			//----------------------------------------------------------------

			//Update vertex 5, triangle 1
			Vertex[y+x+5].x  = (16 + offset_x + x_);
			Vertex[y+x+5].y  = (-16 + offset_y + y_);
			Vertex[y+x+5].z  = Terrain_Data.Height_Map[wrld_x][wrld_y].v5;
			Vertex[y+x+5].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals5.x;
			Vertex[y+x+5].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals5.y;
			Vertex[y+x+5].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals5.z;
			Vertex[y+x+5].u  = 1;
			Vertex[y+x+5].v  = 1;

			//Put vector data in height map
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec5.x = Vertex[y+x+5].x;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec5.y = Vertex[y+x+5].y;
			Terrain_Data.Height_Map[wrld_x][wrld_y].vec5.z = Vertex[y+x+5].z;
			//----------------------------------------------------------------
	
			//Update loop variables
			offset_x += 16;
			++wrld_x;

		}//end x for

		//Update loop variables
		offset_x  = 0;
		offset_y -= 16;
		++wrld_y;
		wrld_x = g_Dx_Obj.pos_x_axis;

	}//end y for

	//Unlock the vertex buffer
	vb_main_map->Unlock();

	return(1);
}
////////////////////////
//End Init_vb_main_map//
////////////////////////



Share this post


Link to post
Share on other sites
I realized I needed to make a clarfication. I have my program setup to change the z values of the vertices with mouse input. The z values in the height data structure are altered by the input, but the normals just arent working right.

Share this post


Link to post
Share on other sites
Well, after more fiddling, I think I have narrowed down the problems.

If I change the z-value of the square uniformly, it seems to cancel out the normal to 0 or something.

Then Im having problems averaging out the normals. What I am doing is simply taking the triangle normal from an adjacent square(appropriate to the vertice) and sending that vector into a function to be averaged out.

The function that finds the adjacent triangle normal seems to logically work right... Im stumped. Ill post some updated code and a few more functions.


/////////////////
//VERTEX_SQUARE//
/////////////////

class VERTEX_SQUARE
{
public:
//Vertex height data
float v0,v1,v2,v3,v4,v5;

//Vertice vectors
//Vertex 0
D3DXVECTOR3 vec0;
D3DXVECTOR3 vec1;
D3DXVECTOR3 vec2;
D3DXVECTOR3 vec3;
D3DXVECTOR3 vec4;
D3DXVECTOR3 vec5;

//Normal faces
D3DXVECTOR3 triangle0;
D3DXVECTOR3 triangle1;

//Vertex normal values stored in vector
D3DVECTOR normals0;
D3DVECTOR normals1;
D3DVECTOR normals2;
D3DVECTOR normals3;
D3DVECTOR normals4;
D3DVECTOR normals5;

//Surrounding square normals
D3DXVECTOR3 nw,n,ne;
D3DXVECTOR3 w,e;
D3DXVECTOR3 sw,s,se;
};
/////////////////
//VERTEX_SQUARE//
/////////////////

////////////////////
//Init_vb_main_map//
////////////////////

int RENDER_CLASS::Init_vb_main_map()
{
//Setup D3D_VERTEX pointer
D3D_VERTEX* Vertex = NULL;
int offset_x = 0;
int offset_y = 0;
int x_ = -512;
int y_ = 384;
int wrld_x = g_Dx_Obj.pos_x_axis; //Starting map position,used for height array
int wrld_y = g_Dx_Obj.pos_y_axis; //Starting map position,used for height array

//Lock the vertex buffer
vb_main_map->Lock(0,0 * sizeof(D3D_VERTEX),(void**)&Vertex,D3DLOCK_DISCARD);

//Fill map buffer
for(int y = 0; y < 12768; y+=336)
{
for(int x = 0; x < 336; x+=6)
{
//Compute the face normals for the triangles
//Triangle 0
Terrain_Data.Height_Map[wrld_x][wrld_y].triangle0 =
g_Dx_Obj.ComputeFaceNormals(Terrain_Data.Height_Map[wrld_x][wrld_y].vec0,
Terrain_Data.Height_Map[wrld_x][wrld_y].vec1,
Terrain_Data.Height_Map[wrld_x][wrld_y].vec2);

//Triangle 1
Terrain_Data.Height_Map[wrld_x][wrld_y].triangle1 =
g_Dx_Obj.ComputeFaceNormals(Terrain_Data.Height_Map[wrld_x][wrld_y].vec3,
Terrain_Data.Height_Map[wrld_x][wrld_y].vec4,
Terrain_Data.Height_Map[wrld_x][wrld_y].vec5);


//Find the average normal from surrounding squares
//Vertex 0
Terrain_Data.Height_Map[wrld_x][wrld_y].normals0 =
g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].w,
Terrain_Data.Height_Map[wrld_x][wrld_y].sw,
Terrain_Data.Height_Map[wrld_x][wrld_y].s);


//Vertex 1
Terrain_Data.Height_Map[wrld_x][wrld_y].normals1 =
g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].nw,
Terrain_Data.Height_Map[wrld_x][wrld_y].n,
Terrain_Data.Height_Map[wrld_x][wrld_y].w);

//Vertex 2
Terrain_Data.Height_Map[wrld_x][wrld_y].normals2 =
g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].n,
Terrain_Data.Height_Map[wrld_x][wrld_y].ne,
Terrain_Data.Height_Map[wrld_x][wrld_y].e);


//Vertex 3
Terrain_Data.Height_Map[wrld_x][wrld_y].normals3 =
g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].w,
Terrain_Data.Height_Map[wrld_x][wrld_y].sw,
Terrain_Data.Height_Map[wrld_x][wrld_y].s);



//Vertex 4
Terrain_Data.Height_Map[wrld_x][wrld_y].normals4 =
g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].n,
Terrain_Data.Height_Map[wrld_x][wrld_y].ne,
Terrain_Data.Height_Map[wrld_x][wrld_y].e);


//Vertex 5
Terrain_Data.Height_Map[wrld_x][wrld_y].normals5 =
g_Dx_Obj.ComputeVertexNormal(3,Terrain_Data.Height_Map[wrld_x][wrld_y].w,
Terrain_Data.Height_Map[wrld_x][wrld_y].sw,
Terrain_Data.Height_Map[wrld_x][wrld_y].s);




//Update vertex 0, triangle 0
Vertex[y+x+0].x = (0 + offset_x + x_);
Vertex[y+x+0].y = (-16 + offset_y + y_);
Vertex[y+x+0].z = Terrain_Data.Height_Map[wrld_x][wrld_y].v0;
Vertex[y+x+0].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals0.x;
Vertex[y+x+0].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals0.y;
Vertex[y+x+0].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals0.z;
Vertex[y+x+0].u = 0;
Vertex[y+x+0].v = 1;

//Put vector data in height map
Terrain_Data.Height_Map[wrld_x][wrld_y].vec0 = D3DXVECTOR3(Vertex[y+x+0].x,
Vertex[y+x+0].y,
Vertex[y+x+0].z);
//----------------------------------------------------------------

//Update vertex 1, triangle 0
Vertex[y+x+1].x = (0 + offset_x + x_);
Vertex[y+x+1].y = (0 + offset_y + y_);
Vertex[y+x+1].z = Terrain_Data.Height_Map[wrld_x][wrld_y].v1;
Vertex[y+x+1].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals1.x;
Vertex[y+x+1].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals1.y;
Vertex[y+x+1].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals1.z;
Vertex[y+x+1].u = 0;
Vertex[y+x+1].v = 0;

//Put vector data in height map
Terrain_Data.Height_Map[wrld_x][wrld_y].vec1 = D3DXVECTOR3(Vertex[y+x+1].x,
Vertex[y+x+1].y,
Vertex[y+x+1].z);
//----------------------------------------------------------------

//Update vertex 2, triangle 0
Vertex[y+x+2].x = (16 + offset_x + x_);
Vertex[y+x+2].y = (0 + offset_y + y_);
Vertex[y+x+2].z = Terrain_Data.Height_Map[wrld_x][wrld_y].v2;
Vertex[y+x+2].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals2.x;
Vertex[y+x+2].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals2.y;
Vertex[y+x+2].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals2.z;
Vertex[y+x+2].u = 1;
Vertex[y+x+2].v = 0;

//Put vector data in height map
Terrain_Data.Height_Map[wrld_x][wrld_y].vec2 = D3DXVECTOR3(Vertex[y+x+2].x,
Vertex[y+x+2].y,
Vertex[y+x+2].z);
//----------------------------------------------------------------

//Update vertex 3, triangle 1
Vertex[y+x+3].x = (0 + offset_x + x_);
Vertex[y+x+3].y = (-16 + offset_y + y_);
Vertex[y+x+3].z = Terrain_Data.Height_Map[wrld_x][wrld_y].v3;
Vertex[y+x+3].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals3.x;
Vertex[y+x+3].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals3.y;
Vertex[y+x+3].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals3.z;
Vertex[y+x+3].u = 0;
Vertex[y+x+3].v = 1;

//Put vector data in height map
Terrain_Data.Height_Map[wrld_x][wrld_y].vec3 = D3DXVECTOR3(Vertex[y+x+3].x,
Vertex[y+x+3].y,
Vertex[y+x+3].z);
//----------------------------------------------------------------

//Update vertex 4, triangle 1
Vertex[y+x+4].x = (16 + offset_x + x_);
Vertex[y+x+4].y = (0 + offset_y + y_);
Vertex[y+x+4].z = Terrain_Data.Height_Map[wrld_x][wrld_y].v4;
Vertex[y+x+4].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals4.x;
Vertex[y+x+4].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals4.y;
Vertex[y+x+4].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals4.z;
Vertex[y+x+4].u = 1;
Vertex[y+x+4].v = 0;

//Put vector data in height map
Terrain_Data.Height_Map[wrld_x][wrld_y].vec4 = D3DXVECTOR3(Vertex[y+x+4].x,
Vertex[y+x+4].y,
Vertex[y+x+4].z);
//----------------------------------------------------------------

//Update vertex 5, triangle 1
Vertex[y+x+5].x = (16 + offset_x + x_);
Vertex[y+x+5].y = (-16 + offset_y + y_);
Vertex[y+x+5].z = Terrain_Data.Height_Map[wrld_x][wrld_y].v5;
Vertex[y+x+5].nx = Terrain_Data.Height_Map[wrld_x][wrld_y].normals5.x;
Vertex[y+x+5].ny = Terrain_Data.Height_Map[wrld_x][wrld_y].normals5.y;
Vertex[y+x+5].nz = Terrain_Data.Height_Map[wrld_x][wrld_y].normals5.z;
Vertex[y+x+5].u = 1;
Vertex[y+x+5].v = 1;

//Put vector data in height map
Terrain_Data.Height_Map[wrld_x][wrld_y].vec5 = D3DXVECTOR3(Vertex[y+x+5].x,
Vertex[y+x+5].y,
Vertex[y+x+5].z);
//----------------------------------------------------------------

//Update loop variables
offset_x += 16;
++wrld_x;

}//end x for

//Update loop variables
offset_x = 0;
offset_y -= 16;
++wrld_y;
wrld_x = g_Dx_Obj.pos_x_axis;

}//end y for

//Unlock the vertex buffer
vb_main_map->Unlock();

return(1);
}
////////////////////////
//End Init_vb_main_map//
////////////////////////

//////////////////////
//ComputeFaceNormals//
//////////////////////

D3DXVECTOR3 D3D_ENGINE::ComputeFaceNormals(D3DXVECTOR3 vert_a,D3DXVECTOR3 vert_b,D3DXVECTOR3 vert_c)
{
//Return vector
D3DXVECTOR3 return_vector;

//Init vector
ZeroMemory(&return_vector,sizeof(return_vector));

//Find the average
D3DXVECTOR3 u = vert_b - vert_a;
D3DXVECTOR3 v = vert_c - vert_a;

//Cross multiply
D3DXVec3Cross(&return_vector,&u,&v);
D3DXVec3Normalize(&return_vector,&return_vector);

//Return the face normal
return(return_vector);
}
//////////////////////////
//End ComputeFaceNormals//
//////////////////////////


///////////////////////
//ComputeVertexNormal//
///////////////////////

D3DXVECTOR3 D3D_ENGINE::ComputeVertexNormal(int num,
D3DXVECTOR3 n0,
D3DXVECTOR3 n1,
D3DXVECTOR3 n2)
{
//The return vector
D3DXVECTOR3 averaged_vector;

//Compute based on number
switch(num)
{
//Borders 2 triangles
case(2):
{
//Average the triangle normals
averaged_vector = (n0 + n1) / 1.5;
}break;

//Borders 3 triangles
case(3):
{
//Average the triangle normals
averaged_vector = (n0 + n1 + n2) / 1.33;
}break;

default:break;
}//end switch

return(averaged_vector);
}
///////////////////////////
//End ComputeVertexNormal//
///////////////////////////

//////////////////
//SetSurrSquares//
//////////////////

void TERRAIN_DATA::SetSurrSquares()
{
//This function sets the normals for the
//squares surrounding current

//Zero vector
D3DVECTOR zero_vector;

//Init zero_vector
ZeroMemory(&zero_vector,sizeof(zero_vector));

//Set surroundin squares for each square
for(int y = 0; y < 114; ++y)
{
for(int x = 0; x < 168; ++x)
{

//Northwest square---------------------------------------
//There is no square if
if(x == 0 || y == 0)
{
Height_Map[x][y].nw = zero_vector;
}//end if

else
{
Height_Map[x][y].nw = Height_Map[x - 1][y - 1].triangle1;
}//end else
//-------------------------------------------------------

//North square-------------------------------------------
//There is no square if
if(y == 0)
{
Height_Map[x][y].n = zero_vector;
}//end if

else
{
Height_Map[x][y].n = Height_Map[x][y - 1].triangle1;
}//end else
//-------------------------------------------------------

//Northeast square---------------------------------------
//There is no square if
if(y == 0 || x == 167)
{
Height_Map[x][y].ne = zero_vector;
}//end if

else
{
Height_Map[x][y].ne = ((Height_Map[x + 1][y - 1].triangle1 + Height_Map[x + 1][y - 1].triangle0) / 1.5);
}//end else
//-------------------------------------------------------

//West square--------------------------------------------
//There is no square if
if(x == 0)
{
Height_Map[x][y].w = zero_vector;
}//end if

else
{
Height_Map[x][y].w = Height_Map[x - 1][y].triangle1;
}//end else
//-------------------------------------------------------

//East square--------------------------------------------
//There is no square if
if(x == 167)
{
Height_Map[x][y].e = zero_vector;
}//end if

else
{
Height_Map[x][y].e = Height_Map[x + 1][y].triangle0;
}//end else
//-------------------------------------------------------

//Southwest square---------------------------------------
//There is no square if
if(x == 0 || y == 113)
{
Height_Map[x][y].sw = zero_vector;
}//end if

else
{
Height_Map[x][y].sw = ((Height_Map[x - 1][y + 1].triangle1 + Height_Map[x - 1][y + 1].triangle0) / 1.5);
}//end else
//-------------------------------------------------------

//South square-------------------------------------------
//There is no square if
if(y == 113)
{
Height_Map[x][y].s = zero_vector;
}//end if

else
{
Height_Map[x][y].s = Height_Map[x][y + 1].triangle0;
}//end else
//-------------------------------------------------------

//Southeast square---------------------------------------
//There is no square if
if(x == 167 || y == 113)
{
Height_Map[x][y].se = zero_vector;
}//end if

else
{
Height_Map[x][y].se = Height_Map[x + 1][y + 1].triangle0;
}//end else
//-------------------------------------------------------
}//end x for

}//end y for

}
//////////////////////
//End SetSurrSquares//
//////////////////////






Any advice would be much appreciated.

Share this post


Link to post
Share on other sites
What a conversation Im having with myself!

I have had a huge brainfart. When I am averaging the triangles, im ignoring the other triangle in the quad! DOH!

Im going to rework my vertices and my normal finding function and see if that solves my problems.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this