🔒 Locked
About rendering a cube
Advertisement
Original Post
Hey guys! I'm a beginner. I just read the DirectFAQ of the forum and it says that we'd better to use Draw[Index]Primitive as fewer as possible. Then i got a idea. Is it possible to render a cube by using one Draw[Index]Primitive? I tried it myself and I cannont get a cube. I have to say what I got looks quite strange. I'm really puzzled. Any thoughts?
What were you doing before? I assume you set up a vertex/index buffer with all the cube data? Are you setting the correct stream? How is it not working? Errors, erratic behavior, etc?
Yes it is very possible to render a cube with up DP call. You must make a list of vertices (wghich I assume you did correctly), then create a VB object and load that vertex data into it. When you actually draw the cude, you must specify the vertex format and stream before calling DP. Hope that helps a little. Just ask if you need some code.
Yes it is very possible to render a cube with up DP call. You must make a list of vertices (wghich I assume you did correctly), then create a VB object and load that vertex data into it. When you actually draw the cude, you must specify the vertex format and stream before calling DP. Hope that helps a little. Just ask if you need some code.
To Sr_Guapo:
Thank u for your offer. :)
Here's my cube vertex data:
// 1
{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,0)},
// 2
{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,255)},
{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,255,0,255)},
// 3
{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,255)},
// 4
{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 5
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,255,255,0)},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,255,255,255)},
{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 6
{-1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,0)},
{-1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{-1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,255)},
{-1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)}
Thank u for your offer. :)
Here's my cube vertex data:
// 1
{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,0)},
// 2
{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,255)},
{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,255,0,255)},
// 3
{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,255)},
// 4
{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,0,255,0)},
{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 5
{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,255,255,0)},
{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,255,255,255)},
{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 6
{-1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,0)},
{-1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(0,255,0,255)},
{-1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,255)},
{-1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)}
I would not use a tri strip for a cube. Try to make a Tri list (It will have 6 sides, 12 tris, 36 verts). A tri strip will continue from the last two vertices. In order for that code to work, you would need 6 draw calls (one for each strip).
It is hard to explain (for me at least). Just change everything to tri strips. The saved memory really is negligible and there is no performance impact.
Another thing to check is the widing order and culling. If the verts on the triangles are would counterclockwise, they will be culled. Either turn culling off or make sure all the tris are set up correctly.
It is hard to explain (for me at least). Just change everything to tri strips. The saved memory really is negligible and there is no performance impact.
Another thing to check is the widing order and culling. If the verts on the triangles are would counterclockwise, they will be culled. Either turn culling off or make sure all the tris are set up correctly.
You won't be able to use a tristrip. While you *could* use a tristrip with DrawIndexedPrimitive and lots of degenerate triangles, it would be a silly thing to do.
Using a trilist with DrawPrimitive you'll need 36 vertices, 18 of which are duplicates. (2 tris per cubeface = 6 verts per cubeface, or 36 verts overall)
Using a trilist with DrawIndexedPrimitive you'll need 24 vertices (2 tris per cubeface, only 4 unique verts per cubeface, or 24 verts overall, plus 36 indices).
Note that there isn't 8 vertices. You typically can't share vertices between cubefaces, because they will have different normals, UVs, possibly colors, etc. While the positions are identical, the entire vertices are not. You'll need 4 vertices on each of the 6 faces.
Using a trilist with DrawPrimitive you'll need 36 vertices, 18 of which are duplicates. (2 tris per cubeface = 6 verts per cubeface, or 36 verts overall)
Using a trilist with DrawIndexedPrimitive you'll need 24 vertices (2 tris per cubeface, only 4 unique verts per cubeface, or 24 verts overall, plus 36 indices).
Note that there isn't 8 vertices. You typically can't share vertices between cubefaces, because they will have different normals, UVs, possibly colors, etc. While the positions are identical, the entire vertices are not. You'll need 4 vertices on each of the 6 faces.
Quote:
Original post by m4gnus
i agree with sir guapo except on the 6 tri strips thingy...couldn't it be done with 2 drawcalls? i think it is possible with 2...
Probably, but why bother? You will only save 12 verts worth of memory (or video memory), hardly worth bothering with. Tri lists may also be a tad faster, but that would also be extremely negligible.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement