Jump to content
  • Advertisement
Sign in to follow this  
ANuy

About rendering a cube

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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)}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!