【发布时间】:2022-10-02 03:47:58
【问题描述】:
目前,我正在用 C++ 计算世界矩阵,然后将其传递给着色器以使精灵始终面向相机:
static D3DXVECTOR3 up(0, 0, 1);
D3DXMATRIX world, view, proj;
// get the world, view and projection matrix
g_pd3dDevice->GetTransform(D3DTS_WORLD, &world);
g_pd3dDevice->GetTransform(D3DTS_VIEW, &view);
g_pd3dDevice->GetTransform(D3DTS_PROJECTION, &proj);
D3DXMATRIX translation, invView, cameraPosition, rotation,invRotation;
// get the camera position by inversing the view matrix
D3DXMatrixInverse(&invView, NULL, &view);
cameraPosition = D3DXVECTOR3(invView._41, invView._42, invView._43);
// translate the sprite position to a world matrix
D3DXMatrixTranslation(&translation, spritePosition.x, spritePosition.y, spritePosition.z);
// calculate the world matrix rotation to look from
// the sprite position to the camera position
D3DXMatrixLookAtRH(&invRotation, &spritePosition, &cameraPosition, &up);
D3DXMatrixInverse(&rotation, NULL, &invRotation);
// pass the world * view * projection to the shader
world = rotation * translation;
worldViewProj = matrix.rotation * matrix.view * matrix.proj;
g_pEffect->SetMatrix(\"WorldViewProj\", &worldViewProj);
我最近几天一直在学习 DirectX 和 HLSL,所以我不知道这是否是最佳和正确的方法。 我认为在顶点着色器中会更好,但我不知道如何,请指导我。
-
请记住,您使用的是旧版 Direct3D 9 和已弃用的 D3DX9 数学库。这意味着您正在使用 20 多年前的 API。请考虑使用 Direct3D 11 作为起点。见Microsoft Docs。
标签: c++ matrix directx directx-9