diff --git a/DXWindow.cpp b/DXWindow.cpp index 78e3cd5..e9bd8ca 100644 --- a/DXWindow.cpp +++ b/DXWindow.cpp @@ -11,6 +11,7 @@ namespace eg3d { initDrawObjects(); rotAngle = 0.0f; + camPosition = 0.0f; } @@ -378,18 +379,20 @@ namespace eg3d { // transzformáció frissítése //DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixScaling(2.0, 1.0, 1.0)); - rotAngle = rotAngle - 0.01; + rotAngle = rotAngle + 0.01f; if (rotAngle > XM_2PI) { rotAngle -= XM_2PI; + // rotAngle = rotAngle - XM_2PI; } + DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixTranspose(XMMatrixRotationZ(rotAngle) * XMMatrixRotationX(-XM_PIDIV2))); + + XMStoreFloat4x4(&constantBuffer.viewMatrix, XMMatrixTranspose(XMMatrixTranslation(0.0f, -4.0f, camPosition))); + float viewHeight = 1.5; DirectX::XMStoreFloat4x4(&constantBuffer.projMatrix, - //XMMatrixScaling(0.00001, 0.00001, 0.00001) * XMMatrixTranslation(0.0f, 0.0f, -100000.0f) * - DirectX::XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 10)); - - DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixRotationZ(rotAngle)); + DirectX::XMMatrixTranspose(XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 1000))); bufferMapCopy(cbResource, static_cast(&constantBuffer), sizeof(constantBuffer)); } @@ -426,7 +429,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; @@ -460,4 +463,9 @@ namespace eg3d { } } + void DXWindow::moveCam(float deltaX) + { + camPosition += deltaX; + } + } diff --git a/DXWindow.h b/DXWindow.h index 17affc1..f2e7f23 100644 --- a/DXWindow.h +++ b/DXWindow.h @@ -22,6 +22,7 @@ struct ConstantBuffer { DirectX::XMFLOAT4X4 transformMatrix; + DirectX::XMFLOAT4X4 viewMatrix; // nézet-mátrix DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix }; @@ -89,6 +90,7 @@ namespace eg3d { ConstantBuffer constantBuffer; // konstansbuffer // TODO TRANSZFORMÁCIÓS TULAJDONSÁGOK float rotAngle; // forgatás szöge + float camPosition; // kamera pozíciója void initDrawObjects(); // kirajzolandó objektumok inicializálása @@ -127,6 +129,8 @@ namespace eg3d { void Draw(const DrawablePool &drawables); ComPtr getDevice() const; // device elkérése + + void moveCam(float deltaX); // kamera mozgatása }; } diff --git a/EventHandler.cpp b/EventHandler.cpp index 5e478d7..918f162 100644 --- a/EventHandler.cpp +++ b/EventHandler.cpp @@ -3,15 +3,58 @@ // #include "EventHandler.h" +#include "Logger.h" eg3d::EventHandler::EventHandler() { - + init(); } +void eg3d::EventHandler::init() +{ + mCamVelocity = 0.0f; // kamera sebességének inicializációja +} + +float eg3d::EventHandler::getCamVelocity() const +{ + return mCamVelocity; +} + +// ----------------------------------- + int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - // TODO eseménykezelés megvalósítása + // TODO eseménykezelés megvalósítása + EventHandler * pThis_EH = static_cast((void *)GetWindowLongPtrA(hwnd, GWLP_USERDATA))->getEventHandler(); // ablakhoz rendelt eseménykezelő elkérése - // switch()..... + switch (message) + { + case WM_KEYDOWN: + { + switch (wParam) + { + case 'A': + pThis_EH->mCamVelocity = -cCAM_VELOCITY; + break; + case 'D': + pThis_EH->mCamVelocity = cCAM_VELOCITY; + break; + } - return 0; -} + } + break; + + case WM_KEYUP: + { + if (wParam == 'A' || wParam == 'D') + { + pThis_EH->mCamVelocity = 0.0f; + } + } + + break; + + default: + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/EventHandler.h b/EventHandler.h index 18d6bdc..9c9561a 100644 --- a/EventHandler.h +++ b/EventHandler.h @@ -5,9 +5,16 @@ namespace eg3d { class EventHandler { + private: + float mCamVelocity; // kamera mozgási sebessége + + void init(); // osztály inicializálása public: + static constexpr float cCAM_VELOCITY = 0.05; + EventHandler(); // konstr. int processEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); // esemény feldolgozása (0 -> feldolgozva, 1 -> nincs feldolgozva) + float getCamVelocity() const; // kamera sebességének lekérése }; } \ No newline at end of file diff --git a/Geometry.h b/Geometry.h index 400afd7..412d078 100644 --- a/Geometry.h +++ b/Geometry.h @@ -119,24 +119,30 @@ namespace eg3d { } // két geometria egyesítése - Geometry& operator+=(const Geometry& other) + void merge(const Geometry& other) { std::vector vertices = mVertices; - vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end()); + vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end()); - uint32_t offset = mIndices.size() - 1; + uint32_t offset = mIndices.size(); std::vector indices = mIndices; indices.insert(indices.end(), other.mIndices.begin(), other.mIndices.end()); - - // indexek megváltozatása offsettel - for (uint32_t i = offset + 1; i < indices.size(); i++) - { - indices[i] += offset; - } - // bufferek újrafoglalása a videokártyán + // indexek megváltozatása offsettel + for (uint32_t i = offset; i < indices.size(); i++) + { + indices[i] += offset; + } + + // bufferek újrafoglalása a videokártyán setVertices(vertices); setIndices(indices); + } + + // két geometria egyesítése + Geometry& operator+=(const Geometry& other) + { + merge(other); return *this; } @@ -159,14 +165,16 @@ namespace eg3d { inFile.seekg(80, std::ios::beg); // fejléc átugrása inFile.read((char *)&triN, sizeof(triN)); // háromszögek számának kiolvasása + LOG(std::to_string(sizeof(Vertex))); + // vertexek kiolvasása for (uint32_t i = 0; i < triN; i++) { STLTriangle triangle; - inFile.read((char *)&triangle, sizeof(triangle)); // egy háromszög betöltése - - vertices.push_back(triangle.v1); - vertices.push_back(triangle.v2); - vertices.push_back(triangle.v3); + inFile.read(reinterpret_cast(&triangle), 50); // egy háromszög betöltése + + vertices.emplace_back(triangle.v1); + vertices.emplace_back(triangle.v2); + vertices.emplace_back(triangle.v3); // indexek vektorának feltöltése uint32_t firstIndex = 3 * i; diff --git a/RUNTIME/assets/utahteapot.stl b/RUNTIME/assets/utahteapot.stl new file mode 100644 index 0000000..d225918 Binary files /dev/null and b/RUNTIME/assets/utahteapot.stl differ diff --git a/RUNTIME/shaders/basic_shader.hlsl b/RUNTIME/shaders/basic_shader.hlsl index 06c9fdc..c92cf0b 100644 --- a/RUNTIME/shaders/basic_shader.hlsl +++ b/RUNTIME/shaders/basic_shader.hlsl @@ -13,6 +13,7 @@ struct VSOutput // vertex shader kimeneti strukt cbuffer CBuf : register(b0) { float4x4 transformMatrix; + float4x4 viewMatrix; float4x4 projMatrix; }; @@ -20,7 +21,7 @@ VSOutput vs_main(VSInput input) { VSOutput output; // vertex shader kimenete - output.position_H = mul(projMatrix, mul(transformMatrix, float4(input.position_D, 1.0))); + output.position_H = mul(float4(input.position_D, 1.0), mul(mul(transformMatrix, viewMatrix), projMatrix)); output.color = float3(1.0, 1.0, 1.0); return output; diff --git a/Window.cpp b/Window.cpp index 79d8233..d00b7d1 100644 --- a/Window.cpp +++ b/Window.cpp @@ -126,9 +126,9 @@ namespace eg3d { break; default: // minden egyéb esemény kezelése if (pWindow != nullptr && pWindow->getEventHandler() != nullptr) { // ha meg van adva eseménykezelő - /*if (pWindow->getEventHandler()->processEvent(hwnd, message, wParam, lParam) != 0) { // ha nem lett lekezelve az esemény + if (pWindow->getEventHandler()->processEvent(hwnd, message, wParam, lParam) != 0) { // ha nem lett lekezelve az esemény return DefWindowProc(hwnd, message, wParam, lParam); // ...akkor rábízzuk a rendszerre - }*/ + } } return DefWindowProc(hwnd, message, wParam, lParam); // ...akkor rábízzuk a rendszerre diff --git a/main.cpp b/main.cpp index 3901dd7..1fd7837 100644 --- a/main.cpp +++ b/main.cpp @@ -10,7 +10,6 @@ #include "Timer.h" #include "Geometry.h" -#include "not_used/vertex_types.h" // FPS-mĂ©rĂ©s size_t gFrames = 0; @@ -31,6 +30,8 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA win.setTitle("Keretszöveg"); // keretszöveg beállĂ­tása // TODO esemĂ©nykezelĹ‘ lĂ©trehozása Ă©s ablakhoz rendelĂ©se + EventHandler eventHandler; + win.setEventHandler(&eventHandler); // FPS-mĂ©rĹ‘ callback-fĂĽggvĂ©ny regisztrálása TimerCallbackData timerCBData; @@ -44,20 +45,21 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA { 0.0f, 0.98f, 1.0f }, }; - std::vector indices = { 0, 2, 1 }; // indexek + std::vector indices = { 0, 1, 2 }; // indexek auto pGeo = std::shared_ptr(new Geometry(win.getDevice())); pGeo->setVertices(vertices); pGeo->setIndices(indices); auto pGeo2 = std::shared_ptr(new Geometry(win.getDevice())); - pGeo2->loadBinarySTL("assets/crate.stl"); + pGeo2->loadBinarySTL("assets/utahteapot.stl"); //drawables.push_back(pGeo2); - //(*pGeo) += (*pGeo2); + (*pGeo) += (*pGeo2); DrawablePool drawables; // geometriák halmaza drawables.push_back(pGeo); // pGeo geometria hozzáadása + //drawables.push_back(pGeo); // ------------------------------------ @@ -74,6 +76,7 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA { Sleep(15); + win.moveCam(eventHandler.getCamVelocity()); // kamera elmozdĂ­tása win.Draw(drawables); // kirajzolás gFrames++; // kĂ©pkockaszám lĂ©ptetĂ©se