backup - needs fixing
[demo] / src / opengl / opengl.cc
1 #include <GL/glew.h>
2 #include <GLFW/glfw3.h>
3 #include <stdio.h>
4
5 #include "gfxapi.h"
6 #include "opengl/opengl.h"
7
8 extern GLFWwindow *win;
9 extern int win_h;
10 extern int win_w;
11
12 static void clear(float r, float g, float b);
13 static void viewport(int x, int y, int width, int height);
14 static void zbuffer(bool enable);
15 static void cull_face(Gfx_cull_face cf);
16 static void reshape(int width, int height) {}
17 static void wireframe(bool enable);
18 static void swapbuffers();
19 static void begin_drawing();
20 static void end_drawing();
21
22 bool init_opengl()
23 {
24         if(!glfwInit()) {
25                 fprintf(stderr, "Failed to initialize GLFW.\n");
26                 return false;
27         }
28
29         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
30         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
31         glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE);
32
33         if(!(win = glfwCreateWindow(win_w, win_h, "glcow", 0, 0))) {
34                 fprintf(stderr, "Failed to create window.\n");
35                 return false;
36         }
37         glfwMakeContextCurrent(win);
38
39         glewInit();
40
41         glEnable(GL_DEPTH_TEST);
42         glEnable(GL_CULL_FACE);
43         glEnable(GL_FRAMEBUFFER_SRGB); // linear colorspace
44
45         gfx_clear = clear;
46         gfx_viewport = viewport;
47         gfx_zbuffer = zbuffer;
48         gfx_cull_face = cull_face;
49         gfx_reshape = reshape;
50         gfx_wireframe = wireframe;
51         gfx_swapbuffers = swapbuffers;
52         gfx_begin_drawing = begin_drawing;
53         gfx_end_drawing = end_drawing;
54
55         // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
56         return true;
57 }
58
59 void cleanup_opengl()
60 {
61         if(win) {
62                 glfwDestroyWindow(win);
63         }
64         glfwTerminate();
65 }
66
67 static void clear(float r, float g, float b)
68 {
69         glClearColor(r, g, b, 1.0);
70         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71 }
72
73 static void viewport(int x, int y, int width, int height)
74 {
75         glViewport(x, y, width, height);
76 }
77
78 static void zbuffer(bool enable)
79 {
80         if(enable)
81                 glEnable(GL_DEPTH_TEST);
82         else
83                 glDisable(GL_DEPTH_TEST);
84 }
85
86 static void cull_face(Gfx_cull_face cf)
87 {
88         switch(cf) {
89         case GFX_CULL_NONE:
90                 glDisable(GL_CULL_FACE);
91                 break;
92         case GFX_CULL_FRONT:
93                 glEnable(GL_CULL_FACE);
94                 glCullFace(GL_FRONT);
95                 break;
96         case GFX_CULL_BACK:
97                 glEnable(GL_CULL_FACE);
98                 glCullFace(GL_BACK);
99                 break;
100         }
101 }
102
103 static void wireframe(bool enabled)
104 {
105         if(enabled)
106                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
107         else
108                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
109 }
110
111 static void swapbuffers()
112 {
113         glfwSwapBuffers(win);
114 }
115
116 static void begin_drawing()
117 {
118 }
119
120 static void end_drawing()
121 {
122 }