Compare commits
2
Commits
024a283898
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97192e1587 | ||
|
|
85d8830b84 |
+4
-13
@@ -1,10 +1,6 @@
|
|||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
|
|
||||||
#include <DirectXMath.h>
|
UINT Camera::init(UINT width, UINT height)
|
||||||
|
|
||||||
using namespace DirectX;
|
|
||||||
|
|
||||||
UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
|
|
||||||
{
|
{
|
||||||
// view matrix
|
// view matrix
|
||||||
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
|
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
|
||||||
@@ -12,10 +8,8 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
|
|||||||
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
|
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up);
|
XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up);
|
||||||
XMFLOAT4X4 v;
|
XMStoreFloat4x4(&_viewMatrix, lookTo);
|
||||||
XMStoreFloat4x4(&v, lookTo);
|
|
||||||
_viewMatrix = *reinterpret_cast<D3DMATRIX*>(&v);
|
|
||||||
|
|
||||||
// projection matrix
|
// projection matrix
|
||||||
float fov = XMConvertToRadians(60.0f);
|
float fov = XMConvertToRadians(60.0f);
|
||||||
float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
|
float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
|
||||||
@@ -23,10 +17,7 @@ UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
|
|||||||
float farClippingPlane = 100.0f;
|
float farClippingPlane = 100.0f;
|
||||||
|
|
||||||
XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
|
XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
|
||||||
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_projectionMatrix), perspective);
|
XMStoreFloat4x4(&_projectionMatrix, perspective);
|
||||||
|
|
||||||
pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix);
|
|
||||||
pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-4
@@ -1,15 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d9.h>
|
#include <DirectXMath.h>
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UINT init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height);
|
UINT init(UINT width, UINT height);
|
||||||
void update(FLOAT dt);
|
void update(FLOAT dt);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
const XMFLOAT4X4& getViewMatrix() const { return _viewMatrix; }
|
||||||
|
const XMFLOAT4X4& getProjectionMatrix() const { return _projectionMatrix; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
D3DMATRIX _viewMatrix = {};
|
XMFLOAT4X4 _viewMatrix = {};
|
||||||
D3DMATRIX _projectionMatrix = {};
|
XMFLOAT4X4 _projectionMatrix = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -199,6 +199,26 @@
|
|||||||
<ClInclude Include="WICTextureLoader9.h" />
|
<ClInclude Include="WICTextureLoader9.h" />
|
||||||
<ClInclude Include="Window.h" />
|
<ClInclude Include="Window.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<FxCompile Include="PixelShader.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="VertexShader.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>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
|
|||||||
@@ -78,4 +78,12 @@
|
|||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<FxCompile Include="VertexShader.hlsl">
|
||||||
|
<Filter>Resource Files</Filter>
|
||||||
|
</FxCompile>
|
||||||
|
<FxCompile Include="PixelShader.hlsl">
|
||||||
|
<Filter>Resource Files</Filter>
|
||||||
|
</FxCompile>
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
+72
-36
@@ -5,62 +5,98 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
|
|||||||
{
|
{
|
||||||
HRESULT hr = {};
|
HRESULT hr = {};
|
||||||
|
|
||||||
// 1. create connection to Direct3D API
|
// 1. define presentation parameters
|
||||||
IDirect3D9* pD3D = Direct3DCreate9(D3D_SDK_VERSION);
|
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
|
||||||
if (pD3D == nullptr) return 200;
|
swapChainDesc.OutputWindow = hWnd; // target window
|
||||||
|
swapChainDesc.Windowed = !isFullscreen; // window mode yes or no
|
||||||
|
swapChainDesc.BufferCount = 1;
|
||||||
|
swapChainDesc.BufferDesc.Width = width;
|
||||||
|
swapChainDesc.BufferDesc.Height = height;
|
||||||
|
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how to use/access the back buffer
|
||||||
|
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
||||||
|
swapChainDesc.SampleDesc.Count = 1; // at least set to one
|
||||||
|
|
||||||
// 2. check compatibility
|
// 2. create Direct3D device, device context & swap chain
|
||||||
D3DCAPS9 caps = {};
|
D3D_FEATURE_LEVEL supportedFeatureLevels[] =
|
||||||
UINT adapter = D3DADAPTER_DEFAULT; // graphic card index, 0 -> primary
|
{
|
||||||
D3DDEVTYPE devType = D3DDEVTYPE_HAL; // hardware or software renderer, here hardware
|
D3D_FEATURE_LEVEL_11_1,
|
||||||
hr = pD3D->GetDeviceCaps(adapter, devType, &caps);
|
D3D_FEATURE_LEVEL_11_0,
|
||||||
CheckFailed(hr, 210);
|
};
|
||||||
|
D3D_FEATURE_LEVEL chosenFeatureLevel = {};
|
||||||
|
|
||||||
DWORD vertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING; // fallback if hardware processing is not available
|
hr = D3D11CreateDeviceAndSwapChain(
|
||||||
if (caps.VertexProcessingCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
|
nullptr, // graphic card adapter
|
||||||
vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING; // hardware processing is available
|
D3D_DRIVER_TYPE_HARDWARE, // hardware or software rendering
|
||||||
|
nullptr, // software rendering module
|
||||||
|
0, // optional flags
|
||||||
|
supportedFeatureLevels, std::size(supportedFeatureLevels), // supported D3D api versions
|
||||||
|
D3D11_SDK_VERSION,
|
||||||
|
&swapChainDesc, &_pDXGISwapChain, &_pD3DDevice, &chosenFeatureLevel, &_pD3DDeviceContext
|
||||||
|
);
|
||||||
|
CheckFailed(hr, 200);
|
||||||
|
|
||||||
// 3. define presentation parameters
|
// 3. create render target view
|
||||||
D3DPRESENT_PARAMETERS pp = {};
|
ID3D11Texture2D* pBackBuffer = nullptr;
|
||||||
pp.hDeviceWindow = hWnd; // target window
|
hr = _pDXGISwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
|
||||||
pp.Windowed = !isFullscreen; // window mode yes or no
|
|
||||||
pp.BackBufferCount = 1;
|
|
||||||
pp.BackBufferWidth = width;
|
|
||||||
pp.BackBufferHeight = height;
|
|
||||||
pp.BackBufferFormat = D3DFMT_A8R8G8B8; // 4 channels, each channel 8bit
|
|
||||||
pp.SwapEffect = D3DSWAPEFFECT_DISCARD; // what happens with the front buffer after a swap
|
|
||||||
|
|
||||||
// 4. create Direct3D device
|
|
||||||
hr = pD3D->CreateDevice(adapter, devType, hWnd, vertexProcessing, &pp, &_pD3DDevice);
|
|
||||||
CheckFailed(hr, 220);
|
CheckFailed(hr, 220);
|
||||||
|
|
||||||
// 5. tidy up
|
hr = _pD3DDevice->CreateRenderTargetView(pBackBuffer, nullptr, &_pRenderTargetView);
|
||||||
safeRelease(pD3D);
|
CheckFailed(hr, 225);
|
||||||
|
|
||||||
|
safeRelease(pBackBuffer);
|
||||||
|
|
||||||
|
// 4. create rasterizer state
|
||||||
|
|
||||||
|
//d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
||||||
|
|
||||||
|
//d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);
|
||||||
|
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
|
||||||
|
//d3d.getDevice()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
//d3d.getDevice()->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
//d3d.getDevice()->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
|
||||||
|
|
||||||
|
// 5. create depth stencil view & state
|
||||||
|
|
||||||
|
// 6. define view port
|
||||||
|
D3D11_VIEWPORT viewPort = {};
|
||||||
|
viewPort.Width = width;
|
||||||
|
viewPort.Height = height;
|
||||||
|
viewPort.MinDepth = 0.0f;
|
||||||
|
viewPort.MaxDepth = 1.0f;
|
||||||
|
|
||||||
|
// 7. setup render pipeline
|
||||||
|
_pD3DDeviceContext->RSSetViewports(1, &viewPort);
|
||||||
|
_pD3DDeviceContext->OMSetRenderTargets(1, &_pRenderTargetView, nullptr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Direct3D::beginScene()
|
void Direct3D::beginScene()
|
||||||
{
|
{
|
||||||
_pD3DDevice->BeginScene();
|
|
||||||
|
|
||||||
// clear buffers
|
// clear buffers
|
||||||
_pD3DDevice->Clear(
|
FLOAT backgroundColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
|
||||||
0, nullptr, // clear regions, here clear everything
|
_pD3DDeviceContext->ClearRenderTargetView(_pRenderTargetView, backgroundColor);
|
||||||
D3DCLEAR_TARGET, // which buffers to clear
|
|
||||||
D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0xffffffff // clear values
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Direct3D::endScene()
|
void Direct3D::endScene()
|
||||||
{
|
{
|
||||||
_pD3DDevice->EndScene();
|
|
||||||
|
|
||||||
// present frame
|
// present frame
|
||||||
_pD3DDevice->Present(nullptr, nullptr, nullptr, nullptr);
|
_pDXGISwapChain->Present(
|
||||||
|
0, // 0 -> vsync deactivated, 1,2,3,4 -> vsync activated with x frames
|
||||||
|
0 // optional flags
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Direct3D::deInit()
|
void Direct3D::deInit()
|
||||||
{
|
{
|
||||||
|
safeRelease(_pDepthStencilState);
|
||||||
|
safeRelease(_pDepthStencilView);
|
||||||
|
safeRelease(_pRasterizerState);
|
||||||
|
safeRelease(_pRenderTargetView);
|
||||||
|
safeRelease(_pDXGISwapChain);
|
||||||
|
safeRelease(_pD3DDeviceContext);
|
||||||
safeRelease(_pD3DDevice);
|
safeRelease(_pD3DDevice);
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-4
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d9.h>
|
#include <d3d11.h>
|
||||||
|
|
||||||
#pragma comment(lib, "d3d9.lib")
|
#pragma comment(lib, "d3d11.lib")
|
||||||
|
|
||||||
class Direct3D
|
class Direct3D
|
||||||
{
|
{
|
||||||
@@ -11,9 +11,16 @@ public:
|
|||||||
void endScene();
|
void endScene();
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
IDirect3DDevice9* getDevice() const { return _pD3DDevice; }
|
ID3D11Device* getDevice() const { return _pD3DDevice; }
|
||||||
|
ID3D11DeviceContext* getDeviceContext() const { return _pD3DDeviceContext; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IDirect3DDevice9* _pD3DDevice = nullptr; // our interface for api communication
|
ID3D11Device* _pD3DDevice = nullptr; // create other api objects, e.g. buffer
|
||||||
|
ID3D11DeviceContext* _pD3DDeviceContext = nullptr; // customize render pipeline, e.g. set buffer or make draw calls
|
||||||
|
IDXGISwapChain* _pDXGISwapChain = nullptr; // holds references to front and back buffer and swap them
|
||||||
|
ID3D11RenderTargetView* _pRenderTargetView = nullptr; // back buffer
|
||||||
|
ID3D11RasterizerState* _pRasterizerState = nullptr; // customize rasterizer stage
|
||||||
|
ID3D11DepthStencilView* _pDepthStencilView = nullptr; // depth & stencil buffer
|
||||||
|
ID3D11DepthStencilState* _pDepthStencilState = nullptr; // customize depth stencil buffering
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+173
-23
@@ -1,41 +1,191 @@
|
|||||||
#include "Material.h"
|
#include "Material.h"
|
||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
|
|
||||||
#include "WICTextureLoader9.h"
|
//#include "WICTextureLoader9.h"
|
||||||
|
|
||||||
UINT Material::init(IDirect3DDevice9* pD3DDevice, LPCTSTR textureFileName)
|
UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
|
||||||
{
|
{
|
||||||
HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
|
//HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
|
||||||
CheckFailed(hr, 600);
|
//CheckFailed(hr, 600);
|
||||||
|
|
||||||
|
UINT error = 0;
|
||||||
|
|
||||||
|
error = createVertexShader(pD3DDevice);
|
||||||
|
CheckError(error);
|
||||||
|
|
||||||
|
error = createPixelShader(pD3DDevice);
|
||||||
|
CheckError(error);
|
||||||
|
|
||||||
|
error = createMatrixBuffer(pD3DDevice);
|
||||||
|
CheckError(error);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Material::render(IDirect3DDevice9* pD3DDevice)
|
void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||||
{
|
{
|
||||||
// set texture
|
//// set texture
|
||||||
pD3DDevice->SetTexture(0, _pTexture);
|
//pD3DDevice->SetTexture(0, _pTexture);
|
||||||
|
|
||||||
// set sampler state
|
//// set sampler state
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(100, 0, 100));
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(100, 0, 100));
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
|
//pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
|
||||||
|
|
||||||
// set material
|
//// set material
|
||||||
D3DMATERIAL9 material = {};
|
//D3DMATERIAL9 material = {};
|
||||||
material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
|
//material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
material.Diffuse = { 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.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
material.Power = 100.0f;
|
//material.Power = 100.0f;
|
||||||
material.Emissive = { 1.0f, 0.0f, 0.0f, 1.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()
|
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)
|
||||||
|
{
|
||||||
|
HRESULT hr = {};
|
||||||
|
ID3DBlob* pCompiledShaderCode = nullptr;
|
||||||
|
|
||||||
|
// two options to get compiled shader code
|
||||||
|
// option 1: compile at runtime
|
||||||
|
//hr = D3DCompileFromFile(
|
||||||
|
// L"VertexShader.hlsl", // shader filename
|
||||||
|
// nullptr, nullptr, // shared shader code
|
||||||
|
// "main", // shader entry function
|
||||||
|
// "vs_4_0", // shader type & model (version)
|
||||||
|
// 0, 0, // optional flags
|
||||||
|
// &pCompiledShaderCode,
|
||||||
|
// nullptr // optional blob for compiler error messages
|
||||||
|
//);
|
||||||
|
|
||||||
|
// option 2: load compiled code from file
|
||||||
|
hr = D3DReadFileToBlob(L"VertexShader.cso", &pCompiledShaderCode);
|
||||||
|
|
||||||
|
CheckFailed(hr, 600);
|
||||||
|
|
||||||
|
hr = pD3DDevice->CreateVertexShader(
|
||||||
|
pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), // compiled shader code
|
||||||
|
nullptr, // shared code code
|
||||||
|
&_pVertexShader
|
||||||
|
);
|
||||||
|
CheckFailed(hr, 605);
|
||||||
|
|
||||||
|
UINT error = createInputLayout(pD3DDevice, pCompiledShaderCode);
|
||||||
|
CheckError(error);
|
||||||
|
|
||||||
|
safeRelease(pCompiledShaderCode);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
|
||||||
|
{
|
||||||
|
HRESULT hr = {};
|
||||||
|
ID3DBlob* pCompiledShaderCode = nullptr;
|
||||||
|
|
||||||
|
hr = D3DReadFileToBlob(L"PixelShader.cso", &pCompiledShaderCode);
|
||||||
|
CheckFailed(hr, 620);
|
||||||
|
|
||||||
|
hr = pD3DDevice->CreatePixelShader(
|
||||||
|
pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), // compiled shader code
|
||||||
|
nullptr, // shared code code
|
||||||
|
&_pPixelShader
|
||||||
|
);
|
||||||
|
CheckFailed(hr, 625);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-4
@@ -1,14 +1,39 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d9.h>
|
#include <d3d11.h>
|
||||||
|
#include <d3dcompiler.h>
|
||||||
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
|
#pragma comment(lib, "d3dcompiler.lib")
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
class Material
|
class Material
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UINT init(IDirect3DDevice9* pD3DDevice, LPCTSTR textureFileName);
|
UINT init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName);
|
||||||
void render(IDirect3DDevice9* pD3DDevice);
|
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
UINT setMatrices(ID3D11DeviceContext* pD3DDeviceContext,
|
||||||
|
const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IDirect3DTexture9* _pTexture = {};
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+51
-92
@@ -2,11 +2,7 @@
|
|||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
#include "Vertex.h"
|
#include "Vertex.h"
|
||||||
|
|
||||||
#include <DirectXMath.h>
|
UINT Mesh::init(ID3D11Device* pD3DDevice)
|
||||||
|
|
||||||
using namespace DirectX;
|
|
||||||
|
|
||||||
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
|
||||||
{
|
{
|
||||||
UINT error = 0;
|
UINT error = 0;
|
||||||
|
|
||||||
@@ -16,10 +12,7 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
|||||||
error = createIndexBuffer(pD3DDevice);
|
error = createIndexBuffer(pD3DDevice);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
XMMATRIX identity = XMMatrixIdentity();
|
XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity());
|
||||||
XMFLOAT4X4 w;
|
|
||||||
XMStoreFloat4x4(&w, identity);
|
|
||||||
_worldMatrix = *reinterpret_cast<D3DMATRIX*>(&w);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -48,25 +41,25 @@ void Mesh::update(FLOAT dt)
|
|||||||
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ);
|
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ);
|
||||||
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_worldMatrix), scale * rotation * translation);
|
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||||
{
|
{
|
||||||
// setup pipeline
|
// setup pipeline
|
||||||
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
UINT offset = 0;
|
||||||
pD3DDevice->SetIndices(_pIndexBuffer);
|
pD3DDeviceContext->IASetVertexBuffers(
|
||||||
pD3DDevice->SetFVF(FVF);
|
0, // which vertex buffer should be set (Startslot)
|
||||||
pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix);
|
1, &_pVertexBuffer, // buffers to set
|
||||||
|
&_vertexStride, // vertex size for each buffer
|
||||||
|
&offset // offset for each buffer
|
||||||
|
);
|
||||||
|
|
||||||
|
pD3DDeviceContext->IASetIndexBuffer(_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
|
||||||
|
pD3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||||
|
|
||||||
// draw call
|
// draw call
|
||||||
pD3DDevice->DrawIndexedPrimitive(
|
pD3DDeviceContext->DrawIndexed(_indexCount, 0, 0);
|
||||||
D3DPT_TRIANGLELIST, // primitive type
|
|
||||||
0, 0, // vertex buffer offsets
|
|
||||||
_vertexCount,
|
|
||||||
0, // index buffer offset
|
|
||||||
_indexCount / 3 // primitive count
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::deInit()
|
void Mesh::deInit()
|
||||||
@@ -75,88 +68,54 @@ void Mesh::deInit()
|
|||||||
safeRelease(_pVertexBuffer);
|
safeRelease(_pVertexBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
|
||||||
{
|
{
|
||||||
// define values
|
// define values
|
||||||
_vertexCount = 4;
|
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),
|
||||||
|
};
|
||||||
|
|
||||||
|
_vertexCount = std::size(vertices);
|
||||||
_vertexStride = sizeof(Vertex);
|
_vertexStride = sizeof(Vertex);
|
||||||
|
|
||||||
// describe & create vertex buffer
|
// describe & create vertex buffer
|
||||||
HRESULT hr = pD3DDevice->CreateVertexBuffer(
|
D3D11_BUFFER_DESC desc = {};
|
||||||
_vertexCount * _vertexStride, // total size of content
|
desc.ByteWidth = _vertexCount * _vertexStride; // total size of content
|
||||||
D3DUSAGE_WRITEONLY, // read and/or write access
|
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // buffer type
|
||||||
FVF, // FVF - Flexible Vertex Format -> vertex structure
|
desc.Usage = D3D11_USAGE_IMMUTABLE; // https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
|
||||||
D3DPOOL_MANAGED, // memmory management
|
|
||||||
&_pVertexBuffer,
|
D3D11_SUBRESOURCE_DATA data = {};
|
||||||
nullptr // deprecated, always nullptr
|
data.pSysMem = vertices;
|
||||||
);
|
|
||||||
|
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pVertexBuffer);
|
||||||
CheckFailed(hr, 300);
|
CheckFailed(hr, 300);
|
||||||
|
|
||||||
// populate vertex buffer
|
return 0;
|
||||||
Vertex* pVertices = nullptr;
|
}
|
||||||
hr = _pVertexBuffer->Lock(
|
|
||||||
0, 0, // offset & size to lock, here everything
|
|
||||||
reinterpret_cast<void**>(&pVertices),
|
|
||||||
0 // optional flags
|
|
||||||
);
|
|
||||||
CheckFailed(hr, 310);
|
|
||||||
|
|
||||||
// triangle
|
UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice)
|
||||||
//pVertices[0] = Vertex(0.0f, 1.0f, 0.0f);
|
{
|
||||||
//pVertices[1] = Vertex(1.0f, -1.0f, 0.0f);
|
unsigned short indices[] =
|
||||||
//pVertices[2] = Vertex(-1.0f, -1.0f, 0.0f);
|
{
|
||||||
|
0, 1, 2,
|
||||||
|
0, 2, 3
|
||||||
|
};
|
||||||
|
_indexCount = std::size(indices);
|
||||||
|
|
||||||
// quad
|
D3D11_BUFFER_DESC desc = {};
|
||||||
//pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f);
|
desc.ByteWidth = _indexCount * sizeof(unsigned short); // total size of content
|
||||||
//pVertices[1] = Vertex(0.5f, 0.5f, 0.0f);
|
desc.BindFlags = D3D11_BIND_INDEX_BUFFER; // buffer type
|
||||||
//pVertices[2] = Vertex(0.5f, -0.5f, 0.0f);
|
desc.Usage = D3D11_USAGE_IMMUTABLE; // https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
|
||||||
//pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f);
|
|
||||||
|
|
||||||
// quad with color
|
D3D11_SUBRESOURCE_DATA data = {};
|
||||||
//pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 255, 0, 0);
|
data.pSysMem = indices;
|
||||||
//pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0, 255, 0);
|
|
||||||
//pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 255, 0, 255);
|
|
||||||
//pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0, 0, 255);
|
|
||||||
|
|
||||||
// quad with uvs & normals
|
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pIndexBuffer);
|
||||||
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
|
|
||||||
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
|
|
||||||
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
|
|
||||||
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
|
|
||||||
|
|
||||||
hr = _pVertexBuffer->Unlock();
|
|
||||||
CheckFailed(hr, 320);
|
CheckFailed(hr, 320);
|
||||||
|
|
||||||
pVertices = nullptr;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
|
||||||
{
|
|
||||||
_indexCount = 6;
|
|
||||||
|
|
||||||
HRESULT hr = pD3DDevice->CreateIndexBuffer(_indexCount * sizeof(unsigned short),
|
|
||||||
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &_pIndexBuffer, nullptr);
|
|
||||||
CheckFailed(hr, 330);
|
|
||||||
|
|
||||||
unsigned short* pIndices = nullptr;
|
|
||||||
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
|
|
||||||
CheckFailed(hr, 340);
|
|
||||||
|
|
||||||
// triangle
|
|
||||||
//pIndices[0] = 0;
|
|
||||||
//pIndices[1] = 1;
|
|
||||||
//pIndices[2] = 2;
|
|
||||||
|
|
||||||
// quad
|
|
||||||
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
|
||||||
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
|
||||||
|
|
||||||
hr = _pIndexBuffer->Unlock();
|
|
||||||
CheckFailed(hr, 350);
|
|
||||||
|
|
||||||
pIndices = nullptr;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-9
@@ -1,25 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d9.h>
|
#include <d3d11.h>
|
||||||
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
class Mesh
|
class Mesh
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UINT init(IDirect3DDevice9* pD3DDevice);
|
UINT init(ID3D11Device* pD3DDevice);
|
||||||
void update(FLOAT dt);
|
void update(FLOAT dt);
|
||||||
void render(IDirect3DDevice9* pD3DDevice);
|
void render(ID3D11DeviceContext* pD3DDeviceContext);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
private:
|
const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; }
|
||||||
UINT createVertexBuffer(IDirect3DDevice9* pD3DDevice);
|
|
||||||
UINT createIndexBuffer(IDirect3DDevice9* pD3DDevice);
|
|
||||||
|
|
||||||
IDirect3DVertexBuffer9* _pVertexBuffer = nullptr; // dumps mesh vertices
|
private:
|
||||||
IDirect3DIndexBuffer9* _pIndexBuffer = nullptr; // dumps mesh indices
|
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 _vertexCount = 0; // amount of vertices
|
||||||
UINT _vertexStride = 0; // size of one vertex in bytes
|
UINT _vertexStride = 0; // size of one vertex in bytes
|
||||||
UINT _indexCount = 0; // amount of indices
|
UINT _indexCount = 0; // amount of indices
|
||||||
|
|
||||||
D3DMATRIX _worldMatrix = {};
|
XMFLOAT4X4 _worldMatrix = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -0,0 +1,4 @@
|
|||||||
|
float4 main() : SV_TARGET
|
||||||
|
{
|
||||||
|
return float4(1.0f, .0f, 1.0f, 1.0f);
|
||||||
|
}
|
||||||
+11
-23
@@ -1,36 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d9.h>
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_NORMAL
|
using namespace DirectX;
|
||||||
|
|
||||||
struct Vertex
|
struct Vertex
|
||||||
{
|
{
|
||||||
// position
|
XMFLOAT3 position;
|
||||||
float x;
|
XMFLOAT3 normal;
|
||||||
float y;
|
XMFLOAT2 uv;
|
||||||
float z;
|
XMFLOAT4 color;
|
||||||
|
|
||||||
// normal
|
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) {}
|
||||||
float nx;
|
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) {}
|
||||||
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(float x, float y, float z, UINT r, UINT g, UINT b) :
|
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) :
|
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) :
|
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.
@@ -0,0 +1,9 @@
|
|||||||
|
cbuffer MatrixBuffer
|
||||||
|
{
|
||||||
|
float4x4 WorldViewProjectionMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
float4 main( float4 pos : POSITION ) : SV_POSITION
|
||||||
|
{
|
||||||
|
return mul(pos, WorldViewProjectionMatrix);
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
|
|||||||
// 1. describe window class
|
// 1. describe window class
|
||||||
WNDCLASS wc = {};
|
WNDCLASS wc = {};
|
||||||
wc.hInstance = hInstance;
|
wc.hInstance = hInstance;
|
||||||
wc.lpszClassName = TEXT("D3D9"); // class name
|
wc.lpszClassName = TEXT("D3D11"); // class name
|
||||||
wc.lpfnWndProc = WindowProc; // callback function for windows to communicate with our application
|
wc.lpfnWndProc = WindowProc; // callback function for windows to communicate with our application
|
||||||
wc.hbrBackground = CreateSolidBrush(RGB(0, 255, 0));
|
wc.hbrBackground = CreateSolidBrush(RGB(0, 255, 0));
|
||||||
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
||||||
|
|||||||
+25
-31
@@ -32,15 +32,6 @@ int WINAPI WinMain(
|
|||||||
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
|
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
|
||||||
|
|
||||||
d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);
|
|
||||||
d3d.getDevice()->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
|
|
||||||
d3d.getDevice()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
|
|
||||||
d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
|
|
||||||
d3d.getDevice()->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
|
|
||||||
d3d.getDevice()->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
|
|
||||||
|
|
||||||
// 3. create a mesh
|
// 3. create a mesh
|
||||||
Mesh mesh = {};
|
Mesh mesh = {};
|
||||||
error = mesh.init(d3d.getDevice());
|
error = mesh.init(d3d.getDevice());
|
||||||
@@ -48,7 +39,7 @@ int WINAPI WinMain(
|
|||||||
|
|
||||||
// 4. create a camera
|
// 4. create a camera
|
||||||
Camera camera = {};
|
Camera camera = {};
|
||||||
error = camera.init(d3d.getDevice(), width, height);
|
error = camera.init(width, height);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
// 5. create time
|
// 5. create time
|
||||||
@@ -61,22 +52,22 @@ int WINAPI WinMain(
|
|||||||
error = material.init(d3d.getDevice(), TEXT("wall.jpg"));
|
error = material.init(d3d.getDevice(), TEXT("wall.jpg"));
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
// 7. create light source
|
//// 7. create light source
|
||||||
Light light = {};
|
//Light light = {};
|
||||||
|
|
||||||
D3DLIGHT9 lightData = {};
|
//D3DLIGHT9 lightData = {};
|
||||||
lightData.Type = D3DLIGHT_POINT;
|
//lightData.Type = D3DLIGHT_POINT;
|
||||||
lightData.Direction = { -1.0f, -1.0f, 1.0f };
|
//lightData.Direction = { -1.0f, -1.0f, 1.0f };
|
||||||
lightData.Position = { 1.0f, 1.0f, -1.0f };
|
//lightData.Position = { 1.0f, 1.0f, -1.0f };
|
||||||
lightData.Diffuse = { 0.7f, 0.7f, 0.7f };
|
//lightData.Diffuse = { 0.7f, 0.7f, 0.7f };
|
||||||
lightData.Specular = { 1.0f, 1.0f, 1.0f };
|
//lightData.Specular = { 1.0f, 1.0f, 1.0f };
|
||||||
lightData.Range = 100.0f;
|
//lightData.Range = 100.0f;
|
||||||
lightData.Attenuation0 = 1.0f;
|
//lightData.Attenuation0 = 1.0f;
|
||||||
lightData.Attenuation1 = 0.2f;
|
//lightData.Attenuation1 = 0.2f;
|
||||||
lightData.Attenuation2 = 0.1f;
|
//lightData.Attenuation2 = 0.1f;
|
||||||
|
|
||||||
error = light.init(lightData);
|
//error = light.init(lightData);
|
||||||
CheckError(error);
|
//CheckError(error);
|
||||||
|
|
||||||
// 8. run application
|
// 8. run application
|
||||||
while (window.run())
|
while (window.run())
|
||||||
@@ -85,23 +76,26 @@ int WINAPI WinMain(
|
|||||||
time.update();
|
time.update();
|
||||||
mesh.update(time.getDeltaTime());
|
mesh.update(time.getDeltaTime());
|
||||||
camera.update(time.getDeltaTime());
|
camera.update(time.getDeltaTime());
|
||||||
light.update(time.getDeltaTime());
|
//light.update(time.getDeltaTime());
|
||||||
|
|
||||||
|
material.setMatrices(
|
||||||
|
d3d.getDeviceContext(), mesh.getWorldMatrix(), camera.getViewMatrix(), camera.getProjectionMatrix());
|
||||||
|
|
||||||
// 8.2. render objects
|
// 8.2. render objects
|
||||||
d3d.beginScene();
|
d3d.beginScene();
|
||||||
|
|
||||||
light.render(d3d.getDevice());
|
//light.render(d3d.getDevice());
|
||||||
material.render(d3d.getDevice());
|
material.render(d3d.getDeviceContext());
|
||||||
mesh.render(d3d.getDevice());
|
mesh.render(d3d.getDeviceContext());
|
||||||
|
|
||||||
d3d.endScene();
|
d3d.endScene();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. tidy up
|
// 9. tidy up
|
||||||
light.deInit();
|
//light.deInit();
|
||||||
time.deInit();
|
|
||||||
camera.deInit();
|
|
||||||
material.deInit();
|
material.deInit();
|
||||||
|
time.deInit();
|
||||||
|
//camera.deInit();
|
||||||
mesh.deInit();
|
mesh.deInit();
|
||||||
d3d.deInit();
|
d3d.deInit();
|
||||||
window.deInit();
|
window.deInit();
|
||||||
|
|||||||
Reference in New Issue
Block a user