2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 15.03.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ENGINE_OBJECT_H
|
|
|
|
#define ENGINE_OBJECT_H
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
#include <map>
|
2021-10-09 13:41:12 +03:00
|
|
|
#include <string>
|
2021-10-17 10:21:10 +03:00
|
|
|
#include <utility>
|
2021-10-23 15:02:25 +03:00
|
|
|
#include <memory>
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2022-07-11 16:58:05 +03:00
|
|
|
#include "math/Vec3D.h"
|
|
|
|
#include "math/Matrix4x4.h"
|
2021-10-29 23:44:37 +03:00
|
|
|
#include <SFML/OpenGL.hpp>
|
2021-10-17 10:21:10 +03:00
|
|
|
|
2021-10-17 11:41:58 +03:00
|
|
|
class ObjectNameTag final {
|
2021-10-17 10:21:10 +03:00
|
|
|
private:
|
|
|
|
const std::string _name;
|
|
|
|
public:
|
|
|
|
explicit ObjectNameTag(std::string name = "") : _name(std::move(name)) {}
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
[[nodiscard]] std::string str() const { return _name; }
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
bool operator==(const ObjectNameTag &tag) const { return _name == tag._name; }
|
|
|
|
bool operator!=(const ObjectNameTag &tag) const { return _name != tag._name; }
|
|
|
|
bool operator<(const ObjectNameTag &tag) const { return _name < tag._name; }
|
2021-11-09 22:54:20 +03:00
|
|
|
|
|
|
|
[[nodiscard]] bool contains(const ObjectNameTag& nameTag) const;
|
2021-10-17 10:21:10 +03:00
|
|
|
};
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-02 21:17:03 +03:00
|
|
|
class Object {
|
2021-10-28 16:58:02 +03:00
|
|
|
private:
|
2021-10-31 11:39:08 +03:00
|
|
|
bool checkIfAttached(Object *obj);
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
const ObjectNameTag _nameTag;
|
|
|
|
|
2021-10-29 23:44:37 +03:00
|
|
|
Matrix4x4 _transformMatrix = Matrix4x4::Identity();
|
2021-10-31 11:39:08 +03:00
|
|
|
Vec3D _position{0, 0, 0};
|
2021-10-31 11:47:00 +03:00
|
|
|
/*
|
|
|
|
* Take into account when you rotate body,
|
2021-10-31 12:01:31 +03:00
|
|
|
* you change '_angle' & '_angleLeftUpLookAt' only for this particular body,
|
2021-10-31 11:47:00 +03:00
|
|
|
* but not for attached objects! This way during rotation
|
2021-10-31 12:01:31 +03:00
|
|
|
* '_angle' & '_angleLeftUpLookAt' stays constant all attached objects.
|
2021-10-31 11:47:00 +03:00
|
|
|
*/
|
2021-10-31 11:39:08 +03:00
|
|
|
Vec3D _angle{0, 0, 0};
|
2021-10-28 16:58:02 +03:00
|
|
|
Vec3D _angleLeftUpLookAt{0, 0, 0};
|
2021-11-10 14:14:10 +03:00
|
|
|
|
|
|
|
std::map<ObjectNameTag, std::weak_ptr<Object>> _attachedObjects;
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
public:
|
2021-10-29 23:44:37 +03:00
|
|
|
explicit Object(ObjectNameTag nameTag) : _nameTag(std::move(nameTag)) {};
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-11-10 14:14:10 +03:00
|
|
|
Object(const Object &object) : _nameTag(object._nameTag),
|
|
|
|
_transformMatrix(object._transformMatrix),
|
|
|
|
_position(object._position),
|
|
|
|
_angle(object._angle),
|
|
|
|
_angleLeftUpLookAt(object._angleLeftUpLookAt) {};
|
2021-10-30 21:29:42 +03:00
|
|
|
|
|
|
|
// TODO: implement rotations using quaternions (?)
|
2021-10-31 11:39:08 +03:00
|
|
|
void transform(const Matrix4x4 &t);
|
|
|
|
void transformRelativePoint(const Vec3D &point, const Matrix4x4 &transform);
|
|
|
|
void translate(const Vec3D &dv);
|
|
|
|
void translateToPoint(const Vec3D &point);
|
2021-10-31 22:11:04 +03:00
|
|
|
void attractToPoint(const Vec3D &point, double value);
|
2021-10-31 11:39:08 +03:00
|
|
|
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);
|
2021-10-18 18:30:02 +03:00
|
|
|
void rotateLeft(double rl);
|
|
|
|
void rotateUp(double ru);
|
|
|
|
void rotateLookAt(double rlAt);
|
2021-10-12 17:12:47 +03:00
|
|
|
|
2022-02-23 17:29:42 +03:00
|
|
|
[[nodiscard]] Vec3D left() const { return _transformMatrix.x().normalized(); }
|
|
|
|
[[nodiscard]] Vec3D up() const { return _transformMatrix.y().normalized(); }
|
|
|
|
[[nodiscard]] Vec3D lookAt() const { return _transformMatrix.z().normalized(); }
|
2021-10-31 11:39:08 +03:00
|
|
|
[[nodiscard]] Vec3D position() const { return _position; }
|
2021-10-28 16:58:02 +03:00
|
|
|
[[nodiscard]] Vec3D angle() const { return _angle; }
|
|
|
|
[[nodiscard]] Vec3D angleLeftUpLookAt() const { return _angleLeftUpLookAt; }
|
2021-10-12 17:12:47 +03:00
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
void attach(std::shared_ptr<Object> object);
|
2021-10-31 11:39:08 +03:00
|
|
|
void unattach(const ObjectNameTag &tag);
|
|
|
|
std::shared_ptr<Object> attached(const ObjectNameTag &tag);
|
2021-10-16 20:22:55 +03:00
|
|
|
|
2021-10-29 23:44:37 +03:00
|
|
|
[[nodiscard]] ObjectNameTag name() const { return _nameTag; }
|
|
|
|
|
2021-10-30 12:13:22 +03:00
|
|
|
[[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; }
|
2022-07-07 13:14:00 +03:00
|
|
|
[[nodiscard]] Matrix4x4 invModel() const { return Matrix4x4::View(model()); }
|
2021-11-03 16:21:39 +03:00
|
|
|
|
2021-10-29 23:44:37 +03:00
|
|
|
// OpenGL function
|
2021-10-31 11:39:08 +03:00
|
|
|
[[nodiscard]] GLfloat *glModel() const;
|
2021-11-05 23:14:12 +03:00
|
|
|
[[nodiscard]] GLfloat *glInvModel() const;
|
2021-10-28 16:58:02 +03:00
|
|
|
|
2021-10-16 20:22:55 +03:00
|
|
|
virtual ~Object();
|
2021-10-02 20:36:07 +03:00
|
|
|
};
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
#endif //MINECRAFT_3DZAVR_OBJECT_H
|