Archived

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

segmentalfault

DirectX code only rendering 8 vertices! Help :)

Recommended Posts

This is driving me bonkers. Maybe to the trained eye, someone may be able to remedy this problem: I have a class called CCube which basically renders a cube based on the size and origin. It uses 24 vertices with normals on each vertice, and 36 indices for the index buffer. When rendering, it will only render the first 8 vertices (basically the front and right faces). It ignores the rendering for the left, top, bottom, and back faces. I know that it is just rendering the _FIRST_ 8, because if I change the vertices around in the array, lets say the 4 vertices for the bottom with the 4 vertices for the right faces, it renders the front and bottom only. Here''s some code snippets... ------------- Cube.h: #define CUBE_VERTEX_COUNT 24 struct CUSTOMVERTEX { D3DXVECTOR3 vertex; // The untransformed, 3D position for the vertex D3DXVECTOR3 normal; // Normals DWORD color; // The vertex color }; -------- Cube.ccp: WORD cube_index[ 36 ] = { 0,1,2, 2,1,3, 2,3,4, 4,3,5, 4,5,6, 6,5,7, 6,7,0, 0,7,1, 1,7,3, 3,7,5, 0,2,6, 2,4,6 }; CCube::CCube( D3DXVECTOR3 origin, D3DXVECTOR3 size, float r, float g, float b ) { ZeroMemory( &m_material, sizeof( m_material ) ); D3DXVECTOR3 extent = origin + size; D3DXCOLOR clr[ 8 ]; float luma[ 8 ] = { 0.7f, 1.0f, 0.5f, 0.8f, 0.15f, 0.45f, 0.35f, 0.65f }; for ( int i = 0; i < 8; i++ ) { clr[ i ] = D3DXCOLOR( r * luma[ i ], g * luma[ i ], b * luma[ i ], 1.0f ); } D3DXVECTOR3 vertex0( origin.x, origin.y, origin.z ); D3DXVECTOR3 vertex1( origin.x, extent.y, origin.z ); D3DXVECTOR3 vertex2( extent.x, origin.y, origin.z ); D3DXVECTOR3 vertex3( extent.x, extent.y, origin.z ); D3DXVECTOR3 vertex4( extent.x, origin.y, extent.z ); D3DXVECTOR3 vertex5( extent.x, extent.y, extent.z ); D3DXVECTOR3 vertex6( origin.x, origin.y, extent.z ); D3DXVECTOR3 vertex7( origin.x, extent.y, extent.z ); D3DXVECTOR3 normal0( 0.0f, 0.0f, -1.0f ); // Front face D3DXVECTOR3 normal1( 1.0f, 0.0f, 0.0f ); // Right face D3DXVECTOR3 normal2( 0.0f, 0.0f, 1.0f ); // Back face D3DXVECTOR3 normal3(-1.0f, 0.0f, 0.0f ); // Left face D3DXVECTOR3 normal4( 0.0f, 1.0f, 0.0f ); // Top face D3DXVECTOR3 normal5( 0.0f, -1.0f, 0.0f ); // Bottom face // Front Face SetVertex( &vertices[ 0 ], &vertex0, &normal0, clr[ 0 ] ); SetVertex( &vertices[ 1 ], &vertex1, &normal0, clr[ 0 ] ); SetVertex( &vertices[ 2 ], &vertex2, &normal0, clr[ 0 ] ); SetVertex( &vertices[ 3 ], &vertex3, &normal0, clr[ 0 ] ); // Right Face SetVertex( &vertices[ 4 ], &vertex2, &normal1, clr[ 1 ] ); SetVertex( &vertices[ 5 ], &vertex3, &normal1, clr[ 1 ] ); SetVertex( &vertices[ 6 ], &vertex4, &normal1, clr[ 1 ] ); SetVertex( &vertices[ 7 ], &vertex5, &normal1, clr[ 1 ] ); // Back face SetVertex( &vertices[ 8 ], &vertex4, &normal2, clr[ 2 ] ); SetVertex( &vertices[ 9 ], &vertex5, &normal2, clr[ 2 ] ); SetVertex( &vertices[10 ], &vertex6, &normal2, clr[ 2 ] ); SetVertex( &vertices[11 ], &vertex7, &normal2, clr[ 2 ] ); // Left face SetVertex( &vertices[12 ], &vertex6, &normal3, clr[ 3 ] ); SetVertex( &vertices[13 ], &vertex7, &normal3, clr[ 3 ] ); SetVertex( &vertices[14 ], &vertex0, &normal3, clr[ 3 ] ); SetVertex( &vertices[15 ], &vertex1, &normal3, clr[ 3 ] ); // Top face SetVertex( &vertices[16 ], &vertex1, &normal4, clr[ 4 ] ); SetVertex( &vertices[17 ], &vertex7, &normal4, clr[ 4 ] ); SetVertex( &vertices[18 ], &vertex3, &normal4, clr[ 4 ] ); SetVertex( &vertices[19 ], &vertex5, &normal4, clr[ 4 ] ); // Bottom face SetVertex( &vertices[20 ], &vertex6, &normal5, clr[ 5 ] ); SetVertex( &vertices[21 ], &vertex0, &normal5, clr[ 5 ] ); SetVertex( &vertices[22 ], &vertex4, &normal5, clr[ 5 ] ); SetVertex( &vertices[23 ], &vertex2, &normal5, clr[ 5 ] ); } void CCube::SetVertex( CUSTOMVERTEX * p, D3DXVECTOR3 * vertex, D3DXVECTOR3 * normal, DWORD color ) { p->vertex = *vertex; p->normal = *normal; p->color = color; } void CCube::draw(LPDIRECT3DDEVICE9 device) { //device->SetMaterial( &m_material ); LPDIRECT3DVERTEXBUFFER9 vb; // Create the vertex buffer. device->CreateVertexBuffer( CUBE_VERTEX_COUNT * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &vb, NULL ); // Fill the vertex buffer. VOID* pVertices; vb->Lock( 0, sizeof( vertices ), &pVertices, 0 ); memcpy( pVertices, vertices, sizeof( vertices ) ); vb->Unlock(); device->SetStreamSource( 0, vb, 0, sizeof(CUSTOMVERTEX) ); device->SetFVF( D3DFVF_CUSTOMVERTEX ); LPDIRECT3DINDEXBUFFER9 ib; device->CreateIndexBuffer( 36 * sizeof( WORD ), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &ib, NULL ); // Fill the index buffer. VOID* pIndices; ib->Lock( 0, sizeof( cube_index ), &pIndices, 0 ); memcpy( pIndices, cube_index, sizeof( cube_index ) ); ib->Unlock(); device->SetIndices( ib ); device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, CUBE_VERTEX_COUNT, 0, 12 ); ib->Release(); vb->Release(); } --- And that''s that. The CCube object is created and called from another class, and the implementation simply goes like this.. This is unimportant in the question, but just showing you how its called doesn''t hurt. m_pCube = new CCube( D3DXVECTOR3( 125.0f -100.0f * j, 0.0f, 125.0f - 100.0f * i ), D3DXVECTOR3( 50.0f, 150.0f, 50.0f ), 0.5, 1.0f, ( j-i * 0.11f ) ); m_pCube->draw( m_pd3dDevice ); --- I''m not expecting anyone to pick through the entire code, but a trained eye should be able to catch my flaw... Thanks, Segmentalfault

Share this post


Link to post
Share on other sites
How is the variable 'verticies' defined? I don't see it anywhere... only used...

I am thinking that maybe it has do with the fact that verticies is a pointer and when you use sizeof(verticies), you et the sizeof the pointer instead of the size of the memory block it points to.. I have done these kinds of things before...

IT seems that the safest way to keep this from happening is to use sizeof(CUSTOMVERTEX) * numofverts... Seems to not cause so many errors for me...

Dwiel



[edited by - Tazzel3d on April 30, 2003 10:28:44 PM]

Share this post


Link to post
Share on other sites
Don''t worry, I figured it out. The problem had to do with the index buffer. Originally I only had 8 verticies with no normals. Here is the updated index buffer:

WORD cube_index[ 36 ] = { 0,1,2,
2,1,3,
2,3,6,
6,3,7, // vert 4 = 6, vert 5 = 7
6,7,10, // vert 6 = 10
10,7,11, // vert 7 = 11
10,11,0,
0,11,1,
1,11,3,
3,11,7,
0,2,10,
2,6,10
};

Share this post


Link to post
Share on other sites