Skip to content

Commit 7c29a8c

Browse files
committed
funcionalidade transformacao
1 parent 31e29a3 commit 7c29a8c

7 files changed

Lines changed: 180 additions & 2 deletions

File tree

include/data_format.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef DATA_FORMAT_H
2+
#define DATA_FORMAT_H
3+
4+
namespace ijengine {
5+
typedef struct Matrix4f{
6+
float m[4][4];
7+
} matrix4f;
8+
}
9+
10+
#endif

include/math3D.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef MATH3D_H
2+
#define MATH3D_H
3+
#include "data_format.h"
4+
5+
namespace ijengine {
6+
7+
class Math3D {
8+
public:
9+
~Math3D() = default;
10+
matrix4f Mult(matrix4f Left, matrix4f Right);
11+
};
12+
}
13+
14+
#endif

include/sdlglgame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "glrenderer3d.h"
99
#include "window.h"
1010
#include "shader_manager.h"
11-
11+
#include "transformation.h"
1212

1313
#include <memory>
1414

include/transformation.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef TRANSFORMATION_H
2+
#define TRANSFORMATION_H
3+
#include "math3D.h"
4+
5+
namespace ijengine {
6+
7+
class Transformation {
8+
public:
9+
Transformation();
10+
~Transformation();
11+
void setTranslation(float x, float y, float z);
12+
void setRotationX(float angle);
13+
void setRotationY(float angle);
14+
void setRotationZ(float angle);
15+
void setScale(float scale);
16+
void setPerspective(float near, float far, float angle, float width, float height);
17+
void setOrtogonal();
18+
matrix4f getTransformation();
19+
private:
20+
void printMatrix(matrix4f matrix);
21+
matrix4f m_Trans;
22+
matrix4f m_RotX;
23+
matrix4f m_RotY;
24+
matrix4f m_RotZ;
25+
matrix4f m_Scale;
26+
matrix4f m_Perspective;
27+
void loadIdentity(matrix4f& Trans);
28+
29+
};
30+
}
31+
32+
#endif

src/math3D.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "math3D.h"
2+
3+
using namespace std;
4+
5+
namespace ijengine{
6+
7+
matrix4f
8+
Math3D::Mult(matrix4f Left, matrix4f Right){
9+
matrix4f Ret;
10+
for (unsigned int i = 0 ; i < 4 ; i++) {
11+
for (unsigned int j = 0 ; j < 4 ; j++) {
12+
Ret.m[i][j] = Left.m[i][0] * Right.m[0][j] +
13+
Left.m[i][1] * Right.m[1][j] +
14+
Left.m[i][2] * Right.m[2][j] +
15+
Left.m[i][3] * Right.m[3][j];
16+
}
17+
}
18+
return Ret;
19+
}
20+
}

src/sdlglgame.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "sdlglgame.h"
22

3+
34
using std::make_shared;
45

56
namespace ijengine {
67

78
SDLGLGame::SDLGLGame()
89
: m_lib_sdl(new LibSDL2())
910
{
11+
1012
if (m_lib_sdl)
1113
m_lib_sdl->init();
1214

@@ -37,6 +39,15 @@ namespace ijengine {
3739
m_window->renderer3d()->notifyEndFrame();
3840
SDL_Delay(300);
3941
}
40-
}
4142

43+
Transformation transformation;
44+
45+
transformation.setTranslation(3.0, 3.0, 3.0);
46+
transformation.setRotationX(45);
47+
transformation.setRotationY(15);
48+
transformation.setRotationZ(45);
49+
transformation.setScale(2.0);
50+
transformation.setPerspective(3.0, 2.0, 45, 3.0, 2.0);
51+
52+
}
4253
}

src/transformation.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "math3D.h"
2+
#include "transformation.h"
3+
#include <math.h>
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
namespace ijengine{
9+
Transformation::Transformation(){
10+
loadIdentity(m_Trans);
11+
loadIdentity(m_Scale);
12+
loadIdentity(m_RotX);
13+
loadIdentity(m_RotY);
14+
loadIdentity(m_RotZ);
15+
loadIdentity(m_Perspective);
16+
}
17+
Transformation::~Transformation(){
18+
19+
}
20+
void
21+
Transformation::loadIdentity(matrix4f& Trans){
22+
Trans = {1, 0, 0, 0,
23+
0, 1, 0, 0,
24+
0, 0, 1, 0,
25+
0, 0, 0, 1};
26+
}
27+
void
28+
Transformation::setTranslation(float x, float y, float z){
29+
m_Trans.m[0][3] = x;
30+
m_Trans.m[1][3] = y;
31+
m_Trans.m[2][3] = z;
32+
printMatrix(m_Trans);
33+
}
34+
void
35+
Transformation::setRotationX(float angle){
36+
m_RotX.m[1][1] = cos(angle);
37+
m_RotX.m[2][1] = -sin(angle);
38+
m_RotX.m[1][2] = sin(angle);
39+
m_RotX.m[2][2] = cos(angle);
40+
printMatrix(m_RotX);
41+
}
42+
void
43+
Transformation::setRotationY(float angle){
44+
m_RotY.m[0][0] = cos(angle);
45+
m_RotY.m[0][2] = sin(angle);
46+
m_RotY.m[2][0] = -sin(angle);
47+
m_RotY.m[2][2] = cos(angle);
48+
printMatrix(m_RotY);
49+
}
50+
void
51+
Transformation::setRotationZ(float angle){
52+
m_RotZ.m[0][0] = cos(angle);
53+
m_RotZ.m[0][1] = sin(angle);
54+
m_RotZ.m[1][0] = -sin(angle);
55+
m_RotZ.m[1][1] = cos(angle);
56+
printMatrix(m_RotZ);
57+
}
58+
void
59+
Transformation::setScale(float scale){
60+
m_Scale.m[0][0] = scale;
61+
m_Scale.m[1][1] = scale;
62+
m_Scale.m[2][2] = scale;
63+
printMatrix(m_Scale);
64+
}
65+
66+
void
67+
Transformation::setPerspective(float near, float far, float angle, float width, float height){
68+
m_Perspective.m[0][0] = 1/((width/height)*tan(angle/2));
69+
m_Perspective.m[1][1] = 1/(tan(angle/2));
70+
m_Perspective.m[2][2] = (-near-far)/(near-far);
71+
m_Perspective.m[2][3] = (2*(near*far))/(near-far);
72+
printMatrix(m_Perspective);
73+
}
74+
75+
void
76+
Transformation::printMatrix(matrix4f matrix){
77+
for (int i = 0; i < 4; ++i) {
78+
for (int j = 0; j < 4; ++j)
79+
cout << matrix.m[i][j] << " ";
80+
cout << endl;
81+
}
82+
}
83+
84+
matrix4f
85+
Transformation::getTransformation(){
86+
matrix4f Trans;
87+
// Trans = Mult(m_RotX, m_Scale);
88+
//Mult(m_Trans ,Mult(m_RotZ ,Mult(m_RotY ,(Mult(m_RotX, m_Scale))));
89+
return Trans;
90+
}
91+
}

0 commit comments

Comments
 (0)