Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
326 lines (261 loc) · 8.98 KB
/
Copy pathmain.cpp
File metadata and controls
326 lines (261 loc) · 8.98 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <iostream>
#include <Eigen/Dense>
#include <cmath>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <vector>
const double pi = 3.14159265358979323846;
// Convert cartesian to polar
Eigen::MatrixXd polConvert(const Eigen::MatrixXd& input) {
Eigen::VectorXd x,y,z;
x = input.row(0);
y = input.row(1);
z = input.row(2);
Eigen::VectorXd r(x.size());
Eigen::VectorXd p(x.size());
Eigen::VectorXd t(x.size());
Eigen::MatrixXd pol(3,x.size());
for (int i = 0; i < x.size(); ++i) {
r(i) = std::sqrt(x(i) * x(i) + y(i) * y(i) + z(i) * z(i));
// atan2 handles div by zero cases but is weird cus its two arguments: first / second
t(i) = std::atan2(y(i),x(i));
p(i) = std::atan2(std::sqrt(x(i)*x(i) + y(i)*y(i)) , z(i));
}
pol.row(0) = r;
pol.row(1) = t;
pol.row(2) = p;
return pol;
}
// define camera
class Camera {
public:
Eigen::Vector3d direction;
double fov; // IN RADIANS
Camera(const Eigen::Vector3d& direction_vec, const double view_fov):
direction(direction_vec),fov(view_fov) {
// Ensures direction is in polar coordinates
direction = polConvert(direction);
}
};
//define shape
class Shape {
public :
Eigen::MatrixXd orVerts;
Shape(int vertexNum) : orVerts(3,vertexNum) {}
// get polar vertices
virtual const Eigen::MatrixXd pVerts() const {
Eigen::MatrixXd pV;
pV << 0;
return pV;
};
};
// define cube
class Cube : public Shape {
public:
double sideLength;
Eigen::Vector3d center;
// Vertices pre-transformation, centered at true origin
Eigen::Matrix<double, 3, 8> orVerts;
// Constructor
Cube(double side_length, const Eigen::Vector3d& centerPoint)
: sideLength(side_length), center(centerPoint), Shape(8)
{
double h = sideLength / 2;
orVerts << h, h, h, h, -h, -h, -h, -h,
h, h, -h, -h, h, h, -h, -h,
h, -h, h, -h, h, -h, h, -h;
}
// Member Function
const Eigen::MatrixXd pVerts() const {
Eigen::MatrixXd polVerts;
polVerts = orVerts.colwise() + center;
polVerts = polConvert(polVerts);
return polVerts;
};
};
// define render option setter
class RenderOptions {
public:
int width;
int height;
float framerate;
RenderOptions(int window_width, int window_height, float window_framerate):
width(window_width), height(window_height), framerate(window_framerate) {}
const int getWidth() const {
return width;
};
public:
const int getHeight() const {
return height;
};
public:
const int getFramerate() const {
return framerate;
};
};
// Project polar to 2D coordinates:
Eigen::MatrixXd project(const Eigen::MatrixXd& polarCoords, const Camera& cam, const RenderOptions& rend) {
// separation variable
assert(polarCoords.rows() == 3);
Eigen::MatrixXd sep = polarCoords.colwise() - cam.direction;
double scale = rend.getWidth() / cam.fov;
// convert to screen coordinates
sep.row(1) = (sep.row(1).array() * scale).round() + (rend.getWidth() / 2);
sep.row(2) = (sep.row(2).array() * scale).round() + (rend.getHeight() / 2);
return sep;
};
std::vector<sf::CircleShape> show(const Eigen::MatrixXd& points) {
std::vector<sf::CircleShape> verts;
int ptCount = points.cols();
for (int i = 0; i < ptCount; ++i) {
sf::CircleShape vertex(50 / (points(0,i) * points(0,i)));
vertex.setPosition(points(1,i),points(2,i));
vertex.setFillColor(sf::Color::Cyan);
verts.push_back(vertex);
}
return verts;
}
int main() {
Eigen::Vector3d displace(4,0,0);
Cube cube(2,displace);
Eigen::Vector3d camDir(1,0,0);
Camera cam(camDir,2);
RenderOptions rend(800,600,75);
Eigen::Matrix3d zRotL,zRotR,yRotL,yRotR;
zRotL << 0.99863, -0.052336, 0,
0.052336, 0.99863, 0,
0, 0, 1;
zRotR << 0.99863, 0.052336, 0,
-0.052336, 0.99863, 0,
0, 0, 1;
yRotR << 0.99863, 0, 0.052336,
0, 1, 0,
-0.052336, 0, 0.99863;
yRotL << 0.99863, 0, -0.052336,
0, 1, 0,
0.052336, 0, 0.99863;
bool right = false;
bool left = false;
bool a = false;
bool d = false;
bool w = false;
bool s = false;
bool up = false;
bool down = false;
sf::RenderWindow window(sf::VideoMode(rend.getWidth(), rend.getHeight()), "Renderer");
window.setFramerateLimit(rend.getFramerate());
while (window.isOpen()) {
// Closes window
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
// motion controls
//key pressed
if (event.type == sf::Event::KeyPressed) {
if (event.key.scancode == sf::Keyboard::Scan::Right)
{
right = true;
}
if (event.key.scancode == sf::Keyboard::Scan::Up)
{
up = true;
}
if (event.key.scancode == sf::Keyboard::Scan::Down)
{
down = true;
}
if (event.key.scancode == sf::Keyboard::Scan::Left)
{
left = true;
}
if (event.key.scancode == sf::Keyboard::Scan::D)
{
d = true;
}
if (event.key.scancode == sf::Keyboard::Scan::A)
{
a = true;
}
if (event.key.scancode == sf::Keyboard::Scan::W)
{
w = true;
}
if (event.key.scancode == sf::Keyboard::Scan::S)
{
s = true;
}
}
// key released
if (event.type == sf::Event::KeyReleased) {
if (event.key.scancode == sf::Keyboard::Scan::Right)
{
right = false;
}
if (event.key.scancode == sf::Keyboard::Scan::Left)
{
left = false;
}
if (event.key.scancode == sf::Keyboard::Scan::Up)
{
up = false;
}
if (event.key.scancode == sf::Keyboard::Scan::Down)
{
down = false;
}
if (event.key.scancode == sf::Keyboard::Scan::D)
{
d = false;
}
if (event.key.scancode == sf::Keyboard::Scan::A)
{
a = false;
}
if (event.key.scancode == sf::Keyboard::Scan::W)
{
w = false;
}
if (event.key.scancode == sf::Keyboard::Scan::S)
{
s = false;
}
}
// results
if (right == true) {
cam.direction = zRotR * cam.direction;
}
if (left == true) {
cam.direction = zRotL * cam.direction;
}
if (d == true) {
cube.orVerts = zRotR * cube.orVerts;
}
if (a == true) {
cube.orVerts = zRotL * cube.orVerts;
}
if (w == true) {
cube.orVerts = yRotR * cube.orVerts;
}
if (s == true) {
cube.orVerts = yRotL * cube.orVerts;
}
if (up == true) {
cube.center = cube.center + 0.1 * (cam.direction / cam.direction.norm());
}
if (down == true) {
cube.center = cube.center - 0.1 * (cam.direction / cam.direction.norm());
}
// Starts new frame
window.clear(sf::Color::Black);
std::vector<sf::CircleShape> circles = show(project(cube.pVerts(), cam, rend));
for (auto& circle : circles) {
window.draw(circle);
}
// Shows drawn frame
window.display();
}
}
return 1;
}