Thread: Vertices and Indices

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321

    Vertices and Indices

    Hi all.
    I am having a bit of trouble understanding vertices and indices...

    Code:
    Vertex* vertices;
    /*Lock the Vertex buffer to access it's memory*/
    VB->Lock(
            0, /*Offset, in bytes, from the start of the buffer to the location to begin the lock*/ 
            0, /*Number of bytes to lock*/
            (void**)&vertices, /*A pointer to the start of the locked memory, converted from Vertex** to void** */ 
            0); /*Flags describing how the lock is done*/
    
    /*Vertices of a unit cube*/
    vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
    vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
    vertices[2] = Vertex(1.0f, 1.0f, -1.0f);
    vertices[3] = Vertex(1.0f, -1.0f, -1.0f);
    vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
    vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
    vertices[6] = Vertex(1.0f, 1.0f, 1.0f);
    vertices[7] = Vertex(1.0f, -1.0f, 1.0f);
    
    VB->Unlock(); /*Unlock the Vertex buffer*/
    I understand the Lock/Unlock concept but i don't get this part...
    Code:
    /*Vertices of a unit cube*/
    vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
    vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
    vertices[2] = Vertex(1.0f, 1.0f, -1.0f);
    vertices[3] = Vertex(1.0f, -1.0f, -1.0f);
    vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
    vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
    vertices[6] = Vertex(1.0f, 1.0f, 1.0f);
    vertices[7] = Vertex(1.0f, -1.0f, 1.0f);
    Mayby if you help me understand this i might not have to ask about indices... Hopefully

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    They are the eight 3-D coordinates of the corners of the cube, stored sequentially in an array.

  3. #3
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    So.... why does it have -1.0f and 1.0f in the paremeters? What does that do?

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Those are the actual coordinates of the vertices corresponding to the corners of the cube.

    (-1.0f, -1.0f, -1.0f) is coordinate in 3-space (lower left back corner)
    (-1.0f, 1.0f, -1.0f) is another coordinate (upper left back corner)
    etc...

  5. #5
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Thanks, i think i'm starting to gradually understand it, but how do you know it's the lower left back corner and such?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because the way the X, Y and Z axis (sp?) are oriented is fixed in the base DirectX coordinate system: X points right, Y points up, and Z points ... can't remember. Either towards the viewer or away. It's one of the points where OpenGL and DirectX differ, and I can never remember which is which.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Oh ok, i get it now, thanks...

  8. #8
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Right... with Vertices i had an idea what was going on, but with Indices, i am completely stumped.

    Code:
    /*Define the triangles of the cube*/
    WORD* indices = 0;
    /*Lock the index buffer to access it's memory*/
    IB->Lock(
          0, /*Offset, in bytes, from the start of the buffer to the location to begin the lock*/
          0, /*Number of bytes to lock*/
          (void**)&indices, /*A pointer to the start of the locked memory, converted from WORD** to void** */ 
          0); /*Flags describing how the lock is done*/
    
    /*Front side*/
    indices[0]  = 0; indices[1]  = 1; indices[2]  = 2;
    indices[3]  = 0; indices[4]  = 2; indices[5]  = 3;
    /*Back side*/
    indices[6]  = 4; indices[7]  = 6; indices[8]  = 5;
    indices[9]  = 4; indices[10] = 7; indices[11] = 6;
    /*Left side*/
    indices[12] = 4; indices[13] = 5; indices[14] = 1;
    indices[15] = 4; indices[16] = 1; indices[17] = 0;
    /*Right side*/
    indices[18] = 3; indices[19] = 2; indices[20] = 6;
    indices[21] = 3; indices[22] = 6; indices[23] = 7;
    /*Top*/
    indices[24] = 1; indices[25] = 5; indices[26] = 6;
    indices[27] = 1; indices[28] = 6; indices[29] = 2;
    /*Bottom*/
    indices[30] = 4; indices[31] = 0; indices[32] = 3;
    indices[33] = 4; indices[34] = 3; indices[35] = 7;
    
    IB->Unlock(); /*Unlock the Index buffer*/
    Has anyone any idea what this means?
    I know, i'm a complete n00b.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Pretty simple. When you define a cube using vertices only, you need 12 triangles (2 per side) and thus 36 vertices. Yet among these 36 vertices, there are only 8 distinct ones. That's a huge waste of space, not to mention a lot of work to change.
    A better way is to define the 8 distinct vertices as an array and then compose the triangles of indices into this array. That's what the index buffer is for.

    Basically, replace every entry indices[i] with the expression vertices[indices[i]] and you've got the vertices that make up the triangles of the cube.

    This follows the old programming proverb, "Every problem can be solved by adding another level of indirection."
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Direct3D Z increases into the screen - or away from the camera. OpenGL is just the opposite.

  11. #11
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    So... (-1.0f, -1.0f, -1.0f) would be the lower left front corner?

  12. #12
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    the lower left back corner if I understand well

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, front corner is correct in DirectX.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Eh, but that would meen that the z axis would point toward the camera.
    I'm confused....

  15. #15
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying a heightmap
    By Dark_Phoenix in forum Game Programming
    Replies: 6
    Last Post: 11-11-2006, 04:24 PM
  2. Switch from .ms3d to the ASCII format?
    By Shamino in forum Game Programming
    Replies: 18
    Last Post: 01-23-2006, 09:28 PM
  3. Need help to understand this STL code.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:59 PM
  4. Appropriate data structure
    By gazsux in forum Game Programming
    Replies: 3
    Last Post: 03-19-2003, 01:26 PM
  5. What does 'indices' mean?
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2002, 07:54 AM