forked from hiyorikotobuki/ComputerGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotation.cpp
96 lines (81 loc) · 2.15 KB
/
rotation.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
#include "screen.h"
#include "math.h"
#include "drawing.h"
/*
150 is the centroid of this cube to get the centroid add up every value alone the x then
divide by the number of values, do this for y and z to get he centroid,
this cubes centroid was 150, 150, 150 so I subtracted it from the point then added after the rotation
*/
void rotate_x(float rad, float* point)
{
// Center rotation
point[0] -= 150;
point[1] -= 150;
point[2] -= 150;
float x_1 = point[0];
float x_2 = (cos(rad) * point[1]) - ((sin(rad) * point[2]));
float x_3 = (sin(rad) * point[1]) + ((cos(rad) * point[2]));
// Center rotation
point[0] = x_1+150;
point[1] = x_2+150;
point[2] = x_3+150;
}
void rotate_y(float rad, float* point)
{
// Center rotation
point[0] -= 150;
point[1] -= 150;
point[2] -= 150;
float x_1 = (cos(rad) * point[0]) + sin(rad) * point[2] ;
float x_2 = point[1];
float x_3 = -(sin(rad)*point[0]) + cos(rad)*point[2];
// Center rotation
point[0] = x_1+150;
point[1] = x_2+150;
point[2] = x_3+150;
}
void rotate_z(float rad, float* point)
{
// Center rotation
point[0] -= 150;
point[1] -= 150;
point[2] -= 150;
// Rot z
float x_1 = (cos(rad) * point[0]) - (sin(rad)) * point[1] ;
float x_2 = (sin(rad) * point[0]) + ((cos(rad) * point[1])) ;
float x_3 = point[2];
// Center rotation
point[0] = x_1+150;
point[1] = x_2+150;
point[2] = x_3+150;
}
int main(int argc, char* argv[])
{
G screen;
int center[3] = {150,150,1};
float cube[8][3] = {
{100,100,100},
{200,100,100},
{200,200,100},
{100,200,100},
{100,100,200},
{200,100,200},
{200,200,200},
{100,200,200},
};
while(true)
{
for(int i = 0; i < 8; i++)
{
rotate_x(0.001,cube[i]);
rotate_y(0.01,cube[i]);
rotate_z(0.001,cube[i]);
screen.drawpixel((int)cube[i][0] + center[0],(int)cube[i][1]+ center[1]);
}
screen.update();
screen.input();
screen.clearpixels();
SDL_Delay(16);
}
return 0;
}