# Building a Square

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

## 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 ) },



##### Share on other sites
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 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 on other sites
Quote:
 Original post by jollyjeffershmm, 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]hthJack

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 on other sites
Quote:
 Original post by Anonymous PosterI 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 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 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 HI'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 on other sites
Ahhh, of course. Yeah, bit of a misunderstanding on my part. Thanks for the explanation though! [smile]

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 9
• 19
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633382
• Total Posts
3011581
• ### Who's Online (See full list)

There are no registered users currently online

×