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});
setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0});
setCollision(true);
setVisible(false);
//setVisible(false);
//setColor({240, 168, 168});
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)});
@ -73,7 +72,7 @@ void Player::addWeapon(std::shared_ptr<Weapon> 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);

View File

@ -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();

View File

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

View File

@ -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;
}

View File

@ -38,8 +38,6 @@ public:
[[nodiscard]] int buffSize() const { return _triangles.size(); }
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();
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);

View File

@ -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]);
}

View File

@ -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();

View File

@ -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<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) {
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<Triangle> &t) {
_tris.clear();
for (auto & tri : t) {

View File

@ -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<Triangle>& tries);
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; }
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]] sf::Color color() const { return _color; }

View File

@ -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();
}

View File

@ -10,6 +10,8 @@
#include <string>
#include <utility>
#include <memory>
#include "Matrix4x4.h"
#include <SFML/OpenGL.hpp>
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<ObjectNameTag, std::weak_ptr<Object>> _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 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> object);
void unattach(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();
};

View File

@ -10,9 +10,12 @@
#include <fstream>
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<sf::Texture> ResourceManager::loadTexture(const std::string& filename) {
if(!_instance) {
if(!_validInstance) {
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) {
if(!_instance) {
if(!_validInstance) {
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) {
if(!_instance) {
if(!_validInstance) {
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>> objects{};
std::map<std::string, sf::Color> 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()) {

View File

@ -18,18 +18,19 @@ private:
std::map<std::string, std::vector<std::shared_ptr<Mesh>>> _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();

View File

@ -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> 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> 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;

View File

@ -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> mesh, const Vec3D& cameraPosition);
[[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; }

View File

@ -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() {
if(_validInstance) {
for(auto& [soundTag, sound] : _instance->_sounds) {
sound.stop();
}
}
_instance->_sounds.clear();
_validInstance = false;
delete _instance;
_instance = nullptr;
}

View File

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

View File

@ -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);

View File

@ -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;
}

View File

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

View File

@ -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) {

View File

@ -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});

View File

@ -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<int>(_instance->_lastFps);
}
void Time::free() {
Time* t = _instance;
_validInstance = false;
delete _instance;
_instance = nullptr;
}

View File

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

View File

@ -82,7 +82,7 @@ std::map<ObjectNameTag, double> Weapon::addTrace(std::function<IntersectionInfor
}
// 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;
_addTraceCallBack(lineFrom, lineTo);