【问题标题】:DirectX 9.0 (World Coordinates shifting my object(triangle) animationDirectX 9.0(世界坐标移动我的对象(三角形)动画
【发布时间】:2013-05-01 23:01:02
【问题描述】:

我还是 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;

【问题讨论】:

    标签: c++ directx directx-9


    【解决方案1】:

    您实际上不需要更改模型顶点即可实现模型空间到世界空间的转换。 它通常是怎么做的:

    • 您只加载一次模型(顶点)。
    • 您决定模型在当前帧中的外观:平移 (x, y, z), 旋转(yaw, pitch, roll), 缩放 (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();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多