![]() |
| | #1 |
| The Right Honourable Join Date: Mar 2004 Location: Where circles begin.
Posts: 1,068
| Heightfield Texture Coords What I mean is, can/how are texture coords from drawing a texture over the entire terrain, obtained from the terrains vertex data. To actually state the question properly; how do you generate texture coords for a heightfield terrain? ![]() terrain rendering code: Code: glBegin(GL_TRIANGLES);
for(NxU32 i=0;i<HEIGHTFIELD_NB_FACES;i++)
{
glNormal3fv(&gHeightfieldNormals[gHeightfieldFaces[i*3+0]].x);
glVertex3fv(&gHeightfieldVerts[gHeightfieldFaces[i*3+0]].x);
glNormal3fv(&gHeightfieldNormals[gHeightfieldFaces[i*3+1]].x);
glVertex3fv(&gHeightfieldVerts[gHeightfieldFaces[i*3+1]].x);
glNormal3fv(&gHeightfieldNormals[gHeightfieldFaces[i*3+2]].x);
glVertex3fv(&gHeightfieldVerts[gHeightfieldFaces[i*3+2]].x);
}
glEnd();
__________________ Memorial University of Newfoundland Computer Science Mac and OpenGL evangelist. |
| psychopath is offline | |
| | #2 |
| Super Moderator Join Date: Aug 2001
Posts: 7,814
| Are you tiling the terrain? If not it's: Code: float uinc=1.0f/width;
float vinc=1.0f/height;
float tu=0.0f;
float tv=0.0f;
for (float i=startx;....)
{
for (float j=starty;...)
{
..Create vertex at i,j with texture coords of tu,tv
tu+=uinc
}
tv+=vinc;
}
Code: void CTerrain::Init(IDirect3DDevice9 *Device,
int NumVertsPerRow,
int NumVertsPerCol,
int CellSize)
{
m_pDevice=Device;
m_iCellSize=CellSize;
m_iNumVertsPerCol=NumVertsPerCol;
m_iNumVertsPerRow=NumVertsPerRow;
m_iNumCellsPerRow=NumVertsPerRow-1;
m_iNumCellsPerCol=NumVertsPerCol-1;
m_iWidth=m_iNumCellsPerRow*CellSize;
m_iDepth=m_iNumCellsPerCol*CellSize;
m_iNumVertices=m_iNumVertsPerRow*m_iNumVertsPerCol;
m_iNumTris=m_iNumCellsPerRow*m_iNumCellsPerCol*2;
m_pDevice->CreateVertexBuffer(m_iNumVertices*sizeof(TerrainVertex),
D3DUSAGE_DYNAMIC,
TerrainVertex::FVF,
D3DPOOL_DEFAULT,
&m_pVB,
NULL);
TerrainVertex *Vertices;
m_pVB->Lock(0,0,(void **)&Vertices,0);
int iStartX=-(m_iWidth>>1);
int iStartZ=(m_iDepth>>1);
int iEndX=m_iWidth>>1;
int iEndZ=-m_iDepth>>1;
unsigned int vnum=0;
float fAngle=0.0f;
int i=0;
int j=0;
int index=0;
float angle=0.0f;
float vy=0.0f;
float uinc=1.0f/(float)(m_iWidth/2);
float vinc=1.0f/(float)(m_iDepth/2);
for (int vz=iStartZ;vz>=iEndZ;vz-=m_iCellSize)
{
for (int vx=iStartX;vx<=iEndX;vx+=m_iCellSize)
{
index=i*m_iNumVertsPerRow+j;
Vertices[index]=TerrainVertex(D3DXVECTOR3((float)vx,0.0f,(float)vz),
vx*uinc,
vz*vinc);
Vertices[index].Diffuse=D3DXCOLOR(0.0f,1.0f,0.0f,1.0f);
Vertices[index].r=0.0f;
Vertices[index].g=1.0f;
Vertices[index].b=0.0f;
j++;
}
i++;
j=0;
}
m_pVB->Unlock();
m_pDevice->CreateIndexBuffer(m_iNumTris*3*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_pIB,
0);
WORD *Indices=0;
m_pIB->Lock(0,0,(void **)&Indices,0);
unsigned int baseindex=0;
i=0;j=0;
for (i=0;i<m_iNumCellsPerCol;i++)
{
for (int j=0;j<m_iNumCellsPerRow;j++)
{
Indices[baseindex]= i * m_iNumVertsPerRow+ j;
Indices[baseindex+1]= i * m_iNumVertsPerRow+ j +1;
Indices[baseindex+2]= (i+1) * m_iNumVertsPerRow+ j;
Indices[baseindex+3]= (i+1) * m_iNumVertsPerRow+ j;
Indices[baseindex+4]= i * m_iNumVertsPerRow+ j +1;
Indices[baseindex+5]= (i+1) * m_iNumVertsPerRow+ j +1;
baseindex+=6;
}
}
m_pIB->Unlock();
}
I perturb the terrain with this sphere algo. Essentially it picks a size and height value for the sphere. Then it perturbs all affected vertices relative to it's distance from the sphere. It's slow to generate on the fly, but you could generate the data, save it, and load it later to speed the process up. Code: void CTerrain::SphereGenerate(int numiterations)
{
TerrainVertex *Vertices;
m_pVB->Lock(0,0,(void **)&Vertices,0);
int num=0;
do
{
int centerx=rand () % m_iNumVertsPerCol;
int centery=rand () % m_iNumVertsPerRow;
int size=15+rand () % 200;
int height=-400 + (rand () % 800);
for (int i=0;i<m_iNumCellsPerCol;i++)
{
for (int j=0;j<m_iNumCellsPerRow;j++)
{
int index=i*m_iNumCellsPerRow+j;
int distx=centerx-i;
int disty=centery-j;
float dist=sqrt((distx*distx)+(disty*disty));
dist*=2.0f;
dist/=(float)size;
if (fabs(dist)<=1.0f)
{
float height2=(float)height *.5f;
Vertices[index].Position.y+=height2+cosf(dist*3.14f)*height2;
}
}
}
num++;
} while (num<numiterations);
m_pVB->Unlock();
}
Here is the end result...with some fogging added.
__________________ If you aim at everything you will hit something but you won't know what it is. Last edited by Bubba; 10-03-2005 at 09:58 AM. |
| Bubba is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| d3d9 c++ texture wrapper... again... | yaya | Game Programming | 0 | 04-01-2009 01:08 PM |
| D3d Texture Wrapper == ARRRGGGGHHH | yaya | Game Programming | 1 | 03-25-2009 06:41 PM |
| Why doesn't glbindtexture recognize the texture id I pass it? | Shamino | Game Programming | 4 | 02-27-2008 03:10 PM |
| OpenGL - look trough surface -nehe lesson | GanglyLamb | Game Programming | 8 | 08-24-2006 11:06 PM |
| Pong is completed!!! | Shamino | Game Programming | 11 | 05-26-2005 10:50 AM |