From 8f03c3226d9b5fe4520cb76d4b71816a8186861a Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sat, 30 Oct 2021 03:44:37 +0700 Subject: [PATCH] Huge refactoring v3 --- Player.cpp | 5 +-- Shooter.cpp | 2 +- Source.cpp | 1 + engine/Camera.cpp | 27 ----------- engine/Camera.h | 2 - engine/Engine.cpp | 6 ++- engine/Matrix4x4.cpp | 22 +++++++-- engine/Matrix4x4.h | 4 ++ engine/Mesh.cpp | 35 ++------------- engine/Mesh.h | 12 +---- engine/Object.cpp | 85 +++++++++++++++++++++++++++-------- engine/Object.h | 44 ++++++++++-------- engine/ResourceManager.cpp | 20 +++++++-- engine/ResourceManager.h | 9 ++-- engine/Screen.cpp | 24 +++++----- engine/Screen.h | 2 +- engine/SoundController.cpp | 19 +++++--- engine/SoundController.h | 1 + engine/World.cpp | 3 +- engine/animation/Timeline.cpp | 14 +++--- engine/animation/Timeline.h | 1 + engine/physics/RigidBody.cpp | 2 +- engine/physics/RigidBody.h | 1 + engine/utils/Time.cpp | 18 +++++--- engine/utils/Time.h | 1 + weapon/Weapon.cpp | 2 +- 26 files changed, 201 insertions(+), 161 deletions(-) diff --git a/Player.cpp b/Player.cpp index de0913c..4ac86f1 100644 --- a/Player.cpp +++ b/Player.cpp @@ -10,9 +10,8 @@ Player::Player(ObjectNameTag name) : RigidBody(name) { loadObj(ShooterConsts::CUBE_OBJ, Vec3D{0.5, 1.9, 0.5}); setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0}); setCollision(true); - setVisible(false); + //setVisible(false); - //setColor({240, 168, 168}); Vec3D randColor = Vec3D::Random(); setColor({static_cast(randColor.x()*255), static_cast(randColor.y()*255), static_cast(randColor.z()*255)}); @@ -73,7 +72,7 @@ void Player::addWeapon(std::shared_ptr weapon) { attach(weapon); _weapons.back()->translate(position()); - _weapons.back()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, Vec3D{0, 1, 0}, _angle.y()); + _weapons.back()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, Vec3D{0, 1, 0}, angle().y()); _weapons.back()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, left(), headAngle()); _weapons.back()->setAddTraceCallBack(_addTraceCallBack); diff --git a/Shooter.cpp b/Shooter.cpp index 3ee97f0..cf95dc6 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -89,11 +89,11 @@ void Shooter::start() { player->initWeapons(); + player->translate(Vec3D{0, 0, 0}); camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0}); player->attach(camera); world->addBody(player); - player->translate(Vec3D{0, 10, 0}); // connecting to the server InitNetwork(); diff --git a/Source.cpp b/Source.cpp index e225a1d..ac6f36f 100644 --- a/Source.cpp +++ b/Source.cpp @@ -6,6 +6,7 @@ using namespace std; + int main() { Shooter game; diff --git a/engine/Camera.cpp b/engine/Camera.cpp index f616f12..838791f 100644 --- a/engine/Camera.cpp +++ b/engine/Camera.cpp @@ -126,30 +126,3 @@ void Camera::clear() { _triangles.clear(); _V = Matrix4x4::View(left(), up(), lookAt(), position()); } - -// OpenGL function -GLfloat *Camera::view() const { - auto* v = (GLfloat*)malloc(4*4*sizeof(GLfloat)); - - v[0] = -(GLfloat)left().x(); - v[4] = -(GLfloat)left().y(); - v[8] = -(GLfloat)left().z(); - v[12] = (GLfloat)position().dot(left()); - - v[1] = (GLfloat)up().x(); - v[5] = (GLfloat)up().y(); - v[9] = (GLfloat)up().z(); - v[13] = -(GLfloat)position().dot(up()); - - v[2] = -(GLfloat)lookAt().x(); - v[6] = -(GLfloat)lookAt().y(); - v[10] = -(GLfloat)lookAt().z(); - v[14] = (GLfloat)position().dot(lookAt()); - - v[3] = (GLfloat)0.0f; - v[7] = (GLfloat)0.0f; - v[11] = (GLfloat)0.0f; - v[15] = (GLfloat)1.0f; - - return v; -} diff --git a/engine/Camera.h b/engine/Camera.h index 382ce8e..eba6498 100644 --- a/engine/Camera.h +++ b/engine/Camera.h @@ -38,8 +38,6 @@ public: [[nodiscard]] int buffSize() const { return _triangles.size(); } std::vector> sorted(); - // OpenGL function - [[nodiscard]] GLfloat* view() const; }; diff --git a/engine/Engine.cpp b/engine/Engine.cpp index e488c3c..d8a20c5 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -43,12 +43,14 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, world->update(); if(_useOpenGL) { - GLfloat* view = camera->view(); + GLfloat* view = camera->glView(); for(auto & it : *world) { if (it.second->isVisible()) { + GLfloat* model = it.second->glModel(); GLfloat* geometry = Screen::glMeshToGLfloatArray(it.second, camera->position()); - screen->glDrawMesh(geometry, view, 3 * it.second->triangles().size()); + screen->glDrawMesh(geometry, view, model, 3 * it.second->triangles().size()); free(geometry); + free(model); } } free(view); diff --git a/engine/Matrix4x4.cpp b/engine/Matrix4x4.cpp index aa79b9f..529e626 100644 --- a/engine/Matrix4x4.cpp +++ b/engine/Matrix4x4.cpp @@ -85,9 +85,13 @@ Matrix4x4 Matrix4x4::Translation(const Vec3D& v) { t._arr[2][2] = 1.0; t._arr[3][3] = 1.0; - t._arr[0][3] = v.x(); - t._arr[1][3] = v.y(); - t._arr[2][3] = v.z(); + //t._arr[0][3] = v.x(); + //t._arr[1][3] = v.y(); + //t._arr[2][3] = v.z(); + + t._arr[3][0] = v.x(); + t._arr[3][1] = v.y(); + t._arr[3][2] = v.z(); return t; } @@ -210,3 +214,15 @@ Matrix4x4 Matrix4x4::View(const Vec3D &left, const Vec3D &up, const Vec3D &lookA return V; } + +Vec3D Matrix4x4::x() const { + return Vec3D(_arr[0][0], _arr[1][0],_arr[2][0]); +} + +Vec3D Matrix4x4::y() const { + return Vec3D(_arr[0][1], _arr[1][1],_arr[2][1]); +} + +Vec3D Matrix4x4::z() const { + return Vec3D(_arr[0][2], _arr[1][2],_arr[2][2]); +} diff --git a/engine/Matrix4x4.h b/engine/Matrix4x4.h index 740e1f9..43563fd 100644 --- a/engine/Matrix4x4.h +++ b/engine/Matrix4x4.h @@ -21,6 +21,10 @@ public: [[nodiscard]] Vec4D operator*(const Vec4D& point4D) const; [[nodiscard]] Vec3D operator*(const Vec3D& vec) const; + [[nodiscard]] Vec3D x() const; + [[nodiscard]] Vec3D y() const; + [[nodiscard]] Vec3D z() const; + // Any useful matrix (static methods) Matrix4x4 static Identity(); Matrix4x4 static Zero(); diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp index 0246d2f..4b3682f 100644 --- a/engine/Mesh.cpp +++ b/engine/Mesh.cpp @@ -29,42 +29,17 @@ void Mesh::loadObj(const std::string& filename, const Vec3D& scale) { this->scale(scale); } -Mesh::Mesh(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale) : Object(nameTag) { +Mesh::Mesh(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale) : Object(std::move(nameTag)) { loadObj(filename, scale); } -Mesh::Mesh(ObjectNameTag nameTag, const vector &tries) : Object(nameTag), _tris(tries) { +Mesh::Mesh(ObjectNameTag nameTag, const vector &tries) : Object(std::move(nameTag)), _tris(tries) { } Mesh Mesh::Obj(ObjectNameTag nameTag, const std::string& filename) { return Mesh(std::move(nameTag), filename); } -void Mesh::rotate(const Vec3D &r) { - Object::rotate(r); - *this *= Matrix4x4::Rotation(r); -} - -void Mesh::rotate(const Vec3D &v, double r) { - Object::rotate(v, r); - *this *= Matrix4x4::Rotation(v, r); -} - -void Mesh::scale(const Vec3D &s) { - Object::scale(s); - *this *= Matrix4x4::Scale(s); -} - -void Mesh::rotateRelativePoint(const Vec3D &s, const Vec3D &r) { - Object::rotateRelativePoint(s, r); - *this *= Matrix4x4::Rotation(r); -} - -void Mesh::rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r) { - Object::rotateRelativePoint(s, v, r); - *this *= Matrix4x4::Rotation(v, r); -} - void Mesh::setColor(const sf::Color& c) { _color = c; @@ -78,7 +53,7 @@ void Mesh::setColor(const sf::Color& c) { Mesh Mesh::LineTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, double line_width, const sf::Color& color) { - Mesh line(nameTag); + Mesh line(std::move(nameTag)); Vec3D v1 = (to - from).normalized(); Vec3D v2 = from.cross(from + Vec3D{1, 0, 0}).normalized(); @@ -115,10 +90,6 @@ Mesh Mesh::LineTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, dou return line; } -Mesh::Mesh(const Mesh &mesh) : Object(mesh.name()), _tris(mesh._tris), _color(mesh._color), _visible(mesh._visible) { - -} - void Mesh::setTriangles(const vector &t) { _tris.clear(); for (auto & tri : t) { diff --git a/engine/Mesh.h b/engine/Mesh.h index 12f2222..2604be4 100644 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -21,7 +21,7 @@ private: public: explicit Mesh(ObjectNameTag nameTag) : Object(std::move(nameTag)) {}; Mesh& operator=(const Mesh& mesh) = delete; - Mesh(const Mesh& mesh); + Mesh(const Mesh& mesh) = default; explicit Mesh(ObjectNameTag nameTag, const std::vector& tries); explicit Mesh(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); @@ -32,16 +32,6 @@ public: [[nodiscard]] std::vector& triangles() { return _tris; } void setTriangles(const std::vector& t); - // Translate body - // Rotate body around XYZ axes - void rotate(const Vec3D& r) override; - // Rotate body around normalised vector 'v' by 'r' radians - void rotate(const Vec3D& v, double r) override; - // Rotate body around XYZ by (r._x, r._y, r.z) radians relative val 'point4D' - void rotateRelativePoint(const Vec3D& point, const Vec3D& r) override; - // Rotate body around normalised vector 'v' by 'r' radians relative val 'point4D' - void rotateRelativePoint(const Vec3D& point4D, const Vec3D& v, double r) override; - void scale(const Vec3D& s) override; [[nodiscard]] int size() const { return _tris.size()*3; } [[nodiscard]] sf::Color color() const { return _color; } diff --git a/engine/Object.cpp b/engine/Object.cpp index cfc721c..8abbf52 100644 --- a/engine/Object.cpp +++ b/engine/Object.cpp @@ -7,6 +7,7 @@ #include "utils/Log.h" void Object::translate(const Vec3D &dv) { + _position = _position + dv; for(auto &[attachedName, attachedObject] : _attachedObjects) { @@ -17,6 +18,9 @@ void Object::translate(const Vec3D &dv) { } void Object::scale(const Vec3D &s) { + + _transformMatrix = Matrix4x4::Scale(s)*_transformMatrix; + for(auto &[attachedName, attachedObject] : _attachedObjects) { if(!attachedObject.expired()) { attachedObject.lock()->scale(s); @@ -29,9 +33,7 @@ void Object::rotate(const Vec3D &r) { Matrix4x4 rotationMatrix = Matrix4x4::RotationZ(r.z())*Matrix4x4::RotationY(r.y())*Matrix4x4::RotationX(r.z()); - _left = rotationMatrix * _left; - _up = rotationMatrix * _up; - _lookAt = rotationMatrix * _lookAt; + _transformMatrix = rotationMatrix*_transformMatrix; for(auto &[attachedName, attachedObject] : _attachedObjects) { if(!attachedObject.expired()) { @@ -43,9 +45,7 @@ void Object::rotate(const Vec3D &r) { void Object::rotate(const Vec3D &v, double rv) { Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, rv); - _left = rotationMatrix * _left; - _up = rotationMatrix * _up; - _lookAt = rotationMatrix * _lookAt; + _transformMatrix = rotationMatrix*_transformMatrix; for(auto &[attachedName, attachedObject] : _attachedObjects) { if(!attachedObject.expired()) { @@ -58,15 +58,13 @@ void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &r) { _angle = _angle + r; // Translate XYZ by vector r1 - Vec3D r1(_position - s); + Vec3D r1(position() - s); // In translated coordinate system we rotate body and position Matrix4x4 rotationMatrix = Matrix4x4::Rotation(r); Vec3D r2(rotationMatrix*r1); - _left = rotationMatrix * _left; - _up = rotationMatrix * _up; - _lookAt = rotationMatrix * _lookAt; + _transformMatrix = rotationMatrix*_transformMatrix; // After rotation we translate XYZ by vector -r2 and recalculate position _position = s + r2; @@ -80,14 +78,12 @@ void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &r) { void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r) { // Translate XYZ by vector r1 - Vec3D r1(_position - s); + Vec3D r1(position() - s); // In translated coordinate system we rotate body and position Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, r); Vec3D r2 = rotationMatrix*r1; - _left = rotationMatrix * _left; - _up = rotationMatrix * _up; - _lookAt = rotationMatrix * _lookAt; + _transformMatrix = rotationMatrix*_transformMatrix; // After rotation we translate XYZ by vector -r2 and recalculate position _position = s + r2; @@ -104,7 +100,7 @@ void Object::rotateLeft(double rl) { _angleLeftUpLookAt.y(), _angleLeftUpLookAt.z()}; - rotate(Vec3D(_left), rl); + rotate(left(), rl); } void Object::rotateUp(double ru) { @@ -112,18 +108,18 @@ void Object::rotateUp(double ru) { _angleLeftUpLookAt.y() + ru, _angleLeftUpLookAt.z()}; - rotate(Vec3D(_up), ru); + rotate(up(), ru); } void Object::rotateLookAt(double rlAt) { _angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x(), _angleLeftUpLookAt.y(), _angleLeftUpLookAt.z() + rlAt}; - rotate(Vec3D(_lookAt), rlAt); + rotate(lookAt(), rlAt); } void Object::translateToPoint(const Vec3D &point) { - translate(point - _position); + translate(point - position()); } void Object::rotateToAngle(const Vec3D &v) { @@ -162,6 +158,59 @@ void Object::unattach(const ObjectNameTag& tag) { _attachedObjects.erase(tag); } +// OpenGL function +GLfloat* Object::glView() const { + auto* v = (GLfloat*)malloc(4*4*sizeof(GLfloat)); + + v[0] = -(GLfloat)left().x(); + v[4] = -(GLfloat)left().y(); + v[8] = -(GLfloat)left().z(); + v[12] = (GLfloat)position().dot(left()); + + v[1] = (GLfloat)up().x(); + v[5] = (GLfloat)up().y(); + v[9] = (GLfloat)up().z(); + v[13] = -(GLfloat)position().dot(up()); + + v[2] = -(GLfloat)lookAt().x(); + v[6] = -(GLfloat)lookAt().y(); + v[10] = -(GLfloat)lookAt().z(); + v[14] = (GLfloat)position().dot(lookAt()); + + v[3] = (GLfloat)0.0f; + v[7] = (GLfloat)0.0f; + v[11] = (GLfloat)0.0f; + v[15] = (GLfloat)1.0f; + + return v; +} + +GLfloat* Object::glModel() const { + auto* m = (GLfloat*)malloc(4*4*sizeof(GLfloat)); + + m[0] = (GLfloat)left().x(); + m[4] = (GLfloat)up().x(); + m[8] = (GLfloat)lookAt().x(); + m[12] = (GLfloat)position().x(); + + m[1] = (GLfloat)left().y(); + m[5] = (GLfloat)up().y(); + m[9] = (GLfloat)lookAt().y(); + m[13] = (GLfloat)position().y(); + + m[2] = (GLfloat)left().z(); + m[6] = (GLfloat)up().z(); + m[10] =(GLfloat)lookAt().z(); + m[14] = (GLfloat)position().z(); + + m[3] = (GLfloat)0.0f; + m[7] = (GLfloat)0.0f; + m[11] = (GLfloat)0.0f; + m[15] = (GLfloat)1.0f; + + return m; +} + Object::~Object() { _attachedObjects.clear(); } diff --git a/engine/Object.h b/engine/Object.h index 8321802..976d06e 100644 --- a/engine/Object.h +++ b/engine/Object.h @@ -10,6 +10,8 @@ #include #include #include +#include "Matrix4x4.h" +#include class ObjectNameTag final { private: @@ -28,10 +30,7 @@ private: bool checkIfAttached(Object* obj); const ObjectNameTag _nameTag; -protected: - Vec3D _left {1, 0, 0}; // internal X - Vec3D _up {0, 1, 0}; // internal Y - Vec3D _lookAt {0, 0, 1}; // internal Z + Matrix4x4 _transformMatrix = Matrix4x4::Identity(); std::map> _attachedObjects; @@ -39,33 +38,40 @@ protected: Vec3D _angle {0, 0, 0}; Vec3D _angleLeftUpLookAt{0, 0, 0}; public: - Object(ObjectNameTag nameTag) : _nameTag(nameTag) {}; + explicit Object(ObjectNameTag nameTag) : _nameTag(std::move(nameTag)) {}; + explicit Object(const Object& object) : _nameTag(object.name()), _transformMatrix(object.model()) {}; - virtual void translate(const Vec3D& dv); - virtual void translateToPoint(const Vec3D& point); - virtual void scale(const Vec3D& s); - virtual void rotate(const Vec3D& r); - virtual void rotate(const Vec3D& v, double rv); - virtual void rotateToAngle(const Vec3D& v); - virtual void rotateRelativePoint(const Vec3D& s, const Vec3D& r); - virtual void rotateRelativePoint(const Vec3D& s, const Vec3D& v, double r); + void translate(const Vec3D& dv); + void translateToPoint(const Vec3D& point); + void scale(const Vec3D& s); + void rotate(const Vec3D& r); + void rotate(const Vec3D& v, double rv); + void rotateToAngle(const Vec3D& v); + void rotateRelativePoint(const Vec3D& s, const Vec3D& r); + void rotateRelativePoint(const Vec3D& s, const Vec3D& v, double r); void rotateLeft(double rl); void rotateUp(double ru); void rotateLookAt(double rlAt); - [[nodiscard]] Vec3D position() const { return _position; } + [[nodiscard]] Vec3D left() const { return _transformMatrix.x(); } + [[nodiscard]] Vec3D up() const { return _transformMatrix.y(); } + [[nodiscard]] Vec3D lookAt() const { return _transformMatrix.z(); } + [[nodiscard]] Vec3D position() const { return _position; } + [[nodiscard]] Vec3D angle() const { return _angle; } [[nodiscard]] Vec3D angleLeftUpLookAt() const { return _angleLeftUpLookAt; } - [[nodiscard]] Vec3D left() const { return _left; } - [[nodiscard]] Vec3D up() const { return _up; } - [[nodiscard]] Vec3D lookAt() const { return _lookAt; } - void attach(std::shared_ptr object); void unattach(const ObjectNameTag& tag); std::shared_ptr attached(const ObjectNameTag& tag); - ObjectNameTag name() const { return _nameTag; } + [[nodiscard]] ObjectNameTag name() const { return _nameTag; } + + [[nodiscard]] Matrix4x4 model() const { return _transformMatrix; } + + // OpenGL function + [[nodiscard]] GLfloat* glModel() const; + [[nodiscard]] GLfloat* glView() const; virtual ~Object(); }; diff --git a/engine/ResourceManager.cpp b/engine/ResourceManager.cpp index 347cd77..40f902b 100644 --- a/engine/ResourceManager.cpp +++ b/engine/ResourceManager.cpp @@ -10,9 +10,12 @@ #include ResourceManager* ResourceManager::_instance = nullptr; +bool ResourceManager::_validInstance = false; + void ResourceManager::init() { _instance = new ResourceManager(); + _validInstance = true; } void ResourceManager::unloadTextures() { @@ -41,6 +44,10 @@ void ResourceManager::unloadObjects() { } void ResourceManager::unloadAllResources() { + if(!_validInstance) { + return; + } + unloadTextures(); unloadSoundBuffers(); unloadFonts(); @@ -49,12 +56,12 @@ void ResourceManager::unloadAllResources() { void ResourceManager::free() { unloadAllResources(); + _validInstance = false; delete _instance; - _instance = nullptr; } std::shared_ptr ResourceManager::loadTexture(const std::string& filename) { - if(!_instance) { + if(!_validInstance) { return nullptr; } @@ -78,7 +85,7 @@ std::shared_ptr ResourceManager::loadTexture(const std::string& fil } std::shared_ptr ResourceManager::loadSoundBuffer(const std::string& filename) { - if(!_instance) { + if(!_validInstance) { return nullptr; } @@ -101,7 +108,7 @@ std::shared_ptr ResourceManager::loadSoundBuffer(const std::str } std::shared_ptr ResourceManager::loadFont(const std::string& filename) { - if(!_instance) { + if(!_validInstance) { return nullptr; } @@ -124,9 +131,14 @@ std::shared_ptr ResourceManager::loadFont(const std::string& filename) } std::vector> ResourceManager::loadObjects(const std::string &filename) { + std::vector> objects{}; std::map maters{}; + if(!_validInstance) { + 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/ResourceManager.h b/engine/ResourceManager.h index 91fcf2c..a3c4c1b 100644 --- a/engine/ResourceManager.h +++ b/engine/ResourceManager.h @@ -18,18 +18,19 @@ private: std::map>> _objects; static ResourceManager* _instance; + static bool _validInstance; ResourceManager() = default; -public: - ResourceManager(const ResourceManager&) = delete; - ResourceManager& operator=(ResourceManager&) = delete; - // Unloads all currently loaded textures. static void unloadObjects(); static void unloadTextures(); static void unloadSoundBuffers(); static void unloadFonts(); +public: + ResourceManager(const ResourceManager&) = delete; + ResourceManager& operator=(ResourceManager&) = delete; + static void unloadAllResources(); static void init(); diff --git a/engine/Screen.cpp b/engine/Screen.cpp index 3ee16af..bc3b76a 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -135,7 +135,7 @@ void Screen::drawText(const sf::Text &text) { } // OpenGL functions -void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, size_t count) { +void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, GLfloat* model, size_t count) { // OpenGL: // Make the window the active window for OpenGL calls _window->setActive(true); @@ -180,6 +180,7 @@ void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, size_t count) { glLoadIdentity(); glLoadMatrixf(view); + glMultMatrixf(model); // Draw the mesh glDrawArrays(GL_TRIANGLES, 0, count); @@ -195,9 +196,10 @@ GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D& c auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat)); for(int i = 0; i < triangles.size(); i++) { + int stride = 21*i; - double dot = triangles[i].norm().dot((pos + Vec3D(triangles[i][0]) - cameraPosition).normalized()); + double dot = triangles[i].norm().dot((pos + mesh->model()*Vec3D(triangles[i][0]) - cameraPosition).normalized()); sf::Color color = triangles[i].color(); sf::Color ambientColor = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot) + 0.7)), (sf::Uint8)(color.g * (0.3 * std::abs(dot) + 0.7)), @@ -205,27 +207,27 @@ GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D& c (sf::Uint8)color.a); - geometry[stride + 0] = (GLfloat)triangles[i][0].x() + pos.x(); - geometry[stride + 1] = (GLfloat)triangles[i][0].y() + pos.y(); - geometry[stride + 2] = (GLfloat)triangles[i][0].z() + pos.z(); + geometry[stride + 0] = (GLfloat)triangles[i][0].x(); + geometry[stride + 1] = (GLfloat)triangles[i][0].y(); + geometry[stride + 2] = (GLfloat)triangles[i][0].z(); geometry[stride + 3] = (GLfloat)ambientColor.r/255.0f; geometry[stride + 4] = (GLfloat)ambientColor.g/255.0f; geometry[stride + 5] = (GLfloat)ambientColor.b/255.0f; geometry[stride + 6] = (GLfloat)ambientColor.a/255.0f; - geometry[stride + 7] = (GLfloat)triangles[i][1].x() + pos.x(); - geometry[stride + 8] = (GLfloat)triangles[i][1].y() + pos.y(); - geometry[stride + 9] = (GLfloat)triangles[i][1].z() + pos.z(); + geometry[stride + 7] = (GLfloat)triangles[i][1].x(); + geometry[stride + 8] = (GLfloat)triangles[i][1].y(); + geometry[stride + 9] = (GLfloat)triangles[i][1].z(); geometry[stride + 10] = (GLfloat)ambientColor.r/255.0f; geometry[stride + 11] = (GLfloat)ambientColor.g/255.0f; geometry[stride + 12] = (GLfloat)ambientColor.b/255.0f; geometry[stride + 13] = (GLfloat)ambientColor.a/255.0f; - geometry[stride + 14] = (GLfloat)triangles[i][2].x() + pos.x(); - geometry[stride + 15] = (GLfloat)triangles[i][2].y() + pos.y(); - geometry[stride + 16] = (GLfloat)triangles[i][2].z() + pos.z(); + geometry[stride + 14] = (GLfloat)triangles[i][2].x(); + geometry[stride + 15] = (GLfloat)triangles[i][2].y(); + geometry[stride + 16] = (GLfloat)triangles[i][2].z(); geometry[stride + 17] = (GLfloat)ambientColor.r/255.0f; geometry[stride + 18] = (GLfloat)ambientColor.g/255.0f; diff --git a/engine/Screen.h b/engine/Screen.h index 33a78aa..e3dd38c 100644 --- a/engine/Screen.h +++ b/engine/Screen.h @@ -53,7 +53,7 @@ public: void setMouseCursorVisible(bool visible); // OpenGL functions - void glDrawMesh(GLfloat* geometry, GLfloat* view, size_t count); + void glDrawMesh(GLfloat* geometry, GLfloat* view, GLfloat* model, size_t count); static GLfloat* glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D& cameraPosition); [[nodiscard]] std::shared_ptr renderWindow() { return _window; } diff --git a/engine/SoundController.cpp b/engine/SoundController.cpp index 6f1e173..86f085a 100644 --- a/engine/SoundController.cpp +++ b/engine/SoundController.cpp @@ -6,13 +6,16 @@ #include "ResourceManager.h" SoundController* SoundController::_instance = nullptr; +bool SoundController::_validInstance = false; + void SoundController::init() { _instance = new SoundController(); + _validInstance = true; } void SoundController::playSound(const SoundTag& soundTag, const std::string& filename) { - if(!_instance) { + if(!_validInstance) { return; } @@ -22,7 +25,7 @@ void SoundController::playSound(const SoundTag& soundTag, const std::string& fil } void SoundController::pauseSound(const SoundTag& soundTag) { - if(!_instance) { + if(!_validInstance) { return; } @@ -32,7 +35,7 @@ void SoundController::pauseSound(const SoundTag& soundTag) { } void SoundController::stopSound(const SoundTag& soundTag) { - if(!_instance) { + if(!_validInstance) { return; } @@ -43,7 +46,7 @@ void SoundController::stopSound(const SoundTag& soundTag) { } sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) { - if(_instance == nullptr) { + if(!_validInstance) { return sf::Sound::Status::Stopped; } @@ -56,11 +59,13 @@ sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) { } void SoundController::free() { - for(auto& [soundTag, sound] : _instance->_sounds) { - sound.stop(); + if(_validInstance) { + for(auto& [soundTag, sound] : _instance->_sounds) { + sound.stop(); + } } _instance->_sounds.clear(); + _validInstance = false; delete _instance; - _instance = nullptr; } diff --git a/engine/SoundController.h b/engine/SoundController.h index 673e92b..b175d8d 100644 --- a/engine/SoundController.h +++ b/engine/SoundController.h @@ -26,6 +26,7 @@ private: std::map _sounds; static SoundController* _instance; + static bool _validInstance; SoundController() = default; public: diff --git a/engine/World.cpp b/engine/World.cpp index 6b6f8d4..ecab937 100644 --- a/engine/World.cpp +++ b/engine/World.cpp @@ -44,6 +44,7 @@ IntersectionInformation World::rayCast(const Vec3D& from, const Vec3D& to, const for (auto& escapeTag : tagsToSkip) { if (name.str().find(escapeTag) != std::string::npos) { escapeThisBody = true; + break; } } if(escapeThisBody) { @@ -51,7 +52,7 @@ IntersectionInformation World::rayCast(const Vec3D& from, const Vec3D& to, const } for(auto& tri : body->triangles()) { - Triangle tri_translated(tri[0] + body->position().makePoint4D(), tri[1] + body->position().makePoint4D(), tri[2] + body->position().makePoint4D()); + Triangle tri_translated(body->model()*tri[0] + body->position().makePoint4D(), body->model()*tri[1] + body->position().makePoint4D(), body->model()*tri[2] + body->position().makePoint4D()); Plane plane(tri_translated); auto intersection = plane.intersection(from, to); diff --git a/engine/animation/Timeline.cpp b/engine/animation/Timeline.cpp index 2b6306f..e698591 100644 --- a/engine/animation/Timeline.cpp +++ b/engine/animation/Timeline.cpp @@ -7,13 +7,15 @@ #include "Timeline.h" Timeline* Timeline::_instance = nullptr; +bool Timeline::_validInstance = false; void Timeline::init() { _instance = new Timeline(); + _validInstance = true; } void Timeline::animate(const AnimationListTag& listName, Animation* anim) { - if(!_instance) { + if(!_validInstance) { return; } @@ -21,7 +23,7 @@ void Timeline::animate(const AnimationListTag& listName, Animation* anim) { } void Timeline::deleteAllAnimations() { - if(!_instance) { + if(!_validInstance) { return; } @@ -36,7 +38,7 @@ void Timeline::deleteAllAnimations() { } void Timeline::deleteAnimationList(const AnimationListTag& listName) { - if(!_instance) { + if(!_validInstance) { return; } @@ -45,7 +47,7 @@ void Timeline::deleteAnimationList(const AnimationListTag& listName) { } [[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag& listName) { - if(!_instance) { + if(!_validInstance) { return false; } @@ -53,7 +55,7 @@ void Timeline::deleteAnimationList(const AnimationListTag& listName) { } void Timeline::update() { - if(!_instance) { + if(!_validInstance) { return; } @@ -85,7 +87,7 @@ void Timeline::update() { void Timeline::free() { Timeline::deleteAllAnimations(); + _validInstance = false; delete _instance; - _instance = nullptr; } diff --git a/engine/animation/Timeline.h b/engine/animation/Timeline.h index 445e956..ae07b5b 100644 --- a/engine/animation/Timeline.h +++ b/engine/animation/Timeline.h @@ -25,6 +25,7 @@ private: std::map> _animations; static Timeline* _instance; + static bool _validInstance; Timeline() = default; public: diff --git a/engine/physics/RigidBody.cpp b/engine/physics/RigidBody.cpp index 35875a0..fd6f166 100644 --- a/engine/physics/RigidBody.cpp +++ b/engine/physics/RigidBody.cpp @@ -20,7 +20,7 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D& direction) { for(auto& tri : triangles()){ for(int i = 0; i < 3; i++){ - Vec3D point = Vec3D(tri[i]) + position(); + Vec3D point = model()*Vec3D(tri[i]) + position(); double distance = point.dot(direction.normalized()); if(distance > maxDistance) { diff --git a/engine/physics/RigidBody.h b/engine/physics/RigidBody.h index 3524779..30fd731 100644 --- a/engine/physics/RigidBody.h +++ b/engine/physics/RigidBody.h @@ -56,6 +56,7 @@ protected: public: explicit RigidBody(ObjectNameTag nameTag) : Mesh(std::move(nameTag)) {}; + explicit RigidBody(const RigidBody& rigidBody) = default; explicit RigidBody(const Mesh& mesh); RigidBody(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); diff --git a/engine/utils/Time.cpp b/engine/utils/Time.cpp index 24003d7..dc63a05 100644 --- a/engine/utils/Time.cpp +++ b/engine/utils/Time.cpp @@ -8,28 +8,33 @@ using namespace std::chrono; Time* Time::_instance = nullptr; +bool Time::_validInstance = false; void Time::init() { _instance = new Time(); + _validInstance = true; } double Time::time() { - if(!_instance) + if(!_validInstance) { return 0; + } return _instance->_time; } double Time::deltaTime() { - if(!_instance) + if(!_validInstance) { return 0; + } return _instance->_deltaTime; } void Time::update() { - if(!_instance) + if(!_validInstance) { return; + } high_resolution_clock::time_point t = high_resolution_clock::now(); @@ -53,15 +58,14 @@ void Time::update() { } int Time::fps() { - if(!_instance) + if(!_validInstance) { return 0; + } // Cast is faster than floor and has the same behavior for positive numbers return static_cast(_instance->_lastFps); } void Time::free() { - Time* t = _instance; - + _validInstance = false; delete _instance; - _instance = nullptr; } diff --git a/engine/utils/Time.h b/engine/utils/Time.h index f9624fd..f2eb5ef 100644 --- a/engine/utils/Time.h +++ b/engine/utils/Time.h @@ -24,6 +24,7 @@ private: double _deltaTime = 0; static Time* _instance; + static bool _validInstance; Time() = default; diff --git a/weapon/Weapon.cpp b/weapon/Weapon.cpp index 6096610..567e530 100644 --- a/weapon/Weapon.cpp +++ b/weapon/Weapon.cpp @@ -82,7 +82,7 @@ std::map Weapon::addTrace(std::function