-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
354 lines (329 loc) · 14.2 KB
/
main.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
row x column
currentPoint[0][0] x
currentPoint[0][1] y
currentPoint[0][2] z
*/
#include "matrixMath.h"
#include "slider.h"
#include <SDL_ttf.h>
#include <iostream>
#include <vector>
#include <SDL.h>
#include <cmath>
#include <string>
int WIDTH = 1280;
int HEIGHT = 720;
double AR = ((double)HEIGHT / WIDTH);
const int textHeight = 20;
void SDL_RenderDrawPointFloat(SDL_Renderer *renderer, double x, double y);
void SDL_RenderDrawLineFloat(SDL_Renderer *renderer, double x, double y, double x1, double y1);
void DrawCircle(SDL_Renderer *renderer, double centreXX, double centreYY, int32_t radius);
void renderText(const char *message, TTF_Font *font, SDL_Renderer *renderer, int x, int y, Uint8 r, Uint8 g, Uint8 b);
void drawFPS(int fps, TTF_Font *font, SDL_Renderer *renderer);
int main(int argc, char *argv[])
{
double angleX = 0, angleY = 0, angleZ = 0;
double scale = 1.5f;
double distance = 2;
bool quit = false, fullScreen = false;
bool rotX = false, rotY = false, rotZ = false;
vec3d rotatepoint = {0, 0, 0};
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Event event;
SDL_DisplayMode *beforeMode;
TTF_Init();
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer);
SDL_SetWindowTitle(window, "3D rotating cube");
TTF_Font *consola = TTF_OpenFont("cour.ttf", textHeight);
TTF_Font *consolaFat = TTF_OpenFont("cour.ttf", textHeight * 3);
if (!consola)
{
(consola) = TTF_OpenFont("C:/Windows/Fonts/cour.ttf", textHeight);
(consolaFat) = TTF_OpenFont("C:/Windows/Fonts/cour.ttf", textHeight * 3);
if (!consola)
SDL_ShowSimpleMessageBox(0, "Missing Fonts", "Looks like there are some fonts missing, add cour.tff", window);
}
matrix points = {{-0.25f, -0.25f, 0.25f}, //8 cube vertices
{0.25f, -0.25f, 0.25f},
{0.25f, 0.25f, 0.25f},
{-0.25f, 0.25f, 0.25f},
{-0.25f, -0.25f, -0.25f},
{0.25f, -0.25f, -0.25f},
{0.25f, 0.25f, -0.25f},
{-0.25f, 0.25f, -0.25f},
{0.0f, 0.0f, 0.0f}
};
points = matScale(points, scale);
Uint32 start_time, frame_time;
int fps;
SDL_Color bg_passiv = {200, 200, 200};
SDL_Color bg_hover = {123, 129, 133};
// SDL_Color fg_passive = {255, 0, 0};
// SDL_Color fg_hover = {200, 0, 0};
// SDL_Color fg_click = {170, 0, 0};
SDL_Color fg_passive = {0, 255, 0};
SDL_Color fg_hover = {0, 200, 0};
SDL_Color fg_click = {0, 170, 0};
Slider XSlider(400, textHeight * 2.5 + 2, textHeight - 4, 300, 0.0f, 2 * M_PI, renderer, bg_passiv, bg_hover, fg_passive, fg_hover, fg_click);
Slider YSlider(400, textHeight * 3.5 + 2, textHeight - 4, 300, 0.0f, 2 * M_PI, renderer, bg_passiv, bg_hover, fg_passive, fg_hover, fg_click);
Slider ZSlider(400, textHeight * 4.5 + 2, textHeight - 4, 300, 0.0f, 2 * M_PI, renderer, bg_passiv, bg_hover, fg_passive, fg_hover, fg_click);
Slider distanceSlider(400, textHeight * 5.5 + 2, textHeight - 4, 300, 0.5f, 4.0f, renderer, bg_passiv, bg_hover, fg_passive, fg_hover, fg_click);
distanceSlider.setValue(2.0f);
while (!quit)
{
start_time = SDL_GetTicks();
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
XSlider.tick(&event);
YSlider.tick(&event);
ZSlider.tick(&event);
distanceSlider.tick(&event);
break;
case SDL_QUIT:
quit = true;
break;
case SDL_WINDOWEVENT:
switch (event.window.event)
{
case SDL_WINDOWEVENT_SIZE_CHANGED:
WIDTH = event.window.data1;
HEIGHT = event.window.data2;
AR = ((double)HEIGHT / WIDTH);
break;
}
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_w:
points = matMove(points, 0, -0.01f, 0);
rotatepoint.y -= 0.01f;
break;
case SDLK_s:
points = matMove(points, 0, 0.01f, 0);
rotatepoint.y += 0.01f;
break;
case SDLK_a:
points = matMove(points, -0.01f, 0, 0);
rotatepoint.x -= 0.01f;
break;
case SDLK_d:
points = matMove(points, 0.01f, 0, 0);
rotatepoint.x += 0.01f;
break;
case SDLK_UP:
points = matMove(points, 0, 0, -0.01f);
rotatepoint.z -= 0.01f;
break;
case SDLK_DOWN:
points = matMove(points, 0, 0, 0.01f);
rotatepoint.z += 0.01f;
break;
case SDLK_LEFT:
distance -= 0.1f;
break;
case SDLK_RIGHT:
distance += 0.1f;
break;
case SDLK_x:
rotX = !rotX;
break;
case SDLK_y:
rotY = !rotY;
break;
case SDLK_z:
rotZ = !rotZ;
break;
case SDLK_F11:
fullScreen = !fullScreen;
if (fullScreen)
{
SDL_DisplayMode *mode;
SDL_GetWindowDisplayMode(window, beforeMode);
// for (int i = 0; i < SDL_GetNumVideoDisplays(); ++i)
// {
// int should_be_zero = SDL_GetCurrentDisplayMode(i, mode);
// if (should_be_zero != 0)
// // In case of error...
// SDL_Log("Could not get display mode for video display #%d: %s", i, SDL_GetError());
// else
// // On success, print the current display mode.
// SDL_Log("Display #%d: current display mode is %dx%dpx @ %dhz.", i, mode->w, mode->h, mode->refresh_rate);
// }
std::cout << SDL_GetError();
SDL_GetCurrentDisplayMode(0, mode);
std::cout << SDL_GetError();
SDL_SetWindowSize(window, mode->w, mode->h);
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
}
else
{
SDL_SetWindowFullscreen(window, 0);
SDL_RenderPresent(renderer);
SDL_SetWindowSize(window, beforeMode->w, beforeMode->h);
}
break;
}
break;
}
}
if (rotX)
angleX += 0.003f;
else
angleX = XSlider.value;
if (rotY)
angleY += 0.006f;
else
angleY = YSlider.value;
if (rotZ)
angleZ += 0.009f;
else
angleZ = ZSlider.value;
distance = distanceSlider.value;
matrix drawPoints;
for (int i = 0; i < points.size(); i++)
{
matrix point{{points[i][0]}, {points[i][1]}, {points[i][2]}}; //get the point ready for matrix multiplication
point[0][0] -= rotatepoint.x; // the rotation matrix around an arbitrary point can be expressed as moving the rotation point to the origin, rotating around the origin and moving back to the original position: https://bit.ly/3mJ3Q2v
point[1][0] -= rotatepoint.y;
point[2][0] -= rotatepoint.z;
rotateX(point, angleX);
rotateY(point, angleY);
rotateZ(point, angleZ);
point[0][0] += rotatepoint.x;
point[1][0] += rotatepoint.y;
point[2][0] += rotatepoint.z;
project(point, distance);
drawPoints.push_back({point[0][0], point[1][0]});
}
std::vector<std::string> textLeft = {
"3D Rotating Cube by thomas weichhart",
" ",
"X angle: " + std::to_string(angleX * (180 / M_PI)) + "10\xB0",
"Y angle: " + std::to_string(angleY * (180 / M_PI)) + "10\xB0",
"Z angle: " + std::to_string(angleZ * (180 / M_PI)) + "10\xB0",
"camera distance: " + std::to_string(distance),
"vertices: " + std::to_string(points.size()),
"rotation point/center of cube:",
" x: " + std::to_string(rotatepoint.x),
" y: " + std::to_string(rotatepoint.y),
" z: " + std::to_string(rotatepoint.z)
};
std::vector<std::string> textRight = {
"Controls:",
"W to move cube up",
"S to move cube down",
"A to move cube left",
"D to move cube right",
"Up Arrow to move cube closer to camera",
"Down Arrow to move cube away from camera",
"Left/Right Arrow to change distance",
"X/Y/Z to toggle between auto rotation and manual rotation using sliders for X/Y/Z axis",
"F11 to toggle fullscreen"};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
XSlider.show();
YSlider.show();
ZSlider.show();
distanceSlider.show();
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
for (int i = 0; i < 4; i++) //draw cube edges
{
SDL_RenderDrawLineFloat(renderer, drawPoints[i][0], drawPoints[i][1], drawPoints[(i + 1) % 4][0], drawPoints[(i + 1) % 4][1]);
SDL_RenderDrawLineFloat(renderer, drawPoints[i + 4][0], drawPoints[i + 4][1], drawPoints[((i + 1) % 4) + 4][0], drawPoints[((i + 1) % 4) + 4][1]);
SDL_RenderDrawLineFloat(renderer, drawPoints[i][0], drawPoints[i][1], drawPoints[i + 4][0], drawPoints[i + 4][1]);
}
for (int i = 0; i < drawPoints.size(); i++)
DrawCircle(renderer, drawPoints[i][0], drawPoints[i][1], 5);
for (int i = 0; i < textLeft.size(); i++)
renderText(textLeft[i].c_str(), consola, renderer, textHeight / 2, textHeight / 2 + i * textHeight, 0, 255, 0);
for (int i = 0; i < textRight.size(); i++)
renderText(textRight[i].c_str(), consola, renderer, textHeight / 2, i * textHeight - textHeight / 2 + (HEIGHT - textHeight * textRight.size()), 0, 255, 0);
drawFPS(fps, consolaFat, renderer);
Uint32 timeout = SDL_GetTicks() + (int)((1 / 80.) * 1000);
while (!SDL_TICKS_PASSED(SDL_GetTicks(), timeout))
{
}
frame_time = SDL_GetTicks() - start_time;
fps = (frame_time > 0) ? 1000.0f / frame_time : 0.0f;
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
void drawFPS(int fps, TTF_Font *font, SDL_Renderer *renderer)
{
SDL_Color alienGreen = {0, 255, 0};
SDL_Surface *surfaceMessage = TTF_RenderText_Solid(font, ("fps: " + std::to_string(fps)).c_str(), alienGreen);
SDL_Texture *textureMessage = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_Rect messageRect = {WIDTH - surfaceMessage->w - textHeight / 2, textHeight / 2, surfaceMessage->w, surfaceMessage->h};
SDL_RenderCopy(renderer, textureMessage, NULL, &messageRect);
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(textureMessage);
}
void renderText(const char *message, TTF_Font *font, SDL_Renderer *renderer, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
SDL_Color alienGreen = {r, g, b};
SDL_Surface *surfaceMessage = TTF_RenderText_Solid(font, message, alienGreen);
SDL_Texture *textureMessage = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_Rect messageRect = {x, y, surfaceMessage->w, surfaceMessage->h};
SDL_RenderCopy(renderer, textureMessage, NULL, &messageRect);
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(textureMessage);
}
void SDL_RenderDrawPointFloat(SDL_Renderer *renderer, double x, double y)
{
SDL_RenderDrawPointF(renderer, map(x * AR, -1.0f, 1.0f, 0, WIDTH), map(y, -1.0f, 1.0f, 0, HEIGHT));
}
void SDL_RenderDrawLineFloat(SDL_Renderer *renderer, double x, double y, double x1, double y1)
{
SDL_RenderDrawLineF(renderer, map(x * AR, -1.0f, 1.0f, 0, WIDTH), map(y, -1.0f, 1.0f, 0, HEIGHT), map(x1 * AR, -1.0f, 1.0f, 0, WIDTH), map(y1, -1.0f, 1.0f, 0, HEIGHT));
}
void DrawCircle(SDL_Renderer *renderer, double centreXX, double centreYY, int32_t radius)
{
//https://stackoverflow.com/questions/38334081/howto-draw-circles-arcs-and-vector-graphics-in-sdl
const int32_t diameter = (radius * 2);
double centreX = map(centreXX * AR, -1.0f, 1.0f, 0, WIDTH); //translate
double centreY = map(centreYY, -1.0f, 1.0f, 0, HEIGHT);
double x = (radius - 1);
double y = 0;
double tx = 1;
double ty = 1;
double error = (tx - diameter);
while (x >= y)
{
// Each of the following renders an octant of the circle
SDL_RenderDrawPoint(renderer, centreX + x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX + x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX + y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX + y, centreY + x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY + x);
if (error <= 0)
{
++y;
error += ty;
ty += 2;
}
if (error > 0)
{
--x;
tx += 2;
error += (tx - diameter);
}
}
}