# Surface Normals How to? [FIXED]

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How can i generate the surface normals from a terrain? i have tried using this but seems it's bad, because the results im getting are not the ones i spected...
ComputeNormal(&out, &tempNormal1, &tempNormal2, &tempNormal3 )
{
D3DXVECTOR3 u = *v1 - *v0;
D3DXVECTOR3 v = *v2 - *v0;
D3DXVec3Cross(out, &u, &v);
D3DXVec3Normalize(out, out);
}

// This is for every vertex on the map ( is a 100*100 map )
{
D3DXVECTOR3 out;
D3DXVECTOR3 tempNormal1;
D3DXVECTOR3 tempNormal2;
D3DXVECTOR3 tempNormal3;

tempNormal1.x = (float)x;
tempNormal1.y = (float)y;
tempNormal1.z = (float)z;

tempNormal2.x = (float)x+1;
tempNormal2.y = (float)y;
tempNormal2.z = (float)z;

tempNormal3.x = (float)x;
tempNormal3.y = (float)y+1;
tempNormal3.z = (float)z;

ComputeNormal(&out, &tempNormal1, &tempNormal2, &tempNormal3 );
}


I don't know what im doing wrong. any help will be welcome.. // Sorry for my english, it's not my first language [Edited by - idloco on June 7, 2005 9:13:02 PM]

##### Share on other sites
Hi!
Here you have an algorithm to generate terrain shadows, where at first step calculates terrain normals:
http://www.gamedev.net/reference/articles/article1817.asp

Hope it helps you ;)

##### Share on other sites
thx but i did't find the part that help me.. :S

##### Share on other sites
idloco,

If the code you've provided is C++, as I assume it's supposed to be, then it should not even compile.

Please paste in the code exactly as you have it in your source files, as that will make it easier for us to help you. As it is now, it appears as though you have extra information going into your ComputeNormal function which could be a potential problem if you're using the wrong parameters.

As well, none of your parameters match the names of variables used in your functions. Finally, if LoadMap is being called for every vertex please pass along the iterator code so we can see how you're traversing the vertices.

##### Share on other sites
Here is the entire code of the terrain, i manage the terrain in sections

