• Advertisement

Archived

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

Drawing a square

This topic is 5039 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
Advertisement
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));
g_pD3DVB->Unlock();


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

#define D3DFVF_SPRITE (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

struct SPRITE_VERTEX
{
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));
m_pVertexBuffer->Unlock();

// Drawing

pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pD3DDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(SPRITE_VERTEX));
pD3DDevice->SetFVF(D3DFVF_SPRITE);
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
quote:
Original post by Etnu
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.


Not if you use a triangle fan or strip, which is what you should be using.

Share this post


Link to post
Share on other sites
Hm, I was rather disappointed to see that there was no way to directly draw quads (squares) -- or more specifically, quad lists -- in DirectX. Sure everything gets boiled down to triangles in the end, but in situations where you want to draw a whole bunch of quads, say, for rendering bitmapped text (one quad per letter), you either have to a) render a triangle fan and make one call per quad or b) render a triangle list and use 6 verts per quad.

Maybe I''m wrong -- not extremely familiar with the DirectX API yet.

Share this post


Link to post
Share on other sites
Yes the largest primitive that D3D can draw is a triangle. The reason that Microsoft use triangles is because all points of a triangle lie on 1 plane whereas that may not be true for 4 or more points. Makes sense when you look at it that way.

Making a class to draw quads is pretty simple though.

Share this post


Link to post
Share on other sites
Coding it isn''t the problem -- it''s just added overhead. I guess it probably makes little difference in the end, though.

Doesn''t DirectX have some kind of sprite function? Could that be used to draw quads? (at least for 2D text/HUD type things?) I''m sure that would add even more overhead than just triangles, though. Probably can''t render a list of them anyway.

Share this post


Link to post
Share on other sites

  • Advertisement