Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
default_components.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cmath>
5#include <string>
6#include <vector>
7
8#include "log.hpp"
9#include "math/matrix_4.h"
10#include "math/vector_3.h"
11#include "sound/soundsource.h"
12
15
16class Renderer;
17class ResourceManager;
18class Window;
19class Engine;
20class Texture;
21class Mesh;
23
25enum class CameraProjection {
27 kOrtho
28};
29
31enum class LightType {
32 kPoint,
36 kSpot
37};
38
40struct AudioCmp {
46 AudioCmp(const std::string& name, const SoundBuffer& buffer,
47 float gain = 1.0f, float pitch = 1.0f)
48 : src(name, buffer, coma::Vec3{0.f,0.f,0.f}, coma::Vec3{0.f,0.f,0.f},
49 gain, pitch) {}
50
52 SoundSource src;
53};
54
55const std::array kAttenuationTable{
56 coma::Vec3(0.0f, 10000.0f, 10000.0f),coma::Vec3(1.0f, 4.0f, 120.0f),
57 coma::Vec3(3.0f, 1.4f, 10.0f),coma::Vec3(7.0f, 0.7f, 1.8f),
58 coma::Vec3(13.0f, 0.35f, 0.44f),coma::Vec3(20.0f, 0.22f, 0.20f),
59 coma::Vec3(32.0f, 0.14f, 0.07f),coma::Vec3(50.0f, 0.09f, 0.032f),
60 coma::Vec3(65.0f, 0.07f, 0.017f),coma::Vec3(100.0f, 0.045f, 0.0075f),
61 coma::Vec3(160.0f, 0.027f, 0.0028f),coma::Vec3(200.0f, 0.022f, 0.0019f),
62 coma::Vec3(325.0f, 0.014f, 0.0007f),coma::Vec3(600.0f, 0.007f, 0.0002f),
63 coma::Vec3(3250.0f, 0.0014f, 0.000007f)};
64
69struct LightCmp {
74 for (int next = 0; next < kAttenuationTable.size(); ++next) {
75 if (max_distance_ < kAttenuationTable[next].x) {
76 float alpha =
77 (max_distance_ - kAttenuationTable[next - 1].x) /
78 (kAttenuationTable[next].x - kAttenuationTable[next - 1].x);
79 linear_value_ = std::lerp(kAttenuationTable[next - 1].y,
80 kAttenuationTable[next].y, alpha);
81 quadratic_value_ = std::lerp(kAttenuationTable[next - 1].z,
82 kAttenuationTable[next].z, alpha);
83 return;
84 }
85 }
86 linear_value_ = kAttenuationTable[kAttenuationTable.size() - 1].y;
87 quadratic_value_ = kAttenuationTable[kAttenuationTable.size() - 1].z;
88 }
89
90 // Generic
91
93 LightType type_ = LightType::kPoint;
95 coma::Vec3 light_color_ = coma::Vec3(1.0f, 1.0f, 1.0f);
97 float intensity_ = 1.0f;
98
99 // Point
100
103 float max_distance_ = 13.0f;
105 float constant_value_ = 1.0f;
107 float linear_value_ = 0.01f;
109 float quadratic_value_ = 0.01f;
110
111 // Spot
112
114 float cutoff_angle_ = 50.0f;
117 float falloff_start_ = 0.8f;
118
120 bool show_shadows_ = true;
121
129 LightCmp(const LightType type, const coma::Vec3& color, const float intensity)
130 : type_(type), light_color_(color), intensity_(intensity) {}
131
136 LightCmp(const coma::Vec3& color, const float intensity,
137 const float max_distance)
139 light_color_(color),
140 intensity_(intensity),
141 max_distance_(max_distance) {
143 }
144
150 LightCmp(const coma::Vec3& color, const float intensity,
151 const float cutoff_angle, float falloff_start)
153 light_color_(color),
154 intensity_(intensity),
155 cutoff_angle_(cutoff_angle),
156 falloff_start_(falloff_start) {}
157};
158
161struct CameraCmp {
163 CameraProjection projection_ = CameraProjection::kPerspective;
164
167 float fov_ = 60.0f;
168
170 float z_near_ = 0.01f;
171
173 float z_far_ = 1000.0f;
174
178
183
190 CameraCmp(const CameraProjection projection, const coma::Vec3& target,
191 const float fov = 60.0f, const float z_near = 0.01f,
192 const float z_far = 1000.0f)
193 : projection_(projection),
194 fov_(fov),
195 z_near_(z_near),
196 z_far_(z_far),
197 target_(target),
199
205 CameraCmp(const CameraProjection projection, const float fov = 60.0f,
206 const float z_near = 0.01f, const float z_far = 1000.0f)
207 : projection_(projection), fov_(fov), z_near_(z_near), z_far_(z_far) {}
208};
209
211struct RenderTreeCmp {
212 friend ComponentManager;
213 friend Window;
214 friend Renderer;
215 friend Editor;
216
217 RenderTreeCmp(const unsigned parent, const unsigned id,
218 const std::string& editor_name,
219 const std::vector<unsigned>& children = {})
220 : editor_name_(editor_name),
221 parent_(parent),
222 id_(id),
223 children_(children) {}
224
225private:
226 std::string editor_name_ = "Object";
227 unsigned parent_ = 0;
228 unsigned id_ = 0;
229 std::vector<unsigned> children_;
230};
231
237struct RenderCmp {
239 Mesh const* mesh_ = nullptr;
240
242 Texture const* texture_ = nullptr;
243
245 coma::Vec3 color_ = {1.0f,1.0f,1.0f};
246
248 float roughness_ = 0.5f;
249
251 float shininess_ = 32.0f;
252
254 bool cast_shadows_ = true;
255
263 RenderCmp(const Mesh* mesh, const Texture* texture,
264 const coma::Vec3& color = {1.0f,1.0f,1.0f},
265 bool castShadows = true, float roughness = 0.5f,
266 float shininess = 32.0f)
267 : mesh_(mesh),
268 texture_(texture),
269 color_(color),
270 roughness_(roughness),
271 shininess_(shininess),
272 cast_shadows_{castShadows} {
273 if (mesh == nullptr) {
274 Log::newLog("Mesh is Null on Render Component Creation", LogColor::Red);
275 }
276 if (texture == nullptr) {
277 Log::newLog("Texture is Null on Render Component Creation",
278 LogColor::Red);
279 }
280 }
281};
282
285public:
286 friend Window;
287 friend Renderer;
288
290 coma::Vec3 position_ = {0.0f,0.0f,0.0f};
291
293 coma::Vec3 rotation_ = {0.0f,0.0f,0.0f};
294
296 coma::Vec3 scale_ = {1.0f,1.0f,1.0f};
297
298
299
303
306 coma::Vec3 up() const { return coma::Vec3::up; }
307
312 }
313
318 explicit TransformCmp(const coma::Vec3& position,
319 const coma::Vec3& rotation = {0.0f,180.0f,0.0f},
320 const coma::Vec3& scale = {1.0f,1.0f,1.0f})
321 : position_(position), rotation_(rotation), scale_(scale) {}
322
328 position_, scale_, coma::Radians(rotation_.x),
329 coma::Radians(rotation_.y), coma::Radians(rotation_.z));
330 }
331
332private:
333 coma::Mat4 tree_transform_ = coma::Mat4::Identity();
334};
335
337struct BoxCmp {
338public:
340 coma::Vec3 position_ = {0.0f,0.0f,0.0f};
341
343 coma::Vec3 size_ = {1.0f,1.0f,1.0f};
344
348 explicit BoxCmp(const coma::Vec3& position,
349 const coma::Vec3& size = {1.0f,1.0f,1.0f})
350 : position_(position), size_(size) {}
351};
Handles anything related to entities and components in the engine.
Definition: component_manager.hpp:29
Display and allows to modify entity information in real time.
Definition: editor.hpp:14
Intiliazes the ComponentManager and the ResourceManager and ofers functions to retrieve them.
Definition: engine.hpp:12
static void newLog(std::string text, LogColor textcolor=LogColor::White)
Add a new log entry.
Class used to render the scene.
Definition: renderer.hpp:49
Stores and offers access to all meshes, textures, and sounds of the engine.
Definition: resource_manager.hpp:27
Creates and updates a window.
Definition: window.hpp:20
represents mathematical matrix with 4 cols and 4 rows
Definition: matrix_4.h:11
static Mat4 GetTransform(const Vec3 &translate, const Vec3 &scale, const Vec3 &rotation)
Definition: matrix_4.h:432
static Mat4 Identity()
Definition: matrix_4.h:254
represents mathematical vector with 3 components
Definition: vector_3.h:12
static const Vec3 up
Vec3(0, 1, 0)
Definition: vector_3.h:117
static Vec3 eulerToForward(const Vec3 &a)
Definition: vector_3.h:187
static const Vec3 zero
Vec3(0, 0, 0)
Definition: vector_3.h:129
static Vec3 CrossProduct(const Vec3 &a, const Vec3 &b)
Definition: vector_3.h:155
Vec3 Normalized() const
Definition: vector_3.h:145
CameraProjection
Camera projection modes.
Definition: default_components.hpp:25
@ kOrtho
Renders a 3D scene as 2D without depth.
@ kPerspective
Renders regular 3D using depth.
LightType
Kinds of lights that can be casted.
Definition: default_components.hpp:31
@ kSpot
Emits light in a cone.
Definition of logging functionality.
copperdielf Math Library
Definition: buffer.hpp:5
Component used for audio reproduction.
Definition: default_components.hpp:40
AudioCmp(const std::string &name, const SoundBuffer &buffer, float gain=1.0f, float pitch=1.0f)
Construct an audio component.
Definition: default_components.hpp:46
SoundSource src
Object used to reproduce the audio.
Definition: default_components.hpp:52
Component used to detect collisions.
Definition: default_components.hpp:337
coma::Vec3 size_
Size of the box not affected by the TransformCmp scale.
Definition: default_components.hpp:343
coma::Vec3 position_
Position of the box relative to the TransformCmp position.
Definition: default_components.hpp:340
BoxCmp(const coma::Vec3 &position, const coma::Vec3 &size={1.0f, 1.0f, 1.0f})
Creates a BoxCmp.
Definition: default_components.hpp:348
Component used to render the scene. There must be one and only one of this component on the scene.
Definition: default_components.hpp:161
coma::Vec3 target_
Defines the position the camera will look at if rotate_towards_target_ is set to true.
Definition: default_components.hpp:177
CameraCmp(const CameraProjection projection, const coma::Vec3 &target, const float fov=60.0f, const float z_near=0.01f, const float z_far=1000.0f)
Creates a CameraCmp with rotate_towards_target_ set to true.
Definition: default_components.hpp:190
CameraProjection projection_
Defines the CameraProjection to use.
Definition: default_components.hpp:163
float z_far_
Maximum distance to render.
Definition: default_components.hpp:173
float z_near_
Minimum distance to render.
Definition: default_components.hpp:170
bool rotate_towards_target_
Defines how the rotation of the camera should behave:
Definition: default_components.hpp:182
float fov_
Angular extent of the observable world that is seen at any given moment.
Definition: default_components.hpp:167
CameraCmp(const CameraProjection projection, const float fov=60.0f, const float z_near=0.01f, const float z_far=1000.0f)
Creates a CameraCmp with rotate_towards_target_ set to false.
Definition: default_components.hpp:205
Component used to simulate different types of lights.
Definition: default_components.hpp:69
float quadratic_value_
Can be handled with max_distance_.
Definition: default_components.hpp:109
bool show_shadows_
Define if the light shows shadows.
Definition: default_components.hpp:120
float max_distance_
Distance at which the light intensity will become 0 due to attenuation.
Definition: default_components.hpp:103
LightType type_
Define the light type.
Definition: default_components.hpp:93
void calculateAttenuation()
After modifying max_distance_, this function should be called to update the rest of the parameters re...
Definition: default_components.hpp:73
coma::Vec3 light_color_
Define the color of the light.
Definition: default_components.hpp:95
LightCmp(const LightType type, const coma::Vec3 &color, const float intensity)
Creates a LightCmp of the specified LightType.
Definition: default_components.hpp:129
float falloff_start_
Percentage of the cutoff_angle_ at which the light starts to attenuate.
Definition: default_components.hpp:117
float cutoff_angle_
Radius of the spot light.
Definition: default_components.hpp:114
LightCmp(const coma::Vec3 &color, const float intensity, const float cutoff_angle, float falloff_start)
Creates a kSpot.
Definition: default_components.hpp:150
LightCmp(const coma::Vec3 &color, const float intensity, const float max_distance)
Creates a kPoint.
Definition: default_components.hpp:136
float linear_value_
Can be handled with max_distance_.
Definition: default_components.hpp:107
float intensity_
Define the power of the light.
Definition: default_components.hpp:97
float constant_value_
Can be handled with max_distance_.
Definition: default_components.hpp:105
Component used to render an object.
Definition: default_components.hpp:237
Mesh const * mesh_
Mesh of the object you want to render. Must have a value.
Definition: default_components.hpp:239
RenderCmp(const Mesh *mesh, const Texture *texture, const coma::Vec3 &color={1.0f, 1.0f, 1.0f}, bool castShadows=true, float roughness=0.5f, float shininess=32.0f)
Creates a RenderCmp allowing setting its parameters.
Definition: default_components.hpp:263
Texture const * texture_
Texture of the object. Must have a value.
Definition: default_components.hpp:242
float shininess_
Affects the size and definition of the reflections it receives.
Definition: default_components.hpp:251
coma::Vec3 color_
Color of the object; it will get mixed with the texture.
Definition: default_components.hpp:245
bool cast_shadows_
Defines if the object projects shadows.
Definition: default_components.hpp:254
float roughness_
Affects the intensity of the reflections it receives.
Definition: default_components.hpp:248
Component used to give a position, rotation, and scale to an entity.
Definition: default_components.hpp:284
coma::Vec3 scale_
Scale in the world relative to the parent's scale.
Definition: default_components.hpp:296
coma::Vec3 right() const
Retrieves direction pointing to the right of the entity.
Definition: default_components.hpp:310
coma::Mat4 getMatrix() const
Calculates the transform matrix using position_, rotation_, and scale_.
Definition: default_components.hpp:326
TransformCmp(const coma::Vec3 &position, const coma::Vec3 &rotation={0.0f, 180.0f, 0.0f}, const coma::Vec3 &scale={1.0f, 1.0f, 1.0f})
Creates a TransformCmp.
Definition: default_components.hpp:318
coma::Vec3 forward() const
Retrieves direction the entity is looking at.
Definition: default_components.hpp:302
coma::Vec3 rotation_
Rotation in the world relative to the parent's rotation.
Definition: default_components.hpp:293
coma::Vec3 position_
Position in the world relative to the parent's position.
Definition: default_components.hpp:290
coma::Vec3 up() const
Retrieves world up vector.
Definition: default_components.hpp:306