#define SECTION_SIZE 25/************************************************************************//* Terrain Section Load Function                                        *//* pHeightMap = the height map for the terrain                          *//* pX = the x position of the terrain section                           *//* pY = the y position of the terrain section                           *//************************************************************************/HRESULT CTerrainSection::LoadSection(unsigned char* pHeightMap, uint16 pX, uint16 pY){	uiXPos = pX*(SECTION_SIZE);	uiYPos = pY*(SECTION_SIZE);	TheLog.log_graphics_normal("Loading Map Section Started......");	m_dwSectionVertices = (SECTION_SIZE+1 ) * (SECTION_SIZE +1 );	m_dwSectionPrimitives = (SECTION_SIZE * SECTION_SIZE * 2);	D3DVERTEX* pVertexData;	short* pIndexData;	D3DXVECTOR3 out;	D3DXVECTOR3 tempNormal1;	D3DXVECTOR3 tempNormal2;	D3DXVECTOR3 tempNormal3;	//create vertex buffer and fill in vertices	TheGraphics.GetDevice()->CreateVertexBuffer(sizeof(D3DVERTEX) * m_dwSectionVertices,D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&m_pSectionVB,NULL);	m_pSectionVB->Lock(0,0,(void**)&pVertexData,0);	for(int iy = 0; iy < ( SECTION_SIZE + 1 );++iy)	{		for(int ix = 0; ix < ( SECTION_SIZE + 1 );++ix)		{			tempNormal1.x = (float)ix+uiXPos;			tempNormal1.y = (float)iy+uiYPos;			tempNormal1.z = (float)(pHeightMap[(iy+uiYPos*100)+ix+uiXPos] / MAP_SCALE);			tempNormal2.x = (float)ix+uiXPos+1;			tempNormal2.y = (float)iy+uiYPos;			tempNormal2.z = (float)(pHeightMap[(iy+uiYPos*100)+ix+1+uiXPos] / MAP_SCALE);			tempNormal3.x = (float)ix+uiXPos+1;			tempNormal3.y = (float)iy+uiYPos+1;			tempNormal3.z = (float)(pHeightMap[(iy+1+uiYPos*100)+ix+uiXPos] / MAP_SCALE);			ComputeNormal(&out, &tempNormal1, &tempNormal2, &tempNormal3 );//			D3DXVec3Normalize(&tempNormal, &tempNormal);			pVertexData[ix + iy * (SECTION_SIZE + 1)].v.x = (float)ix;			pVertexData[ix + iy * (SECTION_SIZE + 1)].v.y = (float)iy;			pVertexData[ix + iy * (SECTION_SIZE + 1)].v.z = (float)pHeightMap[((iy+uiYPos)*100)+(ix+uiXPos)] / MAP_SCALE;			pVertexData[ix + iy * (SECTION_SIZE + 1)].dwColor = 0xffffffff;			pVertexData[ix + iy * (SECTION_SIZE + 1)].n = out;			pVertexData[ix + iy * (SECTION_SIZE + 1)].tu1 = (float)(ix);			pVertexData[ix + iy * (SECTION_SIZE + 1)].tv1 = (float)(iy);			pVertexData[ix + iy * (SECTION_SIZE + 1)].tu2 = (float)(ix);			pVertexData[ix + iy * (SECTION_SIZE + 1)].tv2 = (float)(iy);		}	}	m_pSectionVB->Unlock();	//create index buffer and fill in indices	TheGraphics.GetDevice()->CreateIndexBuffer(sizeof(short) * m_dwSectionPrimitives * 3,D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&m_pSectionIB,NULL);	m_pSectionIB->Lock(0,0,(void**)&pIndexData,0);	bool notIndex = true;	int x=SECTION_SIZE; 	int y=SECTION_SIZE;	if (uiXPos == 75)		x--;		if(uiYPos == 75)		y--;	for(int iy = 0;iy < y ;++iy)	{		for(int ix = 0;ix < x;++ix)		{			*pIndexData++ = ix + iy * (SECTION_SIZE + 1);			*pIndexData++ = ix + 1 + iy * (SECTION_SIZE + 1);			*pIndexData++ = ix + 1 + (iy + 1) * (SECTION_SIZE+ 1);			*pIndexData++ = ix + iy * (SECTION_SIZE + 1);			*pIndexData++ = ix + 1 + (iy + 1) * (SECTION_SIZE + 1);			*pIndexData++ = ix + (iy + 1) * (SECTION_SIZE + 1);		}	}	m_pSectionIB->Unlock();		TheLog.log_graphics_normal("Loading Map Section Ended......");	return S_OK;}void CTerrainSection::RenderSection(){	D3DXMATRIX SectionMat;	D3DXMatrixIdentity(&SectionMat);	D3DXMatrixTranslation(&SectionMat, (float)uiXPos, (float)uiYPos, 0.0f);		TheGraphics.GetDevice()->SetTransform(D3DTS_WORLD, &SectionMat);		TheGraphics.GetDevice()->SetStreamSource(0,m_pSectionVB,0,sizeof(D3DVERTEX));	TheGraphics.GetDevice()->SetIndices(m_pSectionIB);	TheGraphics.GetDevice()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_dwSectionVertices, 0, m_dwSectionPrimitives);}void CTerrainSection::ComputeNormal(D3DXVECTOR3* out, D3DXVECTOR3* v0, D3DXVECTOR3* v1, D3DXVECTOR3* v2){	D3DXVECTOR3 u = *v1 - *v0;	D3DXVECTOR3 v = *v2 - *v0;	D3DXVec3Cross(out, &u, &v);	D3DXVec3Normalize(out, out);}

bump

thx all!!!

• 23
• 10
• 19
• 15
• 14