Add input layout

Add matrix buffer
Convert camera to D3D11
This commit is contained in:
2026-11-27 16:08:22 +01:00
parent 85d8830b84
commit 97192e1587
12 changed files with 171 additions and 69 deletions
+3 -12
View File
@@ -1,10 +1,6 @@
#include "Camera.h"
#include <DirectXMath.h>
using namespace DirectX;
UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
UINT Camera::init(UINT width, UINT height)
{
// view matrix
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
@@ -12,9 +8,7 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up);
XMFLOAT4X4 v;
XMStoreFloat4x4(&v, lookTo);
_viewMatrix = *reinterpret_cast<D3DMATRIX*>(&v);
XMStoreFloat4x4(&_viewMatrix, lookTo);
// projection matrix
float fov = XMConvertToRadians(60.0f);
@@ -23,10 +17,7 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
float farClippingPlane = 100.0f;
XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_projectionMatrix), perspective);
pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix);
pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix);
XMStoreFloat4x4(&_projectionMatrix, perspective);
return 0;
}
+10 -4
View File
@@ -1,15 +1,21 @@
#pragma once
#include <d3d9.h>
#include <DirectXMath.h>
#include <Windows.h>
using namespace DirectX;
class Camera
{
public:
UINT init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height);
UINT init(UINT width, UINT height);
void update(FLOAT dt);
void deInit();
const XMFLOAT4X4& getViewMatrix() const { return _viewMatrix; }
const XMFLOAT4X4& getProjectionMatrix() const { return _projectionMatrix; }
private:
D3DMATRIX _viewMatrix = {};
D3DMATRIX _projectionMatrix = {};
XMFLOAT4X4 _viewMatrix = {};
XMFLOAT4X4 _projectionMatrix = {};
};
+113 -20
View File
@@ -5,39 +5,86 @@
UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
{
HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
CheckFailed(hr, 600);
//HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
//CheckFailed(hr, 600);
UINT error = 0;
error = createVertexShader(pD3DDevice);
CheckError(error);
error = createPixelShader(pD3DDevice);
CheckError(error);
error = createMatrixBuffer(pD3DDevice);
CheckError(error);
return 0;
}
void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
{
// set texture
pD3DDevice->SetTexture(0, _pTexture);
//// set texture
//pD3DDevice->SetTexture(0, _pTexture);
// set sampler state
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(100, 0, 100));
pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
//// set sampler state
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
//pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(100, 0, 100));
//pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
//pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
//pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
//pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
// set material
D3DMATERIAL9 material = {};
material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
material.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
material.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
material.Power = 100.0f;
material.Emissive = { 1.0f, 0.0f, 0.0f, 1.0f };
//// set material
//D3DMATERIAL9 material = {};
//material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
//material.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
//material.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
//material.Power = 100.0f;
//material.Emissive = { 1.0f, 0.0f, 0.0f, 1.0f };
pD3DDevice->SetMaterial(&material);
//pD3DDevice->SetMaterial(&material);
// set shader
pD3DDeviceContext->VSSetShader(_pVertexShader, nullptr, 0);
pD3DDeviceContext->PSSetShader(_pPixelShader, nullptr, 0);
// set input layout
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
// set matrix buffer
pD3DDeviceContext->VSSetConstantBuffers(0, 1, &_pMatrixBuffer);
}
void Material::deInit()
{
safeRelease(_pInputLayout);
safeRelease(_pPixelShader);
safeRelease(_pVertexShader);
}
UINT Material::setMatrices(ID3D11DeviceContext* pD3DDeviceContext,
const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix)
{
// 1. load matrix data
XMMATRIX w = XMLoadFloat4x4(&worldMatrix);
XMMATRIX v = XMLoadFloat4x4(&viewMatrix);
XMMATRIX p = XMLoadFloat4x4(&projectionMatrix);
// 2. map matrix buffer
D3D11_MAPPED_SUBRESOURCE data = {};
HRESULT hr = pD3DDeviceContext->Map(_pMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
CheckFailed(hr, 665);
// 3. set data
MatrixBuffer* pMatrixBuffer = static_cast<MatrixBuffer*>(data.pData);
XMStoreFloat4x4(&pMatrixBuffer->WorldViewProjectionMatrix, XMMatrixTranspose(w * v * p));
// 4. unmap matrix buffer
pD3DDeviceContext->Unmap(_pMatrixBuffer, 0);
return 0;
}
UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
@@ -69,6 +116,9 @@ UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
);
CheckFailed(hr, 605);
UINT error = createInputLayout(pD3DDevice, pCompiledShaderCode);
CheckError(error);
safeRelease(pCompiledShaderCode);
return 0;
@@ -94,5 +144,48 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShader)
{
// https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
D3D11_INPUT_ELEMENT_DESC inputs[4] = {};
// position
inputs[0].SemanticName = "POSITION";
inputs[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
// normal
inputs[1].SemanticName = "NORMAL";
inputs[1].Format = DXGI_FORMAT_R32G32B32_FLOAT;
inputs[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
// uv
inputs[2].SemanticName = "TEXCOORD";
inputs[2].Format = DXGI_FORMAT_R32G32_FLOAT;
inputs[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
// color
inputs[3].SemanticName = "COLOR";
inputs[3].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
inputs[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
HRESULT hr = pD3DDevice->CreateInputLayout(
inputs, std::size(inputs), // array of element descriptions & size of array
pCompiledVertexShader->GetBufferPointer(), pCompiledVertexShader->GetBufferSize(), // check vertex compatability with vertex shader
&_pInputLayout
);
CheckFailed(hr, 640);
return 0;
}
UINT Material::createMatrixBuffer(ID3D11Device* pD3DDevice)
{
D3D11_BUFFER_DESC desc = {};
desc.ByteWidth = sizeof(MatrixBuffer);
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; // has to be set if D3D11_USAGE_DYNAMIC or D3D11_USAGE_STAGING is used
HRESULT hr = pD3DDevice->CreateBuffer(&desc, nullptr, &_pMatrixBuffer);
CheckFailed(hr, 660);
return 0;
}
+14
View File
@@ -1,9 +1,12 @@
#pragma once
#include <d3d11.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
#pragma comment(lib, "d3dcompiler.lib")
using namespace DirectX;
class Material
{
public:
@@ -11,15 +14,26 @@ public:
void render(ID3D11DeviceContext* pD3DDeviceContext);
void deInit();
UINT setMatrices(ID3D11DeviceContext* pD3DDeviceContext,
const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix);
private:
UINT createVertexShader(ID3D11Device* pD3DDevice);
UINT createPixelShader(ID3D11Device* pD3DDevice);
UINT createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShader);
UINT createMatrixBuffer(ID3D11Device* pD3DDevice);
ID3D11VertexShader* _pVertexShader = nullptr;
ID3D11PixelShader* _pPixelShader = nullptr;
ID3D11InputLayout* _pInputLayout = nullptr;
//IDirect3DTexture9* _pTexture = nullptr;
struct MatrixBuffer
{
XMFLOAT4X4 WorldViewProjectionMatrix;
};
ID3D11Buffer* _pMatrixBuffer = nullptr;
};
+1 -1
View File
@@ -56,7 +56,7 @@ void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
);
pD3DDeviceContext->IASetIndexBuffer(_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
pD3DDeviceContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pD3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// draw call
pD3DDeviceContext->DrawIndexed(_indexCount, 0, 0);
+2
View File
@@ -12,6 +12,8 @@ public:
void render(ID3D11DeviceContext* pD3DDeviceContext);
void deInit();
const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; }
private:
UINT createVertexBuffer(ID3D11Device* pD3DDevice);
UINT createIndexBuffer(ID3D11Device* pD3DDevice);
Binary file not shown.
+1 -1
View File
@@ -1,4 +1,4 @@
float4 main() : SV_TARGET
{
return float4(1.0f, 1.0f, 1.0f, 1.0f);
return float4(1.0f, .0f, 1.0f, 1.0f);
}
+11 -23
View File
@@ -1,36 +1,24 @@
#pragma once
#include <d3d9.h>
#include <DirectXMath.h>
#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_NORMAL
using namespace DirectX;
struct Vertex
{
// position
float x;
float y;
float z;
XMFLOAT3 position;
XMFLOAT3 normal;
XMFLOAT2 uv;
XMFLOAT4 color;
// normal
float nx;
float ny;
float nz;
// color
D3DCOLOR color;
// texture coordinates
float u;
float v;
Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xffffffff), u(0.0f), v(0.0f), nx(0.0f), ny(0.0f), nz(0.0f) {}
Vertex(float x, float y, float z) : x(x), y(y), z(z), color(0xffffffff), u(0.0f), v(0.0f), nx(0.0f), ny(0.0f), nz(0.0f) {}
Vertex() : position(0.0f, 0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f) {}
Vertex(float x, float y, float z) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f) {}
Vertex(float x, float y, float z, UINT r, UINT g, UINT b) :
x(x), y(y), z(z), color(D3DCOLOR_XRGB(r, g, b)), u(0.0f), v(0.0f), nx(0.0f), ny(0.0f), nz(0.0f) {}
position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f) {}
Vertex(float x, float y, float z, float u, float v) :
x(x), y(y), z(z), color(0xffffffff), u(u), v(v), nx(0.0f), ny(0.0f), nz(0.0f) {}
position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(u, v), normal(0.0f, 0.0f, 0.0f) {}
Vertex(float x, float y, float z, float nx, float ny, float nz, float u, float v) :
x(x), y(y), z(z), color(0xffffffff), u(u), v(v), nx(nx), ny(ny), nz(nz) {}
position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(u, v), normal(nx, ny, nz) {}
};
Binary file not shown.
+6 -1
View File
@@ -1,4 +1,9 @@
cbuffer MatrixBuffer
{
float4x4 WorldViewProjectionMatrix;
};
float4 main( float4 pos : POSITION ) : SV_POSITION
{
return pos;
return mul(pos, WorldViewProjectionMatrix);
}
+9 -6
View File
@@ -37,10 +37,10 @@ int WINAPI WinMain(
error = mesh.init(d3d.getDevice());
CheckError(error);
//// 4. create a camera
//Camera camera = {};
//error = camera.init(d3d.getDevice(), width, height);
//CheckError(error);
// 4. create a camera
Camera camera = {};
error = camera.init(width, height);
CheckError(error);
// 5. create time
Time time = {};
@@ -75,14 +75,17 @@ int WINAPI WinMain(
// 8.1. update objects
time.update();
mesh.update(time.getDeltaTime());
//camera.update(time.getDeltaTime());
camera.update(time.getDeltaTime());
//light.update(time.getDeltaTime());
material.setMatrices(
d3d.getDeviceContext(), mesh.getWorldMatrix(), camera.getViewMatrix(), camera.getProjectionMatrix());
// 8.2. render objects
d3d.beginScene();
//light.render(d3d.getDevice());
material.render(d3d.getDevice());
material.render(d3d.getDeviceContext());
mesh.render(d3d.getDeviceContext());
d3d.endScene();