-
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
-
They are the eight 3-D coordinates of the corners of the cube, stored sequentially in an array.
-
So.... why does it have -1.0f and 1.0f in the paremeters? What does that do?
-
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...
-
Thanks, i think i'm starting to gradually understand it, but how do you know it's the lower left back corner and such?
-
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.
-
Oh ok, i get it now, thanks...
-
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.
-
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." :)
-
Direct3D Z increases into the screen - or away from the camera. OpenGL is just the opposite.
-
So... (-1.0f, -1.0f, -1.0f) would be the lower left front corner?
-
the lower left back corner if I understand well
-
No, front corner is correct in DirectX.
-
Eh, but that would meen that the z axis would point toward the camera.
I'm confused....
-