Huge refactoring v3

master
Vectozavr 2021-10-30 03:44:37 +07:00
parent 65f2c63643
commit 8f03c3226d
26 changed files with 201 additions and 161 deletions

View File

@ -10,9 +10,8 @@ Player::Player(ObjectNameTag name) : RigidBody(name) {
loadObj(ShooterConsts::CUBE_OBJ, Vec3D{0.5, 1.9, 0.5}); loadObj(ShooterConsts::CUBE_OBJ, Vec3D{0.5, 1.9, 0.5});
setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0}); setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0});
setCollision(true); setCollision(true);
setVisible(false); //setVisible(false);
//setColor({240, 168, 168});
Vec3D randColor = Vec3D::Random(); Vec3D randColor = Vec3D::Random();
setColor({static_cast<sf::Uint8>(randColor.x()*255), static_cast<sf::Uint8>(randColor.y()*255), static_cast<sf::Uint8>(randColor.z()*255)}); setColor({static_cast<sf::Uint8>(randColor.x()*255), static_cast<sf::Uint8>(randColor.y()*255), static_cast<sf::Uint8>(randColor.z()*255)});
@ -73,7 +72,7 @@ void Player::addWeapon(std::shared_ptr<Weapon> weapon) {
attach(weapon); attach(weapon);
_weapons.back()->translate(position()); _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()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, left(), headAngle());
_weapons.back()->setAddTraceCallBack(_addTraceCallBack); _weapons.back()->setAddTraceCallBack(_addTraceCallBack);

View File

@ -89,11 +89,11 @@ void Shooter::start() {
player->initWeapons(); player->initWeapons();
player->translate(Vec3D{0, 0, 0});
camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0}); camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0});
player->attach(camera); player->attach(camera);
world->addBody(player); world->addBody(player);
player->translate(Vec3D{0, 10, 0});
// connecting to the server // connecting to the server
InitNetwork(); InitNetwork();

View File

@ -6,6 +6,7 @@
using namespace std; using namespace std;
int main() { int main() {
Shooter game; Shooter game;

View File

@ -126,30 +126,3 @@ void Camera::clear() {
_triangles.clear(); _triangles.clear();
_V = Matrix4x4::View(left(), up(), lookAt(), position()); _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;
}

View File

@ -38,8 +38,6 @@ public:
[[nodiscard]] int buffSize() const { return _triangles.size(); } [[nodiscard]] int buffSize() const { return _triangles.size(); }
std::vector<std::shared_ptr<Triangle>> sorted(); std::vector<std::shared_ptr<Triangle>> sorted();
// OpenGL function
[[nodiscard]] GLfloat* view() const;
}; };

View File

@ -43,12 +43,14 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name,
world->update(); world->update();
if(_useOpenGL) { if(_useOpenGL) {
GLfloat* view = camera->view(); GLfloat* view = camera->glView();
for(auto & it : *world) { for(auto & it : *world) {
if (it.second->isVisible()) { if (it.second->isVisible()) {
GLfloat* model = it.second->glModel();
GLfloat* geometry = Screen::glMeshToGLfloatArray(it.second, camera->position()); 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(geometry);
free(model);
} }
} }
free(view); free(view);

View File

@ -85,9 +85,13 @@ Matrix4x4 Matrix4x4::Translation(const Vec3D& v) {
t._arr[2][2] = 1.0; t._arr[2][2] = 1.0;
t._arr[3][3] = 1.0; t._arr[3][3] = 1.0;
t._arr[0][3] = v.x(); //t._arr[0][3] = v.x();
t._arr[1][3] = v.y(); //t._arr[1][3] = v.y();
t._arr[2][3] = v.z(); //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; return t;
} }
@ -210,3 +214,15 @@ Matrix4x4 Matrix4x4::View(const Vec3D &left, const Vec3D &up, const Vec3D &lookA
return V; 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]);
}

View File

