XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Coloured Cubes - Data Structure

Last post 7/8/2007 1:28 PM by Leaf Garland. 4 replies.
  • 7/8/2007 6:41 AM

    Coloured Cubes - Data Structure

    Hello!

    OK, I've got some basic 3D stuff working, but I'm now wondering where to go from here.  So far, I have a rotating 3D cube, with vertices stored as VertexPositionNormalColoured type (i.e. untextured).  I then use a vertex buffer / index buffer to draw this.  All working fine.

    Now what I'd like to do is have lots of different cubes all at different positions.  By analogy to another thread I've seen, I think the best way to do this is to have a wrapper class containing (a) the world matrix for each cube and (b) a reference to the vertex buffer / index buffer above.  Does that sound sensible??  I think that should work fine - though could require multiple Draw() calls, one per cube?  Memory-wise, only one instance of the buffers, which is good.

    My next question is: what if I wanted different colours for the different cubes?  The issue then is, these are essentially different vertex buffers aren't they (given the colour is stored in the vertex).  Do I need (option 1) one vertex buffer per potential colour of box or what?

    Other options would be:
        (option 2) fake the colour change by altering the ambientLighting on the shader - but that seems hokey.  Or,
        (option 3) before drawing each cube, loop through the vertices in the buffer, update the colour, SetData again and then draw the cube.  But that will be slow. 

    Presumably the memory hit is the right move here?

    Finally - one more question!!  I've wrapped the vertex buffer / index buffer / vertices in a TriangleManager<T> class.  I was attempting (option 3) above, so I added my initial (bland) vertices to the TriangleManager and then subsequently went:

    foreach(VertexPositionColourNormal v in triangleManager.vertices)
    {
    v.SetColor(newColor);
    }

    Only problem is - nothing changes in the vertex list when I do this (despite solid code in the SetColor() function).  Will List<T> not allow this kind of updating??  I do then call SetData again, but the initial vertex colours persist.

    Thanks in advance for any tips!  Sorry for the long post, not sure what to cut out to make it shorter :|.

    Muzz
    Muzz http://www.PositronicArts.com/blog
  • 7/8/2007 7:21 AM In reply to

    Re: Coloured Cubes - Data Structure

    From what you describe it sounds like your cubes are each one single colour, so there's no need to store the colours in the vertices. You only want to store data per vertex when you know that the data will be different for each (or some number of) vertices.

    In your case I would just store the positions, normals and texture coordinates in the vertex buffer and set the colour using the shader. It sounds like you're using BasicEffect so you can just set BasicEffect.DiffuseColor before rendering each cube.

    In general you want to avoid changing vertex buffers after you've created them.

    Cheers,
    Leaf.
  • 7/8/2007 8:04 AM In reply to

    Re: Coloured Cubes - Data Structure

    Hi Leaf,

    Thanks for the speedy reply!  That solution works great for single-coloured cubes.  I'm not actually using BasicEffect - I wrote my own noddy shader - but got that working fine.  A much neater solution than anything I suggested!

    I had actually been experimenting with multi-coloured cubes - i.e. where each vertex did have a different colour associated with it. Any tips for that case?

    Thanks!

    Muzz

    PS. And will shortly be heading over to you XNA UK User Group page :-)
    Muzz http://www.PositronicArts.com/blog
  • 7/8/2007 10:20 AM In reply to

    Re: Coloured Cubes - Data Structure

    OK, I just saw the sample about Color Replacement.  I think I'll use that basis for anything multi-coloured (i.e. piggy-back off the alpha value).  Again it uses the Shader though, which I hadn't really thought of for this before!

    Thanks.

    Muzz
    Muzz http://www.PositronicArts.com/blog
  • 7/8/2007 1:28 PM In reply to

    Re: Coloured Cubes - Data Structure

    Cool, glad you got it sorted. There's so many ways you can split, combine, fabricate, refactor data for graphics when you're using programmable hardware, it is very exciting. Look forward to seeing you over on the XNA UKUG.

    Cheers,
    Leaf.
Page 1 of 1 (5 items) Previous Next