gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:50:41 24 4
gpt4 key购买 nike

我对 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;

最佳答案

您实际上不需要更改模型顶点来实现模型空间到世界空间的转换。这通常是如何完成的:

  • 您加载模型(顶点)一次。
  • 您决定模型在当前框架中的外观:平移 (x,y, z), 旋转(yaw, pitch, roll), scale (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() 或通过更新着色器变量)。通常是这样的:

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

关于c++ - DirectX 9.0(世界坐标移动我的对象(三角形)动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16318855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com