I still new with direct 9.0. How to move my object or triangle on runtime?
According, to this tutorial. http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-5
I understand what it does is move the camera coordinate, set the world coordinate and proj coordinate. What if I have I want to move a triangle position on run-time? let say move x-axis by 1px per frame.
//A structure for our custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
DWORD color; // The vertex color
FLOAT tu, tv; // Texture position
};
I have a feeling that I need to shift the position of x,y,z per vertices. But it is impossible for me to release the vertices buffer, repeat the reallocate of memory just because of x,y,z. It will take too much computation. let alone rendering
How can I access invidual vertices on run-time and just modify its content (X, Y, Z) without the need to destroy and copy?
1) However this lead to another question. The coordinates itself is model coordinates. So the question is how do I change the world coordinate or define per object and change it.
LPDIRECT3DVERTEXBUFFER9 g_pVB;
You actually don't need to change your model vertices to achieve model space to world space transformation. That how it usually done:
In your tutorial:
D3DXMATRIX matTranslate; // a matrix to store the translation information
// build a matrix to move the model 12 units along the x-axis and 4 units along the y-axis
// store it to matTranslate
D3DXMatrixTranslation(&matTranslate, 12.0f, 4.0f, 0.0f);
// tell Direct3D about our matrix
d3ddev->SetTransform(D3DTS_WORLD, &matTranslate);
So, if you want move your object in runtime, you must change world matrix, then push that new matrix to DirectX (via SetTransform() or via updating shader variable). Usually something like this:
// deltaTime is a difference in time between current frame and previous one
OnUpdate(float deltaTime)
{
x += deltaTime * objectSpeed; // Add to x coordinate
D3DXMatrixTranslation(&matTranslate, x, y, z);
device->SetTransform(D3DTS_WORLD, &matTranslate);
}
float deltaTime = g_Timer->GetGelta(); // Get difference in time between current frame and previous one
OnUpdate(deltaTime);
Or, if you don't have timer (yet) you can simply increment coordinate each frame.
Next, if you have multiple objects (it can be same model or different) every frame you doing something like:
for( all objects )
{
// Tell DirectX what vertexBuffer (model) you want to render;
SetStreamSource();
// Tell DirectX what translation must be applied to that object;
SetTransform();
// Render it
Draw();
}