Skip to main content
gamedev.net
🔒 Locked

About rendering a cube

Started by ANuy 7 replies 1.2k views
Original Post
ANuy
ANuy
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?
Sr_Guapo
Sr_Guapo
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.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
ANuy
ANuy
All right.
I create the vertex buffer and filled it with all the data. Then i use a DrawPrimitive to render the cube. I use the D3DPT_TRIANGESTRIP type to draw the primitives.
Thx for your reply. :)
ANuy
ANuy
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)}
Sr_Guapo
Sr_Guapo
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.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
m4gnus
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...
"There are 10 types of people in the world... those who understand binary and those who don't."
Namethatnobodyelsetook
Namethatnobodyelsetook
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.
Sr_Guapo
Sr_Guapo
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.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
m4gnus
m4gnus
yeah sure i'm also in the opinion that using tri lists is much easier..

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."

Topic Locked

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

Sign in to reply to this topic.