add world matrix

add view matrix
add projection matrix
This commit is contained in:
2025-09-17 16:08:23 +02:00
parent e5451c10fc
commit 53cf8b96a3
4 changed files with 38 additions and 3 deletions
+24 -1
View File
@@ -1,12 +1,35 @@
#include "Camera.h"
#include <DirectXMath.h>
using namespace DirectX;
UINT Camera::init(UINT width, UINT height)
{
// view matrix
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); // camera position
XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // camera forward vector
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // camera up vector
XMMATRIX viewMatrix = XMMatrixLookToLH(position, forward, up);
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_viewMatrix), viewMatrix);
// projection matrix
FLOAT fov = XM_PI * 0.3333333f; // field of view
FLOAT aspectRatio = static_cast<FLOAT>(width) / static_cast<FLOAT>(height); // CAUTION: UINT / UINT => UINT
FLOAT nearClippingPlane = 0.3f;
FLOAT farClippingPlane = 100.0f;
XMMATRIX projectionMatrix = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_projectionMatrix), projectionMatrix);
return 0;
}
void Camera::update(FLOAT dt)
void Camera::update(FLOAT dt, IDirect3DDevice9* pD3DDevice)
{
pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix);
pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix);
}
void Camera::deInit()
+1 -1
View File
@@ -5,7 +5,7 @@ class Camera
{
public:
UINT init(UINT width, UINT height);
void update(FLOAT dt);
void update(FLOAT dt, IDirect3DDevice9* pD3DDevice);
void deInit();
private:
+12
View File
@@ -2,6 +2,10 @@
#include "Utilities.h"
#include "Vertex.h"
#include <DirectXMath.h>
using namespace DirectX;
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
{
UINT error = 0;
@@ -12,6 +16,13 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
error = createIndexBuffer(pD3DDevice);
CheckError(error);
// identity matrix
XMMATRIX identityMatrix = XMMatrixIdentity();
XMFLOAT4X4 worldMatrix;
XMStoreFloat4x4(&worldMatrix, identityMatrix);
D3DMATRIX* pWorldMatrix = reinterpret_cast<D3DMATRIX*>(&worldMatrix);
_worldMatrix = *pWorldMatrix;
return 0;
}
@@ -25,6 +36,7 @@ void Mesh::render(IDirect3DDevice9* pD3DDevice)
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
pD3DDevice->SetFVF(FVF);
pD3DDevice->SetIndices(_pIndexBuffer);
pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix);
// 2. draw call
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3);
+1 -1
View File
@@ -65,7 +65,7 @@ int WINAPI WinMain(
// 8.1. update objects
time.update();
mesh.update(time.getDeltaTime());
camera.update(time.getDeltaTime());
camera.update(time.getDeltaTime(), d3d.getDevice());
light.update(time.getDeltaTime());
// 8.2. render objects