-
Notifications
You must be signed in to change notification settings - Fork 19
/
init_gl.h
executable file
·93 lines (79 loc) · 2.97 KB
/
init_gl.h
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
#pragma once
// #include <GLFW/glfw3.h>
// #include <stdio.h>
// #include "Input.h"
bool init_gl(GLFWwindow* &window, const char* title, int window_width, int window_height) {
/* start GL context and O/S window using the GLFW helper library */
if(!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
getchar();
return false;
}
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
window = glfwCreateWindow(window_width, window_height, "GJK", NULL, NULL);
if(!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
getchar();
return false;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
//Setup callbacks
glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, window_resize_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_pos_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetCursorEnterCallback(window, cursor_enter_callback);
//Load OpenGL functions
if(!gl_lite_init()){
printf("Error in gl_lite_init\n");
return false;
}
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
const GLubyte* glsl_version = glGetString(GL_SHADING_LANGUAGE_VERSION);
printf("Renderer: %s\n", renderer);
printf("OpenGL version supported %s\n", version);
printf("GLSL version supported: %s\n", glsl_version);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
return true;
}
//TODO put this somewhere sensible!
#if defined(__clang__) || defined(__GNUC__)
#define _BREAKPOINT_CALL __asm__ volatile("int $0x03")
#elif defined(_MSC_VER_)
#define _BREAKPOINT_CALL __debugbreak()
#endif
#define assert(exp) \
{if(!(exp)) { \
printf("Assertion failed in %s, Line %d:\n%s\n...", __FILE__, __LINE__, #exp); \
_BREAKPOINT_CALL; \
}} \
#define check_gl_error() _checkOglError(__FILE__, __LINE__)
static int _checkOglError(const char *file, int line){
GLenum glErr = glGetError();
if(glErr != GL_NO_ERROR) {
printf("glError in file %s @ line %d:\n%d - ", file, line, glErr);
switch(glErr) {
case GL_INVALID_OPERATION: printf("INVALID_OPERATION\n"); return 1;
case GL_INVALID_ENUM: printf("INVALID_ENUM\n"); return 1;
case GL_INVALID_VALUE: printf("INVALID_VALUE\n"); return 1;
case GL_OUT_OF_MEMORY: printf("OUT_OF_MEMORY\n"); return 1;
case GL_INVALID_FRAMEBUFFER_OPERATION: printf("INVALID_FRAMEBUFFER_OPERATION\n"); return 1;
default: printf("UNRECOGNISED ERROR\n"); return 1;
}
}
return 0;
}