Added: mesh transformation

Added: input
Added: texture shader
This commit is contained in:
2026-10-13 18:30:21 +02:00
parent d2a487a4f6
commit 13691e3da3
14 changed files with 1371 additions and 20 deletions
+30
View File
@@ -184,6 +184,7 @@
<ClCompile Include="Mesh.cpp" />
<ClCompile Include="Time.cpp" />
<ClCompile Include="Utilities.cpp" />
<ClCompile Include="WICTextureLoader11.cpp" />
<ClCompile Include="Window.cpp" />
</ItemGroup>
<ItemGroup>
@@ -195,6 +196,7 @@
<ClInclude Include="Time.h" />
<ClInclude Include="Utilities.h" />
<ClInclude Include="Vertex.h" />
<ClInclude Include="WICTextureLoader11.h" />
<ClInclude Include="Window.h" />
</ItemGroup>
<ItemGroup>
@@ -216,8 +218,36 @@
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="TexturePixelShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="TextureVertexShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
</FxCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\Assimp.redist.3.0.0\build\native\Assimp.redist.targets" Condition="Exists('..\packages\Assimp.redist.3.0.0\build\native\Assimp.redist.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Assimp.redist.3.0.0\build\native\Assimp.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Assimp.redist.3.0.0\build\native\Assimp.redist.targets'))" />
</Target>
</Project>
@@ -42,6 +42,9 @@
<ClCompile Include="Window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="WICTextureLoader11.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Camera.h">
@@ -71,6 +74,9 @@
<ClInclude Include="Vertex.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="WICTextureLoader11.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<FxCompile Include="ColorVertexShader.hlsl">
@@ -79,5 +85,14 @@
<FxCompile Include="ColorPixelShader.hlsl">
<Filter>Resource Files</Filter>
</FxCompile>
<FxCompile Include="TextureVertexShader.hlsl">
<Filter>Resource Files</Filter>
</FxCompile>
<FxCompile Include="TexturePixelShader.hlsl">
<Filter>Resource Files</Filter>
</FxCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
+35 -3
View File
@@ -1,5 +1,6 @@
#include "Material.h"
#include "Utilities.h"
#include "WICTextureLoader11.h"
UINT Material::init(ID3D11Device* pD3DDevice, LPCWSTR textureFileName)
{
@@ -32,6 +33,9 @@ void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
// setup the matrix buffer
pD3DDeviceContext->VSSetConstantBuffers(0, 1, &_pMatrixBuffer);
// setup texture & sampler
pD3DDeviceContext->PSSetShaderResources(0, 1, &_pTexture);
pD3DDeviceContext->PSSetSamplers(0, 1, &_pSamplerState);
}
void Material::deInit()
@@ -94,7 +98,8 @@ UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
//);
// option 2: load compiled shader from disk
hr = D3DReadFileToBlob(L"ColorVertexShader.cso", &pCompiledShaderCode);
//hr = D3DReadFileToBlob(L"ColorVertexShader.cso", &pCompiledShaderCode);
hr = D3DReadFileToBlob(L"TextureVertexShader.cso", &pCompiledShaderCode);
CheckFailed(hr, 600);
@@ -121,7 +126,8 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
{
ID3DBlob* pCompiledShaderCode = nullptr;
HRESULT hr = D3DReadFileToBlob(L"ColorPixelShader.cso", &pCompiledShaderCode);
//HRESULT hr = D3DReadFileToBlob(L"ColorPixelShader.cso", &pCompiledShaderCode);
HRESULT hr = D3DReadFileToBlob(L"TexturePixelShader.cso", &pCompiledShaderCode);
CheckFailed(hr, 620);
hr = pD3DDevice->CreatePixelShader(pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), nullptr, &_pPixelShader);
@@ -134,7 +140,7 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShaderCode)
{
D3D11_INPUT_ELEMENT_DESC elements[2] = {};
D3D11_INPUT_ELEMENT_DESC elements[3] = {};
// position
elements[0].SemanticName = "POSITION";
@@ -145,6 +151,11 @@ UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVe
elements[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
elements[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
// uv
elements[2].SemanticName = "TEXCOORD";
elements[2].Format = DXGI_FORMAT_R32G32_FLOAT;
elements[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
HRESULT hr = pD3DDevice->CreateInputLayout(
elements, // array of input layout elements
std::size(elements), // count of elements
@@ -172,5 +183,26 @@ UINT Material::createMatrixBuffer(ID3D11Device* pD3DDevice)
UINT Material::createTextureAndSamplerState(ID3D11Device* pD3DDevice, LPCWSTR textureFileName)
{
// load texture & create ShaderResourceView
// https://github.com/microsoft/DirectXTex/tree/main
HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, nullptr, &_pTexture);
CheckFailed(hr, 670);
// create SamplerState
D3D11_SAMPLER_DESC samplerDesc = {};
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; // always set W
samplerDesc.BorderColor[0] = 1.0f;
samplerDesc.BorderColor[1] = 0.0f;
samplerDesc.BorderColor[2] = 1.0f;
samplerDesc.BorderColor[3] = 1.0f;
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = pD3DDevice->CreateSamplerState(&samplerDesc, &_pSamplerState);
CheckFailed(hr, 680);
return 0;
}
+31 -11
View File
@@ -19,6 +19,27 @@ UINT Mesh::init(ID3D11Device* pD3DDevice)
void Mesh::update(FLOAT dt)
{
static float x = 0.0f;
static float y = 0.0f;
static float z = 0.0f;
static float rotZ = 0.0f;
//rotZ += XM_PIDIV4 * dt;
float move = 10.0f * dt;
if (GetAsyncKeyState(VK_LEFT) & 0x8000 || GetAsyncKeyState('A') & 0x8000) x -= move;
if (GetAsyncKeyState(VK_RIGHT) & 0x8000 || GetAsyncKeyState('D') & 0x8000) x += move;
if (GetAsyncKeyState(VK_UP) & 0x8000 || GetAsyncKeyState('W') & 0x8000) y += move;
if (GetAsyncKeyState(VK_DOWN) & 0x8000 || GetAsyncKeyState('S') & 0x8000) y -= move;
if (GetAsyncKeyState(VK_SUBTRACT) & 0x8000 || GetAsyncKeyState('Q') & 0x8000) z -= move;
if (GetAsyncKeyState(VK_ADD) & 0x8000 || GetAsyncKeyState('E') & 0x8000) z += move;
XMMATRIX translation = XMMatrixTranslation(x, y, z);
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ);
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation);
}
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
@@ -58,21 +79,20 @@ void Mesh::deInit()
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
{
// TODO: create a quad (square)
// TODO: create a cube
// 1. define the structure (vertices)
Vertex vertices[] = {
// triangle
Vertex(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f), // 0
Vertex(1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f), // 1
Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f) // 2
//Vertex(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f), // 0
//Vertex(1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f), // 1
//Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f) // 2
// quad
//Vertex(-0.5f, 0.5f, 0.0f),
//Vertex(0.5f, 0.5f, 0.0f),
//Vertex(0.5f, -0.5f, 0.0f),
//Vertex(-0.5f, -0.5f, 0.0f)
Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f),
Vertex(0.5f, 0.5f, 0.0f, 1.0f, 0.0f),
Vertex(0.5f, -0.5f, 0.0f, 1.0f, 1.0f),
Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 1.0f)
};
_vertexCount = std::size(vertices);
@@ -98,11 +118,11 @@ UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice)
{
USHORT indices[] = {
// triangles
0, 1, 2
//0, 1, 2
// quad
//0, 1, 2,
//0, 2, 3
0, 1, 2,
0, 2, 3
};
_indexCount = std::size(indices);
Binary file not shown.
@@ -0,0 +1,13 @@
Texture2D MainTexture;
SamplerState MainSampler;
struct PixelInput
{
float4 position : SV_Position;
float2 uv : TEXCOORD;
};
float4 main(PixelInput INPUT) : SV_TARGET
{
return MainTexture.Sample(MainSampler, INPUT.uv);
}
Binary file not shown.
@@ -0,0 +1,29 @@
cbuffer MatrixBuffer
{
float4x4 WorldViewProjectionMatrix;
float4x4 WorldMatrix;
//float4 tilingOffset; // option 1
};
struct VertexInput
{
float4 position : POSITION;
float2 uv : TEXCOORD;
};
struct VertexOutput
{
float4 position : SV_Position;
float2 uv : TEXCOORD;
};
VertexOutput main(VertexInput INPUT)
{
VertexOutput OUTPUT;
OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix);
OUTPUT.uv = INPUT.uv;
//OUTPUT.uv = INPUT.uv * float2(1.1f, 1.1f) + float2(-0.05f, -0.05f); // uv * tiling + offset
return OUTPUT;
}
+2 -2
View File
@@ -15,11 +15,11 @@ void Time::update()
_deltaTime = diff.count();
_lastTime = now;
#if _DEBUG
#if _DEBUG && UNICODE
wstring output = L"delta time: " + to_wstring(_deltaTime) + L"\n";
output += L"fps: " + to_wstring(1.0f / _deltaTime) + L"\n";
OutputDebugString(output.c_str());
#endif // _DEBUG
#endif // _DEBUG && UNICODE
}
void Time::deInit()
+6 -4
View File
@@ -7,9 +7,11 @@ struct Vertex
{
XMFLOAT3 position;
XMFLOAT4 color;
XMFLOAT2 uv;
Vertex() : position(0.0f, 0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f) {}
Vertex(float x, float y, float z) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f) {}
Vertex(XMFLOAT3 p) : position(p), color(1.0f, 1.0f, 1.0f, 1.0f) {}
Vertex(float x, float y, float z, float r, float g, float b) : position(x, y, z), color(r, g, b, 1.0f) {}
Vertex() : position(0.0f, 0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f), uv(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) {}
Vertex(XMFLOAT3 p) : position(p), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f) {}
Vertex(float x, float y, float z, float r, float g, float b) : position(x, y, z), color(r, g, b, 1.0f), uv(0.0f, 0.0f) {}
Vertex(float x, float y, float z, float u, float v) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(u, v) {}
};
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,141 @@
//--------------------------------------------------------------------------------------
// File: WICTextureLoader11.h
//
// Function for loading a WIC image and creating a Direct3D runtime texture for it
// (auto-generating mipmaps if possible)
//
// Note: Assumes application has already called CoInitializeEx
//
// Warning: CreateWICTexture* functions are not thread-safe if given a d3dContext instance for
// auto-gen mipmap support.
//
// Note these functions are useful for images created as simple 2D textures. For
// more complex resources, DDSTextureLoader is an excellent light-weight runtime loader.
// For a full-featured DDS file reader, writer, and texture processing pipeline see
// the 'Texconv' sample and the 'DirectXTex' library.
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// https://go.microsoft.com/fwlink/?LinkId=248926
// http://go.microsoft.com/fwlink/?LinkId=248929
//--------------------------------------------------------------------------------------
#pragma once
#include <d3d11_1.h>
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "windowscodecs.lib")
#include <cstddef>
#include <cstdint>
namespace DirectX
{
#ifndef WIC_LOADER_FLAGS_DEFINED
#define WIC_LOADER_FLAGS_DEFINED
enum WIC_LOADER_FLAGS : uint32_t
{
WIC_LOADER_DEFAULT = 0,
WIC_LOADER_FORCE_SRGB = 0x1,
WIC_LOADER_IGNORE_SRGB = 0x2,
WIC_LOADER_SRGB_DEFAULT = 0x4,
WIC_LOADER_FIT_POW2 = 0x20,
WIC_LOADER_MAKE_SQUARE = 0x40,
WIC_LOADER_FORCE_RGBA32 = 0x80,
};
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec"
#pragma clang diagnostic ignored "-Wextra-semi-stmt"
#endif
DEFINE_ENUM_FLAG_OPERATORS(WIC_LOADER_FLAGS);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif
// Standard version
HRESULT CreateWICTextureFromMemory(_In_ ID3D11Device* d3dDevice,
_In_reads_bytes_(wicDataSize) const uint8_t* wicData,
_In_ size_t wicDataSize,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_In_ size_t maxsize = 0) noexcept;
HRESULT CreateWICTextureFromFile(_In_ ID3D11Device* d3dDevice,
_In_z_ const wchar_t* szFileName,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_In_ size_t maxsize = 0) noexcept;
// Standard version with optional auto-gen mipmap support
HRESULT CreateWICTextureFromMemory(_In_ ID3D11Device* d3dDevice,
_In_opt_ ID3D11DeviceContext* d3dContext,
_In_reads_bytes_(wicDataSize) const uint8_t* wicData,
_In_ size_t wicDataSize,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_In_ size_t maxsize = 0) noexcept;
HRESULT CreateWICTextureFromFile(_In_ ID3D11Device* d3dDevice,
_In_opt_ ID3D11DeviceContext* d3dContext,
_In_z_ const wchar_t* szFileName,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView,
_In_ size_t maxsize = 0) noexcept;
// Extended version
HRESULT CreateWICTextureFromMemoryEx(_In_ ID3D11Device* d3dDevice,
_In_reads_bytes_(wicDataSize) const uint8_t* wicData,
_In_ size_t wicDataSize,
_In_ size_t maxsize,
_In_ D3D11_USAGE usage,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ WIC_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView) noexcept;
HRESULT CreateWICTextureFromFileEx(_In_ ID3D11Device* d3dDevice,
_In_z_ const wchar_t* szFileName,
_In_ size_t maxsize,
_In_ D3D11_USAGE usage,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ WIC_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView) noexcept;
// Extended version with optional auto-gen mipmap support
HRESULT CreateWICTextureFromMemoryEx(_In_ ID3D11Device* d3dDevice,
_In_opt_ ID3D11DeviceContext* d3dContext,
_In_reads_bytes_(wicDataSize) const uint8_t* wicData,
_In_ size_t wicDataSize,
_In_ size_t maxsize,
_In_ D3D11_USAGE usage,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ WIC_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView) noexcept;
HRESULT CreateWICTextureFromFileEx(_In_ ID3D11Device* d3dDevice,
_In_opt_ ID3D11DeviceContext* d3dContext,
_In_z_ const wchar_t* szFileName,
_In_ size_t maxsize,
_In_ D3D11_USAGE usage,
_In_ unsigned int bindFlags,
_In_ unsigned int cpuAccessFlags,
_In_ unsigned int miscFlags,
_In_ WIC_LOADER_FLAGS loadFlags,
_Outptr_opt_ ID3D11Resource** texture,
_Outptr_opt_ ID3D11ShaderResourceView** textureView) noexcept;
} // namespace DirectX
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Assimp.redist" version="3.0.0" targetFramework="native" />
</packages>
Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB