DirectX9 C++ Questions

LostTime77

Member
Aug 18, 2005
91
0
0
Hello everybody. Hopefully somebody out there can help me with this problem I am having. I have searched extensively online for an answer but nobody seems to come quite close.

I am trying to design a simple 2d physics engine that can handle anywhere from 500 to 1000 simple circles on the screen, interacting with each other, at one time. The way I have it set up now is I have a class that handles the creation of circles using sin and cos to get the required vertex data coordinates. I want to be able to dynamically size a vertex buffer so that I don't have to code a set size into the program - so I can have say a vertex buffer of size 0 on program start up, and eventually allow people to draw polygons (for interaction) and have the vertex buffer re size when they draw. I have some ideas such as when a person draws, destroy the vertex buffer and create a new one with the given size. Would this be the correct way to do it?

Question two. Aside from the re size problem, I am trying to get a demo going. Basically, I want to be able to hard code circle positions into the engine and have them draw accordingly using a single vertex buffer. (I don't want to have a vertex buffer for each object because it would be inefficient with say 500 objects) The problem I am having is the copying of circle object vertices to the buffer. I basically lock the buffer and try to copy each individual circle in the program using a for loop. For example

for(int i = 0; i < numberCircles; i++)
{
memcpy(pVoid, ball.getVerts(i), sizeof(Ball)
}

This is not having the desired effect. Basically what I want to happen is for each set of ball vertices memcpy should add the verts to the end of the buffer. Im not exactly sure how it works, but it seems that its clearing the entire buffer every time I call memcpy. I don't want this to happen. Is this what I should be doing? I considered using another vector in my program so that everytime a ball is created, its vertices get added to the end of the vector. Then I memcpy the vertices to the buffer and draw. This did not work.

Third question. Since I will have to update a lot of positions for balls each frame, I considered updating the position of the center point of each ball, then using an offset to calculate the rest of the positions of the other vertices. This did not seem efficient because from what I understand, the GPU should take care of transformations, not the CPU. I am now deciding to use a d3dxmatrix. If I use a matrix will the transformation automatically get handled by the GPU so that I don't have to hand code the offsets for each point in each ball? Say I do use matrices. Since there are say 1000 balls on screen, each ball has its own position. Should each ball then also have its own matrix? I would then have to go through a loop each frame to call each balls matrix and do a directx settransform function to get the desired position change for each ball. Is this the way to do it?

 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Question #1 - I do not have an understanding of what you mean by vertex buffers.
Can you just allocate additional memory for what ever buffer you need and then have a list of pointers to each buffer?

Question #2
Are you updating pVoid when you run through the loop?
This will point to the next storage area
 

LostTime77

Member
Aug 18, 2005
91
0
0
A vertex buffer is just a directx structure defined by LPDIRECT3DVERTEXBUFFER9. Anyways instead of say allocating vertexes in system memory, it is a place to allocate vertexes in graphics card memory so that they can be drawn directly without going through system memory. Im not exactly sure if it is possible to do what you say. Vertex buffers sizes are allocated when they are created. Nonetheless, the vertex buffer memory size is not the big issue here. Getting the vertexes copied to the buffer is. pVoid is a void pointer used by a vertex buffer for its needs. I think it points to a memory location in the vertex buffer. Anyhow, how would I go about updating it to the next storage area as you say?

Thanks for the response.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You should know the size of the vertex buffer.

If you have an array that you can index, set pVoid to the proper index.
Else you need to define a pVoidMaster which points to where pVoid is pointing to at present. Then as you step through the ball.getVerts array, you need to add the buffer size to pVoid to have it point to the next memory slot. Keep pVoidMaster for reference to the start of the memory block if you need it.
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,674
146
106
www.neftastic.com
Taken directly from the D3D documentation regarding accessing vertex buffers:

// This code example assumes the g_pVB is a variable of type
// LPDIRECT3DVERTEXBUFFER9 and that g_Vertices has been
// properly initialized with vertices

// Lock the buffer to gain access to the vertices
VOID* pVertices;

if(FAILED(g_pVB->Lock(0, sizeof(g_Vertices),
(BYTE**)&pVertices, 0 ) ) )
return E_FAIL;

memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
g_pVB->Unlock();

Your question:
This is not having the desired effect. Basically what I want to happen is for each set of ball vertices memcpy should add the verts to the end of the buffer. Im not exactly sure how it works, but it seems that its clearing the entire buffer every time I call memcpy. I don't want this to happen. Is this what I should be doing? I considered using another vector in my program so that everytime a ball is created, its vertices get added to the end of the vector. Then I memcpy the vertices to the buffer and draw. This did not work.

That's exactly what memcpy does - it copies the data from *Src to the address specified by *Dst. If you want to add data, you'll need to increment *Dst in your loop as well... something like:

for(int i = 0; i < numberCircles; i++)
{
memcpy(pVoid + (i * sizeof(Ball)), ball.getVerts(i), sizeof(Ball));
}

Again, I'm not privy to all your structures or whatnot, but you should get the idea even if I'm using the wrong variables here. The takeaway here is that you need to increment where pVoid is pointing otherwise memcpy will simply overwrite what's at the location pVoid. Also keep in mind that you'll need to harden your code here a bunch to do things like bounds checking and whatnot, otherwise you're likely to write to an invalid pointer area if you're not careful, etc.

(Edit: basically what EagleKeeper said, just with more visual aides.)

---

As far as question #1 goes... why not just use a linked list of circle/ball objects? Seeing as you're copying the vertex lists in a for loop anyway, that should make it relatively simple to step through a list of objects at the same time with very little complexity. Every time you add a circle/ball/whatever, you just add an object to the list, which handles all the tracking, traversing, etc. It's also not terribly difficult to have a list such that it can also work using the array operators (eg: operator[] overloading).
 

LostTime77

Member
Aug 18, 2005
91
0
0
Thanks for the responses everybody. I have resolved to using SunnyD and Eagle's method of incrementing the memory pointer to load the vertexes. I have another question however regarding my initial question three. Basically everytime through the render frame function I call a loop to render all the primitive circles. I have a vector keeping track of all the circle data including position, velocity, and acceleration (which get updated every frame based on physics equations). The loop looks like this

for(int i = 0; i < numObjects; i++)
{
SetTransformtranslation(&translationmatrix, positionX, Y, Z);
DrawPrimitive(D3DPT_TRIANGLELIST, startvert, primitives);
}

Now as you can see, I use a single matrix that gets transformed every render loop, by every single circle object, to do the translation. In turn, I make an inefficient amount of draw calls each render frame. I would like to get this down to one draw call for all the primitives each frame and have the positions be updated. How would I go about doing this? I know that I can already get down to one draw call because all the vertex data is already loaded. The real problem is updating the transformation matrix for each and every object, to do their individual transforms. Any ideas?
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,674
146
106
www.neftastic.com
Originally posted by: LostTime77
Thanks for the responses everybody. I have resolved to using SunnyD and Eagle's method of incrementing the memory pointer to load the vertexes. I have another question however regarding my initial question three. Basically everytime through the render frame function I call a loop to render all the primitive circles. I have a vector keeping track of all the circle data including position, velocity, and acceleration (which get updated every frame based on physics equations). The loop looks like this

for(int i = 0; i < numObjects; i++)
{
SetTransformtranslation(&translationmatrix, positionX, Y, Z);
DrawPrimitive(D3DPT_TRIANGLELIST, startvert, primitives);
}

Now as you can see, I use a single matrix that gets transformed every render loop, by every single circle object, to do the translation. In turn, I make an inefficient amount of draw calls each render frame. I would like to get this down to one draw call for all the primitives each frame and have the positions be updated. How would I go about doing this? I know that I can already get down to one draw call because all the vertex data is already loaded. The real problem is updating the transformation matrix for each and every object, to do their individual transforms. Any ideas?

No, actually you can't get it down to one draw call because the renderer will thing all of the vertices belong to the same primitive. You're doing it right by separating them.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |