-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject3D.cpp
More file actions
182 lines (170 loc) · 5.76 KB
/
Object3D.cpp
File metadata and controls
182 lines (170 loc) · 5.76 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
/*
Object3D class function definitions
*/
// Include libraries
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>
// Include local header files
#include "Object3D.h"
// Create Object3D object
Object3D::Object3D() {
// Set coords for each point
x_coords = new double[8]{1, 1, -1, -1, 1, 1, -1, -1};
y_coords = new double[8]{1, -1, 1, -1, 1, -1, 1, -1};
z_coords = new double[8]{-1, -1, -1, -1, 1, 1, 1, 1};
// Set focal length and camera position
focal_length = 5;
camera = new double[3]{0, 0, -2};
// Set pitch, yaw, and roll angles
pitch_angle = 0;
yaw_angle = 0;
roll_angle = 0;
// Set coords for each temp rot point
x_rot_coords = new double[8]{0};
y_rot_coords = new double[8]{0};
z_rot_coords = new double[8]{0};
// Create a vertex array
vertices = sf::VertexArray(sf::PrimitiveType::Points, 8);
// Calc 2d pos of 3d points for first time, also cache them so x and y camera
// movements don't need to recompute y and x 2d coords
// Reset rot coords
for (int i = 0; i < 8; i++) {
x_rot_coords[i] = x_coords[i];
y_rot_coords[i] = y_coords[i];
z_rot_coords[i] = z_coords[i];
}
set2DPosOfPoints();
// Load font
font = new sf::Font;
bool isExisting = font->openFromFile("PublicPixel.ttf");
if (!isExisting) {
std::cout << "Font failed to load.\n";
exit(1);
}
font->setSmooth(false);
// Create new text object and set properties
text_obj = new sf::Text(*font);
text_obj->setCharacterSize(8);
text_obj->setFillColor(sf::Color::White);
text_obj->setPosition({4, 4});
}
// Get inputs and do stuff with them
void Object3D::getInputs(sf::Keyboard::Key x_dec, sf::Keyboard::Key x_inc,
sf::Keyboard::Key y_dec, sf::Keyboard::Key y_inc,
sf::Keyboard::Key z_dec, sf::Keyboard::Key z_inc,
sf::Keyboard::Key x_angle_dec, sf::Keyboard::Key x_angle_inc,
sf::Keyboard::Key y_angle_dec, sf::Keyboard::Key y_angle_inc,
sf::Keyboard::Key z_angle_dec, sf::Keyboard::Key z_angle_inc,
sf::Keyboard::Key focal_dec, sf::Keyboard::Key focal_inc) {
// If user presses key, move/rotate points
if (sf::Keyboard::isKeyPressed(x_dec)) {
camera[0] += -0.1;
}
if (sf::Keyboard::isKeyPressed(x_inc)) {
camera[0] += 0.1;
}
if (sf::Keyboard::isKeyPressed(y_dec)) {
camera[1] += -0.1;
}
if (sf::Keyboard::isKeyPressed(y_inc)) {
camera[1] += 0.1;
}
if (sf::Keyboard::isKeyPressed(z_dec)) {
camera[2] += -0.1;
}
if (sf::Keyboard::isKeyPressed(z_inc)) {
camera[2] += 0.1;
}
if (sf::Keyboard::isKeyPressed(x_angle_dec)) {
pitch_angle += -0.1;
}
if (sf::Keyboard::isKeyPressed(x_angle_inc)) {
pitch_angle += 0.1;
}
if (sf::Keyboard::isKeyPressed(y_angle_dec)) {
yaw_angle += -0.1;
}
if (sf::Keyboard::isKeyPressed(y_angle_inc)) {
yaw_angle += 0.1;
}
if (sf::Keyboard::isKeyPressed(z_angle_dec)) {
roll_angle += -0.1;
}
if (sf::Keyboard::isKeyPressed(z_angle_inc)) {
roll_angle += 0.1;
}
if (sf::Keyboard::isKeyPressed(focal_dec)) {
focal_length += -0.1;
}
if (sf::Keyboard::isKeyPressed(focal_inc)) {
focal_length += 0.1;
}
}
// Calc roll (z rotation)
void Object3D::calcRoll(int i) {
x_rot_coords[i] = x_coords[i] * cos(roll_angle) - z_coords[i] * sin(roll_angle);
y_rot_coords[i] = y_coords[i];
z_rot_coords[i] = x_coords[i] * sin(roll_angle) - z_coords[i] * cos(roll_angle);
}
// Calc pitch (x rotation)
void Object3D::calcPitch(int i) {
x_rot_coords[i] = x_rot_coords[i];
y_rot_coords[i] = cos(pitch_angle) * (y_rot_coords[i] - camera[1]) + sin(pitch_angle) * (camera[2] - z_rot_coords[i]) + camera[1];
z_rot_coords[i] = sin(pitch_angle) * (y_rot_coords[i] - camera[1]) + cos(pitch_angle) * (z_rot_coords[i] - camera[2]) + camera[2];
}
// Calc yaw (y rotation)
void Object3D::calcYaw(int i) {
x_rot_coords[i] = cos(yaw_angle) * (x_rot_coords[i] - camera[0]) + sin(yaw_angle) * (camera[2] - z_rot_coords[i]) + camera[0];
y_rot_coords[i] = y_rot_coords[i];
z_rot_coords[i] = sin(yaw_angle) * (x_rot_coords[i] - camera[0]) + cos(yaw_angle) * (z_rot_coords[i] - camera[2]) + camera[2];
}
// Set the 2d position of each 3d point
void Object3D::set2DPosOfPoints() {
// For loop that uses functions to calc 2d position for each 3d point
for (int i = 0; i < 8; i++) {
// Calc rotations
calcRoll(i);
calcPitch(i);
calcYaw(i);
// Calc 2d pos of 3d points after rotation
vertices[i].position = sf::Vector2f(calc2DXPos(i) + 160, calc2DYPos(i) + 120);
}
}
// Calc 2d xpos
double Object3D::calc2DXPos(int i) {
// std::cout << "2D X of point index " << i << ": " << focal_length * (x_coords[i] - camera[0]) / (z_coords[i] - camera[2]) << std::endl;
return focal_length * (x_rot_coords[i] - camera[0]) / (z_rot_coords[i] - camera[2]);
}
// Calc 2d ypos
double Object3D::calc2DYPos(int i) {
// std::cout << "2D Y of point index " << i << ": " << focal_length * (y_coords[i] - camera[1]) / (z_coords[i] - camera[2]) << std::endl;
return focal_length * (y_rot_coords[i] - camera[1]) / (z_rot_coords[i] - camera[2]);
}
// Render Object3D
void Object3D::renderObject3D(sf::RenderWindow* win) {
win->draw(vertices);
}
// Render text object
void Object3D::renderTextObject(sf::RenderWindow* win) {
// Change string set
text_obj->setString("CAM X: " + std::to_string(camera[0]) +
"\nCAM Y: " + std::to_string(camera[1]) +
"\nCAM Z: " + std::to_string(camera[2]) +
"\nFOCAL LENGTH: " + std::to_string(focal_length) +
"\nPITCH ANGLE: " + std::to_string(pitch_angle) +
"\nYAW ANGLE: " + std::to_string(yaw_angle) +
"\nROLL ANGLE: " + std::to_string(roll_angle));
win->draw(*text_obj);
}
// Delete all Object3D related objects
Object3D::~Object3D() {
// Delete all dynamic arrays
delete x_coords;
delete y_coords;
delete z_coords;
delete camera;
delete x_rot_coords;
delete y_rot_coords;
delete z_rot_coords;
}