diff --git a/GraphicsProgramming/.gitignore b/GraphicsProgramming/.gitignore index f3b4ba7..613b4bf 100644 --- a/GraphicsProgramming/.gitignore +++ b/GraphicsProgramming/.gitignore @@ -418,4 +418,7 @@ FodyWeavers.xsd # JetBrains Rider .idea/ -*.sln.iml \ No newline at end of file +*.sln.iml + +# vcpkg +vcpkg_installed/ \ No newline at end of file diff --git a/GraphicsProgramming/D3D11/Camera.cpp b/GraphicsProgramming/D3D11/Camera.cpp index d871ea6..9ae144e 100644 --- a/GraphicsProgramming/D3D11/Camera.cpp +++ b/GraphicsProgramming/D3D11/Camera.cpp @@ -3,7 +3,7 @@ UINT Camera::init(UINT width, UINT height) { // view matrix - XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); + XMVECTOR position = XMVectorSet(0.0f, 100.0f, -500.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); @@ -14,7 +14,7 @@ UINT Camera::init(UINT width, UINT height) float fov = XMConvertToRadians(60.0f); float aspectRatio = static_cast(width) / static_cast(height); float nearClippingPlane = 0.3f; - float farClippingPlane = 10.0f; + float farClippingPlane = 1000.0f; XMMATRIX projection = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane); XMStoreFloat4x4(&_projectionMatrix, projection); diff --git a/GraphicsProgramming/D3D11/ColorPixelShader.cso b/GraphicsProgramming/D3D11/ColorPixelShader.cso index d2ca164..a239954 100644 Binary files a/GraphicsProgramming/D3D11/ColorPixelShader.cso and b/GraphicsProgramming/D3D11/ColorPixelShader.cso differ diff --git a/GraphicsProgramming/D3D11/ColorVertexShader.cso b/GraphicsProgramming/D3D11/ColorVertexShader.cso index 08213bd..3122193 100644 Binary files a/GraphicsProgramming/D3D11/ColorVertexShader.cso and b/GraphicsProgramming/D3D11/ColorVertexShader.cso differ diff --git a/GraphicsProgramming/D3D11/D3D11.vcxproj b/GraphicsProgramming/D3D11/D3D11.vcxproj index b856a39..845340a 100644 --- a/GraphicsProgramming/D3D11/D3D11.vcxproj +++ b/GraphicsProgramming/D3D11/D3D11.vcxproj @@ -97,6 +97,9 @@ + + true + Level3 diff --git a/GraphicsProgramming/D3D11/Direct3D.cpp b/GraphicsProgramming/D3D11/Direct3D.cpp index d505023..ae50236 100644 --- a/GraphicsProgramming/D3D11/Direct3D.cpp +++ b/GraphicsProgramming/D3D11/Direct3D.cpp @@ -54,8 +54,39 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen) safeRelease(pBackBuffer); // 4. create depth stencil buffer + ID3D11Texture2D* pDepthStencilBuffer = nullptr; + D3D11_TEXTURE2D_DESC depthStencilBufferDesc = {}; + depthStencilBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; + depthStencilBufferDesc.Width = width; + depthStencilBufferDesc.Height = height; + depthStencilBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; + depthStencilBufferDesc.ArraySize = 1; + depthStencilBufferDesc.Usage = D3D11_USAGE_DEFAULT; + depthStencilBufferDesc.SampleDesc.Count = 1; + + hr = _pD3DDevice->CreateTexture2D(&depthStencilBufferDesc, nullptr, &pDepthStencilBuffer); + CheckFailed(hr, 225); + + hr = _pD3DDevice->CreateDepthStencilView(pDepthStencilBuffer, nullptr, &_pD3DDepthStencilView); + CheckFailed(hr, 230); + + safeRelease(pDepthStencilBuffer); + + D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {}; + depthStencilDesc.DepthEnable = true; + depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; + depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + + hr = _pD3DDevice->CreateDepthStencilState(&depthStencilDesc, &_pD3DDepthStencilState); + CheckFailed(hr, 240); // 5. create rasterizer state + D3D11_RASTERIZER_DESC rasterizerDesc = {}; + rasterizerDesc.FillMode = D3D11_FILL_SOLID; + rasterizerDesc.CullMode = D3D11_CULL_BACK; + + hr = _pD3DDevice->CreateRasterizerState(&rasterizerDesc, &_pD3DRasterizerState); + CheckFailed(hr, 250); // 6. describe viewport D3D11_VIEWPORT viewport = {}; @@ -66,7 +97,9 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen) // 7. setup render pipeline (device context) _pD3DDeviceContext->RSSetViewports(1, &viewport); - _pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, nullptr); // TODO: create depth stencil view and set it for output merger stage + _pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, _pD3DDepthStencilView); + _pD3DDeviceContext->OMSetDepthStencilState(_pD3DDepthStencilState, 0xFFFFFFFF); + _pD3DDeviceContext->RSSetState(_pD3DRasterizerState); return 0; } @@ -79,6 +112,7 @@ void Direct3D::beginScene() // 8. clear the buffers _pD3DDeviceContext->ClearRenderTargetView(_pD3DRenderTargetView, color); + _pD3DDeviceContext->ClearDepthStencilView(_pD3DDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0xFFFFFFFF); } void Direct3D::endScene() diff --git a/GraphicsProgramming/D3D11/Direct3D.h b/GraphicsProgramming/D3D11/Direct3D.h index 3ab5719..3993248 100644 --- a/GraphicsProgramming/D3D11/Direct3D.h +++ b/GraphicsProgramming/D3D11/Direct3D.h @@ -19,5 +19,8 @@ private: 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 + ID3D11DepthStencilView* _pD3DDepthStencilView = nullptr; // saves depth & stencil value of every pixel + ID3D11DepthStencilState* _pD3DDepthStencilState = nullptr; // set the current settings for depth-stenciling + ID3D11RasterizerState* _pD3DRasterizerState = nullptr; // set additional settings for the render pipeline }; diff --git a/GraphicsProgramming/D3D11/Mesh.cpp b/GraphicsProgramming/D3D11/Mesh.cpp index ea81653..597caff 100644 --- a/GraphicsProgramming/D3D11/Mesh.cpp +++ b/GraphicsProgramming/D3D11/Mesh.cpp @@ -2,16 +2,16 @@ #include "Utilities.h" #include "Vertex.h" +#include +#include +#include + +#include + UINT Mesh::init(ID3D11Device* pD3DDevice) { UINT error = 0; - error = createVertexBuffer(pD3DDevice); - CheckError(error); - - error = createIndexBuffer(pD3DDevice); - CheckError(error); - XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity()); return 0; @@ -22,11 +22,14 @@ void Mesh::update(FLOAT dt) static float x = 0.0f; static float y = 0.0f; static float z = 0.0f; + static float rotY = 0.0f; static float rotZ = 0.0f; + static float scale = 0.5f; + rotY += XM_PIDIV4 * dt; //rotZ += XM_PIDIV4 * dt; - float move = 10.0f * dt; + float move = 100.0f * dt; if (GetAsyncKeyState(VK_LEFT) & 0x8000 || GetAsyncKeyState('A') & 0x8000) x -= move; if (GetAsyncKeyState(VK_RIGHT) & 0x8000 || GetAsyncKeyState('D') & 0x8000) x += move; @@ -36,10 +39,10 @@ void Mesh::update(FLOAT dt) if (GetAsyncKeyState(VK_ADD) & 0x8000 || GetAsyncKeyState('E') & 0x8000) z += move; XMMATRIX translation = XMMatrixTranslation(x, y, z); - XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ); - XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f); + XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ); + XMMATRIX localScale = XMMatrixScaling(scale, scale, scale); - XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation); + XMStoreFloat4x4(&_worldMatrix, localScale * rotation * translation); } void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext) @@ -77,11 +80,94 @@ void Mesh::deInit() safeRelease(_pIndexBuffer); } -UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice) +UINT Mesh::loadMeshFromFile(ID3D11Device* pD3DDevice, string filename) { + // clear old buffers + safeRelease(_pVertexBuffer); + safeRelease(_pIndexBuffer); + + // assimp implementation + Assimp::Importer importer; + + const aiScene* pScene = importer.ReadFile( + filename, + aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_ConvertToLeftHanded | aiProcess_PreTransformVertices + ); + + if (pScene == nullptr || !pScene->HasMeshes()) return 350; + + const aiMesh* pMesh = pScene->mMeshes[0]; + + // import vertices + vector vertices; + + for (size_t v = 0; v < pMesh->mNumVertices; v++) + { + // position + const aiVector3D& position = pMesh->mVertices[v]; + + // uv + if (pMesh->HasTextureCoords(0)) + { + const aiVector3D& uv = pMesh->mTextureCoords[0][v]; + + vertices.emplace_back( + position.x, position.y, position.z, + uv.x, uv.y + ); + } + + // vertex color + if (pMesh->HasVertexColors(0)) + { + const aiColor4D& color = pMesh->mColors[0][v]; + + vertices.emplace_back( + position.x, position.y, position.z, + color.r, color.g, color.b + ); + } + + } + + // import indices + vector indices; + if (pMesh->mNumVertices > USHRT_MAX) return 360; + + for (size_t f = 0; f < pMesh->mNumFaces; f++) + { + const aiFace& face = pMesh->mFaces[f]; + + if (face.mNumIndices != 3) continue; + + indices.push_back(face.mIndices[0]); + indices.push_back(face.mIndices[1]); + indices.push_back(face.mIndices[2]); + } + + // update mesh properties + _vertexCount = vertices.size(); + _vertexStride = sizeof(Vertex); + _indexCount = indices.size(); + + UINT error = createVertexBuffer(pD3DDevice, vertices.data()); + CheckError(error); + + error = createIndexBuffer(pD3DDevice, indices.data()); + CheckError(error); + + return 0; +} + +UINT Mesh::loadDefaultMesh(ID3D11Device* pD3DDevice) +{ + // clear old buffers + safeRelease(_pVertexBuffer); + safeRelease(_pIndexBuffer); + // TODO: create a cube - // 1. define the structure (vertices) + // vertices Vertex vertices[] = { // triangle //Vertex(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f), // 0 @@ -97,9 +183,30 @@ UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice) _vertexCount = std::size(vertices); _vertexStride = sizeof(Vertex); + + // indices + USHORT indices[] = { + // triangles + //0, 1, 2 - // 2. create the buffer - // for Usage: https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage + // quad + 0, 1, 2, + 0, 2, 3 + }; + + _indexCount = std::size(indices); + + UINT error = createVertexBuffer(pD3DDevice, vertices); + CheckError(error); + + error = createIndexBuffer(pD3DDevice, indices); + CheckError(error); + + return 0; +} + +UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice, Vertex* vertices) +{ D3D11_BUFFER_DESC desc = {}; desc.ByteWidth = _vertexCount * _vertexStride; // size of the buffer desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // buffer type @@ -114,19 +221,8 @@ UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice) return 0; } -UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice) +UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice, USHORT* indices) { - USHORT indices[] = { - // triangles - //0, 1, 2 - - // quad - 0, 1, 2, - 0, 2, 3 - }; - - _indexCount = std::size(indices); - D3D11_BUFFER_DESC desc = {}; desc.ByteWidth = _indexCount * sizeof(USHORT); desc.BindFlags = D3D11_BIND_INDEX_BUFFER; diff --git a/GraphicsProgramming/D3D11/Mesh.h b/GraphicsProgramming/D3D11/Mesh.h index 6a33bcb..53f7f77 100644 --- a/GraphicsProgramming/D3D11/Mesh.h +++ b/GraphicsProgramming/D3D11/Mesh.h @@ -1,8 +1,12 @@ #pragma once #include #include +#include + +#include "Vertex.h" using namespace DirectX; +using namespace std; class Mesh { @@ -13,10 +17,12 @@ public: void deInit(); const XMFLOAT4X4& getWorldMatrix() const { return _worldMatrix; } + UINT loadMeshFromFile(ID3D11Device* pD3DDevice, string filename); + UINT loadDefaultMesh(ID3D11Device* pD3DDevice); private: - UINT createVertexBuffer(ID3D11Device* pD3DDevice); - UINT createIndexBuffer(ID3D11Device* pD3DDevice); + UINT createVertexBuffer(ID3D11Device* pD3DDevice, Vertex* vertices); + UINT createIndexBuffer(ID3D11Device* pD3DDevice, USHORT* indices); ID3D11Buffer* _pVertexBuffer = nullptr; // dumps mesh vertices ID3D11Buffer* _pIndexBuffer = nullptr; // dumps mesh indices diff --git a/GraphicsProgramming/D3D11/TexturePixelShader.cso b/GraphicsProgramming/D3D11/TexturePixelShader.cso index a2d3144..53ba712 100644 Binary files a/GraphicsProgramming/D3D11/TexturePixelShader.cso and b/GraphicsProgramming/D3D11/TexturePixelShader.cso differ diff --git a/GraphicsProgramming/D3D11/TexturePixelShader.hlsl b/GraphicsProgramming/D3D11/TexturePixelShader.hlsl index c43eeb2..8e9f88f 100644 --- a/GraphicsProgramming/D3D11/TexturePixelShader.hlsl +++ b/GraphicsProgramming/D3D11/TexturePixelShader.hlsl @@ -5,9 +5,10 @@ struct PixelInput { float4 position : SV_Position; float2 uv : TEXCOORD; + float4 color : COLOR; }; float4 main(PixelInput INPUT) : SV_TARGET { - return MainTexture.Sample(MainSampler, INPUT.uv); + return MainTexture.Sample(MainSampler, INPUT.uv) * INPUT.color; } \ No newline at end of file diff --git a/GraphicsProgramming/D3D11/TextureVertexShader.cso b/GraphicsProgramming/D3D11/TextureVertexShader.cso index 7eb5044..37136e9 100644 Binary files a/GraphicsProgramming/D3D11/TextureVertexShader.cso and b/GraphicsProgramming/D3D11/TextureVertexShader.cso differ diff --git a/GraphicsProgramming/D3D11/TextureVertexShader.hlsl b/GraphicsProgramming/D3D11/TextureVertexShader.hlsl index ef53e6a..2c2f1cb 100644 --- a/GraphicsProgramming/D3D11/TextureVertexShader.hlsl +++ b/GraphicsProgramming/D3D11/TextureVertexShader.hlsl @@ -9,12 +9,14 @@ struct VertexInput { float4 position : POSITION; float2 uv : TEXCOORD; + float4 color : COLOR; }; struct VertexOutput { float4 position : SV_Position; float2 uv : TEXCOORD; + float4 color : COLOR; }; VertexOutput main(VertexInput INPUT) @@ -24,6 +26,7 @@ VertexOutput main(VertexInput INPUT) OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix); OUTPUT.uv = INPUT.uv; //OUTPUT.uv = INPUT.uv * float2(1.1f, 1.1f) + float2(-0.05f, -0.05f); // uv * tiling + offset + OUTPUT.color = INPUT.color; return OUTPUT; } \ No newline at end of file diff --git a/GraphicsProgramming/D3D11/colormap.png b/GraphicsProgramming/D3D11/colormap.png new file mode 100644 index 0000000..c8ecd47 Binary files /dev/null and b/GraphicsProgramming/D3D11/colormap.png differ diff --git a/GraphicsProgramming/D3D11/main.cpp b/GraphicsProgramming/D3D11/main.cpp index df5ee38..d1f5883 100644 --- a/GraphicsProgramming/D3D11/main.cpp +++ b/GraphicsProgramming/D3D11/main.cpp @@ -36,6 +36,9 @@ int WINAPI WinMain( Mesh mesh = {}; error = mesh.init(d3d.getDevice()); CheckError(error); + //error = mesh.loadDefaultMesh(d3d.getDevice()); + error = mesh.loadMeshFromFile(d3d.getDevice(), "ship-pirate-large.fbx"); + CheckError(error); // 4. create a camera Camera camera = {}; @@ -49,7 +52,7 @@ int WINAPI WinMain( // 6. create a material Material material = {}; - error = material.init(d3d.getDevice(), TEXT("wall.jpg")); + error = material.init(d3d.getDevice(), TEXT("colormap.png")); CheckError(error); // 7. create light source diff --git a/GraphicsProgramming/D3D11/ship-pirate-large.fbx b/GraphicsProgramming/D3D11/ship-pirate-large.fbx new file mode 100644 index 0000000..23b00fc Binary files /dev/null and b/GraphicsProgramming/D3D11/ship-pirate-large.fbx differ diff --git a/GraphicsProgramming/vcpkg-configuration.json b/GraphicsProgramming/vcpkg-configuration.json new file mode 100644 index 0000000..affe441 --- /dev/null +++ b/GraphicsProgramming/vcpkg-configuration.json @@ -0,0 +1,14 @@ +{ + "default-registry": { + "kind": "git", + "baseline": "3af1d1e60af2b2abf55760538cd607829029b07a", + "repository": "https://github.com/microsoft/vcpkg" + }, + "registries": [ + { + "kind": "artifact", + "location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip", + "name": "microsoft" + } + ] +} diff --git a/GraphicsProgramming/vcpkg.json b/GraphicsProgramming/vcpkg.json new file mode 100644 index 0000000..57b13e9 --- /dev/null +++ b/GraphicsProgramming/vcpkg.json @@ -0,0 +1,5 @@ +{ + "dependencies": [ + "assimp" + ] +}