Advertisement Jump to content
Sign in to follow this  

Rendering from a vertex buffer

This topic is 4893 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

I have recently begun programming in Direct X and have come across a somewhat trivial problem! I have modified code found in the vertices sample of the Direct X sample browser to fill a verrtex buffer and render from it! The problem I have come across is that the verts seem to be rendered by screen coordinates and not in world space. Any help would be appreciated!

Share this post

Link to post
Share on other sites
If the vertices are hard coded then the floating point values you put in there will the coor. of the VB. Am I on the same page, or not?

Share this post

Link to post
Share on other sites
you can put the vertices into the buffer in any coordinate system you want. The matrices (World/View/Projection) perform the neccessary transformation to put the triangles on screen.

Share this post

Link to post
Share on other sites
Ok, thanks for your replies and sorry for my late reply (I lost the thread).

Here is my code, sorry for the size, ive included everything!

Here is the code I use to generate my Vertex and Index buffers and populate them;

HRESULT CTerrain::CreateTerrain( LPDIRECT3DDEVICE9 m_pd3dDevice )
HRESULT hResult;

///Dimension Variables
D3DXVECTOR3 minBounds;
D3DXVECTOR3 maxBounds;

//Define boundaries
minBounds.x = 100; minBounds.y = 0; minBounds.z = 100;
maxBounds.x = 400; maxBounds.y = 1; maxBounds.z = 400;

//Define Cell dimensions
int numCellsWide = 1;
int numCellsHigh = 1;

//Calculate number of verts on each axis
int numVertsX = numCellsWide + 1;
int numVertsZ = numCellsHigh + 1;

//Calculate total number of verts
NumVerts = numVertsX * numVertsZ;

//Calculate total number of polygons
NumPolygons = (numCellsWide * numCellsHigh) * 2;

//Calculate step between each vertex
float stepX = (maxBounds.x - minBounds.x) / numCellsWide;
float stepZ = (maxBounds.z - minBounds.z) / numCellsHigh;

// Set the start position
D3DXVECTOR3 pos(minBounds.x, 0, maxBounds.z);

//Initialise counter
int count = 0;

//Create custom data structure variable to hold info

// Loop across and down
for (int z = 0; z < numVertsZ ;z++)
//Set X position
pos.x = minBounds.x;

for (int x = 0; x < numVertsX; x++)
// Create the verts
pVertices[count].vInfo = D3DXVECTOR4( pos.x, pos.z, 0.0f, 1.0f );
pVertices[count].color = 0xffff0f00;

// Increment x across
pos.x += stepX;

// Increment Z down
pos.z -= stepZ;

////Vertex Buffer
// Create a vextex buffer for the viewport
if( FAILED( m_pd3dDevice->CreateVertexBuffer( NumVerts * sizeof(TERRAINVERTEX),
D3DPOOL_MANAGED, &m_TerrainVB, NULL ) ) )
return DXTRACE_ERR( TEXT("m_pd3dDevice->CreateVertexBuffer"), S_OK);

//Custom vertex format

m_TerrainVB->Lock( 0, 0, (void**)&Verts, 0 );

//Copy Vertex data across
memcpy( Verts, pVertices, sizeof(pVertices) );

//Unlock buffer

////Index Buffer
int NumIndices = (numCellsWide * 6) * numCellsHigh;

// Create an index buffer for the Terrain
if( FAILED( m_pd3dDevice->CreateIndexBuffer( NumIndices * sizeof(WORD),
D3DPOOL_MANAGED, &m_TerrainIB, NULL ) ) )
return false;

//Reinitialise Count
count = 0;
int vIndex = 0;
WORD* pIndices;
WORD* Indices = new WORD[NumIndices];

for (int z = 0; z < numCellsHigh; z++)
for (int x = 0;x < numCellsWide; x++)
// first triangle
Indices[count++] = (WORD)vIndex;
Indices[count++] = (WORD)(vIndex + 1);
Indices[count++] = (WORD)(vIndex + numVertsX);

// second triangle
Indices[count++] = (WORD)(vIndex + numVertsX);
Indices[count++] = (WORD)(vIndex + 1);
Indices[count++] = (WORD)(vIndex + numVertsX + 1);



//Get a pointer to the index buffer indices and lock the index buffer
m_TerrainIB->Lock(0, NumIndices * sizeof(WORD), (void**)&pIndices, 0);

//Copy indices data across
memcpy( pIndices, Indices, NumIndices * sizeof(WORD) );

//Unlock the index buffer

return S_OK;

And This is my rendering code;

BOOL CTerrain::RenderTerrain( LPDIRECT3DDEVICE9 m_pd3dDevice )
D3DXMATRIX matTerrainWorld;

D3DXMatrixIdentity( &matTerrainWorld );

m_pd3dDevice->SetTransform( D3DTS_WORLD, &matTerrainWorld );

D3DMATERIAL9 m_matMaterial;

//Set material default values (R, G, B, A)
D3DCOLORVALUE rgbaDiffuse = {1.0f, 1.0f, 1.0f, 0.0f};
D3DCOLORVALUE rgbaAmbient = {1.0, 1.0, 1.0, 0.0};
D3DCOLORVALUE rgbaEmissive = {0.0, 0.0, 0.0, 0.0};

//Set the RGBA for diffuse light reflected from this material.
m_matMaterial.Diffuse = rgbaDiffuse;

//Set the RGBA for ambient light reflected from this material.
m_matMaterial.Ambient = rgbaAmbient;

//Set the RGBA for light emitted from this material.
m_matMaterial.Emissive = rgbaEmissive;

//Select the material to use

// Tell DX from which index buffer you want to render. We have our indices
// stored in m_pIndexBuffer, so we tell dx to take indices from there.

//Set vertex format and terrain vertex buffer as stream source
m_pd3dDevice->SetStreamSource(0, m_TerrainVB, 0, sizeof(TERRAINVERTEX));

//Draw the terrain
m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, NumVerts, 0, NumPolygons);

return TRUE;

This code generates nothing on my screen at all but I cant for the life of me figure out whats wrong.

Share this post

Link to post
Share on other sites
I have to bump this thread!

Just to clear up, im expecting just a simple flat grid (hence no need for normal calculations just yet). If I pass in just three vertices to the Vertex Buffer then I can succesfully render a triangle but it is in screen space, I dont get anything however when I use vertices for my terrain grid. I have tried adjusting the world matrix and setting it before rendering but no cigar!

I guess there is something glaringly obvious to the experienced eye but I cant see anything that would stop the rendering. Currently I see nothing on the screen with this code, which is annoying as you an imagine!

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!