Jump to content
  • Advertisement
Sign in to follow this  

Building a Square

This topic is 4416 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

when i build a square in Direct3D do i have to use Triangle Strip with 6 Vertices? Here is the code for my 2d Square (is there any way i can make this with less lines?):
	{-2.0f, -2.0f, 0.0f,  D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) },
	{-2.0f, 1.1f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
    { 2.0f, 1.1f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },


    { 2.0f, 1.1f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },   
	{ 2.0f, -2.0f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ) },
	{-2.0f, -2.0f, 0.0f,  D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) }, 

thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
hmm, interesting way to generate a square [smile]

You need 6 vertices to define a square using a triangle LIST and 4 vertices using a triangle STRIP (ignoring index buffers for now).

For the triangle-strip form:

{-0.5, -0.5, 0.0f }
{ 0.5, -0.5, 0.0f }
{-0.5, 0.5, 0.0f }
{ 0.5, 0.5, 0.0f }


That will generate a 1x1 square on the XY plane. Notice the pattern - it's an easy one to remember, and is essentially the same sort of pattern you get if you want to write out binary truth tables and other 2n patterns that pop up all over CompSci [smile]

hth
Jack

Share this post


Link to post
Share on other sites
You should really use D3DCOLOR_RGBA instead of D3DCOLOR_COLORVALUE, it's more efficient (Although it'll get compiled away if you're using all constants) and won't have floating point rounding errors.

You can do it your way with triangle list and 6 vertices, or you can do it with 4 and a triangle strip:

{-2.0f, -2.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 0, 255) },
{-2.0f, 2.0f, 0.0f, D3DCOLOR_RGBA(0, 255, 0, 255) },
{ 2.0f, -2.0f, 0.0f, D3DCOLOR_RGBA(0, 255, 0, 255) },
{ 2.0f, 2.0f, 0.0f, D3DCOLOR_RGBA(0, 255, 0, 255) }



EDIT: Too slow [smile]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by jollyjeffers
hmm, interesting way to generate a square [smile]

You need 6 vertices to define a square using a triangle LIST and 4 vertices using a triangle STRIP (ignoring index buffers for now).

For the triangle-strip form:

{-0.5, -0.5, 0.0f }
{ 0.5, -0.5, 0.0f }
{-0.5, 0.5, 0.0f }
{ 0.5, 0.5, 0.0f }

That will generate a 1x1 square on the XY plane. Notice the pattern - it's an easy one to remember, and is essentially the same sort of pattern you get if you want to write out binary truth tables and other 2n patterns that pop up all over CompSci [smile]

hth
Jack



I thought that to make a square you could use a traingle fan and only use two vertices, so long as back-face clipping is disabled.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I thought that to make a square you could use a traingle fan and only use two vertices, so long as back-face clipping is disabled.
You'd still need 4 vertices for a triangle fan:

1-------2
| /|
| / |
|/ |
0-------3

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
4 vertices using a triangle STRIP (ignoring index buffers for now).


Just a quick side question here. Although you only need to supply 4 vertices to create a square with a triangle strip, does it still internally represent that square with 6 vertices? I'm trying to work out why you mention an index buffer here, and the only thing I could come up with would be to share the 2 "overlapping" vertices for each triangle (which I think if you didn't use an index buffer, wouldn't happen).

Am I correct or just horribly misunderstanding the situation?

Share this post


Link to post
Share on other sites
Quote:
Original post by John H
Quote:
Original post by jollyjeffers
4 vertices using a triangle STRIP (ignoring index buffers for now).


Just a quick side question here. Although you only need to supply 4 vertices to create a square with a triangle strip, does it still internally represent that square with 6 vertices?
No, it should be able to represent it exactly as you provide it. There might well be some issues at the GPU-level with the pre/post transform caches requiring it to re-transform them, but thats a bit of an unknown/grey area [smile]

Quote:
Original post by John H
I'm trying to work out why you mention an index buffer here, and the only thing I could come up with would be to share the 2 "overlapping" vertices for each triangle (which I think if you didn't use an index buffer, wouldn't happen).

Am I correct or just horribly misunderstanding the situation?
My original post might have been slightly misleading... the "(ignoring index buffers for now)" was in reference to the triangle LIST and not the STRIP. A list requires 6 vertices without an index buffer, or 4 vertices with 6 indices. In this particular instance they don't help with triangle strips.

hth
Jack

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!