Archived

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

Vertex Buffer : Need help

This topic is 6015 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 I have currently used a vertex buffer to render one quad using 4 vertices and a triangle strip using using memcpy(vtxQuad, vtx, sizeof(vtx) ); Where vtx is an array of 4 vertices and vtxQuad is a unsinged char pointer. This works fine but now I want to render multiple quads in the same vertex buffer. I know I need to use a trianglelist for this. But how do I do this what do I put between the lock commands. I saw something like this but it doesn''t work CVec4 *vtxs; Buf->Lock(0, 0, (BYTE **)&vtxs, D3DLOCK_DISCARD); vtxs[0] = Part.getVtx(0); vtxs[1] = Part.getVtx(1); vtxs[2] = Part.getVtx(2); vtxs[3] = Part.getVtx(2); vtxs[4] = Part.getVtx(1); vtxs[5] = Part.getVtx(3); Buf->Unlock(); Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); But this doesn''t work, even for one triangle, please somebody help I am tearing my hair out. I am only working in proccessed triangles. That is I use const DWORD D3DFVF_CVec4 = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1; as my format for my vertices. Please ANY advice would be appreicated. I want to get vertices from multiple quads and render them using one vertex buffer -=Last Bastardo=-

Share this post


Link to post
Share on other sites
First of all, you are using D3DLOCK_DISCARD as a flag for the lock function. This is fine, but make sure you created the vertex buffer using D3DUSAGE_DYNAMIC, or it won''t work.

Also, if that is the ONLY code you use for drawing, then you are missing some stuff. (I realize that perhaps you are only including relevant code, but just to be safe, check that you take care of the following too) You should have something like this, before you call DrawPrimitive:

  
Device->BeginScene();
Device->SetStreamSource(0, Buf, sizeof(CVec4));
Device->SetVertexShader(D3DFVF_CVec4);

Share this post


Link to post
Share on other sites
If you want to render two quads the call to DrawPrimitive() has to look like this:

DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);

You have to render 4 primitives in order to get two quads (two triangles for each quad). Here''s what the vertex buffer could look like if you wanted to create on quad (untransformed coords).

// first triangle
{ 0.0f, 0.0f, 0.5f, 0xffffffff, 0.0f, 0.0f }, // upper left corner
{ 100.0f, 0.0f, 0.5f, 0xffffffff, 1.0f, 0.0f }, // upper right corner
{ 0.0f, 100.0f, 0.5f, 0xffffffff, 0.0f, 1.0f }, // lower left corner

// second triangle
{ 0.0f, 100.0f, 0.5f, 0xffffffff, 0.0f, 1.0f }, // lower left corner
{ 100.0f, 0.0f, 0.5f, 0xffffffff, 1.0f, 0.0f }, // upper right corner
{ 100.0f, 100.0f, 0.5f, 0xffffffff, 1.0f, 1.0f } // lower right corner

So if you wanted two quads you simply repeat the following code with different coordinates.
You simply copy this into the vertex buffer after you locked it.

-René

Share this post


Link to post
Share on other sites
Thanks for the input, currently my problem is with filling the vertex buffer. When I use the code below between lock and unlock
it dones''t work

vtxs[0] = Part.getVtx(0);
vtxs[1] = Part.getVtx(1);
vtxs[2] = Part.getVtx(2);
vtxs[3] = Part.getVtx(2);
vtxs[4] = Part.getVtx(1);
vtxs[5] = Part.getVtx(3);

it''s doesn''t work, thats the main problem



-=Last Bastardo=-

Share this post


Link to post
Share on other sites
Hmm...I cant really help you when I dont know anything about the implementation of Part.getVtx(). Try to post the entire function and I will see what I can do.

Also, make sure that the call to Lock() actually succeeds and that you are setting up the vertices correctly.

-René

Share this post


Link to post
Share on other sites
Ok I have narrowed down the problem to this. When I use :

Buf->Lock(0, 0, (BYTE **)&vtxs, D3DLOCK_DISCARD);
memcpy(vtxs, Part.vtx, sizeof(Part.vtx));
Buf->Unlock();

My code renders the quad using a triangle strip (Part.vtx is a CVec4 vtx[4] just an array of 4 vertices). But when I use

Buf->Lock(0, 0, (BYTE **)&vtxs, D3DLOCK_DISCARD);
vtxs[0] = Part.getVtx(0);
vtxs[1] = Part.getVtx(1);
vtxs[2] = Part.getVtx(2);
vtxs[3] = Part.getVtx(3);
Buf->Unlock();

CVec4 getVtx(int num) { return vtx[num]; }

Then it does nothing. What am I doing wrong ?


-=Last Bastardo=-

Share this post


Link to post
Share on other sites