Accessing Vertex Buffer in D3D (Read)

Started by
5 comments, last by V7iktor 20 years, 8 months ago
I want to read the coordinates of vertices stored in the vertex buffer (say triangles), how do I do that? MSDN wasn''t much help. I have: LPDIRECT3DVERTEXBUFFER9 Vertex_Buffer //My vertex buffer struct Vertex //My vertices { FLOAT x, y, z; DWORD color; FLOAT tu, tv; }; I want to be able to get the x, y, z coordinates of the triangles stored in the buffer (reverse of inserting stuff into the vertex buffer). From MSDN: // This code example assumes the g_pVB is a variable of type // LPDIRECT3DVERTEXBUFFER9 and that g_Vertices has been // properly initialized with vertices. // To fill the vertex buffer, you need to lock the buffer to // gain access to the vertices. This mechanism is required // because vertex buffers may be in device memory. VOID* pVertices; if( FAILED( g_pVB->Lock( 0, // Fill from the start of // the buffer. sizeof(g_Vertices), // Size of the data to // load. (BYTE**)&pVertices, // Returned vertex data. 0 ) ) ) // Send default flags to // the lock. return E_FAIL; memcpy( pVertices, g_Vertices, sizeof(g_Vertices) ); g_pVB->Unlock(); This is to give an idea what I think I am talking about, anyway: g_pVB is Vertex_Buffer in my case, not sure about the rest =) I''m sure this is quite simple, right?
Advertisement
First off, why do you need to read the vertice data in the vertex buffer. And secondly, is that only an example from MSDN or are you using it in your code?
That was from MSDN,

I would like to read the vertex coordinates so that I could for example control my triangles (for example if coordinate x of triangle 1 is more than 2.0f - make it -4.0f), and I just managed to do that:

Vertex* Vertices;

Vertex_Buffer->Lock(0, sizeof(Vertex_Buffer), (void**)&Vertices, 0);

memcpy(Vertices, Vertex_Buffer, sizeof(Vertex_Buffer));

Vertex_Buffer->Unlock();

Vertices[0].x = 5.0f;


This is my code.


And, yes now for my second question:
Is there any better way to read coordinates of my triangles which are in the buffer? (I lost the array where I initialized them before copying to the Vertex_Buffer and they get modifyied, translated and rotated in the process, so I guess my Vertex_Buffer is the right place to look for current coordinates).

Or no? I don''t like the idea that I have to copy them from the buffer (memcpy function). But what can I do?

Thx for that quick reply =)
I got another idea:
Maybe I should keep all my triangles in arrays or some other structures and do all translating and rotating on those arrays, and then with every Main Loop pass copy the result array to the Vertex Buffer?

That way I have my current vertex coordinates in that array for reading/writting. But I have to update the Vertex Buffer. Is that where I should go?
Sorry for the late reply. Yea that''s how I do it. Make all the updates to you''re vertex data and then put them in a dynamic buffer.
Hello Dude..

I did it in my engine. Have a look at hexed, its part of the mesh manager. http://emotion.sourceforge.net

Later, Ben
You could also use Trans and rotation matrices to move vertices in a vertex buffer.
search for Direct3D matrices or matrix.

This topic is closed to new replies.

Advertisement