runs in VR
[vrtris] / src / opengl.c
1 #include <stdlib.h>
2 #include "opengl.h"
3 #include "logger.h"
4
5
6 static void GLAPIENTRY gldebug_logger(GLenum src, GLenum type, GLuint id, GLenum severity,
7                 GLsizei len, const char *msg, const void *cls);
8
9 static const char *gldebug_srcstr(unsigned int src);
10 static const char *gldebug_typestr(unsigned int type);
11
12 struct GLCaps glcaps;
13
14 int init_opengl(void)
15 {
16         glewInit();
17
18         glcaps.debug = GLEW_ARB_debug_output;
19
20 #ifndef NDEBUG
21         if(glcaps.debug) {
22                 info_log("Installing OpenGL debug callback\n");
23                 glDebugMessageCallbackARB(gldebug_logger, 0);
24         }
25 #endif
26
27         return 0;
28 }
29
30 void dump_gl_texture(unsigned int tex, const char *fname)
31 {
32         FILE *fp;
33         int i, width, height;
34         unsigned char *pixels;
35
36         glBindTexture(GL_TEXTURE_2D, tex);
37         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
38         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
39
40         if(!(pixels = malloc(width * height * 3))) {
41                 return;
42         }
43         glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
44
45         if(!(fp = fopen(fname, "wb"))) {
46                 free(pixels);
47                 return;
48         }
49         fprintf(fp, "P6\n%d %d\n255\n", width, height);
50         for(i=0; i<width * height * 3; i++) {
51                 fputc(pixels[i], fp);
52         }
53         fclose(fp);
54         free(pixels);
55 }
56
57
58 static void GLAPIENTRY gldebug_logger(GLenum src, GLenum type, GLuint id, GLenum severity,
59                 GLsizei len, const char *msg, const void *cls)
60 {
61         static const char *fmt = "[GLDEBUG] (%s) %s: %s\n";
62         switch(type) {
63         case GL_DEBUG_TYPE_ERROR:
64                 error_log(fmt, gldebug_srcstr(src), gldebug_typestr(type), msg);
65                 break;
66
67         case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
68         case GL_DEBUG_TYPE_PORTABILITY:
69         case GL_DEBUG_TYPE_PERFORMANCE:
70                 warning_log(fmt, gldebug_srcstr(src), gldebug_typestr(type), msg);
71                 break;
72
73         default:
74                 debug_log(fmt, gldebug_srcstr(src), gldebug_typestr(type), msg);
75         }
76 }
77
78 static const char *gldebug_srcstr(unsigned int src)
79 {
80         switch(src) {
81         case GL_DEBUG_SOURCE_API:
82                 return "api";
83         case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
84                 return "wsys";
85         case GL_DEBUG_SOURCE_SHADER_COMPILER:
86                 return "sdrc";
87         case GL_DEBUG_SOURCE_THIRD_PARTY:
88                 return "3rdparty";
89         case GL_DEBUG_SOURCE_APPLICATION:
90                 return "app";
91         case GL_DEBUG_SOURCE_OTHER:
92                 return "other";
93         default:
94                 break;
95         }
96         return "unknown";
97 }
98
99 static const char *gldebug_typestr(unsigned int type)
100 {
101         switch(type) {
102         case GL_DEBUG_TYPE_ERROR:
103                 return "error";
104         case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
105                 return "deprecated";
106         case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
107                 return "undefined behavior";
108         case GL_DEBUG_TYPE_PORTABILITY:
109                 return "portability warning";
110         case GL_DEBUG_TYPE_PERFORMANCE:
111                 return "performance warning";
112         case GL_DEBUG_TYPE_OTHER:
113                 return "other";
114         default:
115                 break;
116         }
117         return "unknown";
118 }