-
Notifications
You must be signed in to change notification settings - Fork 3
/
planet.cpp
811 lines (523 loc) · 20.4 KB
/
planet.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
GLM header include directives. Please use GLM 0.9.9.3 or greater for known
results. Previous versions of GLM are not guaranteed to work correctly.
*/
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
/*
libnoise header include directives. libnoise is used to generate coherent
noise for generating procedural planets.
*/
#include <noise/noise.h>
/*
noiseutils header include directives. noiseutils is used as a utility library
on top of libnoise.
*/
#include "noiseutils.h"
/*
GLAD header include directives. GLAD is used to load OpenGL 3.3 Core
functions.
*/
#include "glad.h"
/*
SDL header include directives. Please use SDL 2.0.9 or greater for known
results. Previous versions of SDL are not guaranteed to work correctly.
*/
#include <SDL2/SDL.h>
/*
Standard header include directives.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <tuple>
/*
A std::tuple<int, int, int> is used to represent a triangle defined by indices
in a list of vertices.
*/
typedef std::tuple<int, int, int> triangle_indices;
/*
Add a vertex to a std::vector<glm::vec3> while ensuring that the vertex lies
on the unit sphere.
*/
int add_vertex(std::vector<glm::vec3>& vector, glm::vec3 vertex)
{
vector.push_back(vertex / glm::length(vertex));
return vector.size() - 1;
}
/*
Return the index of a vertex in the middle of p_1 and p_2.
*/
int get_middle_point(std::vector<glm::vec3>& vector, int p_1, int p_2)
{
glm::vec3 pt_1 = vector[p_1];
glm::vec3 pt_2 = vector[p_2];
glm::vec3 pt_middle = (pt_1 + pt_2) / 2.0f;
int i = add_vertex(vector, pt_middle);
return i;
}
/*
Create an icosphere with the given amount of subdivisions.
*/
std::vector<glm::vec3> create_icosphere(int subdivisions = 8)
{
// Generate the icosphere's vertices.
std::vector<glm::vec3> icosphere_vertices;
// Generate the 12 vertices of an icosahedron.
float t = (1.0f + sqrt(5.0f)) / 2.0f;
add_vertex(icosphere_vertices, glm::vec3(-1.0f, t, 0.0f));
add_vertex(icosphere_vertices, glm::vec3( 1.0f, t, 0.0f));
add_vertex(icosphere_vertices, glm::vec3(-1.0f, -t, 0.0f));
add_vertex(icosphere_vertices, glm::vec3( 1.0f, -t, 0.0f));
add_vertex(icosphere_vertices, glm::vec3(0.0f, -1.0f, t));
add_vertex(icosphere_vertices, glm::vec3(0.0f, 1.0f, t));
add_vertex(icosphere_vertices, glm::vec3(0.0f, -1.0f, -t));
add_vertex(icosphere_vertices, glm::vec3(0.0f, 1.0f, -t));
add_vertex(icosphere_vertices, glm::vec3( t, 0.0f, -1.0f));
add_vertex(icosphere_vertices, glm::vec3( t, 0.0f, 1.0f));
add_vertex(icosphere_vertices, glm::vec3(-t, 0.0f, -1.0f));
add_vertex(icosphere_vertices, glm::vec3(-t, 0.0f, 1.0f));
// Generate the 20 faces of an icosahedron.
std::vector<triangle_indices> icosphere_indices;
icosphere_indices.push_back(triangle_indices(0x0, 0xB, 0x5));
icosphere_indices.push_back(triangle_indices(0x0, 0x5, 0x1));
icosphere_indices.push_back(triangle_indices(0x0, 0x1, 0x7));
icosphere_indices.push_back(triangle_indices(0x0, 0x7, 0xA));
icosphere_indices.push_back(triangle_indices(0x0, 0xA, 0xB));
icosphere_indices.push_back(triangle_indices(0x1, 0x5, 0x9));
icosphere_indices.push_back(triangle_indices(0x5, 0xB, 0x4));
icosphere_indices.push_back(triangle_indices(0xB, 0xA, 0x2));
icosphere_indices.push_back(triangle_indices(0xA, 0x7, 0x6));
icosphere_indices.push_back(triangle_indices(0x7, 0x1, 0x8));
icosphere_indices.push_back(triangle_indices(0x3, 0x9, 0x4));
icosphere_indices.push_back(triangle_indices(0x3, 0x4, 0x2));
icosphere_indices.push_back(triangle_indices(0x3, 0x2, 0x6));
icosphere_indices.push_back(triangle_indices(0x3, 0x6, 0x8));
icosphere_indices.push_back(triangle_indices(0x3, 0x8, 0x9));
icosphere_indices.push_back(triangle_indices(0x4, 0x9, 0x5));
icosphere_indices.push_back(triangle_indices(0x2, 0x4, 0xB));
icosphere_indices.push_back(triangle_indices(0x6, 0x2, 0xA));
icosphere_indices.push_back(triangle_indices(0x8, 0x6, 0x7));
icosphere_indices.push_back(triangle_indices(0x9, 0x8, 0x1));
// Subdivide the icosphere.
for (int i = 0; i < subdivisions; i++)
{
// Generate a temporary mesh to hold the result of the next
// subdivision.
std::vector<triangle_indices> new_icosphere_indices;
// Subdivide each triangle in the current mesh.
for (int j = 0; j < icosphere_indices.size(); j++)
{
triangle_indices tri = icosphere_indices[j];
int a = get_middle_point(icosphere_vertices, std::get<0>(tri), std::get<1>(tri));
int b = get_middle_point(icosphere_vertices, std::get<1>(tri), std::get<2>(tri));
int c = get_middle_point(icosphere_vertices, std::get<2>(tri), std::get<0>(tri));
// Add the 4 new triangles to the temporary mesh.
new_icosphere_indices.push_back(triangle_indices(std::get<0>(tri), a, c));
new_icosphere_indices.push_back(triangle_indices(std::get<1>(tri), b, a));
new_icosphere_indices.push_back(triangle_indices(std::get<2>(tri), c, b));
new_icosphere_indices.push_back(triangle_indices(a, b, c));
}
// Replace the current mesh with the temporary mesh.
icosphere_indices = new_icosphere_indices;
}
// Convert the icosphere's structured triangle_indices vector to a list of
// ordered vertices.
std::vector<glm::vec3> icosphere_mesh;
for (int i = 0; i < icosphere_indices.size(); i++)
{
icosphere_mesh.push_back(icosphere_vertices[std::get<0>(icosphere_indices[i])]);
icosphere_mesh.push_back(icosphere_vertices[std::get<1>(icosphere_indices[i])]);
icosphere_mesh.push_back(icosphere_vertices[std::get<2>(icosphere_indices[i])]);
}
// Return the icosphere's mesh.
return icosphere_mesh;
};
/*
Load a shader program from two files.
*/
GLuint load_shader_program
(
std::string shader_path_0,
std::string shader_path_1,
GLenum shader_type_0,
GLenum shader_type_1
)
{
// Open shader_path_0 and shader_path_1 as input file streams.
std::ifstream shader_file_0(shader_path_0);
std::ifstream shader_file_1(shader_path_1);
if (!shader_file_0.is_open())
{
std::cout << "Could not open file \"" << shader_path_0 << "\"." << std::endl;
exit(EXIT_FAILURE);
}
else if (!shader_file_1.is_open())
{
std::cout << "Could not open file \"" << shader_path_1 << "\"." << std::endl;
exit(EXIT_FAILURE);
}
// Load the text context of shader_path_0 and shader_path_1 into
// shader_buffer_0 and shader_buffer_1.
std::stringstream shader_buffer_0;
std::stringstream shader_buffer_1;
shader_buffer_0 << shader_file_0.rdbuf() << "\0";
shader_buffer_1 << shader_file_1.rdbuf() << "\0";
// Convert shader_buffer_0 and shader_buffer_1 from std::stringstream to
// std::string, and then to const GLchar* (const char*).
std::string shader_text_0 = shader_buffer_0.str();
std::string shader_text_1 = shader_buffer_1.str();
const GLchar* shader_data_0 = shader_text_0.c_str();
const GLchar* shader_data_1 = shader_text_1.c_str();
// Create shader_0 and shader_1 with the types shader_type_0 and
// shader_type_1, then source them with shader_data_0 and shader_data_1.
GLuint shader_0 = glCreateShader(shader_type_0);
GLuint shader_1 = glCreateShader(shader_type_1);
glShaderSource(shader_0, 1, &shader_data_0, NULL);
glShaderSource(shader_1, 1, &shader_data_1, NULL);
// Compile shader_0 and shader_1.
glCompileShader(shader_0);
glCompileShader(shader_1);
// Check if shader_0 or shader_1 failed compilation. If so, print out the
// error message provided by OpenGL.
GLint success_0 = 0;
GLint success_1 = 0;
GLchar crash_information_0[16 * 1024];
GLchar crash_information_1[16 * 1024];
glGetShaderiv(shader_0, GL_COMPILE_STATUS, &success_0);
glGetShaderiv(shader_1, GL_COMPILE_STATUS, &success_1);
if (!success_0)
{
std::cout << "Could not compile shader loaded from \"" << shader_path_0 << "\"." << std::endl;
glGetShaderInfoLog(shader_0, 16 * 1024, NULL, crash_information_0);
std::cout << crash_information_0;
exit(EXIT_FAILURE);
}
else if (!success_1)
{
std::cout << "Could not compile shader loaded from \"" << shader_path_1 << "\"." << std::endl;
glGetShaderInfoLog(shader_1, 16 * 1024, NULL, crash_information_1);
std::cout << crash_information_1;
exit(EXIT_FAILURE);
}
// Create an empty shader program.
GLuint shader_program = glCreateProgram();
// Attach shader_0 and shader_1 to shader_program, and then link
// shader_program.
glAttachShader(shader_program, shader_0);
glAttachShader(shader_program, shader_1);
glLinkProgram(shader_program);
// Check if shader_program failed linkage. If so, print out the error
// message provied by OpenGL.
GLint success_program = 0;
glGetProgramiv(shader_program, GL_LINK_STATUS, &success_program);
if (!success_program)
{
std::cout << "Could not link shader program loaded from \"" << shader_path_0 << "\" and \"" << shader_path_1 << "\"." << std::endl;
GLchar crash_information_program[16 * 1024];
glGetProgramInfoLog(shader_program, 16 * 1024, NULL, crash_information_program);
std::cout << crash_information_program;
exit(EXIT_FAILURE);
}
// Delete shader_0 and shader_1, then return shader_program.
glDeleteShader(shader_0);
glDeleteShader(shader_1);
return shader_program;
}
/*
Entry point.
*/
int main(int argc, char** argv)
{
// Initialize SDL.
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "Could not initialize SDL." << std::endl;
return EXIT_FAILURE;
}
// Create a SDL_Window*.
int sdl_x_res = 960;
int sdl_y_res = 960;
SDL_Window* sdl_window = SDL_CreateWindow
(
"SDL 2.0 with OpenGL 3.3 Core",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
sdl_x_res,
sdl_y_res,
SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL
);
// Make sure the SDL_Window* was created successfully.
if (!sdl_window)
{
std::cout << "Could not create a SDL_Window*." << std::endl;
return EXIT_FAILURE;
}
// Request OpenGL 3.3 Core.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// Create a SDL_GLContext.
SDL_GLContext gl_context = SDL_GL_CreateContext(sdl_window);
// Make sure the SDL_GLContext was created successfully.
if (!gl_context)
{
std::cout << "Could not create a SDL_GLContext." << std::endl;
return EXIT_FAILURE;
}
// Load all OpenGL 3.3 Core functions using GLAD.
if (!gladLoadGLLoader(SDL_GL_GetProcAddress))
{
std::cout << "Could not load OpenGL 3.3 Core functions using GLAD." << std::endl;
return EXIT_FAILURE;
}
// Make sure the OpenGL version that was loaded by GLAD is greater than or
// equal to OpenGL 3.3.
if (GLVersion.major * 10 + GLVersion.minor < 33)
{
std::cout << "Could not load OpenGL 3.3 Core functions using GLAD." << std::endl;
return EXIT_FAILURE;
}
// Create and initialize a noise::module::Perlin. This noise module will
// dictate the general shape of the islands on the planet.
noise::module::Perlin noise_1;
{
// Set the seed to the current time, so that the output noise will be
// slightly different every time.
noise_1.SetSeed(time(NULL));
// Set the octave count to 16 for a high level of detail.
noise_1.SetOctaveCount(16);
// Set the frequency to 2.0f to make the noise more random and less
// coherent.
noise_1.SetFrequency(2.0f);
}
// Create and initialize a noise::module::RidgedMulti. This noise module
// will create round basins and sharp mountain ranges.
noise::module::RidgedMulti noise_2;
{
// Set the seed to the current time, so that the output noise will be
// slightly different every time.
noise_2.SetSeed(time(NULL));
// Set the octave count to 16 for a high level of detail.
noise_2.SetOctaveCount(16);
// Set the frequency to 2.0f to make the noise more random and less
// coherent.
noise_2.SetFrequency(1.0f);
}
// Create a gradient to define the color of points on the planet based on
// the point's elevation.
noise::utils::GradientColor color_map;
color_map.Clear();
color_map.AddGradientPoint(0.0f - 1.0000f, noise::utils::Color(0x00, 0x00, 0x80, 0xFF));
color_map.AddGradientPoint(0.0f - 0.2500f, noise::utils::Color(0x00, 0x00, 0xFF, 0xFF));
color_map.AddGradientPoint(0.0f + 0.0000f, noise::utils::Color(0x00, 0x80, 0xFF, 0xFF));
color_map.AddGradientPoint(0.0f + 0.0625f, noise::utils::Color(0xF0, 0xF0, 0x40, 0xFF));
color_map.AddGradientPoint(0.0f + 0.1250f, noise::utils::Color(0x20, 0xA0, 0x00, 0xFF));
color_map.AddGradientPoint(0.0f + 0.3750f, noise::utils::Color(0xE0, 0xE0, 0x00, 0xFF));
color_map.AddGradientPoint(0.0f + 0.7500f, noise::utils::Color(0x80, 0x80, 0x80, 0xFF));
color_map.AddGradientPoint(0.0f + 1.0000f, noise::utils::Color(0xFF, 0xFF, 0xFF, 0xFF));
// Generate the base icosphere.
std::vector<glm::vec3> icosphere_managed_vertices = create_icosphere(8);
// Allocate space to hold the vertex data of the icosphere.
float* icosphere_vertices = (float*)malloc(icosphere_managed_vertices.size() * (9 * sizeof(float)));
// Perturb the terrain using the noise modules by iterating through each
// triangle rather than each vertex. This is done to make it easy to
// calculate triangle normals.
for (int i = 0; i < icosphere_managed_vertices.size(); i += 3)
{
// Create an array to hold the noise values at the three vertices of
// the current triangle.
float noise_map[3];
for (int j = 0; j < 3; j++)
{
// Get the current vertex.
glm::vec3 vertex = icosphere_managed_vertices[i + j];
// Get the noise value at the current vertex.
float actual_noise_value = noise_1.GetValue(vertex.x, vertex.y, vertex.z) * (noise_2.GetValue(vertex.x, vertex.y, vertex.z) + 0.2f);
// Clamp the noise value to create smooth, flat water.
float noise_value = std::max(0.0f, actual_noise_value);
noise_map[j] = actual_noise_value;
// Perturb the current vertex by the noise value.
icosphere_managed_vertices[i + j] = vertex * (1.0f + noise_value * 0.075f);
}
// Calculate the triangle's normal.
glm::vec3 edge_1 = icosphere_managed_vertices[i + 1] - icosphere_managed_vertices[i];
glm::vec3 edge_2 = icosphere_managed_vertices[i + 2] - icosphere_managed_vertices[i];
glm::vec3 normal = glm::normalize(glm::cross(edge_1, edge_2));
float nx = normal.x;
float ny = normal.y;
float nz = normal.z;
// Generate the vertex data.
for (int j = 0; j < 3; j++)
{
utils::Color color = color_map.GetColor(noise_map[j]);
// Write the position of the current vertex.
icosphere_vertices[(i + j) * 9 + 0] = icosphere_managed_vertices[i + j].x;
icosphere_vertices[(i + j) * 9 + 1] = icosphere_managed_vertices[i + j].y;
icosphere_vertices[(i + j) * 9 + 2] = icosphere_managed_vertices[i + j].z;
// Write the color of the current vertex.
icosphere_vertices[(i + j) * 9 + 3] = color.red / 255.0f;
icosphere_vertices[(i + j) * 9 + 4] = color.green / 255.0f;
icosphere_vertices[(i + j) * 9 + 5] = color.blue / 255.0f;
// Write the surface normal of the current vertex.
icosphere_vertices[(i + j) * 9 + 6] = nx;
icosphere_vertices[(i + j) * 9 + 7] = ny;
icosphere_vertices[(i + j) * 9 + 8] = nz;
}
}
// Generate a VAO and a VBO for the icosphere.
GLuint icosphere_vao;
GLuint icosphere_vbo;
glGenVertexArrays(1, &icosphere_vao);
glGenBuffers(1, &icosphere_vbo);
// Bind the VAO and the VBO of the icosphere to the current state.
glBindVertexArray(icosphere_vao);
glBindBuffer(GL_ARRAY_BUFFER, icosphere_vbo);
// Upload the icosphere data to the VBO.
glBufferData(GL_ARRAY_BUFFER, icosphere_managed_vertices.size() * (10 * sizeof(float)), icosphere_vertices, GL_STATIC_DRAW);
// Enable the required vertex attribute pointers.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(0 * sizeof(float)));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(3 * sizeof(float)));
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// Unbind the VAO and the VBO of the icosphere from the current state.
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Load the default shader program.
GLuint default_shader_program = load_shader_program("default_vertex.glsl", "default_fragment.glsl", GL_VERTEX_SHADER, GL_FRAGMENT_SHADER);
// Define variables to hold the state of the mouse and the application's
// state.
int sdl_mouse_x = 0;
int sdl_mouse_y = 0;
bool sdl_mouse_l = false;
bool sdl_mouse_r = false;
bool sdl_running = true;
// Enter the main loop.
while (sdl_running)
{
// Refresh the window's size.
SDL_GetWindowSize(sdl_window, &sdl_x_res, &sdl_y_res);
// Poll and handle events.
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
// The application was quit.
sdl_running = false;
}
else if (e.type == SDL_MOUSEMOTION)
{
// The mouse moved.
sdl_mouse_x = e.motion.x;
sdl_mouse_y = e.motion.y;
}
else if (e.type == SDL_MOUSEBUTTONDOWN)
{
// A mouse button was pressed.
if (e.button.button == SDL_BUTTON_LEFT)
{
sdl_mouse_l = true;
}
else if (e.button.button == SDL_BUTTON_RIGHT)
{
sdl_mouse_r = true;
}
}
else if (e.type == SDL_MOUSEBUTTONUP)
{
// A mouse button was released.
if (e.button.button == SDL_BUTTON_LEFT)
{
sdl_mouse_l = false;
}
else if (e.button.button == SDL_BUTTON_RIGHT)
{
sdl_mouse_r = false;
}
}
else if (e.type == SDL_KEYDOWN)
{
// A key was pressed.
SDL_Keycode key = e.key.keysym.sym;
if (key == SDLK_ESCAPE)
{
// Quit the application.
sdl_running = false;
}
}
}
// Clear the screen to black.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
// Enable the default shader program.
glUseProgram(default_shader_program);
// Enable depth testing.
glEnable(GL_DEPTH_TEST);
// Enable backface culling.
glEnable(GL_CULL_FACE);
{
// Calculate the aspect ratio.
float aspect_ratio = (float)sdl_x_res / (float)sdl_y_res;
// Calculate the projection matrix.
glm::mat4 matrix_projection = glm::perspective(glm::radians(70.0f), aspect_ratio, 0.128f, 1024.0f);
// Calculate the view matrix.
glm::mat4 matrix_view = glm::mat4(1.0f);
// Rotate the view matrix.
matrix_view = glm::rotate(matrix_view, glm::radians(0.0f), glm::vec3(1.0f, 0.0f, 0.0f));
matrix_view = glm::rotate(matrix_view, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
// Calculate the model matrix.
glm::mat4 matrix_model = glm::mat4(1.0f);
// Translate the model matrix.
matrix_model = glm::translate(matrix_model, glm::vec3(0.0f, 0.0f, 0.2f * -10.0f));
// Rotate the model matrix.
matrix_model = glm::rotate(matrix_model, glm::radians(SDL_GetTicks() / 100.0f), glm::vec3(1.0f, 0.0f, 0.0f));
matrix_model = glm::rotate(matrix_model, glm::radians(SDL_GetTicks() / 100.0f), glm::vec3(0.0f, 1.0f, 0.0f));
// Pass matrix_projection, matrix_view and matrix_model to the
// default_shader_program.
glUniformMatrix4fv(glGetUniformLocation(default_shader_program, "matrix_projection"), 1, GL_FALSE, &matrix_projection[0][0]);
glUniformMatrix4fv(glGetUniformLocation(default_shader_program, "matrix_view"), 1, GL_FALSE, &matrix_view[0][0]);
glUniformMatrix4fv(glGetUniformLocation(default_shader_program, "matrix_model"), 1, GL_FALSE, &matrix_model[0][0]);
}
// Bind the icosphere VAO to the current state.
glBindVertexArray(icosphere_vao);
// Set the polygon mode to render wireframes.
if (false)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
// Draw the icosphere VAO as an array of triangles.
glDrawArrays(GL_TRIANGLES, 0, icosphere_managed_vertices.size());
// Unbind the icosphere VAO from the current state.
glBindVertexArray(0);
// Disable backface culling.
glDisable(GL_CULL_FACE);
// Disable depth testing.
glDisable(GL_DEPTH_TEST);
// Disable the default shader program.
glUseProgram(0);
}
// Swap the sdl_window's current buffer to display the contents of the
// back buffer to the screen.
SDL_GL_SwapWindow(sdl_window);
}
// Free the icosphere's vertices.
free(icosphere_vertices);
// Destroy the default shader program.
glDeleteProgram(default_shader_program);
// Destroy all SDL_GL resources.
SDL_GL_DeleteContext(gl_context);
// Destroy all SDL resources.
SDL_DestroyWindow(sdl_window);
// Quit SDL.
SDL_Quit();
// Exit successfully.
return EXIT_SUCCESS;
}