Skip to main content
GameDev.net gamedev.net
🔒 Locked

index buffer for a cube

Started by malyskolacek Oct 15, 2001 at 10:23 AM 2 replies 300+ views
Original Post
malyskolacek
malyskolacek
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
malyskolacek
malyskolacek
The vertices needn''t to be in my order. I simply want to render a cube...
_stalker
_stalker
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;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.