From b1cba59ea837b8d6f35b4472bec23bbfbb86f6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Wiesner?= Date: Fri, 24 Apr 2020 17:46:23 +0200 Subject: [PATCH] =?UTF-8?q?grafika-lights=20kezd=C5=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DXWindow.cpp | 5 +-- Entity.cpp | 2 +- Geometry.cpp | 13 ++++++-- RUNTIME/shaders/basic_shader.hlsl | 4 ++- Vertex.cpp | 13 ++++++++ Vertex.h | 2 ++ main.cpp | 53 ++++++++++++++++++++++++++++--- 7 files changed, 82 insertions(+), 10 deletions(-) diff --git a/DXWindow.cpp b/DXWindow.cpp index 043dd24..28c5a33 100644 --- a/DXWindow.cpp +++ b/DXWindow.cpp @@ -141,7 +141,7 @@ namespace eg3d { LOG("Creating descriptor heaps!"); // ---------- RTV - D3D12_DESCRIPTOR_HEAP_DESC hD = {}; + D3D12_DESCRIPTOR_HEAP_DESC hD = {}; hD.NumDescriptors = cSwapChainBufferCount; hD.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; hD.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; @@ -363,6 +363,7 @@ namespace eg3d { { inputElements = { {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, + {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0} //{"COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0} // TODO további elemek létrehozása }; @@ -418,7 +419,7 @@ namespace eg3d { psoD.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT); //psoD.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; - psoD.RasterizerState.FillMode = D3D12_FILL_MODE_WIREFRAME; + //psoD.RasterizerState.FillMode = D3D12_FILL_MODE_WIREFRAME; psoD.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT); psoD.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT); psoD.SampleMask = UINT_MAX; diff --git a/Entity.cpp b/Entity.cpp index 45a022e..1c9cecd 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -16,7 +16,7 @@ eg3d::Entity::Entity(const Entity& other): Geometry(other.device) } void eg3d::Entity::loadDefaults() { - mConstBuf.color = { 1.0f, 1.0f, 1.0f, 1.0f }; + mConstBuf.color = { 1.0f, 1.0f, 1.0f, 1.0f }; XMStoreFloat4x4(&mConstBuf.worldMatrix, XMMatrixIdentity()); // egységmátrix betöltése a világ-mátrixba mSRTProps.loadDefaults(); diff --git a/Geometry.cpp b/Geometry.cpp index a9413b3..0bdeb41 100644 --- a/Geometry.cpp +++ b/Geometry.cpp @@ -141,11 +141,20 @@ namespace eg3d { STLTriangle triangle; inFile.read(reinterpret_cast(&triangle), 50); // egy háromszög betöltése - for (auto& vertex : triangle.vertices) + for (auto& stlVertex : triangle.vertices) { if (pIAD != nullptr) { - pIAD(vertex); + pIAD(stlVertex); } + + // koordináták másolása + Vertex vertex = stlVertex; + + // normálvektorok hozzárendelése TODO szebben! + vertex.nx = triangle.normal.x; + vertex.ny = triangle.normal.y; + vertex.nz = triangle.normal.z; + vertices.emplace_back(vertex); } diff --git a/RUNTIME/shaders/basic_shader.hlsl b/RUNTIME/shaders/basic_shader.hlsl index 88410b3..26f7f4f 100644 --- a/RUNTIME/shaders/basic_shader.hlsl +++ b/RUNTIME/shaders/basic_shader.hlsl @@ -1,7 +1,7 @@ struct VSInput // vertex shader bemeneti struktúra { float3 position_D : POSITION; - //float3 color : COLOR; + float3 normal : NORMAL; }; struct VSOutput // vertex shader kimeneti struktúra @@ -29,6 +29,8 @@ VSOutput vs_main(VSInput input) output.position_H = mul(float4(input.position_D, 1.0), mul(worldMatrix, mul(viewMatrix, projMatrix))); output.color = color; + // TODO fények + return output; } diff --git a/Vertex.cpp b/Vertex.cpp index 3f803c8..e2cbe7c 100644 --- a/Vertex.cpp +++ b/Vertex.cpp @@ -17,4 +17,17 @@ eg3d::Vertex::Vertex(std::initializer_list initList) { y = pParams[1]; z = pParams[2]; } +} + +eg3d::Vertex::Vertex (const Vertex& other) +{ + // koordináták másolása + x = other.x; + y = other.y; + z = other.z; + + // normálvektor másolása + nx = other.nx; + ny = other.ny; + nz = other.nz; } \ No newline at end of file diff --git a/Vertex.h b/Vertex.h index 41f06a1..2427057 100644 --- a/Vertex.h +++ b/Vertex.h @@ -13,11 +13,13 @@ namespace eg3d { struct Vertex { public: float x, y, z; // koordináták + float nx, ny, nz; // normálvektor koordinátái // float nx, ny, nz; // normálvektorok TODO Vertex(); // default konstruktor Vertex(const DirectX::XMFLOAT3 xmf3); // betöltés XMFLOAT3-ból Vertex(std::initializer_list initList); // konstruktor struktúraszerű inicializációval + Vertex(const Vertex& other); // másolókonstruktor }; } diff --git a/main.cpp b/main.cpp index 265825d..60aacb6 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,7 @@ #include "Timer.h" #include "Geometry.h" #include "Entity.h" +#include // FPS-mĂŠrĂŠs size_t gFrames = 0; @@ -31,6 +32,7 @@ using namespace eg3d; Cam gCam; // virtuĂĄlis kamera DrawablePool gDrawables; // geometriĂĄk halmaza +DrawablePool gTeapots; // ÁLLAPOTVÁLTOZÓK bool gMoveForward = false; @@ -83,9 +85,44 @@ void mouseCB(EventHandlerCBData * pCBData) pTeapot->loadBinarySTL("assets/utahteapot.stl", ImportAdapters::IAD_SwapYZ_RH); pTeapot->setEntitySRT(srtProps); - pTeapot->setColor(0.8f, 0.8f, 0.1f); + // szĂ­n random generĂĄlĂĄsa + std::random_device rndDevice; + std::uniform_real_distribution dist(0.0f, 1.0f); + + float r = dist(rndDevice); + float g = dist(rndDevice); + float b = dist(rndDevice); - gDrawables.push_back(pTeapot); + pTeapot->setColor(r, g, b); + + gTeapots.push_back(pTeapot); +} + +void randomizeColor() +{ + std::random_device rndDevice; + std::uniform_real_distribution dist(0.0f, 1.0f); + + for (auto teapot : gTeapots) + { + auto pTeapot = static_cast(teapot.get()); + + float r = dist(rndDevice); + float g = dist(rndDevice); + float b = dist(rndDevice); + + pTeapot->setColor(r, g, b); + } +} + +void lMouseCB(EventHandlerCBData * pCbData) +{ + randomizeColor(); +} + +void timedRandomizeColor(eg3d::Timer *pTimer, const double *pTrigTimeElapsed, void *ptr) +{ + randomizeColor(); } void moveForward_H(EventHandlerCBData * pCBData) @@ -141,6 +178,7 @@ void regEventHandlers(EventHandler * pEH) // kattintĂĄs pEH->regMouseCB(mouseCB, nullptr, { ET_RMouseDown }); + pEH->regMouseCB(lMouseCB, nullptr, { ET_LMouseDown }); // mozgĂĄs pEH->regKeyCB('W', moveForward_H, nullptr, { ET_KeyDown, ET_KeyUp }); // előre @@ -178,7 +216,10 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA TimerCallbackData timerCBData; timerCBData.callBackFn = CB_FPSaux; timerCBData.ptr = nullptr; - gTmr1s.reg(timerCBData); + gTmr1s.reg(timerCBData); + + timerCBData.callBackFn = timedRandomizeColor; + gTmr1s.reg(timerCBData); std::vector vertices = { // vertexek { -0.5f, -0.5f, 1.0f }, @@ -250,7 +291,11 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA std::string winTitle = "Open world game - P: (" + std::to_string(position.x) + "; " + std::to_string(position.y) + "; " + std::to_string(position.z) + "), " + "yaw: " + std::to_string(yaw) + "°, pitch: " +std::to_string(pitch) + "° ---- " + gFPS_FT_msg; win.setTitle(winTitle); - win.Draw(gDrawables); // kirajzolĂĄs + + DrawablePool mergedDrawables = gDrawables; + mergedDrawables.insert(mergedDrawables.end(), gTeapots.begin(), gTeapots.end()); + + win.Draw(mergedDrawables); // kirajzolĂĄs gFrames++; // kĂŠpkockaszĂĄm lĂŠptetĂŠse }