Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
SceneObject.h
Go to the documentation of this file.
1#pragma once
2#include <glm/glm.hpp>
3#include <glm/gtc/quaternion.hpp>
4#include <glm/gtc/matrix_transform.hpp>
5#include <utility>
7#include "IDrawable.h"
9
10#include "SceneManager.h"
11#include "IPickable.h"
14
15class Scene;
16class SceneManager;
17namespace Physics {
18 class RigidBody;
19}
20
21// TODO: put setters and getter definitions in the header files
22class SceneObject : public QObject, public IInstancedDrawable, public IPickable{
23 Q_OBJECT
24public:
25 SceneObject(SceneManager* sceneManager, const std::string &meshName, Shader *sdr, const CreationOptions &options, QObject* parent = nullptr);
27
28 void setPosition(const glm::vec3& pos);
29 void setRotation(const glm::vec3& rot);
30 void setRotationQuat(const glm::quat& q);
31 void setScale(const glm::vec3& scl);
32 glm::vec3 getPosition() const;
33 glm::vec3 getRotation() const;
34 glm::quat getRotationQuat() const;
35 glm::vec3 getScale() const;
36
37 Shader* getShader() const override;
39 Physics::PhysicsBody* getPhysicsBody() const { return physicsBody.get(); }
40 std::optional<float> intersectsRay(const Math::Ray& ray) const override;
41 glm::mat4 getModelMatrix() const override;
42
43 void handleClick(const Math::Ray& ray, float distance) override;
44 void setHovered(bool hovered) override;
45 bool getHovered() const override;
46 Mesh* getMesh() const override { return mesh; }
47 uint32_t getObjectID() const override;
48 const std::string& getName() const { return objectName; }
49 const std::string& getMeshName() const { return meshName; }
50 void setName(std::string name) { objectName = std::move(name); }
51 CreationOptions getCreationOptions() const { return creationOptions; }
52
53 using PosMap = std::unordered_map<Physics::PhysicsBody*, glm::vec3>;
54
55 static void setPhysicsPosMap(const PosMap& m) {
56 std::lock_guard<std::mutex> lk(posMapMutex);
57 posMap = m;
58 }
59
60 static void setRenderOrigin(const glm::vec3& origin) {
61 std::lock_guard<std::mutex> lk(posMapMutex);
62 renderOrigin = origin;
63 }
64 static glm::vec3 getRenderOrigin() {
65 std::lock_guard<std::mutex> lk(posMapMutex);
66 return renderOrigin;
67 }
68 static glm::mat4 worldToRenderMatrix() {
69 std::lock_guard<std::mutex> lk(posMapMutex);
70 return glm::translate(glm::mat4(1.0f), -renderOrigin);
71 }
72
73private:
74 inline static std::mutex posMapMutex;
75 inline static PosMap posMap{};
76 inline static glm::vec3 renderOrigin{0.0f};
77
78 QObject* parent;
79 Mesh* mesh;
80 std::string meshName;
81 Shader* shader;
82 Scene* ownerScene;
83 SceneManager* sceneManager;
84 std::unique_ptr<Physics::PhysicsBody> physicsBody = nullptr;
85
86 CreationOptions creationOptions;
87 glm::vec3 position {0.0f};
88 glm::vec3 rotation {0.0f};
89 glm::quat orientation {1.0f, 0.0f, 0.0f, 0.0f};
90 glm::vec3 scale {1.0f};
91
92 bool isHovered = false;
93
94 uint32_t objectID;
95 std::string objectName;
96
97 glm::mat4 getRenderModelMatrix() const;
98 glm::mat4 buildModelMatrix(bool relativeToRenderOrigin) const;
99
100 std::optional<float> intersectsAABB(const Math::Ray& ray) const;
101 std::optional<float> intersectsMesh(const Math::Ray& ray) const;
102};
Core interfaces for the rendering system supporting both instanced and custom drawing.
Interface for objects that support mouse-based selection and interaction.
std::variant< ObjectOptions, PointMassOptions, RigidBodyOptions > CreationOptions
Interface for objects that support GPU instanced rendering.
Definition IDrawable.h:111
Abstract interface for objects that can be selected and interacted with using the mouse.
Definition IPickable.h:28
GPU mesh representation with support for instanced rendering.
Definition Mesh.h:58
void setHovered(bool hovered) override
Sets the hover state of this object.
static glm::mat4 worldToRenderMatrix()
Definition SceneObject.h:68
glm::mat4 getModelMatrix() const override
Gets the model transformation matrix for this instance.
CreationOptions getCreationOptions() const
Definition SceneObject.h:51
glm::vec3 getRotation() const
void setScale(const glm::vec3 &scl)
glm::vec3 getPosition() const
glm::vec3 getScale() const
static glm::vec3 getRenderOrigin()
Definition SceneObject.h:64
void handleClick(const Math::Ray &ray, float distance) override
Handles a mouse click on this object.
Rendering::InstanceData getInstanceData() const override
Gets the complete per-instance data for GPU upload.
void setRotation(const glm::vec3 &rot)
void setRotationQuat(const glm::quat &q)
const std::string & getMeshName() const
Definition SceneObject.h:49
std::unordered_map< Physics::PhysicsBody *, glm::vec3 > PosMap
Definition SceneObject.h:53
glm::quat getRotationQuat() const
Shader * getShader() const override
Gets the shader used to render this object.
Physics::PhysicsBody * getPhysicsBody() const
Definition SceneObject.h:39
const std::string & getName() const
Definition SceneObject.h:48
static void setPhysicsPosMap(const PosMap &m)
Definition SceneObject.h:55
static void setRenderOrigin(const glm::vec3 &origin)
Definition SceneObject.h:60
uint32_t getObjectID() const override
Gets the unique identifier for this object.
void setName(std::string name)
Definition SceneObject.h:50
Mesh * getMesh() const override
Gets the mesh geometry for this object.
Definition SceneObject.h:46
void setPosition(const glm::vec3 &pos)
std::optional< float > intersectsRay(const Math::Ray &ray) const override
Tests if a ray intersects this object's geometry.
bool getHovered() const override
Gets the current hover state of this object.
Definition Scene.h:11
Definition Shader.h:6
Represents a ray in 3D space.
Definition Ray.h:11
Per-instance data for instanced rendering.