diff --git a/CMakeLists.txt b/CMakeLists.txt index da49d69..b781e12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,6 @@ project(WinApi) set(CMAKE_CXX_STANDARD 14) -add_executable(WinApi WIN32 main.cpp d3dx12.h DXWindow.cpp DXWindow.h Logger.cpp Logger.h utils.cpp utils.h Window.cpp Window.h Timer.cpp Timer.h IDrawable.h Geometry.h EventHandler.cpp EventHandler.h Vertex.cpp Vertex.h) +add_executable(WinApi WIN32 main.cpp d3dx12.h DXWindow.cpp DXWindow.h Logger.cpp Logger.h utils.cpp utils.h Window.cpp Window.h Timer.cpp Timer.h IDrawable.h Geometry.h EventHandler.cpp EventHandler.h Vertex.cpp Vertex.h Cam.cpp Cam.h Geometry.cpp const_bufs.h Entity.cpp Entity.h IHasDefault.h) target_link_libraries(WinApi d3d12.lib dxgi.lib dxguid.lib d3dcompiler.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib) \ No newline at end of file diff --git a/Cam.cpp b/Cam.cpp new file mode 100644 index 0000000..211cba5 --- /dev/null +++ b/Cam.cpp @@ -0,0 +1,95 @@ +// +// Created by Epagris on 2020. 03. 16.. +// + +#include "Cam.h" +#include "utils.h" + +eg3d::Cam::Cam() { + +} + +void eg3d::Cam::loadDefaults() { + mProjParams.loadDefaults(); + mViewParams.loadDefaults(); +} + +const XMFLOAT4X4 &eg3d::Cam::getViewMatrix() const { + return mMView; +} + +const XMFLOAT4X4 &eg3d::Cam::getProjMatrix() const { + return mMProj; +} + +void eg3d::Cam::constructViewMatrix() { + XMMATRIX r = XMMatrixRotationX(mViewParams.pitch) * XMMatrixRotationY(mViewParams.yaw); // forgatások (yaw, pitch) + XMMATRIX t = XMMatrixTranslation(mViewParams.position.x, mViewParams.position.y, mViewParams.position.z); // eltolás + XMStoreFloat4x4(&mMView, MT(t*r)); // view mátrix letárolása + + XMVECTOR dir = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f); // (0,0,-1) irányba néz alapból a kamera + XMStoreFloat3(&mVViewDirection, XMVector3Transform(dir, r)); // forgatott vektor kiszámítása és letárolása +} + +void eg3d::Cam::constructProjMatrix() { + XMMATRIX p = XMMatrixPerspectiveFovLH(mProjParams.FOV, mProjParams.AspectRatio, mProjParams.NearPlane, mProjParams.FarPlane); + XMStoreFloat4x4(&mMProj, MT(p)); +} + +void eg3d::Cam::setProjParams(const eg3d::ProjParams &pp) { + mProjParams = pp; + constructProjMatrix(); // projekciós mátrix újragenerálása +} + +eg3d::ProjParams eg3d::Cam::getProjParams() const { + return mProjParams; +} + +void eg3d::Cam::setViewParams(const eg3d::ViewParams &vp) { + mViewParams = vp; + constructViewMatrix(); +} + +eg3d::ViewParams eg3d::Cam::getViewParams() const { + return mViewParams; +} + +void eg3d::Cam::advance(float ds) { + XMVECTOR s = XMLoadFloat3(&mViewParams.position); + XMVECTOR dir = XMLoadFloat3(&mVViewDirection); + s = XMVectorAdd(s, XMVectorScale(dir, ds)); + XMStoreFloat3(&mViewParams.position, s); + + constructViewMatrix(); +} + +void eg3d::Cam::yaw(float dphi) { + mViewParams.yaw += dphi; + constructViewMatrix(); +} + +void eg3d::Cam::pitch(float drho) { + mViewParams.pitch += drho; + constructViewMatrix(); +} + +// ----------------------------- + +eg3d::ProjParams::ProjParams() { + loadDefaults(); +} + +void eg3d::ProjParams::loadDefaults() { + FOV = XM_PIDIV2; // 90 fokos látószög + AspectRatio = 1.0f; // 1:1-es képarány + NearPlane = 1.0f; // közeli sík + FarPlane = 10.0f; +} + +eg3d::ViewParams::ViewParams() {} + +void eg3d::ViewParams::loadDefaults() { + position.x = position.y = position.z = 0.0f; // (0,0,0), origó + yaw = 0.0f; // nincs forgatás + pitch = 0.0f; // nincs billentés +} diff --git a/Cam.h b/Cam.h new file mode 100644 index 0000000..8161473 --- /dev/null +++ b/Cam.h @@ -0,0 +1,66 @@ +// +// Created by Epagris on 2020. 03. 16.. +// + +#ifndef CAM +#define CAM + +#include +#include "IHasDefault.h" + +using namespace DirectX; + +namespace eg3d { + + // vetítés tulajdonságai + struct ProjParams : public IHasDefault { + float FOV; // látószög + float AspectRatio; // képarány + float NearPlane; // közeli sík + float FarPlane; // távoli sík + + ProjParams(); // konstr. + void loadDefaults(); // alapértelmezett paraméterek betöltése (FOV: 90 fok, AR: 1:1, N: 1.0, F: 10.0) + }; + + // nézet tulajdonságai + struct ViewParams : public IHasDefault { + XMFLOAT3 position; // nézet pozíciója + float yaw; // forgatás + float pitch; // billentés + + ViewParams(); // konstr. + void loadDefaults() override; + }; + + class Cam : IHasDefault { + private: + ProjParams mProjParams; // vetítés paraméterei + ViewParams mViewParams; // nézet paraméterei + + XMFLOAT3 mVViewDirection; // nézet iránya + + XMFLOAT4X4 mMView, mMProj; // view és proj mátrixok + void constructViewMatrix(); // view mátrix összeállítása + void constructProjMatrix(); // projekciós mátrix összeállítása + + void loadDefaults() override; + public: + Cam(); + const XMFLOAT4X4& getViewMatrix() const; // view mátrix elkérése + const XMFLOAT4X4& getProjMatrix() const; // proj mátrix elkérése + + void setProjParams(const ProjParams& pp); // projekciós paraméterek beállítása + ProjParams getProjParams() const; // vetítési paraméterek elkérése + void setViewParams(const ViewParams& vp); // nézet paramétereinek beállítása + ViewParams getViewParams() const; // nézetparaméterek elkérése + + void advance(float ds); // kamera mozgatása a nézet irányába + void yaw(float dphi); // kamera forgatása + void pitch(float drho); // kamera billentése + }; + +} + + +#endif //CAM diff --git a/DXWindow.cpp b/DXWindow.cpp index e9bd8ca..042e45b 100644 --- a/DXWindow.cpp +++ b/DXWindow.cpp @@ -376,24 +376,15 @@ namespace eg3d { void DXWindow::updateConstantBuffers() { - // transzformáció frissítése - //DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, DirectX::XMMatrixScaling(2.0, 1.0, 1.0)); + DirectX::XMStoreFloat4x4(&constantBuffer.transformMatrix, XMMatrixRotationX(-XM_PIDIV2)); - 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))); - + // TODO vetítés mátrixának kicserélése float viewHeight = 1.5; DirectX::XMStoreFloat4x4(&constantBuffer.projMatrix, DirectX::XMMatrixTranspose(XMMatrixPerspectiveLH(viewHeight * getAspectRatio(), viewHeight, 0.5, 1000))); - + + // TODO nézet mátrixának átadása + bufferMapCopy(cbResource, static_cast(&constantBuffer), sizeof(constantBuffer)); } @@ -463,9 +454,10 @@ namespace eg3d { } } - void DXWindow::moveCam(float deltaX) - { - camPosition += deltaX; - } + void DXWindow::setCam(Cam *pCam) { + this->pCam = pCam; + } + + } diff --git a/DXWindow.h b/DXWindow.h index f2e7f23..d2a35cd 100644 --- a/DXWindow.h +++ b/DXWindow.h @@ -17,14 +17,9 @@ #include #include "Vertex.h" +#include "Cam.h" -// konstansbuffer struktúrája -struct ConstantBuffer -{ - DirectX::XMFLOAT4X4 transformMatrix; - DirectX::XMFLOAT4X4 viewMatrix; // nézet-mátrix - DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix -}; +#include "const_bufs.h" namespace eg3d { @@ -88,9 +83,9 @@ namespace eg3d { // TODO konstansbuffer példány és DX-objektum ComPtr cbResource; // konstansbuffer DX-objektuma ConstantBuffer constantBuffer; // konstansbuffer - // TODO TRANSZFORMÁCIÓS TULAJDONSÁGOK - float rotAngle; // forgatás szöge - float camPosition; // kamera pozíciója + + // virtuális kamera + Cam * pCam; void initDrawObjects(); // kirajzolandó objektumok inicializálása @@ -125,12 +120,15 @@ namespace eg3d { public: DXWindow(); virtual ~DXWindow(); - + + // TODO paraméter átírása void Draw(const DrawablePool &drawables); ComPtr getDevice() const; // device elkérése - void moveCam(float deltaX); // kamera mozgatása + void setCam(Cam * pCam); // kamera beállítása + + }; } diff --git a/Entity.cpp b/Entity.cpp new file mode 100644 index 0000000..e173705 --- /dev/null +++ b/Entity.cpp @@ -0,0 +1,37 @@ +// +// Created by Epagris on 2020. 03. 19.. +// + +#include "Entity.h" + +eg3d::Entity::Entity(const ComPtr &device) : Geometry(device) { + loadDefaults(); +} + +void eg3d::Entity::loadDefaults() { + XMStoreFloat4x4(&mWorldMatrix, XMMatrixIdentity()); // egységmátrix betöltése a világ-mátrixba +} + +void eg3d::Entity::setEntitySRT(const eg3d::SRTProps &srtProps) { + mSRTProps = srtProps; +} + +eg3d::SRTProps eg3d::Entity::getEntitySRT() const { + return mSRTProps; +} + +void eg3d::Entity::constructWorldMatrix() { + // TODO (Cam alapján) +} + +// ---------------------- + +eg3d::SRTProps::SRTProps() { + loadDefaults(); +} + +void eg3d::SRTProps::loadDefaults() { + scaling = { 1.0f, 1.0f, 1.0f }; + rotation = { 0.0f, 0.0f, 0.0f }; + translation = {0.0f, 0.0f, 0.0f }; +} diff --git a/Entity.h b/Entity.h new file mode 100644 index 0000000..fbeb4f6 --- /dev/null +++ b/Entity.h @@ -0,0 +1,41 @@ +// +// Created by Epagris on 2020. 03. 19.. +// + +#ifndef ENTITY +#define ENTITY + +#include "Geometry.h" +#include "IHasDefault.h" + +namespace eg3d { + + // nagyítás, forgatás, eltolás + struct SRTProps : public IHasDefault { + XMFLOAT3 scaling; // nagyítás + XMFLOAT3 rotation; // forgatás + XMFLOAT3 translation; // pozíció + + SRTProps(); // konstr. + void loadDefaults() override; + }; + + class Entity : public Geometry, public IHasDefault { + private: + DirectX::XMFLOAT4X4 mWorldMatrix; // világ-mátrix + SRTProps mSRTProps; // STR-tulajdonságok + + void constructWorldMatrix(); // világ-mátrix újragenerálása + public: + Entity(const ComPtr &device); + + void loadDefaults() override; + + void setEntitySRT(const SRTProps& srtProps); // SRT-tulajdonságok beállítása + SRTProps getEntitySRT() const; // SRT-tualjdonságok elkérése + }; + +} + + +#endif //ENTITY diff --git a/EventHandler.cpp b/EventHandler.cpp index 918f162..17961d6 100644 --- a/EventHandler.cpp +++ b/EventHandler.cpp @@ -4,7 +4,7 @@ #include "EventHandler.h" #include "Logger.h" - +#include eg3d::EventHandler::EventHandler() { init(); } @@ -12,6 +12,9 @@ eg3d::EventHandler::EventHandler() { void eg3d::EventHandler::init() { mCamVelocity = 0.0f; // kamera sebességének inicializációja + mMouseLocked = false; // nincs befogva az egér + + } float eg3d::EventHandler::getCamVelocity() const @@ -25,33 +28,57 @@ int eg3d::EventHandler::processEvent(HWND hwnd, UINT message, WPARAM wParam, LPA // 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 + UINT rawDataLength = 64; + BYTE pRawData[64]; + switch (message) { + + case WM_INPUT: + { + GetRawInputData((HRAWINPUT)lParam, RID_INPUT, pRawData, &rawDataLength, sizeof(RAWINPUTHEADER)); + + RAWINPUT * pRawInput = (RAWINPUT *)pRawData; + + if (pRawInput->header.dwType == RIM_TYPEMOUSE) + { + int displacementX = pRawInput->data.mouse.lLastX; + int displacementY = pRawInput->data.mouse.lLastY; + + LOG(std::string("(") + std::to_string(displacementX) + ", " + std::to_string(displacementY) + ")"); + } + } + break; + case WM_KEYDOWN: { switch (wParam) { - case 'A': + case 'W': pThis_EH->mCamVelocity = -cCAM_VELOCITY; break; - case 'D': + case 'S': pThis_EH->mCamVelocity = cCAM_VELOCITY; break; + case 'L': + pThis_EH->mMouseLocked = !pThis_EH->mMouseLocked; + LOG(std::string("Mouse ") + (pThis_EH->mMouseLocked ? "" : "un") + "locked"); + break; } } break; - + case WM_KEYUP: { - if (wParam == 'A' || wParam == 'D') + if (wParam == 'W' || wParam == 'S') { pThis_EH->mCamVelocity = 0.0f; } } break; - + default: return 1; } diff --git a/EventHandler.h b/EventHandler.h index 9c9561a..ff9e640 100644 --- a/EventHandler.h +++ b/EventHandler.h @@ -7,8 +7,9 @@ namespace eg3d { class EventHandler { private: float mCamVelocity; // kamera mozgási sebessége - - void init(); // osztály inicializálása + bool mMouseLocked; // lockolvan van az egér? + + void init(); // osztály inicializálása public: static constexpr float cCAM_VELOCITY = 0.05; diff --git a/Geometry.cpp b/Geometry.cpp new file mode 100644 index 0000000..50aa79e --- /dev/null +++ b/Geometry.cpp @@ -0,0 +1,145 @@ +// +// Created by Epagris on 2020. 03. 16.. +// + +#include "Geometry.h" + +namespace eg3d { + + Geometry::Geometry(ComPtr device) : mDevice(device) { + // topológia beállítása + setTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + } + + Geometry::Geometry(const Geometry &other) : mDevice(other.mDevice) { + setTopology(other.getTopology()); + setVertices(other.getVertices()); + setIndices(other.getIndices()); + } + + void Geometry::setVertices(const std::vector &vertices) { + mVertices = vertices; + + size_t bufferSize = mVertices.size() * sizeof(Vertex); // buffer mérete + + mVertexBuffer.Reset(); // buffer törlése + mVertexBuffer = createBuffer_UH(mDevice, bufferSize); // buffer létrehozása + bufferMapCopy(mVertexBuffer, (void *) mVertices.data(), bufferSize); // buffer feltöltése + + // vertex buffer view beállítása + mVertexBufferView.BufferLocation = mVertexBuffer->GetGPUVirtualAddress(); + mVertexBufferView.StrideInBytes = sizeof(Vertex); + mVertexBufferView.SizeInBytes = bufferSize; + } + + std::vector Geometry::getVertices() const { + return mVertices; + } + + void Geometry::setIndices(const std::vector &indices) { + mIndices = indices; + + size_t bufferSize = mIndices.size() * sizeof(uint32_t); // buffer mérete + + mIndexBuffer.Reset(); // buffer törlése + mIndexBuffer = createBuffer_UH(mDevice, bufferSize); // buffer létrehozása + bufferMapCopy(mIndexBuffer, (void *) mIndices.data(), bufferSize); // buffer feltöltése + + // index buffer view beállítása + mIndexBufferView.Format = /*(sizeof(IT) == 2) ? DXGI_FORMAT_R16_UINT :*/ DXGI_FORMAT_R32_UINT; + mIndexBufferView.BufferLocation = mIndexBuffer->GetGPUVirtualAddress(); + mIndexBufferView.SizeInBytes = bufferSize; + } + + std::vector Geometry::getIndices() const { + return mIndices; + } + + void Geometry::setTopology(D3D_PRIMITIVE_TOPOLOGY topology) { + mTopology = topology; + } + + D3D_PRIMITIVE_TOPOLOGY Geometry::getTopology() const { + return mTopology; + } + + void Geometry::setupInputAssembler(ComPtr commandList) + const { + commandList->IASetVertexBuffers(0, 1, &mVertexBufferView); + commandList->IASetIndexBuffer(&mIndexBufferView); + commandList->IASetPrimitiveTopology(mTopology); + } + + void Geometry::draw(ComPtr commandList) const { + setupInputAssembler(commandList); // input assembler beállítása + commandList->DrawIndexedInstanced(mIndices.size(), 1, 0, 0, 0); // rajzolás + } + + void Geometry::merge(const Geometry &other) { + std::vector vertices = mVertices; + vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end()); + + 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; i < indices.size(); i++) { + indices[i] += offset; + } + + // bufferek újrafoglalása a videokártyán + setVertices(vertices); + setIndices(indices); + } + + Geometry &Geometry::operator+=(const Geometry &other) { + merge(other); + + return *this; + } + + void Geometry::loadBinarySTL(const std::string &fileName) { + auto inFile = std::ifstream(fileName, std::ios::in | std::ios::binary); // bináris fájl megnyitása olvasásra + + if (!inFile.is_open()) { // megnézzük, hogy sikerült-e megnyitni a fájlt + LOG("Failed to open file: '" + fileName + "'! STL geometry loading failed!"); + return; + } + + // vertexek és indexek + std::vector vertices; + std::vector indices; + + uint32_t triN; // háromszögek száma + + 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(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; + indices.push_back(firstIndex); + indices.push_back(firstIndex + 1); + indices.push_back(firstIndex + 2); + } + + inFile.close(); + + setVertices(vertices); + setIndices(indices); + + LOG("Successful geometry loading from file '" + fileName + "' of " + std::to_string(triN) + " triangles."); + } + +} diff --git a/Geometry.h b/Geometry.h index 412d078..8b46194 100644 --- a/Geometry.h +++ b/Geometry.h @@ -37,162 +37,21 @@ namespace eg3d { D3D12_VERTEX_BUFFER_VIEW mVertexBufferView; D3D12_INDEX_BUFFER_VIEW mIndexBufferView; public: - explicit Geometry(ComPtr device) : mDevice(device) { - // topológia beállítása - setTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - } - - // másolókonstruktor - Geometry(const Geometry& other) : mDevice(other.mDevice) { - setTopology(other.getTopology()); - setVertices(other.getVertices()); - setIndices(other.getIndices()); - } - - // vertexek beállítása - void setVertices(const std::vector &vertices) { - mVertices = vertices; - - size_t bufferSize = mVertices.size() * sizeof(Vertex); // buffer mérete - - mVertexBuffer.Reset(); // buffer törlése - mVertexBuffer = createBuffer_UH(mDevice, bufferSize); // buffer létrehozása - bufferMapCopy(mVertexBuffer, (void *) mVertices.data(), bufferSize); // buffer feltöltése - - // vertex buffer view beállítása - mVertexBufferView.BufferLocation = mVertexBuffer->GetGPUVirtualAddress(); - mVertexBufferView.StrideInBytes = sizeof(Vertex); - mVertexBufferView.SizeInBytes = bufferSize; - } - - // vertexek lekérése - std::vector getVertices() const { - return mVertices; - } - - // indexek beállítása - void setIndices(const std::vector &indices) { - mIndices = indices; - - size_t bufferSize = mIndices.size() * sizeof(uint32_t); // buffer mérete - - mIndexBuffer.Reset(); // buffer törlése - mIndexBuffer = createBuffer_UH(mDevice, bufferSize); // buffer létrehozása - bufferMapCopy(mIndexBuffer, (void *) mIndices.data(), bufferSize); // buffer feltöltése - - // index buffer view beállítása - mIndexBufferView.Format = /*(sizeof(IT) == 2) ? DXGI_FORMAT_R16_UINT :*/ DXGI_FORMAT_R32_UINT; - mIndexBufferView.BufferLocation = mIndexBuffer->GetGPUVirtualAddress(); - mIndexBufferView.SizeInBytes = bufferSize; - } - - // indexek lekérése - std::vector getIndices() const { - return mIndices; - } - - // topológia beállítása - void setTopology(D3D_PRIMITIVE_TOPOLOGY topology) { - mTopology = topology; - } - - // topológia beállítása - D3D_PRIMITIVE_TOPOLOGY getTopology() const { - return mTopology; - } - - void loadFromFile(const std::string& fileName) { - // TODO Epagrisnak: Assimpot le kell fordítani mindenkinek! - } - - // input assembler beállítása - void setupInputAssembler(ComPtr commandList) const { - commandList->IASetVertexBuffers(0, 1, &mVertexBufferView); - commandList->IASetIndexBuffer(&mIndexBufferView); - commandList->IASetPrimitiveTopology(mTopology); - } - - // kirajzolás - void draw(ComPtr commandList) const override { - setupInputAssembler(commandList); // input assembler beállítása - commandList->DrawIndexedInstanced(mIndices.size(), 1, 0, 0, 0); // rajzolás - } - - // két geometria egyesítése - void merge(const Geometry& other) - { - std::vector vertices = mVertices; - vertices.insert(vertices.end(), other.mVertices.begin(), other.mVertices.end()); - - 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; 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; - } - - // bináris STL fájl betöltése - void loadBinarySTL(const std::string& fileName) { - auto inFile = std::ifstream(fileName, std::ios::in | std::ios::binary); // bináris fájl megnyitása olvasásra - - if (!inFile.is_open()) { // megnézzük, hogy sikerült-e megnyitni a fájlt - LOG("Failed to open file: '" + fileName + "'! STL geometry loading failed!"); - return; - } - - // vertexek és indexek - std::vector vertices; - std::vector indices; - - uint32_t triN; // háromszögek száma - - 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(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; - indices.push_back(firstIndex); - indices.push_back(firstIndex + 1); - indices.push_back(firstIndex + 2); - } - - inFile.close(); - - setVertices(vertices); - setIndices(indices); - - LOG("Successful geometry loading from file '" + fileName + "' of " + std::to_string(triN) + " triangles."); - } + explicit Geometry(ComPtr device); // konstr. + Geometry(const Geometry& other); // másolókonstruktor + void setVertices(const std::vector &vertices); // vertexek beállítása + std::vector getVertices() const; // vertexek lekérése + void setIndices(const std::vector &indices); // indexek beállítása + std::vector getIndices() const; // indexek lekérése + void setTopology(D3D_PRIMITIVE_TOPOLOGY topology); // topológia beállítása + D3D_PRIMITIVE_TOPOLOGY getTopology() const; // topológia lekérése + void setupInputAssembler(ComPtr commandList) const; // input assembler beállítása + void draw(ComPtr commandList) const override; // kirajzolás + void merge(const Geometry& other); // két geometria egyesítése + Geometry& operator+=(const Geometry& other); // két geometria egyesítése + void loadBinarySTL(const std::string& fileName); // bináris STL fájl betöltése }; - // geometriatároló típusa using GeometryPool = std::vector>; } diff --git a/IHasDefault.h b/IHasDefault.h new file mode 100644 index 0000000..d7e1193 --- /dev/null +++ b/IHasDefault.h @@ -0,0 +1,17 @@ +// +// Created by Epagris on 2020. 03. 19.. +// + +#ifndef IHASDEFAULT +#define IHASDEFAULT + +namespace eg3d { + + class IHasDefault { + public: + virtual void loadDefaults() = 0; // alapértelmezett értékek betöltése + }; + +} + +#endif //IHASDEFAULT diff --git a/Window.cpp b/Window.cpp index d00b7d1..c822917 100644 --- a/Window.cpp +++ b/Window.cpp @@ -1,4 +1,5 @@ #include "Window.h" +#include namespace eg3d { @@ -10,6 +11,8 @@ namespace eg3d { { initializeWndClass(this->pWindowFunction); // window class inicializálása create(); // ablak létrehozása + registerRawInputDevice(); // RID-ok regisztrálása + this->show(show); } @@ -137,6 +140,17 @@ namespace eg3d { return 0; } + void Window::registerRawInputDevice() + { + RAWINPUTDEVICE device; + device.usUsagePage = HID_USAGE_PAGE_GENERIC; + device.usUsage = HID_USAGE_GENERIC_MOUSE; + device.dwFlags = RIDEV_INPUTSINK; + device.hwndTarget = mHWND; + + RegisterRawInputDevices(&device, 1, sizeof(RAWINPUTDEVICE)); + } + void Window::create() { // ablak létrehozása diff --git a/Window.h b/Window.h index ffe454d..ff5db58 100644 --- a/Window.h +++ b/Window.h @@ -21,6 +21,8 @@ namespace eg3d { // TODO Epagrisnak: ezt majd még átírom EventHandler* pmEventHandler; // eseményfeldolgozó osztály pointere + void registerRawInputDevice(); // ... + protected: void create(); // ablak létrehozása HWND mHWND; // ablak-handler diff --git a/const_bufs.h b/const_bufs.h new file mode 100644 index 0000000..b71a42c --- /dev/null +++ b/const_bufs.h @@ -0,0 +1,23 @@ +// +// Created by Epagris on 2020. 03. 19.. +// + +#ifndef CONST_BUFS +#define CONST_BUFS + +#include + +// konstansbuffer struktúrája +// TODO refactor! +struct ConstantBuffer +{ + DirectX::XMFLOAT4X4 transformMatrix; + DirectX::XMFLOAT4X4 viewMatrix; // nézet-mátrix + DirectX::XMFLOAT4X4 projMatrix; // vetítési mátrix +}; + +struct CB_Entity { + DirectX::XMFLOAT4X4 worldMatrix; // világ-mátrix (modell-mátrix) +}; + +#endif //CONST_BUFS diff --git a/main.cpp b/main.cpp index 1fd7837..a214380 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,9 @@ #include +// mátrixok transzponálása be +#define MT_ON + #include "utils.h" #include "Logger.h" @@ -54,7 +57,6 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszA auto pGeo2 = std::shared_ptr(new Geometry(win.getDevice())); pGeo2->loadBinarySTL("assets/utahteapot.stl"); - //drawables.push_back(pGeo2); (*pGeo) += (*pGeo2); DrawablePool drawables; // geometriák halmaza diff --git a/utils.h b/utils.h index e73f9bc..0a8b9c1 100644 --- a/utils.h +++ b/utils.h @@ -75,4 +75,12 @@ inline void bufferMapCopy(ComPtr bufferResource, void * data, si bufferResource->Map(0, nullptr, reinterpret_cast(&mappedData)); memcpy(mappedData, data, size); bufferResource->Unmap(0, nullptr); -} \ No newline at end of file +} + +// feltételes transzponálás (ha definiálva van, akkor automatikusan transzponál minden mátrixot) +#ifdef MT_ON +#define MT(m) XMMatrixTranspose(m) +#endif +#ifndef MT_ON +#define MT(m) (m) +#endif \ No newline at end of file