Sign in to follow this  
Robert B Colton

D3D9 Terrain Trianglestrip and IndexBuffer

Recommended Posts

Hassanbasil    195

void hxObject::MakePlane ( float size, int xsegments, int zsegments )
{
//...
//...
int tris = ((xsegments+1)*(zsegments+1))*2;
int verts = (xsegments+2)*(zsegments+2);

hVertex* v;
if ( FAILED ( _baseMesh->LockVertexBuffer ( 0, (void**)&v ) ) )
{
_MainCore->PostError ( this, "MakePlane : Unable to lock the vertex buffer", false );
return;
}

//scaling vertex
hVertex vSize ( size/20, size/20, size/20, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f );
//centering vertex
hVertex vSubt ( 0.5f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f );

int vpc = zsegments+2, vpr =xsegments+2;
float uI = 1.0f/(float)(vpc-1);
float vI = 1.0f/(float)(vpr-1);

float _u = 0.0f, _v = 0.0f;
int curVert = 0;
for ( int z = 0; z < vpc; z++ )
{
for ( int x = 0; x < vpr; x++ )
{
v[curVert++] = ( hVertex ( (float)x/(vpr-1), 0.0f, (float)z/(vpc-1), 0.0f, 0.0f, 0.0f, _u, _v ) - vSubt ) * vSize;
_u += uI;
}
_v += vI;
_u = 0;
}

if ( FAILED ( _baseMesh->UnlockVertexBuffer ( ) ) )
{
_MainCore->PostError ( this, "MakePlane : Unable to unlock the vertex buffer", false );
return;
}

WORD *i;
if ( FAILED ( _baseMesh->LockIndexBuffer ( 0, (void**)&i ) ) )
{
_MainCore->PostError ( this, "MakePlane : Unable to lock the index buffer", false );
return;
}

//indices
int numt = 0;
int offset = 0;
for ( int t = 0; t < tris/2; t++ )
{
i[t*6 + 0] = 1 + offset;
i[t*6 + 1] = 0 + offset;
i[t*6 + 2] = vpr + offset;

i[t*6 + 3] = vpr + offset + 1;
i[t*6 + 4] = 1 + offset;
i[t*6 + 5] = vpr + offset;

numt++;
offset++;

if ( numt == vpr-1 )
{
offset++;
numt = 0;
}
}

if ( FAILED ( _baseMesh->UnlockIndexBuffer ( ) ) )
{
_MainCore->PostError ( this, "MakePlane : Unable to unlock the index buffer", false );
return;
}

//....
//.....
}





i had this working on my dx9 engine, note that the parameters x/y segments are not equal to columns and rows in the plain:
*___*___*___*
|../..|../..|../..|
*___*___*___* PLAIN ( * = vertex, . is empty )
|../..|../..|../..|
*___*___*___*

there are 2 x segments and 1 z segment over there, also note that the hVertex is my vertex structure, and it's constructor is (x,y,z,nx,ny,nz,u,v)

vSubt is used to keep the object's center at 0,0
vSize is used to re-scale the object

Hope you find that somewhat useful

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