Jump to content
  • Advertisement


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


Drawing a square

This topic is 5129 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, Ive been drawing a square using 2 vertex buffers.. coz i havent seen a tutorial drawing a square using 1 vertex buffer.. can this be done? I mean, can you draw a square using 1 vertex buffer only. I think it can be done... Can anyone share a code for drawing a square using 1 vertex buffer then i'll be analyzing it... thanks... Above them all is Truth and Truth must be set free. If the wings of Truth are unclipped, the voices will fall silent [edited by - Superpig on May 7, 2004 9:07:52 AM]

Share this post

Link to post
Share on other sites
I've seen several tutorials using one vertex buffer for a square. I'm not entirely sure what your code looks like, but here's the vertex buffer I use for a square.

if(FAILED(g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)*4, 0, VertexFVF, D3DPOOL_MANAGED, &g_pD3DVB)))
return FALSE;
g_pD3DVB->Lock(0, 0, (BYTE**)&Ptr, 0);
memcpy(Ptr, Verts, sizeof(Verts));

You could also check the tutorials that came with the directx SDK.

[edited by - Corvwyn on May 7, 2004 6:38:21 AM]

Share this post

Link to post
Share on other sites
Just put 2 primitives (6 vertices) into the buffer. They can handle well over 1000 vertices.

I would share code, but it''s all in VB; don''t know how helpful that''ll be...

Yes... VB6 is here to steal your minds fps. Very slowly.
Call .optimize(you.sig)

Share this post

Link to post
Share on other sites
i tried that code, of course with 4 vertices on the CustomVertex structure, but it doesnt work, it still draws a triangle on the window.. whats wrong with that? I dont see any square tutorials using 1 vertex buffer, if you know one could you refer that site to me? i would like to post my code here but it will be long... thanks for the help.. any more suggestions?

Share this post

Link to post
Share on other sites
maybe double check you draw primitive routine parameters, you should be rendering two primitives - triangles. Also you may switch off culling - watch in SDK for D3DRS_CULL render state (Iam not sure if the name is correct).

Share this post

Link to post
Share on other sites
Inside the VertexBuffer is vertex data, that is (in this case) supposed to be triangles. Every 3 vertecies is a triangle, a Quad (Square) requries 2 triangles, ie: 6 Vertecies.

I have a good idea you are only drawing 3 of these verts. Try doing this in your draw code:

DrawPrimative( PrimativeType.TriangleList, 0, 2); // start at 0, draw 2 triangles (6 verts).

Hope that helps.

Share this post

Link to post
Share on other sites
You only need 4 vertices, or 6 in your index buffer if you want to render from an IB.

This should get you going:

// Definition


D3DXVECTOR3 m_vPosition;
DWORD dwColour;
float tu, tv;

// Creation of VB:

SPRITE_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((float)iWidth, 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, (float)iHeight, 1.0f);
vertices[2].dwColour = 0x00FFFFFF;
vertices[2].tu = 0.0f;
vertices[2].tv = 1.0f;
vertices[3].m_vPosition = D3DXVECTOR3((float)iWidth, (float)iHeight, 1.0f);
vertices[3].dwColour = 0x00FFFFFF;
vertices[3].tu = 1.0f;
vertices[3].tv = 1.0f;

SPRITE_VERTEX *pVertices = 0x00;

pD3DDevice->CreateVertexBuffer(4 * sizeof(SPRITE_VERTEX), 0, D3DFVF_SPRITE, D3DPOOL_DEFAULT, &m_pVertexBuffer, 0x00);

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

// Drawing

pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pD3DDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(SPRITE_VERTEX));
pD3DDevice->SetTexture(0, m_pTexture);
pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

(My notation is crap, its from a dead project)

You can't just cut and paste this and expect it to work.

[edited by - GroZZleR on May 7, 2004 2:38:50 PM]

Share this post

Link to post
Share on other sites
It sounds like the whole problem is that you only have 4 vertices. A square is drawn using 6 vertices (2 tringles).

You can use just 4 vertices if you also create an index buffer, but that''s usually more complicated than it''s worth at this stage of the learning process.

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!