From b0559caac329ad364b8cbd7afbb022676d82deda Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sat, 6 Nov 2021 03:14:12 +0700 Subject: [PATCH] small optimizations with Triangle.cpp (normal vector calculated once in constructor) --- Source.cpp | 2 +- engine/Engine.cpp | 2 +- engine/Object.cpp | 2 +- engine/Object.h | 4 ++-- engine/ResourceManager.cpp | 1 + engine/Screen.cpp | 4 ++-- engine/Triangle.cpp | 26 +++++++++++++++----------- engine/Triangle.h | 5 ++++- engine/gui/Window.h | 2 +- engine/physics/RigidBody.cpp | 2 +- 10 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Source.cpp b/Source.cpp index 1737a7f..6525480 100644 --- a/Source.cpp +++ b/Source.cpp @@ -10,7 +10,7 @@ using namespace std; int main() { Shooter game; - game.create(1280, 720, ShooterConsts::PROJECT_NAME, true); + game.create(1280, 720, ShooterConsts::PROJECT_NAME, false); //game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); //game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false); diff --git a/engine/Engine.cpp b/engine/Engine.cpp index a2af2c5..d7ed69e 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -57,7 +57,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, Time::startTimer("d projections"); if (_useOpenGL) { - GLfloat *view = camera->glView(); + GLfloat *view = camera->glInvModel(); for (auto &it : *world) { if (it.second->isVisible()) { GLfloat *model = it.second->glModel(); diff --git a/engine/Object.cpp b/engine/Object.cpp index 0e86a6c..9a1ff3b 100644 --- a/engine/Object.cpp +++ b/engine/Object.cpp @@ -139,7 +139,7 @@ void Object::unattach(const ObjectNameTag &tag) { } // OpenGL function -GLfloat *Object::glView() const { +GLfloat *Object::glInvModel() const { auto *v = new GLfloat[4 * 4]; v[0] = -static_cast(left().x()); diff --git a/engine/Object.h b/engine/Object.h index ea2f9ac..45d1aef 100644 --- a/engine/Object.h +++ b/engine/Object.h @@ -105,12 +105,12 @@ public: [[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; } - [[nodiscard]] Matrix4x4 view() const { return Matrix4x4::View(left(), up(), lookAt(), position()); } + [[nodiscard]] Matrix4x4 invModel() const { return Matrix4x4::View(left(), up(), lookAt(), position()); } // OpenGL function [[nodiscard]] GLfloat *glModel() const; - [[nodiscard]] GLfloat *glView() const; + [[nodiscard]] GLfloat *glInvModel() const; virtual ~Object(); }; diff --git a/engine/ResourceManager.cpp b/engine/ResourceManager.cpp index ffbc320..460e4f1 100644 --- a/engine/ResourceManager.cpp +++ b/engine/ResourceManager.cpp @@ -169,6 +169,7 @@ std::vector> ResourceManager::loadObjects(const std::strin return objects; } + // If objects is already loaded - return pointer to it auto it = _instance->_objects.find(filename); if (it != _instance->_objects.end()) { diff --git a/engine/Screen.cpp b/engine/Screen.cpp index f1d0f05..d073217 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -177,10 +177,10 @@ GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr mesh) { int stride = 21 * i; Triangle triangle = triangles[i]; + Vec3D norm = triangles[i].norm(); + float dot = norm.dot(Vec3D(2, 1, 0).normalized()); for (int k = 0; k < 3; k++) { - float dot = 0.5; - sf::Color color = triangle.color(); GLfloat ambientColor[4] = { color.r * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, diff --git a/engine/Triangle.cpp b/engine/Triangle.cpp index d488553..d2e2eef 100644 --- a/engine/Triangle.cpp +++ b/engine/Triangle.cpp @@ -7,10 +7,23 @@ Triangle::Triangle(const Vec4D &p1, const Vec4D &p2, const Vec4D &p3, sf::Color color) : _color(color), _points{p1, p2, p3} { + calculateNormal(); +} + +void Triangle::calculateNormal() { + Vec3D v1 = Vec3D(_points[1] - _points[0]); + Vec3D v2 = Vec3D(_points[2] - _points[0]); + Vec3D crossProduct = v1.cross(v2); + + if (crossProduct.sqrAbs() > Consts::EPS) { + _normal = crossProduct.normalized(); + } else { + _normal = Vec3D(0); + } } Triangle::Triangle(const Triangle &triangle) : _points{triangle._points[0], triangle._points[1], triangle._points[2]}, - _color(triangle._color) { + _color(triangle._color), _normal(triangle._normal) { } Triangle Triangle::operator*(const Matrix4x4 &matrix4X4) const { @@ -18,16 +31,7 @@ Triangle Triangle::operator*(const Matrix4x4 &matrix4X4) const { } Vec3D Triangle::norm() const { - - Vec3D v1 = Vec3D(_points[1] - _points[0]); - Vec3D v2 = Vec3D(_points[2] - _points[0]); - Vec3D crossProduct = v1.cross(v2); - - if (crossProduct.sqrAbs() > Consts::EPS) { - return crossProduct.normalized(); - } else { - return Vec3D(0); - } + return _normal; } const Vec4D& Triangle::operator[](int i) const { diff --git a/engine/Triangle.h b/engine/Triangle.h index 836c1c6..593dbac 100644 --- a/engine/Triangle.h +++ b/engine/Triangle.h @@ -15,8 +15,11 @@ class Triangle final { private: sf::Color _color; Vec4D _points[3]; + Vec3D _normal; + + void calculateNormal(); public: - Triangle() : _points{Vec4D{}, Vec4D{}, Vec4D{}} {}; + Triangle() = default; Triangle(const Triangle &triangle); diff --git a/engine/gui/Window.h b/engine/gui/Window.h index d6e0208..1b9c1d8 100644 --- a/engine/gui/Window.h +++ b/engine/gui/Window.h @@ -26,7 +26,7 @@ private: std::shared_ptr _mouse; public: explicit Window(std::shared_ptr screen, std::shared_ptr mouse, std::string name = "Menu", - std::string backTexture = "") : _screen(std::move(screen)), _mouse(std::move(mouse)), + std::string backTexture = "") : _screen(screen), _mouse(mouse), _name(std::move(name)), _backTexture(std::move(backTexture)) {} void addButton(int x, int y, int w, int h, diff --git a/engine/physics/RigidBody.cpp b/engine/physics/RigidBody.cpp index 81a4b43..663c319 100644 --- a/engine/physics/RigidBody.cpp +++ b/engine/physics/RigidBody.cpp @@ -24,7 +24,7 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) { double maxDistance = -std::numeric_limits::max(); - Vec3D transformedDirection = (view() * direction).normalized(); + Vec3D transformedDirection = (invModel() * direction).normalized(); for(auto & it : _hitBox) { double distance = it.dot(transformedDirection);