· 6 years ago · Oct 12, 2019, 03:02 PM
1/*
2* Copyright 2019 Vienna University of Technology.
3* Institute of Computer Graphics and Algorithms.
4* This file is part of the ECG Lab Framework and must not be redistributed.
5*/
6
7
8#include "Utils.h"
9#include <string>
10#include <sstream>
11
12static std::string FormatDebugOutput(GLenum source, GLenum type, GLuint id, GLenum severity, const char* msg) {
13 std::stringstream stringStream;
14 std::string sourceString;
15 std::string typeString;
16 std::string severityString;
17
18 switch (source) {
19 case GL_DEBUG_SOURCE_API: {
20 sourceString = "API";
21 break;
22 }
23 case GL_DEBUG_SOURCE_APPLICATION: {
24 sourceString = "Application";
25 break;
26 }
27 case GL_DEBUG_SOURCE_WINDOW_SYSTEM: {
28 sourceString = "Window System";
29 break;
30 }
31 case GL_DEBUG_SOURCE_SHADER_COMPILER: {
32 sourceString = "Shader Compiler";
33 break;
34 }
35 case GL_DEBUG_SOURCE_THIRD_PARTY: {
36 sourceString = "Third Party";
37 break;
38 }
39 case GL_DEBUG_SOURCE_OTHER: {
40 sourceString = "Other";
41 break;
42 }
43 default: {
44 sourceString = "Unknown";
45 break;
46 }
47 }
48
49 switch (type) {
50 case GL_DEBUG_TYPE_ERROR: {
51 typeString = "Error";
52 break;
53 }
54 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: {
55 typeString = "Deprecated Behavior";
56 break;
57 }
58 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: {
59 typeString = "Undefined Behavior";
60 break;
61 }
62 case GL_DEBUG_TYPE_PORTABILITY_ARB: {
63 typeString = "Portability";
64 break;
65 }
66 case GL_DEBUG_TYPE_PERFORMANCE: {
67 typeString = "Performance";
68 break;
69 }
70 case GL_DEBUG_TYPE_OTHER: {
71 typeString = "Other";
72 break;
73 }
74 default: {
75 typeString = "Unknown";
76 break;
77 }
78 }
79
80 switch (severity) {
81 case GL_DEBUG_SEVERITY_HIGH: {
82 severityString = "High";
83 break;
84 }
85 case GL_DEBUG_SEVERITY_MEDIUM: {
86 severityString = "Medium";
87 break;
88 }
89 case GL_DEBUG_SEVERITY_LOW: {
90 severityString = "Low";
91 break;
92 }
93 default: {
94 severityString = "Unknown";
95 break;
96 }
97 }
98
99 stringStream << "OpenGL Error: " << msg;
100 stringStream << " [Source = " << sourceString;
101 stringStream << ", Type = " << typeString;
102 stringStream << ", Severity = " << severityString;
103 stringStream << ", ID = " << id << "]";
104
105 return stringStream.str();
106}
107
108
109static void APIENTRY DebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam) {
110 if (id == 131185 || id == 131218) return; // ignore performance warnings (buffer uses GPU memory, shader recompilation) from nvidia
111
112 std::string error = FormatDebugOutput(source, type, id, severity, message);
113 std::cout << error << std::endl;
114}
115void key_callback(GLFWwindow* window, int key, int scancode,
116 int action, int mods)
117{
118 if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
119 {
120 glfwSetWindowShouldClose(window, true);
121 }
122}
123
124
125/* --------------------------------------------- */
126// Prototypes
127/* --------------------------------------------- */
128
129
130
131/* --------------------------------------------- */
132// Global variables
133/* --------------------------------------------- */
134
135
136
137/* --------------------------------------------- */
138// Main
139/* --------------------------------------------- */
140
141int main(int argc, char** argv)
142{
143 /* --------------------------------------------- */
144 // Load settings.ini
145 /* --------------------------------------------- */
146
147 // init reader for ini files
148 INIReader reader("assets/settings.ini");
149
150 // load values from ini file
151 // first param: section [window], second param: property name, third param: default value
152 int width = reader.GetInteger("window", "width", 800);
153 int height = reader.GetInteger("window", "height", 800);
154 std::string window_title = reader.Get("window", "title", "ECG");
155 glfwInit();
156
157 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // OpenGL version
158 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
159 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
160 glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // Fixed size window
161
162 glClear(GL_COLOR_BUFFER_BIT);
163 glClearColor(1, 1, 1, 1);
164 GLFWwindow* window = glfwCreateWindow(width, height, window_title.c_str(), NULL, NULL);
165 if (!window) {
166 glfwTerminate(); //Free all resources before exit
167 //Error and Exit
168
169 glfwMakeContextCurrent(window);
170
171
172#if _DEBUG
173 // Create a debug OpenGL context or tell your OpenGL library (GLFW, SDL) to do so.
174 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
175#endif
176 // Loading function, your GLFW and GLEW initialization happens here
177
178
179 glewExperimental = true;
180 glewInit();
181
182 // Loading function, your GLFW and GLEW initialization happens here
183
184 initFramework();
185
186#if _DEBUG
187 // Register your callback function.
188 glDebugMessageCallback(DebugCallback, NULL);
189 // Enable synchronous callback. This ensures that your callback function is called
190 // right after an error has occurred.
191 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
192#endif
193
194 // more initialization and content loading happens here
195
196 // more initialization and content loading happens here
197 GLenum err = glewInit();
198
199 if (err != GLEW_OK) { /* Error and Exit */ }
200
201 }
202
203
204 /* --------------------------------------------- */
205 // Init framework
206 /* --------------------------------------------- */
207
208 if (!initFramework()) {
209 EXIT_WITH_ERROR("Failed to init framework")
210 }
211
212
213
214
215 /* --------------------------------------------- */
216 // Initialize scene and render loop
217 /* --------------------------------------------- */
218
219 while (!glfwWindowShouldClose(window)) {
220 //Clear screen
221 glClear(GL_COLOR_BUFFER_BIT);
222 //Update and render scene, handle input
223 glClearColor(1,1,1,1);
224 glfwSetKeyCallback(window, key_callback);
225 glfwPollEvents();
226 drawTeapot();
227 glfwSwapBuffers(window);
228 }
229
230
231 /* --------------------------------------------- */
232 // Destroy framework
233 /* --------------------------------------------- */
234
235 destroyFramework();
236
237
238 /* --------------------------------------------- */
239 // Destroy context and exit
240 /* --------------------------------------------- */
241
242
243 return EXIT_SUCCESS;
244}