-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_first_SFML.cpp
197 lines (183 loc) · 5.77 KB
/
my_first_SFML.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
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
#include <SFML/Graphics.hpp>
#include <SFML/System/Vector3.hpp>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
using namespace sf;
int dir = 0;
int graph[8][3] = {{2, 4, 5}, {1, 3, 6}, {2, 4, 7}, {1, 3, 8}, {1, 6, 8}, {2, 5, 7}, {3, 6, 8}, {4, 5, 7}};
struct World
{
float x, y, z, screen_x, screen_y;
} w[100];
struct Camera
{
float x = 0, y = 0, z = 250, theta_x = 0, theta_y = 0, theta_z = 0;
}camera;
float Rotation1(float theta, float a, float b);
float Rotation2(float theta, float a, float b);
float Rotation3(float theta, float a, float b);
void SpawnObject()
{
w[1].x = -50;
w[1].y = 50;
w[1].z = 50;
w[2].x = 50;
w[2].y = 50;
w[2].z = 50;
w[3].x = 50;
w[3].y = -50;
w[3].z = 50;
w[4].x = -50;
w[4].y = -50;
w[4].z = 50;
w[5].x = -50;
w[5].y = 50;
w[5].z = -50;
w[6].x = 50;
w[6].y = 50;
w[6].z = -50;
w[7].x = 50;
w[7].y = -50;
w[7].z = -50;
w[8].x = -50;
w[8].y = -50;
w[8].z = -50;
}
void SetCamera(int order)
{
float x = camera.x, y = camera.y, z = camera.z;
if (order == 1) {
camera.theta_x += (float)5 / 180 * M_PI;
} else if (order == 2) {
camera.theta_x -= (float)5 / 180 * M_PI;
} else if (order == 3) {
camera.theta_z += (float)5 / 180 * M_PI;
} else if (order == 4) {
camera.theta_z -= (float)5 / 180 * M_PI;
}
x = Rotation1(camera.theta_z, 0, 0);
y = Rotation2(camera.theta_z, 0, 0);
z = Rotation1(camera.theta_y, 250, x);
camera.x = Rotation3(camera.theta_y, x, 250);
camera.y = Rotation1(camera.theta_x, y, z);
camera.z = Rotation2(camera.theta_x, y, z);
}
void Mapping()
{
float t, x, y, z;
sf::Vector3f vec(0, 0, 0);
for (int i = 1; i <= 8; i++) {
vec.x = w[i].x - camera.x;
vec.y = w[i].y - camera.y;
vec.z = w[i].z - camera.z;
t = -(camera.x*camera.x+camera.y*camera.y+camera.z*camera.z) / (camera.x*vec.x + camera.y*vec.y + camera.z*vec.z);
vec.x = vec.x*t + camera.x;
vec.y = vec.y*t + camera.y;
vec.z = vec.z*t + camera.z;
y = Rotation1(-camera.theta_x, vec.y, vec.z);
z = Rotation2(-camera.theta_x, vec.y, vec.z);
x = Rotation3(-camera.theta_y, vec.x, z);
z = Rotation1(-camera.theta_y, z, vec.x);
w[i].screen_x = Rotation1(-camera.theta_z, x, y);
w[i].screen_y = Rotation2(-camera.theta_z, x, y);
}
}
float Rotation1(float theta, float a, float b){
float output;
output = (float)(cos(theta)*a - sin(theta)*b);
return output;
}
float Rotation2(float theta, float a, float b){
float output;
output = (float)(sin(theta)*a + cos(theta)*b);
return output;
}
float Rotation3(float theta, float a, float b){
float output;
output = (float)(cos(theta)*a + sin(theta)*b);
return output;
}
int main()
{
srand(time(0));
RenderWindow window(sf::VideoMode(500, 500), "SFML");
CircleShape circle0(3.f), circle1(3.f), dot(1.f);
circle0.setFillColor(sf::Color::White);
circle1.setFillColor(sf::Color::Green);
Clock clock;
float timer = 0, delay = 0.1;
SpawnObject();
while (window.isOpen()) {
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
Event e;
while (window.pollEvent(e)) {
if (e.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Down)) {
dir = 1;
} else if (Keyboard::isKeyPressed(Keyboard::Up)) {
dir = 2;
} else if (Keyboard::isKeyPressed(Keyboard::Left)) {
dir = 3;
} else if (Keyboard::isKeyPressed(Keyboard::Right)) {
dir = 4;
} else {
dir = 0;
}
if (timer > delay) {
timer = 0;
SetCamera(dir);
dir = 0;
Mapping();
}
window.clear();
// for (int i = 1; i <= 4; i++) {
// circle0.setPosition(w[i].screen_x + 250, -w[i].screen_y + 250);
// window.draw(circle0);
// }
float vec_x, vec_y, t = 2;
for (int i = 1; i <= 8; i++) {
circle1.setPosition(w[i].screen_x + 250, -w[i].screen_y + 250);
window.draw(circle1);
for (int j = 0; j < 3; j++) {
vec_x = w[graph[i-1][j]].screen_x - w[i].screen_x;
vec_y = (w[graph[i-1][j]].screen_y - w[i].screen_y) / (float)abs(vec_x);
while (t < vec_x) {
dot.setPosition((w[i].screen_x + t) + 250, -(w[i].screen_y + vec_y * t) + 250);
window.draw(dot);
t += 2;
}
t= 2;
}
for (int j = 0; j < 3; j++) {
vec_y = w[graph[i-1][j]].screen_y - w[i].screen_y;
vec_x = (w[graph[i-1][j]].screen_x - w[i].screen_x) / (float)abs(vec_y);
while (t < vec_y) {
dot.setPosition((w[i].screen_x + vec_x * t) + 250, -(w[i].screen_y + t) + 250);
window.draw(dot);
t += 2;
}
t= 2;
}
}
// for (int i = 1; i <= 8; i++) {
// for (int j = 0; j < 3; j++) {
// vec_x = w[graph[i-1][j]].screen_x - w[i].screen_x;
// vec_y = (w[graph[i-1][j]].screen_y - w[i].screen_y) / (float)abs(vec_x);
// while (t < vec_x) {
// dot.setPosition((w[i].screen_x + t) + 250, -(w[i].screen_y + vec_y * t) + 250);
// window.draw(dot);
// t += 2;
// }
// t= 2;
// }
// }
window.display();
}
return 0;
}