hx3d  1
2D/3D Simple Game Framework
transform.cpp
1 /*
2  Transform.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/transform.hpp"
22 
23 #include "hx3d/utils/log.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 
28 Transform::Transform():
29  position(0, 0, 0),
30  scale(1, 1, 1),
31  size(1, 1, 1),
32  rotation(0, 0, 0) {}
33 
34 Transform& Transform::operator=(const Transform& transform) {
35  position = transform.position;
36  scale = transform.scale;
37  size = transform.size;
38  rotation = transform.rotation;
39 
40  return *this;
41 }
42 
43 glm::mat4 Transform::compute() {
44  glm::mat4 model = glm::mat4(1.f);
45  model = glm::translate(model, glm::vec3(position.x, position.y, position.z));
46  model = glm::rotate(model, rotation.x, glm::vec3(1, 0, 0));
47  model = glm::rotate(model, rotation.y, glm::vec3(0, 1, 0));
48  model = glm::rotate(model, rotation.z, glm::vec3(0, 0, 1));
49  model = glm::scale(model, glm::vec3(size.x, size.y, size.z));
50  model = glm::scale(model, glm::vec3(scale.x, scale.y, scale.z));
51 
52  return model;
53 }
54 
55 Transform Transform::add(const Transform& transform) {
56  Transform t;
57  t.position = position;
58  t.rotation = rotation;
59  t.scale = scale;
60  t.size = size;
61 
62  t.rotation += transform.rotation;
63 
64  float s = std::sin(transform.rotation.z);
65  float c = std::cos(transform.rotation.z);
66 
67  float nx = c * position.x - s * position.y + transform.position.x;
68  float ny = s * position.x + c * position.y + transform.position.y;
69 
70  t.position.x = nx;
71  t.position.y = ny;
72 
73  return t;
74 }
75 
76 glm::vec3 Transform::realSize() {
77  return size * scale;
78 }
79 
80 bool Transform::contains(glm::vec2 point) {
81  float width = scale.x * size.x;
82  float height = scale.y * size.y;
83 
84  float minX = position.x - width / 2;
85  float maxX = position.x + width / 2;
86  float minY = position.y - height / 2;
87  float maxY = position.y + height / 2;
88 
89  return ((minX <= point.x && maxX >= point.x) &&
90  (minY <= point.y && maxY >= point.y));
91 }
92 
94  Log.Info("-- Transform");
95  Log.Info("\t Pos: [%f, %f, %f]", position.x, position.y, position.z);
96  Log.Info("\t Sca: [%f, %f, %f]", scale.x, scale.y, scale.z);
97  Log.Info("\t Siz: [%f, %f, %f]", size.x, size.y, size.z);
98  Log.Info("\t Rot: [%f, %f, %f]", rotation.x, rotation.y, rotation.z);
99  Log.Info("-- END Transform");
100 }
101 
102 } /* graphics */
103 } /* hx3d */
2D/3D transform.
Definition: transform.hpp:35
glm::vec3 position
Position.
Definition: transform.hpp:80
hx3d framework namespace
Definition: audio.hpp:26
Transform add(const Transform &transform)
Add a transform to another.
Definition: transform.cpp:55
bool contains(glm::vec2 point)
Check a point in the transform.
Definition: transform.cpp:80
glm::mat4 compute()
Build the model matrix using the position, scale, size and rotation.
Definition: transform.cpp:43
glm::vec3 rotation
Rotation.
Definition: transform.hpp:86
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
glm::vec3 size
Size.
Definition: transform.hpp:84
void show()
Show the transform information. (Debug)
Definition: transform.cpp:93
glm::vec3 realSize()
Get the real size (size * scale).
Definition: transform.cpp:76
glm::vec3 scale
Scale.
Definition: transform.hpp:82
void Info(const std::string fmt,...)
Write an info message.
Definition: log.cpp:58