Proper Way To Draw A Quad?

Started by
6 comments, last by Estauns 19 years, 11 months ago
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?
"Where genius ends, madness begins."Estauns
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.
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]
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.
"Where genius ends, madness begins."Estauns
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.
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.
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]
Chris ByersMicrosoft DirectX MVP - 2005
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 =)
"Where genius ends, madness begins."Estauns

This topic is closed to new replies.

Advertisement