-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimitivesExample.cpp
206 lines (165 loc) · 5.97 KB
/
PrimitivesExample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/MeshTools/Compile.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Primitives/Cube.h>
#include <Magnum/Shaders/Phong.h>
#include <Magnum/Trade/MeshData3D.h>
#include <Magnum/Math/Quaternion.h>
#include "externals/entt.hpp"
namespace Magnum { namespace Examples {
// --------------------------------------------------------------
//
// Components
//
// --------------------------------------------------------------
using Position = Math::Vector3<float>;
using Orientation = Math::Quaternion<float>;
using Radian = Math::Rad<float>;
using Color = Color4;
struct Scale : public Vector3 {
using Vector3::Vector3;
};
struct Identity {
std::string name;
};
struct Witness {
Radian fov;
float aspectRatio;
float near;
float far;
Vector2i viewport;
};
struct Drawable {
GL::Mesh mesh { NoCreate };
Shaders::Phong shader{ NoCreate };
Color4 color;
};
// ---------------------------------------------------------
//
// Systems
//
// ---------------------------------------------------------
static void MouseMoveSystem(entt::registry& registry, Vector2 distance) {
registry.view<Orientation>().each([distance](auto& ori) {
ori = (
Quaternion::rotation(Rad{ distance.y() }, Vector3(1.0f, 0, 0)) *
ori *
Quaternion::rotation(Rad{ distance.x() }, Vector3(0, 1.0f, 0))
).normalized();
});
}
static void MouseReleaseSystem(entt::registry& registry) {
registry.view<Drawable>().each([](auto& drawable) {
drawable.color = Color3::fromHsv({ drawable.color.hue() + 50.0_degf, 1.0f, 1.0f });
});
}
static void AnimationSystem(entt::registry& registry) {
Debug() << "Animating..";
}
static void PhysicsSystem(entt::registry& registry) {
Debug() << "Simulating..";
}
static void RenderSystem(entt::registry& registry, Matrix4 projection) {
Debug() << "Rendering..";
registry.view<Identity, Position, Orientation, Scale, Drawable>().each(
[projection](auto& id, auto& pos, auto& ori, auto& scale, auto& drawable)
{
auto transform = (
Matrix4::scaling(scale) *
Matrix4::rotation(ori.angle(), ori.axis().normalized()) *
Matrix4::translation(pos)
);
// Problem area 1: Shader program with function and data combined
// Ideal solution: Uniforms a separate component
drawable.shader.setLightPosition({7.0f, 7.0f, 2.5f})
.setLightColor(Color3{1.0f})
.setDiffuseColor(drawable.color)
.setAmbientColor(Color3::fromHsv({drawable.color.hue(), 1.0f, 0.3f}))
.setTransformationMatrix(transform)
.setNormalMatrix(transform.rotationScaling())
.setProjectionMatrix(projection);
// Problem area 2: Vertex data and rendering function combined
// Ideal solution: Vertex data a separate component, shader takes mesh as component
drawable.mesh.draw(drawable.shader);
});
}
// ---------------------------------------------------------
//
// Implementation
//
// ---------------------------------------------------------
using namespace Magnum::Math::Literals;
class ECSExample : public Platform::Application {
public:
explicit ECSExample(const Arguments& arguments);
private:
void drawEvent() override;
void mousePressEvent(MouseEvent& event) override;
void mouseReleaseEvent(MouseEvent& event) override;
void mouseMoveEvent(MouseMoveEvent& event) override;
GL::Mesh _mesh;
Shaders::Phong _shader;
entt::registry _registry;
Matrix4 _projection;
Vector2i _previousMousePosition;
};
ECSExample::ECSExample(const Arguments& arguments) :
Platform::Application{ arguments, Configuration{}
.setTitle("Magnum Primitives Example") }
{
GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
GL::Renderer::enable(GL::Renderer::Feature::FaceCulling);
_projection =
Matrix4::perspectiveProjection(
35.0_degf, Vector2{ windowSize() }.aspectRatio(), 0.01f, 100.0f) *
Matrix4::translation(Vector3::zAxis(-10.0f));
// Create entities
auto box = _registry.create();
// Assign components
_registry.assign<Identity>(box, "Box");
_registry.assign<Position>(box, 0.0f, 0.0f, 0.0f);
_registry.assign<Orientation>(box, Quaternion::rotation(30.0_degf, Vector3(0, 1.0f, 0)));
_registry.assign<Scale>(box, 1.0f);
_registry.assign<Drawable>(box,
MeshTools::compile(Primitives::cubeSolid()),
Shaders::Phong{},
Color4(.4f, .2f, .9f)
);
}
void ECSExample::drawEvent() {
GL::defaultFramebuffer.clear(
GL::FramebufferClear::Color | GL::FramebufferClear::Depth);
// Should the system take _projection as argument?
RenderSystem(_registry, _projection);
swapBuffers();
}
void ECSExample::mousePressEvent(MouseEvent& event) {
if (event.button() != MouseEvent::Button::Left) return;
_previousMousePosition = event.position();
event.setAccepted();
}
void ECSExample::mouseReleaseEvent(MouseEvent& event) {
if (event.button() != MouseEvent::Button::Left) return;
// Should the system handle all mouse events, instead of individual ones?
MouseReleaseSystem(_registry);
event.setAccepted();
redraw();
}
void ECSExample::mouseMoveEvent(MouseMoveEvent& event) {
if (!(event.buttons() & MouseMoveEvent::Button::Left)) return;
const float sensitivity = 3.0f;
const Vector2 distance = (
Vector2{ event.position() - _previousMousePosition } /
Vector2{ GL::defaultFramebuffer.viewport().size() }
) * sensitivity;
// Should the system compute delta?
// If so, where does state go, i.e. _previousMousePosition?
MouseMoveSystem(_registry, distance);
_previousMousePosition = event.position();
event.setAccepted();
redraw();
}
}}
MAGNUM_APPLICATION_MAIN(Magnum::Examples::ECSExample)