-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
104 lines (92 loc) · 2.78 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
#include "main.h"
GLFWwindow* window;
void error_callback(int error, const char* description)
{
// Print error
fputs(description, stderr);
}
void setup_callbacks()
{
// Set the error callback
glfwSetErrorCallback(error_callback);
// Set the key callback
glfwSetKeyCallback(window, Window::key_callback);
// Set the window resize callback
glfwSetFramebufferSizeCallback(window, Window::resize_callback);
// Callbacks for CAMERA
// Set the mouse button callback
glfwSetMouseButtonCallback(window, Window::mouse_button_callback);
// Set the cursor callback
glfwSetCursorPosCallback(window, Window::cursor_callback);
// Set the mouse scroll callback
glfwSetScrollCallback(window, Window::scroll_callback);
}
void setup_glew()
{
// Initialize GLEW. Not needed on OSX systems.
#ifndef __APPLE__
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
glfwTerminate();
}
fprintf(stdout, "Current GLEW version: %s\n", glewGetString(GLEW_VERSION));
#endif
}
void setup_opengl_settings()
{
#ifndef __APPLE__
// Setup GLEW. Don't do this on OSX systems.
setup_glew();
#endif
// Enable depth buffering
glEnable(GL_DEPTH_TEST);
// Related to shaders and z value comparisons for the depth buffer
glDepthFunc(GL_LEQUAL);
// Set polygon drawing mode to fill front and back of each polygon
// You can also use the paramter of GL_LINE instead of GL_FILL to see wireframes
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// Disable backface culling to render both sides of polygons
glDisable(GL_CULL_FACE);
// Set clear color
glClearColor(0.05f, 0.8f, 0.85f, 1.0f);
}
void print_versions()
{
// Get info of GPU and supported OpenGL version
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("OpenGL version supported %s\n", glGetString(GL_VERSION));
//If the shading language symbol is defined
#ifdef GL_SHADING_LANGUAGE_VERSION
std::printf("Supported GLSL version is %s.\n", (char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
#endif
}
int main(void)
{
// Create the GLFW window
window = Window::create_window(640, 480);
// Print OpenGL and GLSL versions
print_versions();
// Setup callbacks
setup_callbacks();
// Setup OpenGL settings, including lighting, materials, etc.
setup_opengl_settings();
// Initialize objects/pointers for rendering
Window::initialize_objects();
// Loop while GLFW window should stay open
while (!glfwWindowShouldClose(window))
{
// Main render display callback. Rendering of objects is done here.
Window::display_callback(window);
// Idle callback. Updating objects, etc. can be done here.
Window::idle_callback();
}
Window::clean_up();
// Destroy the window
glfwDestroyWindow(window);
// Terminate GLFW
glfwTerminate();
exit(EXIT_SUCCESS);
}