@ -21,6 +21,10 @@ public:
[[nodiscard]] Vec4D operator*(const Vec4D& point4D) const; [[nodiscard]] Vec4D operator*(const Vec4D& point4D) const;
[[nodiscard]] Vec3D operator*(const Vec3D& vec) 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) // Any useful matrix (static methods)
Matrix4x4 static Identity(); Matrix4x4 static Identity();
Matrix4x4 static Zero(); Matrix4x4 static Zero();

View File

@ -29,42 +29,17 @@ void Mesh::loadObj(const std::string& filename, const Vec3D& scale) {
this->scale(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); loadObj(filename, scale);
} }
Mesh::Mesh(ObjectNameTag nameTag, const vector<Triangle> &tries) : Object(nameTag), _tris(tries) { Mesh::Mesh(ObjectNameTag nameTag, const vector<Triangle> &tries) : Object(std::move(nameTag)), _tris(tries) {
} }
Mesh Mesh::Obj(ObjectNameTag nameTag, const std::string& filename) { Mesh Mesh::Obj(ObjectNameTag nameTag, const std::string& filename) {
return Mesh(std::move(nameTag), 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) { void Mesh::setColor(const sf::Color& c) {
_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 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 v1 = (to - from).normalized();
Vec3D v2 = from.cross(from + Vec3D{1, 0, 0}).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; return line;
} }
Mesh::Mesh(const Mesh &mesh) : Object(mesh.name()), _tris(mesh._tris), _color(mesh._color), _visible(mesh._visible) {
}
void Mesh::setTriangles(const vector<Triangle> &t) { void Mesh::setTriangles(const vector<Triangle> &t) {
_tris.clear(); _tris.clear();
for (auto & tri : t) { for (auto & tri : t) {

View File

@ -21,7 +21,7 @@ private:
public: public:
explicit Mesh(ObjectNameTag nameTag) : Object(std::move(nameTag)) {}; explicit Mesh(ObjectNameTag nameTag) : Object(std::move(nameTag)) {};
Mesh& operator=(const Mesh& mesh) = delete; Mesh& operator=(const Mesh& mesh) = delete;
Mesh(const Mesh& mesh); Mesh(const Mesh& mesh) = default;
explicit Mesh(ObjectNameTag nameTag, const std::vector<Triangle>& tries); explicit Mesh(ObjectNameTag nameTag, const std::vector<Triangle>& tries);
explicit Mesh(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); explicit Mesh(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});
@ -32,16 +32,6 @@ public:
[[nodiscard]] std::vector<Triangle>& triangles() { return _tris; } [[nodiscard]] std::vector<Triangle>& triangles() { return _tris; }
void setTriangles(const std::vector<Triangle>& t); void setTriangles(const std::vector<Triangle>& 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]] int size() const { return _tris.size()*3; }
[[nodiscard]] sf::Color color() const { return _color; } [[nodiscard]] sf::Color color() const { return _color; }

View File

@ -7,6 +7,7 @@
#include "utils/Log.h" #include "utils/Log.h"
void Object::translate(const Vec3D &dv) { void Object::translate(const Vec3D &dv) {
_position = _position + dv; _position = _position + dv;
for(auto &[attachedName, attachedObject] : _attachedObjects) { for(auto &[attachedName, attachedObject] : _attachedObjects) {
@ -17,6 +18,9 @@ void Object::translate(const Vec3D &dv) {
} }
void Object::scale(const Vec3D &s) { void Object::scale(const Vec3D &s) {
_transformMatrix = Matrix4x4::Scale(s)*_transformMatrix;
for(auto &[attachedName, attachedObject] : _attachedObjects) { for(auto &[attachedName, attachedObject] : _attachedObjects) {
if(!attachedObject.expired()) { if(!attachedObject.expired()) {
attachedObject.lock()->scale(s); 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()); Matrix4x4 rotationMatrix = Matrix4x4::RotationZ(r.z())*Matrix4x4::RotationY(r.y())*Matrix4x4::RotationX(r.z());
_left = rotationMatrix * _left; _transformMatrix = rotationMatrix*_transformMatrix;
_up = rotationMatrix * _up;
_lookAt = rotationMatrix * _lookAt;
for(auto &[attachedName, attachedObject] : _attachedObjects) { for(auto &[attachedName, attachedObject] : _attachedObjects) {
if(!attachedObject.expired()) { if(!attachedObject.expired()) {
@ -43,9 +45,7 @@ void Object::rotate(const Vec3D &r) {
void Object::rotate(const Vec3D &v, double rv) { void Object::rotate(const Vec3D &v, double rv) {
Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, rv); Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, rv);
_left = rotationMatrix * _left; _transformMatrix = rotationMatrix*_transformMatrix;
_up = rotationMatrix * _up;
_lookAt = rotationMatrix * _lookAt;
for(auto &[attachedName, attachedObject] : _attachedObjects) { for(auto &[attachedName, attachedObject] : _attachedObjects) {
if(!attachedObject.expired()) { if(!attachedObject.expired()) {
@ -58,15 +58,13 @@ void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &r) {
_angle = _angle + r; _angle = _angle + r;
// Translate XYZ by vector r1 // Translate XYZ by vector r1
Vec3D r1(_position - s); Vec3D r1(position() - s);
// In translated coordinate system we rotate body and position // In translated coordinate system we rotate body and position
Matrix4x4 rotationMatrix = Matrix4x4::Rotation(r); Matrix4x4 rotationMatrix = Matrix4x4::Rotation(r);
Vec3D r2(rotationMatrix*r1); Vec3D r2(rotationMatrix*r1);
_left = rotationMatrix * _left; _transformMatrix = rotationMatrix*_transformMatrix;
_up = rotationMatrix * _up;
_lookAt = rotationMatrix * _lookAt;
// After rotation we translate XYZ by vector -r2 and recalculate position // After rotation we translate XYZ by vector -r2 and recalculate position
_position = s + r2; _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) { void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r) {
// Translate XYZ by vector r1 // Translate XYZ by vector r1
Vec3D r1(_position - s); Vec3D r1(position() - s);
// In translated coordinate system we rotate body and position // In translated coordinate system we rotate body and position
Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, r); Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, r);
Vec3D r2 = rotationMatrix*r1; Vec3D r2 = rotationMatrix*r1;
_left = rotationMatrix * _left; _transformMatrix = rotationMatrix*_transformMatrix;
_up = rotationMatrix * _up;
_lookAt = rotationMatrix * _lookAt;
// After rotation we translate XYZ by vector -r2 and recalculate position // After rotation we translate XYZ by vector -r2 and recalculate position
_position = s + r2; _position = s + r2;
@ -104,7 +100,7 @@ void Object::rotateLeft(double rl) {
_angleLeftUpLookAt.y(), _angleLeftUpLookAt.y(),
_angleLeftUpLookAt.z()}; _angleLeftUpLookAt.z()};
rotate(Vec3D(_left), rl); rotate(left(), rl);
} }
void Object::rotateUp(double ru) { void Object::rotateUp(double ru) {
@ -112,18 +108,18 @@ void Object::rotateUp(double ru) {
_angleLeftUpLookAt.y() + ru, _angleLeftUpLookAt.y() + ru,
_angleLeftUpLookAt.z()}; _angleLeftUpLookAt.z()};
rotate(Vec3D(_up), ru); rotate(up(), ru);
} }
void Object::rotateLookAt(double rlAt) { void Object::rotateLookAt(double rlAt) {
_angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x(), _angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x(),
_angleLeftUpLookAt.y(), _angleLeftUpLookAt.y(),
_angleLeftUpLookAt.z() + rlAt}; _angleLeftUpLookAt.z() + rlAt};
rotate(Vec3D(_lookAt), rlAt); rotate(lookAt(), rlAt);
} }
void Object::translateToPoint(const Vec3D &point) { void Object::translateToPoint(const Vec3D &point) {
translate(point - _position); translate(point - position());
} }
void Object::rotateToAngle(const Vec3D &v) { void Object::rotateToAngle(const Vec3D &v) {
@ -162,6 +158,59 @@ void Object::unattach(const ObjectNameTag& tag) {
_attachedObjects.erase(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() { Object::~Object() {
_attachedObjects.clear(); _attachedObjects.clear();
} }

View File

@ -10,6 +10,8 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <memory> #include <memory>
#include "Matrix4x4.h"
#include <SFML/OpenGL.hpp>
class ObjectNameTag final { class ObjectNameTag final {
private: private:
@ -28,10 +30,7 @@ private:
bool checkIfAttached(Object* obj); bool checkIfAttached(Object* obj);
const ObjectNameTag _nameTag; const ObjectNameTag _nameTag;
protected: Matrix4x4 _transformMatrix = Matrix4x4::Identity();
Vec3D _left {1, 0, 0}; // internal X
Vec3D _up {0, 1, 0}; // internal Y
Vec3D _lookAt {0, 0, 1}; // internal Z
std::map<ObjectNameTag, std::weak_ptr<Object>> _attachedObjects; std::map<ObjectNameTag, std::weak_ptr<Object>> _attachedObjects;
@ -39,33 +38,40 @@ protected:
Vec3D _angle {0, 0, 0}; Vec3D _angle {0, 0, 0};
Vec3D _angleLeftUpLookAt{0, 0, 0}; Vec3D _angleLeftUpLookAt{0, 0, 0};
public: 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); void translate(const Vec3D& dv);
virtual void translateToPoint(const Vec3D& point); void translateToPoint(const Vec3D& point);
virtual void scale(const Vec3D& s); void scale(const Vec3D& s);
virtual void rotate(const Vec3D& r); void rotate(const Vec3D& r);
virtual void rotate(const Vec3D& v, double rv); void rotate(const Vec3D& v, double rv);
virtual void rotateToAngle(const Vec3D& v); void rotateToAngle(const Vec3D& v);
virtual void rotateRelativePoint(const Vec3D& s, const Vec3D& r); void rotateRelativePoint(const Vec3D& s, const Vec3D& r);
virtual void rotateRelativePoint(const Vec3D& s, const Vec3D& v, double r); void rotateRelativePoint(const Vec3D& s, const Vec3D& v, double r);
void rotateLeft(double rl); void rotateLeft(double rl);
void rotateUp(double ru); void rotateUp(double ru);
void rotateLookAt(double rlAt); 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 angle() const { return _angle; }
[[nodiscard]] Vec3D angleLeftUpLookAt() const { return _angleLeftUpLookAt; } [[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> object); void attach(std::shared_ptr<Object> object);
void unattach(const ObjectNameTag& tag); void unattach(const ObjectNameTag& tag);
std::shared_ptr<Object> attached(const ObjectNameTag& tag); std::shared_ptr<Object> 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(); virtual ~Object();
}; };

View File

@ -10,9 +10,12 @@
#include <fstream> #include <fstream>
ResourceManager* ResourceManager::_instance = nullptr; ResourceManager* ResourceManager::_instance = nullptr;
bool ResourceManager::_validInstance = false;
void ResourceManager::init() { void ResourceManager::init() {
_instance = new ResourceManager(); _instance = new ResourceManager();
_validInstance = true;
} }
void ResourceManager::unloadTextures() { void ResourceManager::unloadTextures() {
@ -41,6 +44,10 @@ void ResourceManager::unloadObjects() {
} }
void ResourceManager::unloadAllResources() { void ResourceManager::unloadAllResources() {
if(!_validInstance) {
return;
}
unloadTextures(); unloadTextures();
unloadSoundBuffers(); unloadSoundBuffers();
unloadFonts(); unloadFonts();
@ -49,12 +56,12 @@ void ResourceManager::unloadAllResources() {
void ResourceManager::free() { void ResourceManager::free() {
unloadAllResources(); unloadAllResources();
_validInstance = false;
delete _instance; delete _instance;
_instance = nullptr;
} }
std::shared_ptr<sf::Texture> ResourceManager::loadTexture(const std::string& filename) { std::shared_ptr<sf::Texture> ResourceManager::loadTexture(const std::string& filename) {
if(!_instance) { if(!_validInstance) {
return nullptr; return nullptr;
} }
@ -78,7 +85,7 @@ std::shared_ptr<sf::Texture> ResourceManager::loadTexture(const std::string& fil
} }
std::shared_ptr<sf::SoundBuffer> ResourceManager::loadSoundBuffer(const std::string& filename) { std::shared_ptr<sf::SoundBuffer> ResourceManager::loadSoundBuffer(const std::string& filename) {
if(!_instance) { if(!_validInstance) {
return nullptr; return nullptr;
} }
@ -101,7 +108,7 @@ std::shared_ptr<sf::SoundBuffer> ResourceManager::loadSoundBuffer(const std::str
} }
std::shared_ptr<sf::Font> ResourceManager::loadFont(const std::string& filename) { std::shared_ptr<sf::Font> ResourceManager::loadFont(const std::string& filename) {
if(!_instance) { if(!_validInstance) {
return nullptr; return nullptr;
} }
@ -124,9 +131,14 @@ std::shared_ptr<sf::Font> ResourceManager::loadFont(const std::string& filename)
} }
std::vector<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::string &filename) { std::vector<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::string &filename) {
std::vector<std::shared_ptr<Mesh>> objects{}; std::vector<std::shared_ptr<Mesh>> objects{};
std::map<std::string, sf::Color> maters{}; std::map<std::string, sf::Color> maters{};
if(!_validInstance) {
return objects;
}
// If objects is already loaded - return pointer to it // If objects is already loaded - return pointer to it
auto it = _instance->_objects.find(filename); auto it = _instance->_objects.find(filename);
if (it != _instance->_objects.end()) { if (it != _instance->_objects.end()) {

View File

@ -18,18 +18,19 @@ private:
std::map<std::string, std::vector<std::shared_ptr<Mesh>>> _objects; std::map<std::string, std::vector<std::shared_ptr<Mesh>>> _objects;
static ResourceManager* _instance; static ResourceManager* _instance;
static bool _validInstance;
ResourceManager() = default; ResourceManager() = default;
public:
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(ResourceManager&) = delete;
// Unloads all currently loaded textures. // Unloads all currently loaded textures.
static void unloadObjects(); static void unloadObjects();
static void unloadTextures(); static void unloadTextures();
static void unloadSoundBuffers(); static void unloadSoundBuffers();
static void unloadFonts(); static void unloadFonts();
public:
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(ResourceManager&) = delete;
static void unloadAllResources(); static void unloadAllResources();
static void init(); static void init();

View File

@ -135,7 +135,7 @@ void Screen::drawText(const sf::Text &text) {
} }
// OpenGL functions // 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: // OpenGL:
// Make the window the active window for OpenGL calls // Make the window the active window for OpenGL calls
_window->setActive(true); _window->setActive(true);
@ -180,6 +180,7 @@ void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, size_t count) {
glLoadIdentity(); glLoadIdentity();
glLoadMatrixf(view); glLoadMatrixf(view);
glMultMatrixf(model);
// Draw the mesh // Draw the mesh
glDrawArrays(GL_TRIANGLES, 0, count); glDrawArrays(GL_TRIANGLES, 0, count);
@ -195,9 +196,10 @@ GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D& c
auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat)); auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat));
for(int i = 0; i < triangles.size(); i++) { for(int i = 0; i < triangles.size(); i++) {
int stride = 21*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 color = triangles[i].color();
sf::Color ambientColor = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot) + 0.7)), 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)), (sf::Uint8)(color.g * (0.3 * std::abs(dot) + 0.7)),
@ -205,27 +207,27 @@ GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D& c
(sf::Uint8)color.a); (sf::Uint8)color.a);
geometry[stride + 0] = (GLfloat)triangles[i][0].x() + pos.x(); geometry[stride + 0] = (GLfloat)triangles[i][0].x();
geometry[stride + 1] = (GLfloat)triangles[i][0].y() + pos.y(); geometry[stride + 1] = (GLfloat)triangles[i][0].y();
geometry[stride + 2] = (GLfloat)triangles[i][0].z() + pos.z(); geometry[stride + 2] = (GLfloat)triangles[i][0].z();
geometry[stride + 3] = (GLfloat)ambientColor.r/255.0f; geometry[stride + 3] = (GLfloat)ambientColor.r/255.0f;
geometry[stride + 4] = (GLfloat)ambientColor.g/255.0f; geometry[stride + 4] = (GLfloat)ambientColor.g/255.0f;
geometry[stride + 5] = (GLfloat)ambientColor.b/255.0f; geometry[stride + 5] = (GLfloat)ambientColor.b/255.0f;
geometry[stride + 6] = (GLfloat)ambientColor.a/255.0f; geometry[stride + 6] = (GLfloat)ambientColor.a/255.0f;
geometry[stride + 7] = (GLfloat)triangles[i][1].x() + pos.x(); geometry[stride + 7] = (GLfloat)triangles[i][1].x();
geometry[stride + 8] = (GLfloat)triangles[i][1].y() + pos.y(); geometry[stride + 8] = (GLfloat)triangles[i][1].y();
geometry[stride + 9] = (GLfloat)triangles[i][1].z() + pos.z(); geometry[stride + 9] = (GLfloat)triangles[i][1].z();
geometry[stride + 10] = (GLfloat)ambientColor.r/255.0f; geometry[stride + 10] = (GLfloat)ambientColor.r/255.0f;
geometry[stride + 11] = (GLfloat)ambientColor.g/255.0f; geometry[stride + 11] = (GLfloat)ambientColor.g/255.0f;
geometry[stride + 12] = (GLfloat)ambientColor.b/255.0f; geometry[stride + 12] = (GLfloat)ambientColor.b/255.0f;
geometry[stride + 13] = (GLfloat)ambientColor.a/255.0f; geometry[stride + 13] = (GLfloat)ambientColor.a/255.0f;
geometry[stride + 14] = (GLfloat)triangles[i][2].x() + pos.x(); geometry[stride + 14] = (GLfloat)triangles[i][2].x();
geometry[stride + 15] = (GLfloat)triangles[i][2].y() + pos.y(); geometry[stride + 15] = (GLfloat)triangles[i][2].y();
geometry[stride + 16] = (GLfloat)triangles[i][2].z() + pos.z(); geometry[stride + 16] = (GLfloat)triangles[i][2].z();
geometry[stride + 17] = (GLfloat)ambientColor.r/255.0f; geometry[stride + 17] = (GLfloat)ambientColor.r/255.0f;
geometry[stride + 18] = (GLfloat)ambientColor.g/255.0f; geometry[stride + 18] = (GLfloat)ambientColor.g/255.0f;

View File

@ -53,7 +53,7 @@ public:
void setMouseCursorVisible(bool visible); void setMouseCursorVisible(bool visible);
// OpenGL functions // 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> mesh, const Vec3D& cameraPosition); static GLfloat* glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D& cameraPosition);
[[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; } [[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; }

View File

@ -6,13 +6,16 @@
#include "ResourceManager.h" #include "ResourceManager.h"
SoundController* SoundController::_instance = nullptr; SoundController* SoundController::_instance = nullptr;
bool SoundController::_validInstance = false;
void SoundController::init() { void SoundController::init() {
_instance = new SoundController(); _instance = new SoundController();
_validInstance = true;
} }
void SoundController::playSound(const SoundTag& soundTag, const std::string& filename) { void SoundController::playSound(const SoundTag& soundTag, const std::string& filename) {
if(!_instance) { if(!_validInstance) {
return; return;
} }
@ -22,7 +25,7 @@ void SoundController::playSound(const SoundTag& soundTag, const std::string& fil
} }
void SoundController::pauseSound(const SoundTag& soundTag) { void SoundController::pauseSound(const SoundTag& soundTag) {
if(!_instance) { if(!_validInstance) {
return; return;
} }
@ -32,7 +35,7 @@ void SoundController::pauseSound(const SoundTag& soundTag) {
} }
void SoundController::stopSound(const SoundTag& soundTag) { void SoundController::stopSound(const SoundTag& soundTag) {
if(!_instance) { if(!_validInstance) {
return; return;
} }
@ -43,7 +46,7 @@ void SoundController::stopSound(const SoundTag& soundTag) {
} }
sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) { sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) {
if(_instance == nullptr) { if(!_validInstance) {
return sf::Sound::Status::Stopped; return sf::Sound::Status::Stopped;
} }
@ -56,11 +59,13 @@ sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) {
} }
void SoundController::free() { void SoundController::free() {
for(auto& [soundTag, sound] : _instance->_sounds) { if(_validInstance) {
sound.stop(); for(auto& [soundTag, sound] : _instance->_sounds) {
sound.stop();
}
} }
_instance->_sounds.clear(); _instance->_sounds.clear();
_validInstance = false;
delete _instance; delete _instance;
_instance = nullptr;
} }

View File

@ -26,6 +26,7 @@ private:
std::map<SoundTag, sf::Sound> _sounds; std::map<SoundTag, sf::Sound> _sounds;
static SoundController* _instance; static SoundController* _instance;
static bool _validInstance;
SoundController() = default; SoundController() = default;
public: public:

View File

@ -44,6 +44,7 @@ IntersectionInformation World::rayCast(const Vec3D& from, const Vec3D& to, const
for (auto& escapeTag : tagsToSkip) { for (auto& escapeTag : tagsToSkip) {
if (name.str().find(escapeTag) != std::string::npos) { if (name.str().find(escapeTag) != std::string::npos) {
escapeThisBody = true; escapeThisBody = true;
break;
} }
} }
if(escapeThisBody) { if(escapeThisBody) {
@ -51,7 +52,7 @@ IntersectionInformation World::rayCast(const Vec3D& from, const Vec3D& to, const
} }
for(auto& tri : body->triangles()) { 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); Plane plane(tri_translated);
auto intersection = plane.intersection(from, to); auto intersection = plane.intersection(from, to);

View File

@ -7,13 +7,15 @@
#include "Timeline.h" #include "Timeline.h"
Timeline* Timeline::_instance = nullptr; Timeline* Timeline::_instance = nullptr;
bool Timeline::_validInstance = false;
void Timeline::init() { void Timeline::init() {
_instance = new Timeline(); _instance = new Timeline();
_validInstance = true;
} }
void Timeline::animate(const AnimationListTag& listName, Animation* anim) { void Timeline::animate(const AnimationListTag& listName, Animation* anim) {
if(!_instance) { if(!_validInstance) {
return; return;
} }
@ -21,7 +23,7 @@ void Timeline::animate(const AnimationListTag& listName, Animation* anim) {
} }
void Timeline::deleteAllAnimations() { void Timeline::deleteAllAnimations() {
if(!_instance) { if(!_validInstance) {
return; return;
} }
@ -36,7 +38,7 @@ void Timeline::deleteAllAnimations() {
} }
void Timeline::deleteAnimationList(const AnimationListTag& listName) { void Timeline::deleteAnimationList(const AnimationListTag& listName) {
if(!_instance) { if(!_validInstance) {
return; return;
} }
@ -45,7 +47,7 @@ void Timeline::deleteAnimationList(const AnimationListTag& listName) {
} }
[[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag& listName) { [[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag& listName) {
if(!_instance) { if(!_validInstance) {
return false; return false;
} }
@ -53,7 +55,7 @@ void Timeline::deleteAnimationList(const AnimationListTag& listName) {
} }
void Timeline::update() { void Timeline::update() {
if(!_instance) { if(!_validInstance) {
return; return;
} }
@ -85,7 +87,7 @@ void Timeline::update() {
void Timeline::free() { void Timeline::free() {
Timeline::deleteAllAnimations(); Timeline::deleteAllAnimations();
_validInstance = false;
delete _instance; delete _instance;
_instance = nullptr;
} }

View File

@ -25,6 +25,7 @@ private:
std::map<AnimationListTag, std::list<Animation*>> _animations; std::map<AnimationListTag, std::list<Animation*>> _animations;
static Timeline* _instance; static Timeline* _instance;
static bool _validInstance;
Timeline() = default; Timeline() = default;
public: public:

View File

@ -20,7 +20,7 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D& direction) {
for(auto& tri : triangles()){ for(auto& tri : triangles()){
for(int i = 0; i < 3; i++){ 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()); double distance = point.dot(direction.normalized());
if(distance > maxDistance) { if(distance > maxDistance) {

View File

@ -56,6 +56,7 @@ protected:
public: public:
explicit RigidBody(ObjectNameTag nameTag) : Mesh(std::move(nameTag)) {}; explicit RigidBody(ObjectNameTag nameTag) : Mesh(std::move(nameTag)) {};
explicit RigidBody(const RigidBody& rigidBody) = default;
explicit RigidBody(const Mesh& mesh); explicit RigidBody(const Mesh& mesh);
RigidBody(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); RigidBody(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});

View File

@ -8,28 +8,33 @@
using namespace std::chrono; using namespace std::chrono;
Time* Time::_instance = nullptr; Time* Time::_instance = nullptr;
bool Time::_validInstance = false;
void Time::init() { void Time::init() {
_instance = new Time(); _instance = new Time();
_validInstance = true;
} }
double Time::time() { double Time::time() {
if(!_instance) if(!_validInstance) {
return 0; return 0;
}
return _instance->_time; return _instance->_time;
} }
double Time::deltaTime() { double Time::deltaTime() {
if(!_instance) if(!_validInstance) {
return 0; return 0;
}
return _instance->_deltaTime; return _instance->_deltaTime;
} }
void Time::update() { void Time::update() {
if(!_instance) if(!_validInstance) {
return; return;
}
high_resolution_clock::time_point t = high_resolution_clock::now(); high_resolution_clock::time_point t = high_resolution_clock::now();
@ -53,15 +58,14 @@ void Time::update() {
} }
int Time::fps() { int Time::fps() {
if(!_instance) if(!_validInstance) {
return 0; return 0;
}
// Cast is faster than floor and has the same behavior for positive numbers // Cast is faster than floor and has the same behavior for positive numbers
return static_cast<int>(_instance->_lastFps); return static_cast<int>(_instance->_lastFps);
} }
void Time::free() { void Time::free() {
Time* t = _instance; _validInstance = false;
delete _instance; delete _instance;
_instance = nullptr;
} }

View File

@ -24,6 +24,7 @@ private:
double _deltaTime = 0; double _deltaTime = 0;
static Time* _instance; static Time* _instance;
static bool _validInstance;
Time() = default; Time() = default;

View File

@ -82,7 +82,7 @@ std::map<ObjectNameTag, double> Weapon::addTrace(std::function<IntersectionInfor
} }
// add trace line // add trace line
Vec3D lineFrom = position() + Vec3D(triangles().back()[0]); Vec3D lineFrom = position() + model()*Vec3D(triangles().back()[0]);
Vec3D lineTo = rayCast.intersected ? rayCast.pointOfIntersection : position() + -lookAt() * ShooterConsts::FIRE_DISTANCE + randV; Vec3D lineTo = rayCast.intersected ? rayCast.pointOfIntersection : position() + -lookAt() * ShooterConsts::FIRE_DISTANCE + randV;
_addTraceCallBack(lineFrom, lineTo); _addTraceCallBack(lineFrom, lineTo);