Make 2D Sprite Face Camera Using Vertex Shader - DirectX 9

Viewed 19

Currently, I'm calculating the world matrix in C++ and then pass it to the shader to make the sprite always face the camera:

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

I've just been learning DirectX and HLSL for the past few days so I don't know if this is the optimal and correct way to do it. I thought it would have been better done in the vertex shader but I don't know how, please guide me.

0 Answers
Related