Jump to content
  • Advertisement
Sign in to follow this  
slyprid

Vertex Buffers and Tiling

This topic is 4553 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've been working on a tiling engine in VC++ to learn DirectX and VC++ better, and so far i'm able to tile a single tile to the screen using a vertex buffer. What I can't figure out is how to render multiple. I've done it a while back in C#, but I can't find the code I used so i'm back to square one. The way I seem to remember doing it in C# was create one quad, and transform on each render to create new tiles. The only thing is I'm not sure how to go about that in C++. Here is what i've got so far. My vertex structure
struct CUSTOMVERTEX
{
    float x, y, z; // Position in 3d space
    DWORD color;   // Color 
    float u, v;    // Texture coordinates
};

I declare my vertex buffer, but when I try to create an array for doing it the non-transform method I get compile errors.
CUSTOMVERTEX g_vertices[] = 
{
    {-1.0f,  1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 0.0f, 0.0f},
    { 1.0f,  1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0.0f},
    { 1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 1.0f},
    {-1.0f,  1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 0.0f, 0.0f},
    { 1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 1.0f},
    {-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 0.0f, 1.0f},
};

void CGameApp::OnRenderFrame( LPDIRECT3DDEVICE9 pDevice, float elapsedTime )
{
    pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 ); 
    pDevice->BeginScene();

    pDevice->SetTransform( D3DTS_WORLD, m_transform.GetTransform() );
    m_VB.Render( pDevice, 2, D3DPT_TRIANGLELIST );
    
    pDevice->EndScene();
    pDevice->Present( 0, 0, 0, 0 );
}

I thought if I could make an array and fill in the vertex buffer with all the individual coordinates and render them it would work fine, but I run into more issues that route. Also is it better to do tiling using the transform method, or the locking and unlocking with an array. I want to add in lighting and such next, but not sure if its possible with one method or both. Thanks for your time, Slyprid

Share this post


Link to post
Share on other sites
Advertisement
Well I got the translate and transform method to work, but for some reason I have to put each command in, it won't display anything if I use a for..loop. Here is the dual tile method.

This method will display two tiles, but I would need to copy one for every tile.

m_transform.TranslateRel(0.0f,0.0f,0.0f);
pDevice->SetTransform( D3DTS_WORLD, m_transform.GetTransform());
m_VB.Render( pDevice, 2, D3DPT_TRIANGLELIST );

m_transform.TranslateRel(1.0f,0.0f,0.0f);
pDevice->SetTransform( D3DTS_WORLD, m_transform.GetTransform());
m_VB.Render( pDevice, 2, D3DPT_TRIANGLELIST );





This method seems to logically work and it compiles fine, but when I run it it displays no tiles.

float x = -1.0f;
float y = 1.0f;
for(int i = 0; i > 5; i++)
{
m_transform.TranslateRel(x,y,0.0f);
pDevice->SetTransform( D3DTS_WORLD, m_transform.GetTransform());
m_VB.Render( pDevice, 2, D3DPT_TRIANGLELIST );
if(((i + 1) % 8) == 0)
{
x = -1.0f;
y -= 0.25f;
}
else
{
x += 0.25f;
}
}



Any ideas why this is happening? Thanks for your time.

Slyprid

Share this post


Link to post
Share on other sites
CUSTOMVERTEX g_vertices[] =
{
{-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 0.0f, 0.0f},
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0.0f},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 1.0f},
{-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 0.0f, 0.0f},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB( 255, 255, 255 ), 0.0f, 1.0f},
};

about this array definition the last ',' is unnecessary and will cause compliation errors.

your for condition is not correct (i > 5) , change it to (i < 5).
with your condition the for loop initializes i = 0 and as long as i > 5 it will be iterated, but i = 0 and the contidion is not met, so your for loop block will never be executed.

Share this post


Link to post
Share on other sites
Geez, that fixed it. Can't believe I put in the wrong sign. Guess thats where a second set of eyes helps for debugging. Couldn't figure out for the life of me why it wouldn't work, the logic behind it worked in my head and all. Thanks for the help again. Oh I tried removing the last line of the vertex, and it only renders half of my quad. Not sure why on that either.

Slyprid

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!