diff --git a/src/client/GuiApplication.h b/src/client/GuiApplication.h index 2d9cbf4..728a736 100644 --- a/src/client/GuiApplication.h +++ b/src/client/GuiApplication.h @@ -19,12 +19,12 @@ public: AbstractApp* getMainApplication() const override; - void run(); - AbstractUIInterface* getUiInterface() const override; void setUiInterfaceBackend(UiInterfaceFactory::Backend backend); + void run(); + protected: virtual void initializeViews(); diff --git a/src/console/MainApplication.cpp b/src/console/MainApplication.cpp index dc61a19..6dde651 100644 --- a/src/console/MainApplication.cpp +++ b/src/console/MainApplication.cpp @@ -48,15 +48,18 @@ void MainApplication::initialize(CommandLineArgsUPtr commandLineArgs, std::uniqu FileLogger::GetInstance().Open(); MLOG_INFO("Launched"); - mDatabaseManager = DatabaseManager::Create(); - mDatabaseManager->openDatabase(launch_path + "/database.db"); - MLOG_INFO("Created DB"); + if (false) + { + mDatabaseManager = DatabaseManager::Create(); + mDatabaseManager->openDatabase(launch_path + "/database.db"); + MLOG_INFO("Created DB"); - mNetworkManager = NetworkManager::Create(); - MLOG_INFO("Created Network Manager"); + mNetworkManager = NetworkManager::Create(); + MLOG_INFO("Created Network Manager"); - mAudioManager = AudioManager::Create(); - MLOG_INFO("Created Audio Manager"); + mAudioManager = AudioManager::Create(); + MLOG_INFO("Created Audio Manager"); + } } void MainApplication::run() @@ -141,8 +144,16 @@ CommandLineArgs* MainApplication::getCommandLineArgs() const void MainApplication::shutDown() { - mDatabaseManager->onShutDown(); - mNetworkManager->ShutDown(); + if (mDatabaseManager) + { + mDatabaseManager->onShutDown(); + } + + if (mNetworkManager) + { + mNetworkManager->ShutDown(); + } + MLOG_INFO("Shut down"); FileLogger::GetInstance().Close(); } diff --git a/src/geometry/CMakeLists.txt b/src/geometry/CMakeLists.txt index 4140dab..09c4664 100644 --- a/src/geometry/CMakeLists.txt +++ b/src/geometry/CMakeLists.txt @@ -7,6 +7,8 @@ list(APPEND geometry_LIB_INCLUDES Grid.h Grid.cpp Line.h + Linalg.h + Linalg.cpp Line.cpp LineSegment.cpp Path.h @@ -18,7 +20,9 @@ list(APPEND geometry_LIB_INCLUDES Rectangle.cpp Triangle.h Triangle.cpp - Transform.cpp) + Transform.cpp + Vector.h + Vector.cpp) # add the library diff --git a/src/geometry/Linalg.cpp b/src/geometry/Linalg.cpp new file mode 100644 index 0000000..932fb1f --- /dev/null +++ b/src/geometry/Linalg.cpp @@ -0,0 +1,11 @@ +#include "Linalg.h" + +double Linalg::dotProduct(const Vector& v0, const Vector& v1) +{ + return v0.dotProduct(v1); +} + +Vector Linalg::crossProduct(const Vector& v0, const Vector& v1) +{ + return v0.crossProduct(v1); +} \ No newline at end of file diff --git a/src/geometry/Linalg.h b/src/geometry/Linalg.h new file mode 100644 index 0000000..82f7622 --- /dev/null +++ b/src/geometry/Linalg.h @@ -0,0 +1,10 @@ +#pragma once + +#include "Vector.h" + +class Linalg +{ + static double dotProduct(const Vector& v0, const Vector& v1); + + static Vector crossProduct(const Vector& v0, const Vector& v1); +}; \ No newline at end of file diff --git a/src/geometry/Point.cpp b/src/geometry/Point.cpp index bab525a..c743725 100644 --- a/src/geometry/Point.cpp +++ b/src/geometry/Point.cpp @@ -49,12 +49,15 @@ double Point::getZ() const double Point::getDistance(const Point& point) const { - return std::sqrt(mX*point.getX() + mY*point.getY() + mZ*point.getZ()); + const auto deltaX = getDeltaX(point); + const auto deltaY = getDeltaY(point); + const auto deltaZ = getDeltaZ(point); + return std::sqrt(deltaX* deltaX + deltaY* deltaY + deltaZ* deltaZ); } -double Point::getDistance(Point* point) const +Vector Point::getDelta(const Point& point) const { - return std::sqrt(mX*point->getX() + mY*point->getY() + mZ*point->getZ()); + return Vector(point.mX - mX, point.mY - mY, point.mZ - mZ); } double Point::getDeltaX(const Point& point) const @@ -66,3 +69,22 @@ double Point::getDeltaY(const Point& point) const { return point.getY() - mY; } + +double Point::getDeltaZ(const Point& point) const +{ + return point.getZ() - mZ; +} + +void Point::scale(double x, double y, double z) +{ + mX = x * mX; + mY = y * mY; + mZ = z * mZ; +} + +void Point::translate(double x, double y, double z) +{ + mX += x; + mY += y; + mZ += z; +} diff --git a/src/geometry/Point.h b/src/geometry/Point.h index 60bdb98..320958e 100644 --- a/src/geometry/Point.h +++ b/src/geometry/Point.h @@ -1,6 +1,7 @@ #pragma once #include "DiscretePoint.h" +#include "Vector.h" #include #include @@ -27,25 +28,17 @@ public: double getDistance(const Point& point) const; - double getDistance(Point* point) const; - double getDeltaX(const Point& point) const; double getDeltaY(const Point& point) const; - void scale(double x, double y, double z = 1.0) - { - mX = x*mX; - mY = y*mY; - mZ = z*mZ; - } + double getDeltaZ(const Point& point) const; - void translate(double x, double y, double z = 0.0) - { - mX += x; - mY += y; - mZ += z; - } + Vector getDelta(const Point& point) const; + + void scale(double x, double y, double z = 1.0); + + void translate(double x, double y, double z = 0.0); bool operator==(const Point& rhs) const { diff --git a/src/geometry/Vector.cpp b/src/geometry/Vector.cpp new file mode 100644 index 0000000..eca12bd --- /dev/null +++ b/src/geometry/Vector.cpp @@ -0,0 +1,61 @@ +#include "Vector.h" + +Vector::Vector(double x, double y, double z) + : mX(x), + mY(y), + mZ(z) +{ + updateLength(); +} + +Vector::~Vector() +{ +}; + +double Vector::getX() const +{ + return mX; +} + +double Vector::getY() const +{ + return mY; +} + +double Vector::getZ() const +{ + return mZ; +} + +double Vector::getLength() const +{ + return mLength; +} + +double Vector::dotProduct(const Vector& v) const +{ + return mX * v.mX + mY * v.mY + mZ * v.mZ; +} + +Vector Vector::crossProduct(const Vector& v) const +{ + return Vector(v.mY * mZ - v.mZ * mY, v.mZ * mX - v.mX * mZ, v.mX * mY - v.mY * mX); +} + +Vector Vector::getNormalized() const +{ + return Vector(mX / mLength, mY / mLength, mZ / mLength); +} + +void Vector::scale(double x, double y, double z) +{ + mX = x * mX; + mY = y * mY; + mZ = z * mZ; + updateLength(); +} + +void Vector::updateLength() +{ + mLength = std::sqrt(mX * mX + mY * mY + mZ * mZ); +} \ No newline at end of file diff --git a/src/geometry/Vector.h b/src/geometry/Vector.h new file mode 100644 index 0000000..098c752 --- /dev/null +++ b/src/geometry/Vector.h @@ -0,0 +1,48 @@ +#pragma once + +#include +#include + +class Vector +{ +public: + Vector(double x = 0, double y = 0, double z = 0); + + ~Vector(); + + double getX() const; + + double getY() const; + + double getZ() const; + + double getLength() const; + + Vector getNormalized() const; + + void scale(double x, double y, double z = 1.0); + + double dotProduct(const Vector& v) const; + + Vector crossProduct(const Vector& v) const; + + bool operator==(const Vector& rhs) const + { + return (mX == rhs.mX) + && (mY == rhs.mY) + && (mZ == rhs.mZ); + } + + bool operator!=(const Vector& rhs) const + { + return !operator==(rhs); + } + +private: + void updateLength(); + + double mLength{ 0 }; + double mX{ 0 }; + double mY{ 0 }; + double mZ{ 0 }; +}; \ No newline at end of file diff --git a/src/graphics/directx/DirectXMesh.cpp b/src/graphics/directx/DirectXMesh.cpp index c622489..12b8d94 100644 --- a/src/graphics/directx/DirectXMesh.cpp +++ b/src/graphics/directx/DirectXMesh.cpp @@ -5,6 +5,8 @@ #include "DrawingSurface.h" #include "TriMesh.h" +#include "TriFace.h" +#include "FileLogger.h" #include @@ -26,18 +28,45 @@ void DirectXMesh::update(DrawingContext* context, ID3D12Device* device) std::vector color = { float(model_color[0]), float(model_color[1]), float(model_color[2]), float(model_color[3]) }; auto transform = mModel->getTransform(); + + for (const auto& face : dynamic_cast(mModel->getMesh())->getFaces()) + { + for (const auto& loc : face->getNodeLocations(AbstractFace::Orientation::CW)) + { + auto x = loc.getX() * transform.getScaleX() + transform.getLocation().getX(); + auto y = loc.getY() * transform.getScaleY() + transform.getLocation().getY(); + x = 2 * x - 1; + y = 2 * y - 1; + + Vertex vert; + vert.position = DirectX::XMFLOAT3(x, y, 0.0); + vert.color = DirectX::XMFLOAT4(color[0], color[1], color[2], color[3]); + MLOG_INFO("Adding vert: " << x << " | " << y); + mVertexBuffer.push_back(vert); + } + } + + /* for (const auto& node : mModel->getMesh()->getNodes()) { auto x = node->getPoint().getX() * transform.getScaleX() + transform.getLocation().getX(); - x = 2 * x / width - 1.0; + //x = 2 * x / width - 1.0; auto y = node->getPoint().getY() * transform.getScaleY() + transform.getLocation().getY(); - y = 2 * y / height; + //y = 2 * y / height; Vertex vert; vert.position = DirectX::XMFLOAT3(x, y, 0.0); vert.color = DirectX::XMFLOAT4(color[0], color[1], color[2], color[3]); + MLOG_INFO("Adding vert: " << x << " | " << y); mVertexBuffer.push_back(vert); } - mIndexBuffer = dynamic_cast(mModel->getMesh())->getFaceNodeIds(); + */ + + //mIndexBuffer = dynamic_cast(mModel->getMesh())->getFaceNodeIds(); + mIndexBuffer = { 0, 1, 2 }; + for (auto id : mIndexBuffer) + { + MLOG_INFO("Adding id: " << id); + } createD3dVertexBuffer(device); createD3dIndexBuffer(device); @@ -88,6 +117,7 @@ void DirectXMesh::createD3dIndexBuffer(ID3D12Device* device) mIndexBufferView.BufferLocation = mD3dIndexBuffer->GetGPUVirtualAddress(); mIndexBufferView.SizeInBytes = buffer_size; + mIndexBufferView.Format = DXGI_FORMAT_R16_UINT; } void DirectXMesh::uploadIndexBuffer(unsigned char* pBuffer) const diff --git a/src/graphics/directx/DirectXMeshPainter.cpp b/src/graphics/directx/DirectXMeshPainter.cpp index c8b0aaf..646485c 100644 --- a/src/graphics/directx/DirectXMeshPainter.cpp +++ b/src/graphics/directx/DirectXMeshPainter.cpp @@ -89,9 +89,10 @@ void DirectXMeshPainter::updateCommandList(ID3D12GraphicsCommandList* commandLis for (const auto& mesh : mDxMeshes) { - commandList->IASetVertexBuffers(0, 1, &mesh->getVertBufferView()); commandList->IASetIndexBuffer(&mesh->getIndexBufferView()); - commandList->DrawInstanced(3, 1, 0, 0); + commandList->IASetVertexBuffers(0, 1, &mesh->getVertBufferView()); + commandList->DrawInstanced(mesh->getVertBufferView().SizeInBytes/ mesh->getVertBufferView().StrideInBytes, 1, 0, 0); + //commandList->DrawIndexedInstanced(3, 1, 0, 0, 0); } } diff --git a/src/mesh/AbstractFace.h b/src/mesh/AbstractFace.h index d0ecfe4..2b03281 100644 --- a/src/mesh/AbstractFace.h +++ b/src/mesh/AbstractFace.h @@ -12,6 +12,13 @@ class Edge; class AbstractFace { public: + + enum class Orientation + { + CW, + CCW + }; + AbstractFace(unsigned id=0); virtual ~AbstractFace(); @@ -32,7 +39,7 @@ public: virtual std::vector getEdgeIds() const = 0; - virtual std::vector getNodeLocations() const = 0; + virtual std::vector getNodeLocations(Orientation orientation = Orientation::CCW) const = 0; protected: unsigned mId{0}; diff --git a/src/mesh/CMakeLists.txt b/src/mesh/CMakeLists.txt index f654d47..6e152db 100644 --- a/src/mesh/CMakeLists.txt +++ b/src/mesh/CMakeLists.txt @@ -1,16 +1,29 @@ list(APPEND mesh_LIB_INCLUDES AbstractMesh.cpp + AbstractMesh.h Edge.cpp + Edge.h AbstractFace.cpp + AbstractFace.h QuadFace.cpp + QuadFace.h TriFace.cpp + TriFace.h Node.cpp + Node.h QuadMesh.cpp + QuadMesh.h TriMesh.cpp + TriMesh.h FaceMesh.cpp + FaceMesh.h LineMesh.cpp + LineMesh.h MeshPrimitives.cpp - MeshBuilder.cpp) + MeshPrimitives.h + MeshBuilder.cpp + MeshBuilder.h +) # add the library diff --git a/src/mesh/TriFace.cpp b/src/mesh/TriFace.cpp index 0935477..709983e 100644 --- a/src/mesh/TriFace.cpp +++ b/src/mesh/TriFace.cpp @@ -75,7 +75,34 @@ void TriFace::associateWidthEdges() mEdge2->associateFace(mId); } -std::vector TriFace::getNodeLocations() const +std::vector TriFace::getNodeLocations(Orientation orientation) const { - return {mEdge0->getNode0()->getPoint(), mEdge0->getNode1()->getPoint(), mEdge1->getNode1()->getPoint()}; + if (orientation != getOrientation()) + { + return { mEdge0->getNode0()->getPoint(), mEdge0->getNode1()->getPoint(), mEdge1->getNode1()->getPoint() }; + } + else + { + return { mEdge0->getNode0()->getPoint(), mEdge1->getNode1()->getPoint(), mEdge0->getNode1()->getPoint() }; + } +} + +Vector TriFace::getNormal() const +{ + auto v0 = mEdge0->getNode0()->getPoint().getDelta(mEdge0->getNode1()->getPoint()); + auto v1 = mEdge0->getNode0()->getPoint().getDelta(mEdge1->getNode1()->getPoint()); + return v0.crossProduct(v1).getNormalized(); +} + +AbstractFace::Orientation TriFace::getOrientation() const +{ + Vector z_norm(0, 0, 1); + if (z_norm.dotProduct(getNormal()) < 0.0) + { + return Orientation::CW; + } + else + { + return Orientation::CCW; + } } diff --git a/src/mesh/TriFace.h b/src/mesh/TriFace.h index 4121f14..bf8971d 100644 --- a/src/mesh/TriFace.h +++ b/src/mesh/TriFace.h @@ -1,6 +1,7 @@ #pragma once #include "AbstractFace.h" +#include "Vector.h" class Edge; @@ -26,7 +27,11 @@ public: unsigned getEdge2Id () const; std::vector getEdgeIds() const override; - std::vector getNodeLocations() const override; + std::vector getNodeLocations(Orientation orientation = Orientation::CCW) const override; + + Vector getNormal() const; + + AbstractFace::Orientation getOrientation() const; private: Edge* mEdge0{nullptr}; diff --git a/test/graphics/TestDirectXRendering.cpp b/test/graphics/TestDirectXRendering.cpp index dce3a6c..8dcc6e5 100644 --- a/test/graphics/TestDirectXRendering.cpp +++ b/test/graphics/TestDirectXRendering.cpp @@ -1,11 +1,16 @@ #include "TestCase.h" #include "TestCaseRunner.h" -#include "MainApplication.h" -#include "GuiApplication.h" +#include "TestUiApplication.h" #include "TestFramework.h" +#include "DesktopManager.h" +#include "MeshPrimitives.h" +#include "MeshNode.h" +#include "Scene.h" +#include "Widget.h" + #include #include #include @@ -14,5 +19,16 @@ TEST_CASE(TestDirectXRendering, "graphics") { auto gui_app = TestCaseRunner::getInstance().getTestApplication(); + auto drawing_context = gui_app->getDesktopManager()->getWindowManager()->getMainWindow(); + + auto scene = drawing_context->getScene(); + + auto mesh = MeshPrimitives::buildRectangleAsTriMesh(); + auto mesh_node = std::make_unique(DiscretePoint(0, 0)); + + mesh_node->setMesh(mesh.get()); + + scene->addNode(mesh_node.get()); + scene->update(nullptr); gui_app->run(); }; \ No newline at end of file diff --git a/test/test_utils/CMakeLists.txt b/test/test_utils/CMakeLists.txt index 57110bb..6b8d93c 100644 --- a/test/test_utils/CMakeLists.txt +++ b/test/test_utils/CMakeLists.txt @@ -4,10 +4,12 @@ add_library(test_utils STATIC TestCaseRunner.h TestFramework.h TestCaseRunner.cpp + TestUiApplication.h + TestUiApplication.cpp ) target_include_directories(test_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) -target_link_libraries(test_utils core) +target_link_libraries(test_utils core client) set_property(TARGET test_utils PROPERTY FOLDER test) configure_file(TestDataLocations.h.in TestDataLocations.h) \ No newline at end of file diff --git a/test/test_utils/TestCaseRunner.cpp b/test/test_utils/TestCaseRunner.cpp index 1519b00..27498cb 100644 --- a/test/test_utils/TestCaseRunner.cpp +++ b/test/test_utils/TestCaseRunner.cpp @@ -1,6 +1,7 @@ #include "TestCaseRunner.h" #include "FileLogger.h" +#include "TestUiApplication.h" #include #include @@ -18,12 +19,12 @@ TestCaseRunner::~TestCaseRunner() } -GuiApplication* TestCaseRunner::getTestApplication() +TestUiApplication* TestCaseRunner::getTestApplication() { return mTestApplication; } -void TestCaseRunner::setTestApplication(GuiApplication* app) +void TestCaseRunner::setTestApplication(TestUiApplication* app) { mTestApplication = app; } diff --git a/test/test_utils/TestCaseRunner.h b/test/test_utils/TestCaseRunner.h index 05802ab..e44b20e 100644 --- a/test/test_utils/TestCaseRunner.h +++ b/test/test_utils/TestCaseRunner.h @@ -5,7 +5,7 @@ #include #include -class GuiApplication; +class TestUiApplication; class TestCaseRunner { @@ -16,7 +16,7 @@ public: ~TestCaseRunner(); - GuiApplication* getTestApplication(); + TestUiApplication* getTestApplication(); void addTestCase(const std::string& label, const std::string& tag, TestCase::TestCaseFunction func); @@ -24,10 +24,10 @@ public: bool run(const std::vector& args); - void setTestApplication(GuiApplication* app); + void setTestApplication(TestUiApplication* app); private: - GuiApplication* mTestApplication{ nullptr }; + TestUiApplication* mTestApplication{ nullptr }; std::vector mFailingTests; static bool sLastTestFailed; static std::string sFailureLine; diff --git a/test/test_utils/TestUiApplication.cpp b/test/test_utils/TestUiApplication.cpp new file mode 100644 index 0000000..e00a339 --- /dev/null +++ b/test/test_utils/TestUiApplication.cpp @@ -0,0 +1,14 @@ +#include "TestUiApplication.h" + +#include "MainApplication.h" + +TestUiApplication::TestUiApplication(std::unique_ptr args, std::unique_ptr mainApp) + : GuiApplication(std::move(args), std::move(mainApp)) +{ + +} + +DesktopManager* TestUiApplication::getDesktopManager() const +{ + return mDesktopManager.get(); +} \ No newline at end of file diff --git a/test/test_utils/TestUiApplication.h b/test/test_utils/TestUiApplication.h new file mode 100644 index 0000000..d75a587 --- /dev/null +++ b/test/test_utils/TestUiApplication.h @@ -0,0 +1,13 @@ +#pragma once + +#include "GuiApplication.h" + +#include + +class TestUiApplication : public GuiApplication +{ +public: + TestUiApplication(std::unique_ptr args = nullptr, std::unique_ptr mainApp = nullptr); + + DesktopManager* getDesktopManager() const; +}; \ No newline at end of file diff --git a/test/ui_test_runner.cpp b/test/ui_test_runner.cpp index 5714422..0f3e06e 100644 --- a/test/ui_test_runner.cpp +++ b/test/ui_test_runner.cpp @@ -2,7 +2,7 @@ #include "CommandLineArgs.h" #include "MainApplication.h" -#include "GuiApplication.h" +#include "TestUiApplication.h" #include "Widget.h" #ifdef _WIN32 @@ -12,16 +12,6 @@ #include -class TestApp : public GuiApplication -{ -public: - TestApp(std::unique_ptr args = nullptr, std::unique_ptr mainApp = nullptr) - : GuiApplication(std::move(args), std::move(mainApp)) - { - - } -}; - #ifdef _WIN32 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { @@ -41,7 +31,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine main_app->initialize(std::move(args), std::move(applicationContext)); - auto gui_app = std::make_unique(nullptr, std::move(main_app)); + auto gui_app = std::make_unique(nullptr, std::move(main_app)); #else int main(int argc, char *argv[])