Transformable.cpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#include <glm/gtc/matrix_transform.hpp>
6#include <smk/RenderTarget.hpp>
7#include <smk/Texture.hpp>
8#include <smk/Transformable.hpp>
9
10namespace smk {
11
12/// @brief Set the rotation to apply before drawing the object.
13/// @see Transformable::Rotate.
14/// @param rotation The angle in radian.
15void Transformable::SetRotation(float rotation) {
16 rotation_ = rotation;
17}
18
19/// @brief Increase the rotation of the object to apply before drawing it.
20/// @see Transformable::SetRotation.
21/// @param rotation The delta of rotation to be added.
22void Transformable::Rotate(float rotation) {
23 rotation_ += rotation;
24}
25
26/// @brief Set the position of the object to be drawn.
27/// @see Transformable::Move.
28/// @param position the position (x,y) of the object.
29void Transformable::SetPosition(const glm::vec2& position) {
30 position_ = position;
31}
32
33/// @brief Set the position of the object to be drawn.
34/// @see Transformable::Move.
35/// @param x The position along the horizontal axis.
36/// @param y The position along the vertical axis.
37void Transformable::SetPosition(float x, float y) {
38 position_ = {x, y};
39}
40
41/// Increase the position of the object being drawn.
42/// @see smk::SetPosition
43/// @param move The increment of position (x,y)
44void Transformable::Move(const glm::vec2& move) {
45 position_ += move;
46}
47
48/// Increase the position of the object being drawn.
49/// @see smk::SetPosition
50/// @param x The increment of position along the horizontal axis.
51/// @param y The increment of position along the vertical axis.
52void Transformable::Move(float x, float y) {
53 Move({x, y});
54}
55
56/// @brief Set the center of the object. It is used as the rotation center. The
57/// center of the object will be drawn exactly on (0,0) on the screen (plus its
58/// potential translation if any)
59/// @param center The center position (x,y) in the object.
60void Transformable::SetCenter(const glm::vec2& center) {
61 center_ = center;
62}
63
64/// @brief Set the center of the object. It is used as the rotation center. The
65/// center of the object will be drawn exactly on (0,0) on the screen (plus its
66/// potential translation if any)
67/// @param x The center position along the horizontal axis.
68/// @param y The center position along the vertical axis.
69void Transformable::SetCenter(float x, float y) {
70 SetCenter({x, y});
71}
72
73/// @brief Increase or decrease the size of the object being drawn.
74/// @param scale The ratio of magnification.
75void Transformable::SetScale(float scale) {
76 SetScale({scale, scale});
77}
78
79/// @brief Increase or decrease the size of the object being drawn.
80/// @param scale The ratio of magnification.
81void Transformable::SetScale(const glm::vec2& scale) {
82 scale_ = scale;
83}
84
85/// @brief Increase or decrease the size of the object being drawn.
86/// @param scale_x The ratio of magnification along the horizontal axis.
87/// @param scale_y The ratio of magnification along the vertical axis.
88void Transformable::SetScale(float scale_x, float scale_y) {
89 scale_.x = scale_x;
90 scale_.y = scale_y;
91}
92
93/// @brief Increase or decrease the size of the object being drawn.
94/// @param scale_x The ratio of magnification along the horizontal axis.
95void Transformable::SetScaleX(float scale_x) {
96 scale_.x = scale_x;
97}
98
99/// @brief Increase or decrease the size of the object being drawn.
100/// @param scale_y The ratio of magnification along the vertical axis.
101void Transformable::SetScaleY(float scale_y) {
102 scale_.y = scale_y;
103}
104
105/// @brief Increase or decrease the size of the object being drawn.
106/// @return the transformation applied to the object. This is the result of
107/// applying the translation, rotation, center and scaling to the the
108/// object.
110 glm::mat4 ret = glm::mat4(1.0);
111 ret = glm::translate(ret, {position_.x, position_.y, 0.0});
112 if (rotation_ != 0.F) {
113 ret =
114 // NOLINTNEXTLINE
115 glm::rotate(ret, -rotation_ * (2.F * 3.1415F / 360.F), {0.0, 0.0, 1.0});
116 }
117 ret =
118 glm::translate(ret, {-center_.x * scale_.x, -center_.y * scale_.y, 0.F});
119 ret = glm::scale(ret, {scale_.x, scale_.y, 1.0});
120 return ret;
121}
122
123/// @brief Modify the color of the object. The resulting pixel is the
124/// multiplication component wise in between this color and the original pixel
125/// color.
126/// @param color The color.
127void TransformableBase::SetColor(const glm::vec4& color) {
128 color_ = color;
129}
130
131/// @brief Set the blending mode to be used for drawing the object.
132/// @param blend_mode the BlendMode to be used.
134 blend_mode_ = blend_mode;
135}
136
137/// Set the object's texture.
139 texture_ = std::move(texture);
140}
141
142/// Set the object's shape.
144 vertex_array_ = std::move(vertex_array);
145}
146
148 state.color *= color();
149 state.texture = texture();
150 state.view *= transformation();
151 state.vertex_array = vertex_array();
152 state.blend_mode = blend_mode();
153 target.Draw(state);
154}
155
156/// @brief set the transformation to use for drawing this object, represented as
157/// 4x4 matrix.
158/// @see https://learnopengl.com/Getting-started/Transformations
159/// @param transformation The 4x4 matrix defining the transformation.
160void Transformable3D::SetTransformation(const glm::mat4& mat) {
161 transformation_ = mat;
162}
163
164glm::mat4 Transformable3D::transformation() const {
165 return transformation_;
166}
167
168} // namespace smk
virtual void Draw(const Drawable &drawable)
Draw on the surface.
void SetTransformation(const glm::mat4 &mat)
set the transformation to use for drawing this object, represented as 4x4 matrix.
void SetColor(const glm::vec4 &color)
Color.
void SetTexture(Texture texture)
Set the object's texture.
void Draw(RenderTarget &target, RenderState state) const override
void SetVertexArray(VertexArray vertex_array)
Set the object's shape.
void SetBlendMode(const BlendMode &)
Set the blending mode to be used for drawing the object.
void SetRotation(float rotation)
Set the rotation to apply before drawing the object.
void SetScale(float scale)
Increase or decrease the size of the object being drawn.
void SetCenter(float center_x, float center_y)
Set the center of the object. It is used as the rotation center. The center of the object will be dra...
void Move(const glm::vec2 &move)
glm::mat4 transformation() const override
Increase or decrease the size of the object being drawn.
void SetPosition(float x, float y)
Set the position of the object to be drawn.
void SetScaleX(float scale_x)
Increase or decrease the size of the object being drawn.
void Rotate(float rotation)
Increase the rotation of the object to apply before drawing it.
void SetScaleY(float scale_y)
Increase or decrease the size of the object being drawn.
An array of smk::Vertex moved to the GPU memory. This represent a set of triangles to be drawn by the...
Definition: VertexArray.hpp:20
Contain all the data needed to draw.
Definition: RenderState.hpp:17
glm::mat4 view
The "view" transformation.
Definition: RenderState.hpp:21
glm::vec4 color
The masking color.
Definition: RenderState.hpp:22
Texture texture
The texture 0 bound.
Definition: RenderState.hpp:19
BlendMode blend_mode
The OpenGL BlendMode.
Definition: RenderState.hpp:23
VertexArray vertex_array
The shape to to be drawn.
Definition: RenderState.hpp:20