Building a Square

Started by
6 comments, last by John H 17 years, 12 months ago
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
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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]
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.
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
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?
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Ahhh, of course. Yeah, bit of a misunderstanding on my part. Thanks for the explanation though! [smile]

This topic is closed to new replies.

Advertisement