Advertisement Jump to content
Sign in to follow this  
DividedByZero

How to create a 'quad'?

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

Hi guys,

I am trying to make a quad. I can currently render a triangle ok with this;

LPDIRECT3DVERTEXBUFFER9 pVertexObject = NULL;
void *pVertexBuffer = NULL; 

struct D3DVERTEX{
				float x,y,z,rhw;
				DWORD color;
				float u;
				float v;
					} vertices[3]; 

vertices[0].x = 0; 
vertices[0].y = 256; 
vertices[0].z = 0; 
vertices[0].rhw = 1.0f; 
vertices[0].color = 0xffffff;
vertices[0].u=0.0;
vertices[0].v=0.0;

vertices[1].x = 0; 
vertices[1].y = 0; 
vertices[1].z = 0; 
vertices[1].rhw = 1.0f; 
vertices[1].color = 0xffffff; 
vertices[1].u=1.0;
vertices[1].v=0.0;

vertices[2].x = 256; 
vertices[2].y = 0; 
vertices[2].z = 0; 
vertices[2].rhw = 1.0f;
vertices[2].color = 0xffffff;
vertices[2].u=0.0;
vertices[2].v=1.0;

if(FAILED(mRenderer->getDevice()->CreateVertexBuffer(3*sizeof(D3DVERTEX),0,D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1,D3DPOOL_DEFAULT,&pVertexObject,NULL)))
	return(0);
 
if(FAILED(pVertexObject->Lock(0,3*sizeof(D3DVERTEX),&pVertexBuffer,0)))
	return(0);

memcpy(pVertexBuffer,vertices,3*sizeof(D3DVERTEX));
pVertexObject->Unlock();

// do the actual render
mRenderer->getDevice()->SetStreamSource(0,pVertexObject,0,sizeof(D3DVERTEX));
mRenderer->getDevice()->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1);
mRenderer->getDevice()->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
But if I expand on this to make a quad. I get nothing at all.

LPDIRECT3DVERTEXBUFFER9 pVertexObject = NULL;
void *pVertexBuffer = NULL; 

struct D3DVERTEX{float x,y,z,rhw;DWORD color;float u;float v;} vertices[4]; 

vertices[0].x = 0; 
vertices[0].y = 256; 
vertices[0].z = 0; 
vertices[0].rhw = 1.0f; 
vertices[0].color = 0xffffff;
vertices[0].u=0.0;
vertices[0].v=0.0;

vertices[1].x = 0; 
vertices[1].y = 0; 
vertices[1].z = 0; 
vertices[1].rhw = 1.0f; 
vertices[1].color = 0xffffff; 
vertices[1].u=1.0;
vertices[1].v=0.0;

vertices[2].x = 256; 
vertices[2].y = 0; 
vertices[2].z = 0; 
vertices[2].rhw = 1.0f;
vertices[2].color = 0xffffff;
vertices[2].u=0.0;
vertices[2].v=1.0;

vertices[3].x = 256; 
vertices[3].y = 256; 
vertices[3].z = 0; 
vertices[3].rhw = 1.0f;
vertices[3].color = 0xffffff;
vertices[3].u=0.0;
vertices[3].v=1.0;

if(FAILED(mRenderer->getDevice()->CreateVertexBuffer(4*sizeof(D3DVERTEX),0,D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1,D3DPOOL_DEFAULT,&pVertexObject,NULL)))
	return(0);
 
if(FAILED(pVertexObject->Lock(0,4*sizeof(D3DVERTEX),&pVertexBuffer,0)))
	return(0);

memcpy(pVertexBuffer,vertices,4*sizeof(D3DVERTEX));
pVertexObject->Unlock();

// do the actual render
mRenderer->getDevice()->SetStreamSource(0,pVertexObject,0,sizeof(D3DVERTEX));
mRenderer->getDevice()->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1);
mRenderer->getDevice()->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
I am scratching my head trying to figure out what is going wrong here.

Any help would be awesome smile.png

Share this post


Link to post
Share on other sites
Advertisement

Well, if you're drawing a triangle list, you need 3 vertices for each triangle. You want to use D3DPT_TRIANGLESTRIP here instead and swap the 1st and 2nd vertex.

Share this post


Link to post
Share on other sites
Thanks for your help.

I now have this (but still renders nothing)

vertices[0].x = 0; 
vertices[0].y = 0; 
vertices[0].z = 0; 
vertices[0].rhw = 1.0f; 
vertices[0].color = 0xffffff; 
vertices[0].u=1.0;
vertices[0].v=0.0;

vertices[1].x = 0; 
vertices[1].y = 256; 
vertices[1].z = 0; 
vertices[1].rhw = 1.0f; 
vertices[1].color = 0xffffff;
vertices[1].u=0.0;
vertices[1].v=0.0;

vertices[2].x = 256; 
vertices[2].y = 0+j; 
vertices[2].z = 0; 
vertices[2].rhw = 1.0f;
vertices[2].color = 0xffffff;
vertices[2].u=0.0;
vertices[2].v=1.0;

vertices[3].x = 256; 
vertices[3].y = 256; 
vertices[3].z = 0; 
vertices[3].rhw = 1.0f;
vertices[3].color = 0xffffff;
vertices[3].u=0.0;
vertices[3].v=1.0;

if(FAILED(mRenderer->getDevice()->CreateVertexBuffer(4*sizeof(D3DVERTEX),D3DUSAGE_WRITEONLY,D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1,D3DPOOL_DEFAULT,&pVertexObject,NULL)))
	return(0);
 
if(FAILED(pVertexObject->Lock(0,4*sizeof(D3DVERTEX),&pVertexBuffer,0)))
	return(0);

memcpy(pVertexBuffer,vertices,4*sizeof(D3DVERTEX));
pVertexObject->Unlock();

// do the actual render
mRenderer->getDevice()->SetStreamSource(0,pVertexObject,0,sizeof(D3DVERTEX));
mRenderer->getDevice()->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1);
mRenderer->getDevice()->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);

Share this post


Link to post
Share on other sites
Oooh, got it now.

Your advice gave the reverse winding order.

This fixes it.

vertices[0].x = 0+i; 
vertices[0].y = 256+j; 
vertices[0].z = 0; 
vertices[0].rhw = 1.0f; 
vertices[0].color = 0xffffff;
vertices[0].u=0.0;
vertices[0].v=0.0;

vertices[1].x = 0+i; 
vertices[1].y = 0+j; 
vertices[1].z = 0; 
vertices[1].rhw = 1.0f; 
vertices[1].color = 0xffffff; 
vertices[1].u=1.0;
vertices[1].v=0.0;

vertices[2].x = 256+i; 
vertices[2].y = 256+j; 
vertices[2].z = 0; 
vertices[2].rhw = 1.0f;
vertices[2].color = 0xffffff;
vertices[2].u=0.0;
vertices[2].v=1.0;

vertices[3].x = 256+i; 
vertices[3].y = 0+j; 
vertices[3].z = 0; 
vertices[3].rhw = 1.0f;
vertices[3].color = 0xffffff;
vertices[3].u=0.0;
vertices[3].v=1.0;
Thanks for your help, you go tme going in the right direction smile.png

Share this post


Link to post
Share on other sites

I guess you probably noticed it, but you have a duplicated UV coordinate. vertices[2].u=0.0; should be 1.0.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!