forked from mremallin/christmas_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
christmas_tree.cpp
266 lines (211 loc) · 6.39 KB
/
christmas_tree.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
/*
* Christmas Tree - Brought to you by SDL2 and OpenGL
* Inspired by: https://github.com/anvaka/atree#
*
* Mike Mallin, 2019 - 2020
*/
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>
#include <cstring>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <GLES3/gl3.h>
#include <cglm/cglm.h>
#include <cglm/io.h>
extern "C" {
#include "shader.h"
#include "spiral.h"
}
static sf::Window* main_window;
static mat4 projection_matrix;
static mat4 view_matrix;
#define ERROR_LOG(...) (fprintf(stderr, __VA_ARGS__))
#define ELEMENTS_IN_ARRAY(_array) (sizeof((_array))/sizeof((_array[0])))
#define WINDOW_WIDTH 1280.0f
#define WINDOW_HEIGHT 800.0f
#define NUM_SPIRALS 4
static spiral rendered_spirals[NUM_SPIRALS];
static sf::Window *
get_window (void)
{
return main_window;
}
static void
free_spirals(void)
{
size_t i;
for (i = 0; i < ELEMENTS_IN_ARRAY(rendered_spirals); i++) {
spiral_free(rendered_spirals[i]);
}
}
static void
init_spirals(void)
{
size_t i;
spiral_init_ctx init_ctx = {
.num_slices = 400,
.num_rotations = 6,
.cycle_time_ms = 100000,
.y_max = 2.0f,
.slope = -3.0f,
.starting_angle_offset = 0.0f,
};
vec4 colors[ELEMENTS_IN_ARRAY(rendered_spirals)] = {
{0.0f, 0.5f, 0.4f, 1.0f},
{0.0f, 1.0f, 0.8f, 1.0f},
{0.535f, 0.1f, 0.1f, 1.0f},
{1.0f, 0.2f, 0.2f, 1.0f}
};
for (i = 0; i < ELEMENTS_IN_ARRAY(rendered_spirals); i += 2) {
memcpy(init_ctx.color, colors[i], sizeof(init_ctx.color));
rendered_spirals[i] = spiral_init(&init_ctx);
init_ctx.slope += 0.1f;
memcpy(init_ctx.color, colors[i+1], sizeof(init_ctx.color));
rendered_spirals[i+1] = spiral_init(&init_ctx);
init_ctx.starting_angle_offset += GLM_PI;
}
}
static void
at_exit (void)
{
if (get_window()) {
deinit_shaders();
free_spirals();
delete main_window;
main_window = NULL;
}
}
static void
upload_shader_constants (void)
{
glUniformMatrix4fv(get_vertex_uniform_projection(),
1, GL_FALSE, (GLfloat *)projection_matrix);
glUniformMatrix4fv(get_vertex_uniform_modelview(),
1, GL_FALSE, (GLfloat *)view_matrix);
}
/*
* https://www.3dgep.com/understanding-the-view-matrix/#The_View_Matrix
* https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix
* https://solarianprogrammer.com/2013/05/22/opengl-101-matrices-projection-view-model/
* https://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/#transformation-matrices
*/
static void
generate_projection_matrix(void)
{
/* Default perspective in 3D space is that the camera is
* looking down the Z-Axis (-Z is further into the screen).
*
* glm_lookat takes the following vectors: Eye, Center, Up
*/
vec3 eye{0.0f, 1.0f, 3.0f};
vec3 center{0, 0.92f, 0};
vec3 up{0, 1.0f, 0};
glm_lookat(eye, center, up, view_matrix);
glm_perspective_default((WINDOW_WIDTH/WINDOW_HEIGHT), projection_matrix);
}
static void
init_opengl (void)
{
glEnable(GL_PROGRAM_POINT_SIZE);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
}
static void
clear_window (void)
{
/* Clear both rendering buffers to black */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
main_window->display();
glClear(GL_COLOR_BUFFER_BIT);
main_window->display();
}
static void
update_frame (uint32_t delta_ms)
{
size_t i;
for (i = 0; i < ELEMENTS_IN_ARRAY(rendered_spirals); i++) {
spiral_update(rendered_spirals[i], delta_ms);
}
}
static void
render_frame (void)
{
size_t i;
glClear(GL_COLOR_BUFFER_BIT);
for (i = 0; i < ELEMENTS_IN_ARRAY(rendered_spirals); i++) {
spiral_render(rendered_spirals[i]);
}
}
static void
run_main_event_loop (void)
{
uint32_t frame_start_ticks = 0;
uint32_t frame_end_ticks = 0;
uint32_t frame_delta_ticks = 0;
printf("Entering main loop\n");
sf::Clock clock;
while (main_window->isOpen()) {
sf::Event event;
/* Bump the delta in case the framerate is too fast */
if (frame_delta_ticks == 0) {
frame_delta_ticks = 1;
}
printf("FPS: %3.3f\r", 1/(frame_delta_ticks*0.001f));
frame_start_ticks = clock.getElapsedTime().asMilliseconds();// SDL_GetTicks();
/* Process incoming events.
* NOTE: This will chew up 100% CPU.
* Would be nice to have a better way to wait between drawing frames */
while (main_window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {
main_window->close();
} else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Q) {
main_window->close();
}
}
}
update_frame(frame_delta_ticks);
render_frame();
/* Move the rendered buffer to the screen */
main_window->display();
sf::sleep(sf::milliseconds(1000/24 - clock.getElapsedTime().asMilliseconds() + frame_start_ticks));
frame_end_ticks = clock.getElapsedTime().asMilliseconds();
frame_delta_ticks = frame_end_ticks - frame_start_ticks;
}
printf("\nExiting...\n");
}
static void
init_sfml (void)
{
sf::ContextSettings settings;
settings.attributeFlags = sf::ContextSettings::Core;
settings.majorVersion = 3;
settings.minorVersion = 2;
main_window = new sf::RenderWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Christmas Tree",
sf::Style::Titlebar | sf::Style::Close, settings );
}
int
main (int argc, char *argv[])
{
/* Setup program exit cleanup routines */
atexit(at_exit);
init_sfml();
init_opengl();
generate_projection_matrix();
initialize_shaders();
init_spirals();
upload_shader_constants();
clear_window();
run_main_event_loop();
/* On Arch Linux, calling all the cleanup via at_exit was causing a
* double free due to GPU driver cleanup occurring before the
* registered at_exit. at_exit is called in the success path to fix this.
*/
at_exit();
return EXIT_SUCCESS;
}