Jump to content
  • Advertisement
Sign in to follow this  
idloco

Surface Normals How to? [FIXED]

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

If you intended to correct an error in the post then please contact us.

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 )
LoadMap()
{
	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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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);
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!