Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Estauns

Proper Way To Draw A Quad?

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

I''m looking to draw a quad, with proper texture co-ordinates and so that I can cull CCW (since that seems to be the norm). I''ve got this:
	QUAD_VERTEX vertices[4];
	vertices[0].m_vPosition = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
	vertices[0].dwColour = 0x00FFFFFF;
	vertices[0].tu = 0.0f;
	vertices[0].tv = 0.0f;
	vertices[1].m_vPosition = D3DXVECTOR3(32.0f, 0.0f, 1.0f);
	vertices[1].dwColour = 0x00FFFFFF;
	vertices[1].tu = 1.0f;
	vertices[1].tv = 0.0f;
	vertices[2].m_vPosition = D3DXVECTOR3(0.0f, 32.0f, 1.0f);
	vertices[2].dwColour = 0x00FFFFFF;
	vertices[2].tu = 0.0f;
	vertices[2].tv = 1.0f;
	vertices[3].m_vPosition = D3DXVECTOR3(32.0f, 32.0f, 1.0f);
	vertices[3].dwColour = 0x00FFFFFF;
	vertices[3].tu = 1.0f;
	vertices[3].tv = 1.0f;
 
But my texture is upside down, and its not being drawn properly for CCW culling. Anyone tell me where I went wrong?

Share this post


Link to post
Share on other sites
Advertisement
Your polygon is not convex ie it crosses over itself, so it''s unlikely to draw properly at all... Plot it on a piece of paper first to make sure of yourself, but here are the coordinates you need:

x=0, y=0
x=32, y=0
x=32, y=32
x=0, y=32

Good luck.


ps. If you''re loading from a .bmp file, .bmps are stored upside-down.

Share this post


Link to post
Share on other sites
I think your vertices are in the wrong order. Take a look here to see the correct order.

[edited by - Jiia on May 9, 2004 7:36:46 PM]

Share this post


Link to post
Share on other sites
I only get one triangle with those, not a quad. =/

Heres how I create the quad (the new way as suggested by Jeffno10):

m_pTexture = 0x00;
if(FAILED(D3DXCreateTextureFromFileEx(pD3DDevice, pszTexture, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &m_pTexture)))
MessageBox(0x00, "Failed To Load Sprite Texture", "CRAP!", MB_OK);

QUAD_VERTEX vertices[4];
vertices[0].m_vPosition = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
vertices[0].dwColour = 0x00FFFFFF;
vertices[0].tu = 0.0f;
vertices[0].tv = 0.0f;
vertices[1].m_vPosition = D3DXVECTOR3(32.0f, 0.0f, 1.0f);
vertices[1].dwColour = 0x00FFFFFF;
vertices[1].tu = 1.0f;
vertices[1].tv = 0.0f;
vertices[2].m_vPosition = D3DXVECTOR3(32.0f, 32.0f, 1.0f);
vertices[2].dwColour = 0x00FFFFFF;
vertices[2].tu = 0.0f;
vertices[2].tv = 1.0f;
vertices[3].m_vPosition = D3DXVECTOR3(0.0f, 32.0f, 1.0f);
vertices[3].dwColour = 0x00FFFFFF;
vertices[3].tu = 1.0f;
vertices[3].tv = 1.0f;

QUAD_VERTEX *pVertices = 0x00;

if(FAILED(pD3DDevice->CreateVertexBuffer(4 * sizeof(QUAD_VERTEX), 0, D3DFVF_QUAD, D3DPOOL_DEFAULT, &m_pVertexBuffer, 0x00)))
MessageBox(0x00, "FAILED TO CREATE VB", "CRAP!", MB_OK);

m_pVertexBuffer->Lock(0, sizeof(vertices), (void **)&pVertices, 0);
memcpy(pVertices, vertices, sizeof(vertices));
m_pVertexBuffer->Unlock();


And heres how I render it:

pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
pD3DDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(QUAD_VERTEX));
pD3DDevice->SetFVF(D3DFVF_QUAD);
pD3DDevice->SetTexture(0, m_pTexture);
pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);


I was getting a quad before, but the texture was upside down and the cull mode wasn''t CCW friendly. I''ll have to play around with it some more, but if someone else knows by all means let me know.

Share this post


Link to post
Share on other sites
I think you had the verticies correct the first time but you had the texture wrong. 0,0 is the top left of the texure but you were applying that to the bottom left. Remember that 0,0 is the centre of the screen and increases going up.

Share this post


Link to post
Share on other sites
Try instead :

QUAD_VERTEX vertices[4];
vertices[0].m_vPosition = D3DXVECTOR3(0.0f, 32.0f, 1.0f);
vertices[0].dwColour = 0x00FFFFFF;
vertices[0].tu = 1.0f;
vertices[0].tv = 1.0f;
vertices[1].m_vPosition = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
vertices[1].dwColour = 0x00FFFFFF;
vertices[1].tu = 0.0f;
vertices[1].tv = 1.0f;
vertices[2].m_vPosition = D3DXVECTOR3(32.0f, 32.0f, 1.0f);
vertices[2].dwColour = 0x00FFFFFF;
vertices[2].tu = 1.0f;
vertices[2].tv = 0.0f;
vertices[3].m_vPosition = D3DXVECTOR3(0.0f, 32.0f, 1.0f);
vertices[3].dwColour = 0x00FFFFFF;
vertices[3].tu = 0.0f;
vertices[3].tv = 0.0f;

That should give you correct winding and u v coords.
Steele.

Share this post


Link to post
Share on other sites
When you draw a triangle strip, every odd triangle is going to be CW and every even one will be CCW, or vice versa. I can't remember, but this topic has come up before and no one ever answered it sufficiently...but I think you may have to turn culling OFF if you want to use a strip for a quad. If that's not the case, then the first triangle you create with the strip defines it's direction. i.e. you want your first triangle to be CW if you cull CCW.

As others have pointed out, your texture coordinates are wrong. 0,0 is the top, left corner of the screen for tu, tv. 0,0 to 32,32 goes from middle of screen(lacking any transformations) toward the top right corner. This is why your texture maps upsidedown.

Your vertices were correct the first time for creating a clockwise triangle, then a counter clockwise triangle. Make life a little easier, and graph it out. Then draw (0,0)in the top left corner of the graph, (1,0) in the top right corner of the graph, (0,1) in the bottom left corner of the graph, and (1,1) in the bottom right corner of the graph. Then you'll know precisely how to set the texture coords for your various vertices. Crowknee's vertices are correct for what you want.

Chris

[edited by - Supernat02 on May 9, 2004 12:20:31 AM]

Share this post


Link to post
Share on other sites
Thanks everyone for their help, this is what I ended up with:

// 1 3
//
// 0 2

QUAD_VERTEX vertices[4];
vertices[0].m_vPosition = D3DXVECTOR3(-32.0f, -32.0f, 1.0f);
vertices[0].dwColour = 0x00FFFFFF;
vertices[0].tu = 0.0f;
vertices[0].tv = 1.0f;
vertices[1].m_vPosition = D3DXVECTOR3(-32.0f, 32.0f, 1.0f);
vertices[1].dwColour = 0x00FFFFFF;
vertices[1].tu = 0.0f;
vertices[1].tv = 0.0f;
vertices[2].m_vPosition = D3DXVECTOR3(32.0f, -32.0f, 1.0f);
vertices[2].dwColour = 0x00FFFFFF;
vertices[2].tu = 1.0f;
vertices[2].tv = 1.0f;
vertices[3].m_vPosition = D3DXVECTOR3(32.0f, 32.0f, 1.0f);
vertices[3].dwColour = 0x00FFFFFF;
vertices[3].tu = 1.0f;
vertices[3].tv = 0.0f;


Works like a charm, thanks again everyone =)

Share this post


Link to post
Share on other sites

  • 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!