Archived

This topic is now archived and is closed to further replies.

malyskolacek

index buffer for a cube

Recommended Posts

malyskolacek    121
I don''t know how to construct an index buffer for a cube defined with these vertices: 7---8 /| /| 3-5-4-6 |/ |/ 1---2 //struct BOX3D {D3DXVECTOR4 position; DWORD color;}; BOX3D *bbox; bbox=new BOX3D[8]; //pMin and pMax contain box dimensions bbox[0].position=D3DXVECTOR4(pMin.x, pMin.y, pMin.z, 1.0f); bbox[1].position=D3DXVECTOR4(pMax.x, pMin.y, pMin.z, 1.0f); bbox[2].position=D3DXVECTOR4(pMin.x, pMax.y, pMin.z, 1.0f); bbox[3].position=D3DXVECTOR4(pMax.x, pMax.y, pMin.z, 1.0f); bbox[4].position=D3DXVECTOR4(pMin.x, pMin.y, pMax.z, 1.0f); bbox[5].position=D3DXVECTOR4(pMax.x, pMin.y, pMax.z, 1.0f); bbox[6].position=D3DXVECTOR4(pMin.x, pMax.y, pMax.z, 1.0f); bbox[7].position=D3DXVECTOR4(pMax.x, pMax.y, pMax.z, 1.0f); for (int k=0;k<8;k++) bbox[k].color=0xffff0000; //how to fill index buffer??? WORD *pIndices; pIndices=new WORD[??]; Thanks in advance

Share this post


Link to post
Share on other sites
_stalker    122
I guess your points are ok if you''re not using any normals.
But as soon as you want to use normal you need more points then that. Here is the points with normals and index buffer. Render it as triangle list. Hope it helps..
p - position vector
n - normal vector

// front vertices
m_pvObjectVertices[0].p = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
m_pvObjectVertices[1].p = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);
m_pvObjectVertices[2].p = D3DXVECTOR3(1.0f, 1.0f, -1.0f);
m_pvObjectVertices[3].p = D3DXVECTOR3(1.0f, -1.0f, -1.0f);
// back vertices
m_pvObjectVertices[4].p = D3DXVECTOR3(-1.0f, -1.0f, 1.0f);
m_pvObjectVertices[5].p = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);
m_pvObjectVertices[6].p = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
m_pvObjectVertices[7].p = D3DXVECTOR3(1.0f, -1.0f, 1.0f);
// top vertices
m_pvObjectVertices[8].p = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);
m_pvObjectVertices[9].p = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);
m_pvObjectVertices[10].p = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
m_pvObjectVertices[11].p = D3DXVECTOR3(1.0f, 1.0f, -1.0f);
// bottom vertices
m_pvObjectVertices[12].p = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
m_pvObjectVertices[13].p = D3DXVECTOR3(-1.0f, -1.0f, 1.0f);
m_pvObjectVertices[14].p = D3DXVECTOR3(1.0f, -1.0f, 1.0f);
m_pvObjectVertices[15].p = D3DXVECTOR3(1.0f, -1.0f, -1.0f);
// left vertices
m_pvObjectVertices[16].p = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);
m_pvObjectVertices[17].p = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);
m_pvObjectVertices[18].p = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
m_pvObjectVertices[19].p = D3DXVECTOR3(-1.0f, -1.0f, 1.0f);
// right vertices
m_pvObjectVertices[20].p = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
m_pvObjectVertices[21].p = D3DXVECTOR3(1.0f, 1.0f, -1.0f);
m_pvObjectVertices[22].p = D3DXVECTOR3(1.0f, -1.0f, -1.0f);
m_pvObjectVertices[23].p = D3DXVECTOR3(1.0f, -1.0f, 1.0f);

// top normals
m_pvObjectVertices[0].n = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
m_pvObjectVertices[1].n = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
m_pvObjectVertices[2].n = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
m_pvObjectVertices[3].n = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
// bottom normals
m_pvObjectVertices[4].n = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
m_pvObjectVertices[5].n = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
m_pvObjectVertices[6].n = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
m_pvObjectVertices[7].n = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
// top normals
m_pvObjectVertices[8].n = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
m_pvObjectVertices[9].n = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
m_pvObjectVertices[10].n = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
m_pvObjectVertices[11].n = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
// bottom normals
m_pvObjectVertices[12].n = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
m_pvObjectVertices[13].n = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
m_pvObjectVertices[14].n = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
m_pvObjectVertices[15].n = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
// left normals
m_pvObjectVertices[16].n = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
m_pvObjectVertices[17].n = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
m_pvObjectVertices[18].n = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
m_pvObjectVertices[19].n = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
// right normals
m_pvObjectVertices[20].n = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
m_pvObjectVertices[21].n = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
m_pvObjectVertices[22].n = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
m_pvObjectVertices[23].n = D3DXVECTOR3(1.0f, 0.0f, 0.0f);

m_pwObjectIndices[0] = 0;
m_pwObjectIndices[1] = 1;
m_pwObjectIndices[2] = 2;
m_pwObjectIndices[3] = 0;
m_pwObjectIndices[4] = 2;
m_pwObjectIndices[5] = 3;

m_pwObjectIndices[6] = 6;
m_pwObjectIndices[7] = 5;
m_pwObjectIndices[8] = 4;
m_pwObjectIndices[9] = 7;
m_pwObjectIndices[10] = 6;
m_pwObjectIndices[11] = 4;

m_pwObjectIndices[12] = 8;
m_pwObjectIndices[13] = 9;
m_pwObjectIndices[14] = 10;
m_pwObjectIndices[15] = 8;
m_pwObjectIndices[16] = 10;
m_pwObjectIndices[17] = 11;

m_pwObjectIndices[18] = 14;
m_pwObjectIndices[19] = 13;
m_pwObjectIndices[20] = 12;
m_pwObjectIndices[21] = 15;
m_pwObjectIndices[22] = 14;
m_pwObjectIndices[23] = 12;

m_pwObjectIndices[24] = 16;
m_pwObjectIndices[25] = 17;
m_pwObjectIndices[26] = 18;
m_pwObjectIndices[27] = 16;
m_pwObjectIndices[28] = 18;
m_pwObjectIndices[29] = 19;

m_pwObjectIndices[30] = 22;
m_pwObjectIndices[31] = 21;
m_pwObjectIndices[32] = 20;
m_pwObjectIndices[33] = 23;
m_pwObjectIndices[34] = 22;
m_pwObjectIndices[35] = 20;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You don''t have to use four vertices for every side of the cube. Since you want sharp edges between the sides you could just use flat shading. You could use something like this:

v1.p = {-1,1,-1}
v1.p = {0,0,-1}
v2.p = {1,1,-1}
v2.n = {0,-1,0}
v3.p = {-1,-1,-1}
v3.n = {0,0,0}
v4.p = {1,-1,-1}
v4.n = {1,0,0)
v5.p = {-1,1,1}
v5.n = {0,1,0}
v6.p = {1,1,1}
v6.n = {0,0,1}
v7.p = {-1,-1,1}
v7.n = {-1,0,0}
v8.p = {1,-1,1)
v8.n = {0,0,0}

with this index buffer:

{1,2,4,1,2,3,
4,2,6,4,6,8,
6,5,7,6,7,8,
7,5,1,7,1,3,
3,4,8,3,8,7,
5,6,2,5,2,1}

One vertex is used as the first vertex for both of the triangles that make up a face, and that vertex''s normal is the face normal. Just turn on flat shading and it should work.

Share this post


Link to post
Share on other sites