-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera3d.cpp
More file actions
128 lines (100 loc) · 2.91 KB
/
Copy pathcamera3d.cpp
File metadata and controls
128 lines (100 loc) · 2.91 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "camera3d.h"
RenderTransform Camera3D::projectPoint(Point3 p) {
//koordinate v moj liking:
// x = desno+, y = gor+, z = noter+
p.y = -p.y;
p.z = -p.z;
//stopinje v radiane
double _rotX = rotX / 57.295779;
double _rotY = rotY / 57.295779;
double _rotZ = rotZ / 57.295779;
//zracunamo in shranmo ker bomo rabl veckrat
double cox = cos(_rotX);
double six = sin(_rotX);
double coy = cos(_rotY);
double siy = sin(_rotY);
double coz = cos(_rotZ);
double siz = sin(_rotZ);
RenderTransform res;
// rotacija
Point3 c;
c = p;
p.x = c.x * coz + c.y * -siz; // z
p.y = c.x * siz + c.y * coz;
c = p;
p.y = c.y * cox + c.z * six; // x
p.z = c.y * -six + c.z * cox;
c = p;
p.x = c.x * coy + c.z * siy; // y
p.z = c.x * -siy + c.z * coy;
//premiki
p.x = p.x + x;
p.y = p.y + y;
p.z = p.z + z;
//projekcija
Point3 raw;
double z_res = 1 / (distance - p.z);
raw.x = p.x;
raw.y = p.y;
raw.z = p.z;
res.screenX = (raw.x * zoom) * z_res + (w / 2);
res.screenY = (raw.y * zoom) * z_res + (h / 2);
res.scale = z_res;
return res;
}
vector<RenderTransform> Camera3D::projectPoints(vector<Point3> pnts) {
//stopinje v radiane
double _rotX = rotX / 57.295779;
double _rotY = rotY / 57.295779;
double _rotZ = rotZ / 57.295779;
//zracunamo in shranmo ker bomo rabl veckrat
double cox = cos(_rotX);
double six = sin(_rotX);
double coy = cos(_rotY);
double siy = sin(_rotY);
double coz = cos(_rotZ);
double siz = sin(_rotZ);
vector<RenderTransform> res;
res.reserve(pnts.size());
for (int i = 0; i < pnts.size(); i++) {
Point3 p = pnts[i];
Point3 c;
//koordinate v moj liking:
// x = desno+, y = gor+, z = noter+
p.y = -p.y;
p.z = -p.z;
// rotacija
c = p;
p.x = c.x * coz + c.y * -siz; // z
p.y = c.x * siz + c.y * coz;
c = p;
p.y = c.y * cox + c.z * six; // x
p.z = c.y * -six + c.z * cox;
c = p;
p.x = c.x * coy + c.z * siy; // y
p.z = c.x * -siy + c.z * coy;
//premiki
p.x = p.x + x;
p.y = p.y + y;
p.z = p.z + z;
//projekcija
Point3 raw;
double z_res = 1 / (distance - p.z);
raw.x = p.x;
raw.y = p.y;
raw.z = p.z;
RenderTransform tmp;
tmp.screenX = (raw.x * zoom) * z_res + (w / 2);
tmp.screenY = (raw.y * zoom) * z_res + (h / 2);
tmp.scale = z_res;
res.push_back(tmp);
}
return res;
}
void Camera3D::render3d(vector<Point3> pnts) {
vector<RenderTransform> projected = projectPoints(pnts);
SDL_SetRenderDrawColor(r, 255, 255, 255, 255);
for (int i = 0; i < projected.size(); i++) {
SDL_RenderDrawPoint(r, projected[i].screenX, projected[i].screenY);
}
}