add D3D11 project
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#include "Camera.h"
|
||||
|
||||
UINT Camera::init(UINT width, UINT height)
|
||||
{
|
||||
// view matrix
|
||||
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
|
||||
XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
|
||||
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
|
||||
XMStoreFloat4x4(&_viewMatrix, XMMatrixLookToLH(position, forward, up));
|
||||
|
||||
// projection matrix
|
||||
XMMATRIX perspectiveMatrix = XMMatrixPerspectiveFovLH(
|
||||
XM_PI * 0.3333333f, // field of view in radians, here 60 degrees
|
||||
static_cast<FLOAT>(width) / static_cast<FLOAT>(height), // aspect ratio
|
||||
0.3f, 10.0f // near & far clipping planes
|
||||
);
|
||||
XMStoreFloat4x4(&_projectionMatrix, perspectiveMatrix);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Camera::update(FLOAT dt)
|
||||
{
|
||||
}
|
||||
|
||||
void Camera::deInit()
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <d3d11.h>
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
UINT init(UINT width, UINT height);
|
||||
void update(FLOAT dt);
|
||||
void deInit();
|
||||
|
||||
const XMFLOAT4X4& getViewMatrix() const { return _viewMatrix; }
|
||||
const XMFLOAT4X4& getProjectionMatrix() const { return _projectionMatrix; }
|
||||
const XMFLOAT3 getCameraPosition() const { return XMFLOAT3(_viewMatrix._14, _viewMatrix._24, _viewMatrix._34); }
|
||||
|
||||
private:
|
||||
XMFLOAT4X4 _viewMatrix = {};
|
||||
XMFLOAT4X4 _projectionMatrix = {};
|
||||
};
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,18 @@
|
||||
struct PixelInput
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float4 color : COLOR;
|
||||
};
|
||||
|
||||
float4 main(PixelInput INPUT) : SV_TARGET
|
||||
{
|
||||
//return float4(1.0f, 1.0f, 1.0f, 1.0f); // solid color
|
||||
return INPUT.color; // vertex color
|
||||
//return INPUT.color * 0.5f; // darker vertex color
|
||||
//return INPUT.color * 2.0f; // lighter vertex color
|
||||
|
||||
//float4 color = INPUT.color;
|
||||
//color.r = 0.0f; // no red channel
|
||||
|
||||
//return color;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix WorldViewProjectionMatrix;
|
||||
};
|
||||
|
||||
struct VertexInput
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float4 color : COLOR;
|
||||
};
|
||||
|
||||
struct VertexOutput
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float4 color : COLOR;
|
||||
};
|
||||
|
||||
VertexOutput main(VertexInput INPUT)
|
||||
{
|
||||
VertexOutput OUTPUT;
|
||||
|
||||
OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix);
|
||||
OUTPUT.color = INPUT.color;
|
||||
|
||||
return OUTPUT;
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|ARM64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>ARM64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|ARM64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>ARM64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{C46808CF-936B-4F98-81C2-1E10D9F1822F}</ProjectGuid>
|
||||
<RootNamespace>D3D9</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Camera.cpp" />
|
||||
<ClCompile Include="Direct3D.cpp" />
|
||||
<ClCompile Include="Light.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Material.cpp" />
|
||||
<ClCompile Include="Mesh.cpp" />
|
||||
<ClCompile Include="Time.cpp" />
|
||||
<ClCompile Include="Utilities.cpp" />
|
||||
<ClCompile Include="WICTextureLoader11.cpp" />
|
||||
<ClCompile Include="Window.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Camera.h" />
|
||||
<ClInclude Include="Direct3D.h" />
|
||||
<ClInclude Include="Light.h" />
|
||||
<ClInclude Include="Material.h" />
|
||||
<ClInclude Include="Mesh.h" />
|
||||
<ClInclude Include="Time.h" />
|
||||
<ClInclude Include="Utilities.h" />
|
||||
<ClInclude Include="Vertex.h" />
|
||||
<ClInclude Include="WICTextureLoader11.h" />
|
||||
<ClInclude Include="Window.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<FxCompile Include="ColorPixelShader.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|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
</FxCompile>
|
||||
<FxCompile Include="ColorVertexShader.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|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LightingPixelShader.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LightingVertexShader.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
</FxCompile>
|
||||
<FxCompile Include="TexturePixelShader.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
</FxCompile>
|
||||
<FxCompile Include="TextureVertexShader.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cso</ObjectFileOutput>
|
||||
</FxCompile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,101 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Camera.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Direct3D.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Light.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Material.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Mesh.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Time.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Utilities.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Window.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="WICTextureLoader11.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Camera.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Direct3D.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Light.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Material.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Mesh.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Time.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Utilities.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Window.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Vertex.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="WICTextureLoader11.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<FxCompile Include="ColorVertexShader.hlsl">
|
||||
<Filter>Resource Files</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="ColorPixelShader.hlsl">
|
||||
<Filter>Resource Files</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="TexturePixelShader.hlsl">
|
||||
<Filter>Resource Files</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="TextureVertexShader.hlsl">
|
||||
<Filter>Resource Files</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LightingPixelShader.hlsl">
|
||||
<Filter>Resource Files</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="LightingVertexShader.hlsl">
|
||||
<Filter>Resource Files</Filter>
|
||||
</FxCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,94 @@
|
||||
#include "Direct3D.h"
|
||||
#include "Utilities.h"
|
||||
|
||||
INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
|
||||
{
|
||||
HRESULT hr = {};
|
||||
|
||||
// 1. define swap chain description
|
||||
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
|
||||
swapChainDesc.OutputWindow = hWnd; // target window instance handle
|
||||
swapChainDesc.Windowed = !isFullscreen; // windowed or fullscreen
|
||||
swapChainDesc.BufferCount = 1; // amount of backbuffers, minimum 1
|
||||
swapChainDesc.BufferDesc.Width = width;
|
||||
swapChainDesc.BufferDesc.Height = height;
|
||||
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // pixel format, here 4 channels (red, green, blue, alpha) with 8bit depth each and unsigned normalized floating points
|
||||
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // which technique is used to render to back buffer
|
||||
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // what happens to front buffer after swap
|
||||
swapChainDesc.SampleDesc.Count = 1; // have to be at least one
|
||||
|
||||
// 2. create Direct3D device, device context & swap chain
|
||||
D3D_FEATURE_LEVEL featureLevels[] = {
|
||||
D3D_FEATURE_LEVEL_11_1,
|
||||
D3D_FEATURE_LEVEL_11_0,
|
||||
D3D_FEATURE_LEVEL_10_1,
|
||||
D3D_FEATURE_LEVEL_10_0
|
||||
};
|
||||
|
||||
D3D_FEATURE_LEVEL chosenFeatureLevel = {};
|
||||
|
||||
hr = D3D11CreateDeviceAndSwapChain(
|
||||
nullptr, // graphic card, nullptr -> default adapter
|
||||
D3D_DRIVER_TYPE_HARDWARE, // rendering with hard- or software renderer
|
||||
nullptr, // software renderer module reference
|
||||
0, // optional flags
|
||||
featureLevels, std::size(featureLevels), // which api features are supported
|
||||
D3D11_SDK_VERSION, &swapChainDesc, &_pDXGISwapChain, &_pD3DDevice, &chosenFeatureLevel, &_pD3DDeviceContext);
|
||||
CheckFailed(hr, 20);
|
||||
|
||||
// 3. create render target
|
||||
ID3D11Texture2D* pBackBuffer = nullptr;
|
||||
hr = _pDXGISwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
|
||||
CheckFailed(hr, 22);
|
||||
|
||||
hr = _pD3DDevice->CreateRenderTargetView(pBackBuffer, nullptr, &_pD3DRenderTargetView);
|
||||
CheckFailed(hr, 24);
|
||||
|
||||
safeRelease(pBackBuffer);
|
||||
|
||||
// 4. create depth stencil buffer
|
||||
|
||||
// 5. create rasterizer state
|
||||
//d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);
|
||||
//d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
||||
//d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
||||
//d3d.getDevice()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
|
||||
|
||||
// 6. describe viewport
|
||||
D3D11_VIEWPORT viewport = {};
|
||||
viewport.TopLeftX = 0.0f;
|
||||
viewport.TopLeftY = 0.0f;
|
||||
viewport.Width = width;
|
||||
viewport.Height = height;
|
||||
|
||||
// 7. setup render pipeline (device context)
|
||||
_pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, nullptr);
|
||||
_pD3DDeviceContext->RSSetViewports(1, &viewport);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Direct3D::beginScene(FLOAT r, FLOAT g, FLOAT b)
|
||||
{
|
||||
// clear back buffer with a solid color
|
||||
FLOAT backgroundColor[] = { r, g, b, 1.0f };
|
||||
_pD3DDeviceContext->ClearRenderTargetView(_pD3DRenderTargetView, backgroundColor);
|
||||
}
|
||||
|
||||
void Direct3D::endScene()
|
||||
{
|
||||
_pDXGISwapChain->Present(
|
||||
0, // sync interval, 0,1,2,3,4 -> (de-)active vsync setting and how often should be synced
|
||||
0 // optional flags
|
||||
);
|
||||
}
|
||||
|
||||
void Direct3D::deInit()
|
||||
{
|
||||
safeRelease(_pD3DRenderTargetView);
|
||||
safeRelease(_pDXGISwapChain);
|
||||
safeRelease(_pD3DDeviceContext);
|
||||
safeRelease(_pD3DDevice);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <d3d11.h>
|
||||
|
||||
#pragma comment(lib, "d3d11.lib")
|
||||
|
||||
class Direct3D
|
||||
{
|
||||
public:
|
||||
INT init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen);
|
||||
void beginScene(FLOAT r, FLOAT g, FLOAT b);
|
||||
void endScene();
|
||||
void deInit();
|
||||
|
||||
ID3D11Device* getDevice() const { return _pD3DDevice; }
|
||||
ID3D11DeviceContext* getDeviceContext() const { return _pD3DDeviceContext; }
|
||||
|
||||
private:
|
||||
ID3D11Device* _pD3DDevice = nullptr; // needed for creating all objects, e.g. buffer, textures, etc., used in init phase
|
||||
ID3D11DeviceContext* _pD3DDeviceContext = nullptr; // access to render pipeline, e.g. set matrices or draw command, used in run phase
|
||||
IDXGISwapChain* _pDXGISwapChain = nullptr; // holds references to front & back buffer & swaps them
|
||||
ID3D11RenderTargetView* _pD3DRenderTargetView = nullptr; // target device context is rendering to
|
||||
};
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "Light.h"
|
||||
#include "Utilities.h"
|
||||
|
||||
UINT Light::init(ID3D11Device* pD3DDevice, LightBuffer& lightData)
|
||||
{
|
||||
D3D11_BUFFER_DESC desc = {};
|
||||
desc.ByteWidth = sizeof(LightBuffer);
|
||||
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
desc.Usage = D3D11_USAGE_IMMUTABLE; // no light update possible
|
||||
//desc.Usage = D3D11_USAGE_DYNAMIC; // light update possible
|
||||
|
||||
D3D11_SUBRESOURCE_DATA initialData = {};
|
||||
initialData.pSysMem = &lightData;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &initialData, &_pLightBuffer);
|
||||
CheckFailed(hr, 70);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Light::update(FLOAT dt)
|
||||
{
|
||||
}
|
||||
|
||||
void Light::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||
{
|
||||
pD3DDeviceContext->PSSetConstantBuffers(0, 1, &_pLightBuffer);
|
||||
}
|
||||
|
||||
void Light::deInit()
|
||||
{
|
||||
safeRelease(_pLightBuffer);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include <d3d11.h>
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
class Light
|
||||
{
|
||||
public:
|
||||
struct LightBuffer
|
||||
{
|
||||
XMFLOAT3 LightVector;
|
||||
FLOAT LightIntensity;
|
||||
XMFLOAT4 DiffuseColor;
|
||||
XMFLOAT4 SpecularColor;
|
||||
XMFLOAT3 CameraPosition;
|
||||
FLOAT Phong;
|
||||
};
|
||||
|
||||
UINT init(ID3D11Device* pD3DDevice, LightBuffer& lightData);
|
||||
void update(FLOAT dt);
|
||||
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
||||
void deInit();
|
||||
|
||||
private:
|
||||
ID3D11Buffer* _pLightBuffer = nullptr;
|
||||
};
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,45 @@
|
||||
Texture2D MainTexture;
|
||||
SamplerState MainSampler;
|
||||
|
||||
cbuffer LightBuffer // has to be always 16 byte aligned
|
||||
{
|
||||
float3 LightVector;
|
||||
float LightIntensity;
|
||||
float4 DiffuseColor;
|
||||
float4 SpecularColor;
|
||||
float3 CameraPosition;
|
||||
float Phong;
|
||||
};
|
||||
|
||||
struct PixelInput
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float3 positionWorld : POSITION1;
|
||||
float3 normal : NORMAL;
|
||||
float2 uv : TEXCOORD;
|
||||
};
|
||||
|
||||
float4 main(PixelInput INPUT) : SV_TARGET
|
||||
{
|
||||
float4 texColor = MainTexture.Sample(MainSampler, INPUT.uv);
|
||||
float4 ambientColor = float4(0.2f, 0.2f, 0.2f, 1.0f);
|
||||
float4 diffuseColor = DiffuseColor;
|
||||
float4 specularColor = SpecularColor;
|
||||
float4 emissionColor = float4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
float3 lightVector = normalize(-LightVector);
|
||||
float3 normalVector = normalize(INPUT.normal);
|
||||
|
||||
// diffuse color
|
||||
float diffuse = dot(lightVector, normalVector); // diffuse intensity
|
||||
diffuseColor *= diffuse * LightIntensity;
|
||||
|
||||
// specular color
|
||||
float3 reflectVector = normalize(reflect(-lightVector, normalVector));
|
||||
//float3 reflectVector = 2 * normalVector * diffuse - lightVector;
|
||||
float3 viewVector = normalize(CameraPosition - INPUT.positionWorld);
|
||||
float specular = pow(max(dot(reflectVector, viewVector), 0), Phong);
|
||||
specularColor *= specular * LightIntensity;
|
||||
|
||||
return saturate(saturate(texColor * saturate(ambientColor + diffuseColor) + specularColor) + emissionColor);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,33 @@
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix WorldViewProjectionMatrix;
|
||||
matrix WorldMatrix;
|
||||
//float4 TilingOffset;
|
||||
};
|
||||
|
||||
struct VertexInput
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 uv : TEXCOORD;
|
||||
};
|
||||
|
||||
struct VertexOutput
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float3 positionWorld : POSITION1;
|
||||
float3 normal : NORMAL;
|
||||
float2 uv : TEXCOORD;
|
||||
};
|
||||
|
||||
VertexOutput main(VertexInput INPUT)
|
||||
{
|
||||
VertexOutput OUTPUT;
|
||||
|
||||
OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix);
|
||||
OUTPUT.positionWorld = mul(INPUT.position, WorldMatrix);
|
||||
OUTPUT.normal = mul(INPUT.normal, (float3x3)WorldMatrix);
|
||||
OUTPUT.uv = INPUT.uv * float2(1.0f, 1.0f) + float2(0.0f, 0.0f); // uv * tiling + offset
|
||||
|
||||
return OUTPUT;
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
#include "Material.h"
|
||||
#include "Utilities.h"
|
||||
#include "WICTextureLoader11.h"
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
|
||||
{
|
||||
UINT error = 0;
|
||||
|
||||
error = createVertexShader(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
error = createPixelShader(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
error = createMatrixBuffer(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
error = createTextureAndSamplerState(pD3DDevice, textureFileName);
|
||||
CheckError(error);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||
{
|
||||
// set shader stages
|
||||
pD3DDeviceContext->VSSetShader(_pVertexShader, nullptr, 0);
|
||||
pD3DDeviceContext->PSSetShader(_pPixelShader, nullptr, 0);
|
||||
|
||||
// set input layout
|
||||
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
|
||||
|
||||
// set shader resources
|
||||
pD3DDeviceContext->VSSetConstantBuffers(0, 1, &_pMatrixBuffer);
|
||||
|
||||
// set texture
|
||||
pD3DDeviceContext->PSSetShaderResources(0, 1, &_pTexture);
|
||||
|
||||
// set sampler state
|
||||
pD3DDeviceContext->PSSetSamplers(0, 1, &_pSamplerState);
|
||||
}
|
||||
|
||||
void Material::deInit()
|
||||
{
|
||||
safeRelease(_pSamplerState);
|
||||
safeRelease(_pTexture);
|
||||
safeRelease(_pMatrixBuffer);
|
||||
safeRelease(_pInputLayout);
|
||||
safeRelease(_pPixelShader);
|
||||
safeRelease(_pVertexShader);
|
||||
}
|
||||
|
||||
void Material::setMatrixBuffer(ID3D11DeviceContext* pD3DDeviceContext, const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix)
|
||||
{
|
||||
// unpack matrix data
|
||||
XMMATRIX w = XMLoadFloat4x4(&worldMatrix);
|
||||
XMMATRIX v = XMLoadFloat4x4(&viewMatrix);
|
||||
XMMATRIX p = XMLoadFloat4x4(&projectionMatrix);
|
||||
|
||||
XMMATRIX wvp = w * v * p;
|
||||
|
||||
// set matrix data
|
||||
D3D11_MAPPED_SUBRESOURCE bufferData = {};
|
||||
HRESULT hr = pD3DDeviceContext->Map(_pMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &bufferData);
|
||||
if (FAILED(hr)) return;
|
||||
|
||||
MatrixBuffer* matrixBuffer = static_cast<MatrixBuffer*>(bufferData.pData);
|
||||
|
||||
XMStoreFloat4x4(static_cast<XMFLOAT4X4*>(&matrixBuffer->WorldViewProjectionMatrix), XMMatrixTranspose(wvp));
|
||||
XMStoreFloat4x4(static_cast<XMFLOAT4X4*>(&matrixBuffer->WorldMatrix), XMMatrixTranspose(w));
|
||||
|
||||
pD3DDeviceContext->Unmap(_pMatrixBuffer, 0);
|
||||
}
|
||||
|
||||
UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
HRESULT hr = {};
|
||||
ID3DBlob* pCompiledShaderCode = nullptr;
|
||||
|
||||
// part 1 get compiled shader code
|
||||
|
||||
// option 1: load high level shader code & compile
|
||||
//hr = D3DCompileFromFile(
|
||||
// L"ColorVertexShader.hlsl", // shader file name
|
||||
// nullptr, // optional macro defines
|
||||
// nullptr, // pre compiled included shader code
|
||||
// "main", // shader entry point (start function)
|
||||
// "vs_4_0", // shader type & version
|
||||
// 0, 0, // optional flags
|
||||
// &pCompiledShaderCode, // compiled shader code memory
|
||||
// nullptr // error messages
|
||||
//);
|
||||
|
||||
// option 2: load compiled shader code
|
||||
hr = D3DReadFileToBlob(L"ColorVertexShader.cso", &pCompiledShaderCode);
|
||||
//hr = D3DReadFileToBlob(L"TextureVertexShader.cso", &pCompiledShaderCode);
|
||||
//hr = D3DReadFileToBlob(L"LightingVertexShader.cso", &pCompiledShaderCode);
|
||||
|
||||
CheckFailed(hr, 60);
|
||||
|
||||
// part 2 create vertex shader object
|
||||
hr = pD3DDevice->CreateVertexShader(pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), nullptr, &_pVertexShader);
|
||||
CheckFailed(hr, 62);
|
||||
|
||||
UINT error = createInputLayout(pD3DDevice, pCompiledShaderCode);
|
||||
CheckError(error);
|
||||
|
||||
safeRelease(pCompiledShaderCode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
HRESULT hr = {};
|
||||
ID3DBlob* pCompiledShaderCode = nullptr;
|
||||
|
||||
hr = D3DReadFileToBlob(L"ColorPixelShader.cso", &pCompiledShaderCode);
|
||||
//hr = D3DReadFileToBlob(L"TexturePixelShader.cso", &pCompiledShaderCode);
|
||||
//hr = D3DReadFileToBlob(L"LightingPixelShader.cso", &pCompiledShaderCode);
|
||||
CheckFailed(hr, 64);
|
||||
|
||||
hr = pD3DDevice->CreatePixelShader(pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), nullptr, &_pPixelShader);
|
||||
CheckFailed(hr, 66);
|
||||
|
||||
safeRelease(pCompiledShaderCode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShaderCode)
|
||||
{
|
||||
// https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
|
||||
D3D11_INPUT_ELEMENT_DESC elements[4] = {};
|
||||
|
||||
// position
|
||||
elements[0].SemanticName = "POSITION";
|
||||
elements[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
|
||||
// normal
|
||||
elements[1].SemanticName = "NORMAL";
|
||||
elements[1].Format = DXGI_FORMAT_R32G32B32_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;
|
||||
|
||||
// color
|
||||
elements[3].SemanticName = "COLOR";
|
||||
elements[3].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||
elements[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateInputLayout(elements, std::size(elements), pCompiledVertexShaderCode->GetBufferPointer(), pCompiledVertexShaderCode->GetBufferSize(), &_pInputLayout);
|
||||
CheckFailed(hr, 68);
|
||||
|
||||
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;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateBuffer(&desc, nullptr, &_pMatrixBuffer);
|
||||
CheckFailed(hr, 61);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
UINT Material::createTextureAndSamplerState(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
|
||||
{
|
||||
// load texture
|
||||
HRESULT hr = CreateWICTextureFromFile(pD3DDevice, textureFileName, nullptr, &_pTexture);
|
||||
CheckFailed(hr, 63);
|
||||
|
||||
// create sampler state
|
||||
D3D11_SAMPLER_DESC samplerDesc = {};
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; // CAUTION: has to be set always
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
|
||||
hr = pD3DDevice->CreateSamplerState(&samplerDesc, &_pSamplerState);
|
||||
CheckFailed(hr, 65);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
#pragma comment(lib, "d3dcompiler.lib")
|
||||
|
||||
class Material
|
||||
{
|
||||
public:
|
||||
UINT init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName);
|
||||
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
||||
void deInit();
|
||||
|
||||
void setMatrixBuffer(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* pCompiledVertexShaderCode);
|
||||
UINT createMatrixBuffer(ID3D11Device* pD3DDevice);
|
||||
UINT createTextureAndSamplerState(ID3D11Device* pD3DDevice, LPCTSTR textureFileName);
|
||||
|
||||
ID3D11VertexShader* _pVertexShader = nullptr;
|
||||
ID3D11PixelShader* _pPixelShader = nullptr;
|
||||
ID3D11InputLayout* _pInputLayout = nullptr; // byte structure of vertex
|
||||
|
||||
struct MatrixBuffer
|
||||
{
|
||||
XMFLOAT4X4 WorldViewProjectionMatrix;
|
||||
XMFLOAT4X4 WorldMatrix;
|
||||
};
|
||||
ID3D11Buffer* _pMatrixBuffer = nullptr;
|
||||
|
||||
ID3D11ShaderResourceView* _pTexture = {};
|
||||
ID3D11SamplerState* _pSamplerState = {};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
#include "Mesh.h"
|
||||
#include "Utilities.h"
|
||||
#include "Vertex.h"
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
UINT Mesh::init(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
UINT error = 0;
|
||||
|
||||
error = createVertexBuffer(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
error = createIndexBuffer(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
// create world matrix
|
||||
XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Mesh::update(FLOAT dt)
|
||||
{
|
||||
static FLOAT x = 0.0f;
|
||||
static FLOAT y = 0.0f;
|
||||
static FLOAT z = 0.0f;
|
||||
static FLOAT rot = 0.0f;
|
||||
rot += 1.0f * dt;
|
||||
|
||||
FLOAT move = 1.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_ADD) & 0x8000) || (GetAsyncKeyState('Q') & 0x8000)) z -= move;
|
||||
if ((GetAsyncKeyState(VK_SUBTRACT) & 0x8000) || (GetAsyncKeyState('E') & 0x8000)) z += move;
|
||||
|
||||
XMMATRIX translation = XMMatrixTranslation(x, y, z);
|
||||
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, 0.0f);
|
||||
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
||||
|
||||
XMVECTOR det = XMMatrixDeterminant(translation);
|
||||
|
||||
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation);
|
||||
}
|
||||
|
||||
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||
{
|
||||
// set data
|
||||
static UINT offset = 0;
|
||||
pD3DDeviceContext->IASetVertexBuffers(0, 1, &_pVertexBuffer, &_vertexStride, &offset);
|
||||
pD3DDeviceContext->IASetIndexBuffer(_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
|
||||
pD3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
|
||||
// draw
|
||||
pD3DDeviceContext->DrawIndexed(_indexCount, 0, 0);
|
||||
}
|
||||
|
||||
void Mesh::deInit()
|
||||
{
|
||||
safeRelease(_pIndexBuffer);
|
||||
safeRelease(_pVertexBuffer);
|
||||
}
|
||||
|
||||
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
// describe vertex buffer content
|
||||
// quad with color
|
||||
Vertex vertices[] = { Vertex(-0.5f, 0.5f, 0.0f, 255, 0, 0),
|
||||
Vertex(0.5f, 0.5f, 0.0f, 0, 255, 0),
|
||||
Vertex(0.5f, -0.5f, 0.0f, 255, 0, 255),
|
||||
Vertex(-0.5f, -0.5f, 0.0f, 0, 0, 255) };
|
||||
|
||||
|
||||
// quad with uv & normal
|
||||
//Vertex vertices[] = {
|
||||
// Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f),
|
||||
// Vertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f),
|
||||
// Vertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f),
|
||||
// Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f)
|
||||
//};
|
||||
|
||||
// general data
|
||||
_vertexCount = std::size(vertices);
|
||||
_vertexStride = sizeof(Vertex);
|
||||
|
||||
// create vertex buffer object
|
||||
D3D11_BUFFER_DESC desc = {};
|
||||
desc.ByteWidth = _vertexCount * _vertexStride; // size of vertex buffer in bytes
|
||||
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
desc.Usage = D3D11_USAGE_IMMUTABLE; // https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
|
||||
|
||||
D3D11_SUBRESOURCE_DATA data = {};
|
||||
data.pSysMem = vertices;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pVertexBuffer);
|
||||
CheckFailed(hr, 30);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
// describe index buffer content
|
||||
WORD indices[] = {
|
||||
0,1,2,
|
||||
0,2,3
|
||||
};
|
||||
|
||||
// general data
|
||||
_indexCount = std::size(indices);
|
||||
|
||||
// create index buffer object
|
||||
D3D11_BUFFER_DESC desc = {};
|
||||
desc.ByteWidth = _indexCount * sizeof(WORD); // size of index buffer in bytes
|
||||
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||
desc.Usage = D3D11_USAGE_IMMUTABLE;
|
||||
|
||||
D3D11_SUBRESOURCE_DATA data = {};
|
||||
data.pSysMem = indices;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pIndexBuffer);
|
||||
CheckFailed(hr, 32);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <d3d11.h>
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
class Mesh
|
||||
{
|
||||
public:
|
||||
UINT init(ID3D11Device* pD3DDevice);
|
||||
void update(FLOAT dt);
|
||||
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
||||
void deInit();
|
||||
|
||||
const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; }
|
||||
|
||||
private:
|
||||
UINT createVertexBuffer(ID3D11Device* pD3DDevice);
|
||||
UINT createIndexBuffer(ID3D11Device* pD3DDevice);
|
||||
|
||||
ID3D11Buffer* _pVertexBuffer = nullptr; // dumps mesh vertices
|
||||
ID3D11Buffer* _pIndexBuffer = nullptr; // dumps mesh indices
|
||||
|
||||
UINT _vertexCount = 0; // amount of vertices
|
||||
UINT _vertexStride = 0; // size of one vertex in bytes
|
||||
UINT _indexCount = 0; // amount of indices
|
||||
|
||||
XMFLOAT4X4 _worldMatrix = {};
|
||||
};
|
||||
|
||||
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,27 @@
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix WorldViewProjectionMatrix;
|
||||
//float4 TilingOffset;
|
||||
};
|
||||
|
||||
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 * float2(1.0f, 1.0f) + float2(0.0f, 0.0f); // uv * tiling + offset
|
||||
|
||||
return OUTPUT;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "Time.h"
|
||||
#include <string>
|
||||
|
||||
UINT Time::init()
|
||||
{
|
||||
_lastTime = CLOCK::now();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Time::update()
|
||||
{
|
||||
TIME_POINT now = CLOCK::now();
|
||||
SECONDS diff = now - _lastTime;
|
||||
_deltaTime = diff.count();
|
||||
_lastTime = now;
|
||||
|
||||
#ifdef _DEBUG
|
||||
#ifdef UNICODE
|
||||
wstring output = L"fps: " + to_wstring(1 / _deltaTime) + L"\n";
|
||||
OutputDebugString(output.c_str());
|
||||
#else // !UNICODE
|
||||
string output = "fps: " + to_string(1 / _deltaTime) + "\n";
|
||||
OutputDebugString(output.c_str());
|
||||
#endif // UNICODE
|
||||
|
||||
|
||||
#endif // DEBUG
|
||||
|
||||
}
|
||||
|
||||
void Time::deInit()
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef chrono::steady_clock CLOCK;
|
||||
typedef CLOCK::time_point TIME_POINT;
|
||||
typedef chrono::duration<FLOAT> SECONDS;
|
||||
|
||||
class Time
|
||||
{
|
||||
public:
|
||||
UINT init();
|
||||
void update();
|
||||
void deInit();
|
||||
|
||||
FLOAT getDeltaTime() const { return _deltaTime; }
|
||||
|
||||
private:
|
||||
FLOAT _deltaTime = 0.0f;
|
||||
TIME_POINT _lastTime = {};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "Utilities.h"
|
||||
|
||||
void ShowError(UINT error)
|
||||
{
|
||||
#if _DEBUG
|
||||
LPCTSTR msg = TEXT("Application quit with an error: ");
|
||||
#if UNICODE
|
||||
msg = (std::wstring(msg) + std::to_wstring(error)).c_str();
|
||||
#else // UNICODE
|
||||
msg = (std::string(msg) + std::to_string(error)).c_str();
|
||||
#endif // UNICODE
|
||||
MessageBox(nullptr, msg, TEXT("Error"), MB_OK);
|
||||
#endif // DEBUG
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
|
||||
void ShowError(UINT error);
|
||||
|
||||
#define CheckError(x) if (x != 0) \
|
||||
{ \
|
||||
ShowError(x); \
|
||||
return x; \
|
||||
}
|
||||
|
||||
#define CheckFailed(hr, x) if (FAILED(hr)) \
|
||||
{ \
|
||||
ShowError(x); \
|
||||
return x; \
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void safeRelease(T*& obj)
|
||||
{
|
||||
if (obj != nullptr)
|
||||
{
|
||||
obj->Release();
|
||||
obj = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <d3d11.h>
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT3 normal;
|
||||
XMFLOAT2 uv;
|
||||
XMFLOAT4 color;
|
||||
|
||||
Vertex() : position(0.0f, 0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f), uv(0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f) {}
|
||||
Vertex(FLOAT x, FLOAT y, FLOAT z) : position(x, y, z), normal(0.0f, 0.0f, 0.0f), uv(0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f) {}
|
||||
Vertex(FLOAT x, FLOAT y, FLOAT z, UINT r, UINT g, UINT b)
|
||||
: position(x, y, z), normal(0.0f, 0.0f, 0.0f), uv(0.0f, 0.0f), color(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f) {}
|
||||
Vertex(FLOAT x, FLOAT y, FLOAT z, FLOAT u, FLOAT v) : position(x, y, z), normal(0.0f, 0.0f, 0.0f), uv(u, v), color(1.0f, 1.0f, 1.0f, 1.0f) {}
|
||||
Vertex(FLOAT x, FLOAT y, FLOAT z, FLOAT nx, FLOAT ny, FLOAT nz, FLOAT u, FLOAT v) : position(x, y, z), normal(nx, ny, nz), uv(u, v), color(1.0f, 1.0f, 1.0f, 1.0f) {}
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,148 @@
|
||||
//--------------------------------------------------------------------------------------
|
||||
// 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.
|
||||
//
|
||||
// http://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"
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "Window.h"
|
||||
|
||||
INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
|
||||
{
|
||||
// 1. describe window class
|
||||
WNDCLASS wc = {};
|
||||
wc.hInstance = hInstance; // application instance
|
||||
wc.lpszClassName = TEXT("D3D11"); // name of window class
|
||||
wc.lpfnWndProc = WindowProc; // message queue handler function
|
||||
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // behaviour style and not visual style
|
||||
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0)); // set window background color
|
||||
wc.hCursor = LoadCursor(nullptr, IDC_ARROW); // set window cursor
|
||||
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); // set window icon
|
||||
|
||||
// 2. register window class
|
||||
if (!RegisterClass(&wc)) return 10;
|
||||
|
||||
// 3. adjust window size (optional)
|
||||
DWORD style = WS_POPUP;
|
||||
RECT rect = { 100, 100, width + 100, height + 100 }; // left-top corner, right-bottom corner
|
||||
AdjustWindowRect(&rect, style, FALSE);
|
||||
|
||||
// 4. create window instance
|
||||
_hWnd = CreateWindow(
|
||||
wc.lpszClassName, // name of window class
|
||||
wc.lpszClassName, // window title
|
||||
style, // visual window style
|
||||
rect.left, rect.top, // window position
|
||||
rect.right - rect.left, rect.bottom - rect.top, // window size
|
||||
nullptr, // parent window handle
|
||||
nullptr, // menu instance handle
|
||||
hInstance, // application instance
|
||||
nullptr // optional parameter
|
||||
);
|
||||
|
||||
if (_hWnd == nullptr) return 12;
|
||||
|
||||
// 5. show window
|
||||
ShowWindow(_hWnd, SW_SHOW);
|
||||
SetFocus(_hWnd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL Window::run()
|
||||
{
|
||||
// 6. run window
|
||||
|
||||
// message queue
|
||||
// GetMessage - event based message method, return something if a new a message occur
|
||||
// PeekMessage - returns a boolean, to signale if a message is available or not
|
||||
// TranslateMessage - translate hardware keyboard codes (scan code) into virtual key table
|
||||
// DispatchMessage - send message to WindowProc
|
||||
// https://learn.microsoft.com/en-us/windows/win32/winmsg/messages-and-message-queues
|
||||
|
||||
MSG msg = {};
|
||||
|
||||
if (PeekMessage(&msg, nullptr, 0, UINT_MAX, PM_REMOVE))
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
Sleep(10);
|
||||
|
||||
return msg.message != WM_QUIT;
|
||||
}
|
||||
|
||||
void Window::deInit()
|
||||
{
|
||||
}
|
||||
|
||||
LRESULT WindowProc(
|
||||
HWND hWnd, // handle to window instance
|
||||
UINT msg, // message id
|
||||
WPARAM wParam, // main paramater
|
||||
LPARAM lParam // additional parameters
|
||||
)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case WM_CLOSE:
|
||||
DestroyWindow(hWnd);
|
||||
break;
|
||||
|
||||
case WM_DESTROY:
|
||||
PostQuitMessage(0);
|
||||
break;
|
||||
|
||||
case WM_KEYDOWN:
|
||||
if (wParam == VK_ESCAPE) DestroyWindow(hWnd);
|
||||
break;
|
||||
|
||||
default:
|
||||
return DefWindowProc(hWnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
INT init(HINSTANCE hInstance, UINT width, UINT height);
|
||||
BOOL run();
|
||||
void deInit();
|
||||
|
||||
HWND getWindowHandle() const { return _hWnd; }
|
||||
|
||||
private:
|
||||
HWND _hWnd = nullptr; // handle to window instance
|
||||
};
|
||||
|
||||
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
||||
@@ -0,0 +1,105 @@
|
||||
#include <Windows.h>
|
||||
#include <random>
|
||||
#include "Window.h"
|
||||
#include "Direct3D.h"
|
||||
#include "Mesh.h"
|
||||
#include "Material.h"
|
||||
#include "Camera.h"
|
||||
#include "Time.h"
|
||||
#include "Light.h"
|
||||
#include "Utilities.h"
|
||||
|
||||
int WINAPI WinMain(
|
||||
HINSTANCE hInstance, // handle to instance of application
|
||||
HINSTANCE hPrevInstance, // deprecated
|
||||
PSTR szCmdLine, // command line
|
||||
int nCmdShow // how should the window be opened (minimized, maximized or normal)
|
||||
)
|
||||
{
|
||||
// some constant values
|
||||
const UINT width = 640;
|
||||
const UINT height = 480;
|
||||
const BOOL isFullscreen = FALSE;
|
||||
const BYTE red = 255;
|
||||
const BYTE green = 255;
|
||||
const BYTE blue = 255;
|
||||
|
||||
UINT error = 0; // error code for identifying errors
|
||||
|
||||
// 1. create window
|
||||
Window window = {};
|
||||
error = window.init(hInstance, width, height);
|
||||
CheckError(error);
|
||||
|
||||
// 2. create connection to Direct3D
|
||||
Direct3D d3d = {};
|
||||
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
|
||||
CheckError(error);
|
||||
|
||||
// 3. create a mesh
|
||||
Mesh mesh = {};
|
||||
error = mesh.init(d3d.getDevice());
|
||||
CheckError(error);
|
||||
|
||||
// 4. create a camera
|
||||
Camera camera = {};
|
||||
error = camera.init(width, height);
|
||||
CheckError(error);
|
||||
|
||||
// 5. create time
|
||||
Time time = {};
|
||||
error = time.init();
|
||||
CheckError(error);
|
||||
|
||||
// 6. create a material
|
||||
Material material = {};
|
||||
error = material.init(d3d.getDevice(), TEXT("wall.jpg"));
|
||||
CheckError(error);
|
||||
|
||||
// 7. create light source
|
||||
Light light = {};
|
||||
Light::LightBuffer lightData = {};
|
||||
lightData.LightVector = { 1.0f, -1.0f, 1.0f };
|
||||
lightData.DiffuseColor = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
lightData.SpecularColor = { 1.0f, 0.0f, 1.0f, 1.0f };
|
||||
lightData.LightIntensity = 1.0f;
|
||||
lightData.Phong = 8.0f;
|
||||
lightData.CameraPosition = { 0.0f, 0.0f, -1.0f };
|
||||
error = light.init(d3d.getDevice(), lightData);
|
||||
CheckError(error);
|
||||
|
||||
// 8. run application
|
||||
while (window.run())
|
||||
{
|
||||
// 8.1. update objects
|
||||
time.update();
|
||||
mesh.update(time.getDeltaTime());
|
||||
camera.update(time.getDeltaTime());
|
||||
light.update(time.getDeltaTime());
|
||||
|
||||
// 8.2. render objects
|
||||
static std::default_random_engine e;
|
||||
static std::uniform_int_distribution<int> d(0, 255);
|
||||
|
||||
//d3d.beginScene(d(e), d(e), d(e));
|
||||
d3d.beginScene(red, green, blue);
|
||||
|
||||
light.render(d3d.getDeviceContext());
|
||||
material.setMatrixBuffer(d3d.getDeviceContext(), mesh.getWorldMatrix(), camera.getViewMatrix(), camera.getProjectionMatrix());
|
||||
material.render(d3d.getDeviceContext());
|
||||
mesh.render(d3d.getDeviceContext());
|
||||
|
||||
d3d.endScene();
|
||||
}
|
||||
|
||||
// 9. tidy up
|
||||
light.deInit();
|
||||
time.deInit();
|
||||
camera.deInit();
|
||||
material.deInit();
|
||||
mesh.deInit();
|
||||
d3d.deInit();
|
||||
window.deInit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 213 KiB |
Reference in New Issue
Block a user