DirectX 9.0(世界坐标移动我的对象(三角形)动画

DirectX 9.0 (World Coordinates shifting my object(triangle) animation

本文关键字:三角形 对象 动画 我的 移动 世界坐标 DirectX      更新时间:2023-10-16

我仍然是direct 9.0的新手。如何在运行时移动我的对象或三角形?

根据本教程。http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-5

我理解它的作用是移动相机坐标,设置世界坐标和项目坐标。如果我想在运行时移动一个三角形的位置呢?假设每帧移动x轴1px。

//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
};

我有一种感觉,我需要移动每个顶点的x,y,z的位置。但是我不可能仅仅因为x,y,z就释放顶点缓冲区,重复内存的重新分配。这将需要太多的计算。更不用说渲染了

我如何在运行时访问单个顶点并修改其内容(X, Y, Z)而不需要销毁和复制?

1)然而,这导致了另一个问题。坐标本身就是模型坐标。所以问题是我如何改变世界坐标或者定义每个对象并改变它。

LPDIRECT3DVERTEXBUFFER9 g_pVB;

你实际上不需要改变你的模型顶点来实现模型空间到世界空间的转换。通常是这样的:

  • 你加载你的模型(顶点)一次
  • 你决定你的模型在当前帧中的外观:Y, z),旋转(偏航,俯仰,横摇),缩放(x, Y, z)
  • 你计算矩阵根据这个信息:mtxTranslation, mtxRotation, mtxScale
  • 你正在计算这个对象的世界矩阵:mtxWorld = mtxScale * mtxRotation * mtxTranslation。注意,矩阵乘法是不可交换的:结果取决于操作数的顺序。
  • 然后你应用这个矩阵(使用固定函数或内部顶点着色器)

在你的教程中:

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);

所以,如果你想在运行时移动你的对象,你必须改变世界矩阵,然后将新矩阵推到DirectX(通过SetTransform()或通过更新shader变量)。通常是这样的:

// 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);

或者,如果你还没有计时器,你可以简单地每帧增加坐标。

接下来,如果你有多个对象(它可以是相同的模型或不同的)每帧你做这样的:

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();
}