DirectX 9.0(世界坐标移动我的对象(三角形)动画
DirectX 9.0 (World Coordinates shifting my object(triangle) animation
我仍然是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();
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 使用对象文件读取三角形数据网格
- 为什么我的三角形对象没有被绘制?
- DirectX 9.0(世界坐标移动我的对象(三角形)动画