Jump to content
  • Advertisement
Sign in to follow this  
brooksyd22

Rendering from a vertex buffer

This topic is 4823 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
Advertisement
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
TERRAINVERTEX* pVertices = new TERRAINVERTEX[NumVerts];

// 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;
count++;
}

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

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

//Custom vertex format
TERRAINVERTEX* Verts;

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

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

//Unlock buffer
m_TerrainVB->Unlock();

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

// Create an index buffer for the Terrain
if( FAILED( m_pd3dDevice->CreateIndexBuffer( NumIndices * sizeof(WORD),
0, D3DFMT_INDEX16,
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);

vIndex++;
}

vIndex++;
}

//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
m_TerrainIB->Unlock();

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
m_pd3dDevice->SetMaterial(&m_matMaterial);

// 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.
m_pd3dDevice->SetIndices(m_TerrainIB);

//Set vertex format and terrain vertex buffer as stream source
m_pd3dDevice->SetFVF(D3DFVF_TERRAINVERTEX);
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 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!