|
| 1 | +/********************************************************************************* |
| 2 | +* The MIT License (MIT) |
| 3 | +* |
| 4 | +* Copyright (c) 2020 Tarek Sherif |
| 5 | +* |
| 6 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +* this software and associated documentation files (the "Software"), to deal in |
| 8 | +* the Software without restriction, including without limitation the rights to |
| 9 | +* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +* the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +* subject to the following conditions: |
| 12 | +* |
| 13 | +* The above copyright notice and this permission notice shall be included in all |
| 14 | +* copies or substantial portions of the Software. |
| 15 | +* |
| 16 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | +* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | +* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +**********************************************************************************/ |
| 23 | +////////////////////////////////////////////////////// |
| 24 | +// Example of basic C++ usage of Simple OpenGL |
| 25 | +// Loader with a custom X11 implementation and |
| 26 | +// constants defined at the command line. |
| 27 | +////////////////////////////////////////////////////// |
| 28 | + |
| 29 | +#define SOGL_IMPLEMENTATION |
| 30 | +#include "../../simple-opengl-loader.h" |
| 31 | +#include <stdio.h> |
| 32 | +#include <stdlib.h> |
| 33 | +#include <X11/Xlib.h> |
| 34 | +#include <GL/glx.h> |
| 35 | +#include <dlfcn.h> |
| 36 | + |
| 37 | +static void* openGLLibHandle = NULL; |
| 38 | + |
| 39 | +void *sogl_loadOpenGLFunction(const char *name) { |
| 40 | + if (!openGLLibHandle) { |
| 41 | + openGLLibHandle = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); |
| 42 | + if (!openGLLibHandle) { |
| 43 | + openGLLibHandle = dlopen("libGL.so", RTLD_LAZY | RTLD_LOCAL); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + void *fn = dlsym(openGLLibHandle, name); |
| 48 | + |
| 49 | + return fn; |
| 50 | +} |
| 51 | + |
| 52 | +void sogl_cleanup() { |
| 53 | + if (openGLLibHandle) { |
| 54 | + dlclose(openGLLibHandle); |
| 55 | + openGLLibHandle = NULL; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +typedef GLXContext (*glXCreateContextAttribsARBFUNC)(Display*, GLXFBConfig, GLXContext, Bool, const int*); |
| 60 | + |
| 61 | +int main(int argc, char const *argv[]) { |
| 62 | + Display* display; |
| 63 | + Window window; |
| 64 | + XEvent event; |
| 65 | + XWindowAttributes xWinAtt; |
| 66 | + |
| 67 | + // X Windows stuff |
| 68 | + display = XOpenDisplay(NULL); |
| 69 | + |
| 70 | + if (display == NULL) { |
| 71 | + printf("Unable to connect to X Server\n"); |
| 72 | + return 1; |
| 73 | + } |
| 74 | + |
| 75 | + window = XCreateSimpleWindow(display, DefaultRootWindow(display), 20, 20, 800, 800, 0, 0, 0); |
| 76 | + |
| 77 | + |
| 78 | + XSelectInput(display, window, ExposureMask | KeyPressMask | ButtonPressMask); |
| 79 | + XStoreName(display, window, "Simple OpenGL Loader X11 C++ Custom Implementation Example"); |
| 80 | + XMapWindow(display, window); |
| 81 | + |
| 82 | + int numFBC = 0; |
| 83 | + GLint visualAtt[] = { |
| 84 | + GLX_RENDER_TYPE, GLX_RGBA_BIT, |
| 85 | + GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, |
| 86 | + GLX_DOUBLEBUFFER, True, |
| 87 | + GLX_RED_SIZE, 1, |
| 88 | + GLX_GREEN_SIZE, 1, |
| 89 | + GLX_BLUE_SIZE, 1, |
| 90 | + GLX_DEPTH_SIZE, 1, |
| 91 | + GLX_STENCIL_SIZE, 1, |
| 92 | + None |
| 93 | + }; |
| 94 | + |
| 95 | + GLXFBConfig *fbc = glXChooseFBConfig(display, DefaultScreen(display), visualAtt, &numFBC); |
| 96 | + |
| 97 | + if (!fbc) { |
| 98 | + fprintf(stderr, "Unable to get framebuffer\n"); |
| 99 | + return -1; |
| 100 | + } |
| 101 | + |
| 102 | + glXCreateContextAttribsARBFUNC glXCreateContextAttribsARB = (glXCreateContextAttribsARBFUNC) glXGetProcAddress((const GLubyte *) "glXCreateContextAttribsARB"); |
| 103 | + |
| 104 | + if (!glXCreateContextAttribsARB) { |
| 105 | + fprintf(stderr, "Unable to get proc glXCreateContextAttribsARB\n"); |
| 106 | + XFree(fbc); |
| 107 | + return -1; |
| 108 | + } |
| 109 | + |
| 110 | + static int contextAttribs[] = { |
| 111 | + GLX_CONTEXT_MAJOR_VERSION_ARB, 4, |
| 112 | + GLX_CONTEXT_MINOR_VERSION_ARB, 5, |
| 113 | + GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB, |
| 114 | + None |
| 115 | + }; |
| 116 | + |
| 117 | + GLXContext ctx = glXCreateContextAttribsARB(display, *fbc, NULL, True, contextAttribs); |
| 118 | + |
| 119 | + XFree(fbc); |
| 120 | + |
| 121 | + if (!ctx) { |
| 122 | + fprintf(stderr, "Unable to create OpenGL context\n"); |
| 123 | + return -1; |
| 124 | + } |
| 125 | + |
| 126 | + glXMakeCurrent(display, window, ctx); |
| 127 | + |
| 128 | + if (!sogl_loadOpenGL()) { |
| 129 | + const char **failures = sogl_getFailures(); |
| 130 | + while (*failures) { |
| 131 | + fprintf(stderr, "SOGL X11 EXAMPLE: Failed to load function %s\n", *failures); |
| 132 | + failures++; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + glClearColor(0.0, 0.0, 0.0, 1.0); |
| 137 | + |
| 138 | + GLuint vertexArray = 0; |
| 139 | + glGenVertexArrays(1, &vertexArray); |
| 140 | + glBindVertexArray(vertexArray); |
| 141 | + |
| 142 | + GLfloat positionData[] = { |
| 143 | + -0.5, -0.5, |
| 144 | + 0.5, -0.5, |
| 145 | + 0.0, 0.5 |
| 146 | + }; |
| 147 | + |
| 148 | + GLuint positions = 0; |
| 149 | + glGenBuffers(1, &positions); |
| 150 | + glBindBuffer(GL_ARRAY_BUFFER, positions); |
| 151 | + glBufferData(GL_ARRAY_BUFFER, 3 * 2 * sizeof(GLfloat), positionData, GL_STATIC_DRAW); |
| 152 | + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); |
| 153 | + glEnableVertexAttribArray(0); |
| 154 | + |
| 155 | + GLubyte colorData[] = { |
| 156 | + 255, 0, 0, |
| 157 | + 0, 255, 0, |
| 158 | + 0, 0, 255 |
| 159 | + }; |
| 160 | + |
| 161 | + GLuint colors = 0; |
| 162 | + glGenBuffers(1, &colors); |
| 163 | + glBindBuffer(GL_ARRAY_BUFFER, colors); |
| 164 | + glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(GLubyte), colorData, GL_STATIC_DRAW); |
| 165 | + glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, NULL); |
| 166 | + glEnableVertexAttribArray(1); |
| 167 | + |
| 168 | + const char* vsSource = R"GLSL(#version 450 |
| 169 | + layout (location=0) in vec4 position; |
| 170 | + layout (location=1) in vec3 color; |
| 171 | + out vec3 vColor; |
| 172 | + void main() { |
| 173 | + vColor = color; |
| 174 | + gl_Position = position; |
| 175 | + }; |
| 176 | + )GLSL"; |
| 177 | + |
| 178 | + const char* fsSource = R"GLSL(#version 450 |
| 179 | + in vec3 vColor; |
| 180 | + out vec4 fragColor; |
| 181 | + void main() { |
| 182 | + fragColor = vec4(vColor, 1.0); |
| 183 | + } |
| 184 | + )GLSL"; |
| 185 | + |
| 186 | + GLuint vs = glCreateShader(GL_VERTEX_SHADER); |
| 187 | + glShaderSource(vs, 1, &vsSource, NULL); |
| 188 | + glCompileShader(vs); |
| 189 | + |
| 190 | + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); |
| 191 | + glShaderSource(fs, 1, &fsSource, NULL); |
| 192 | + glCompileShader(fs); |
| 193 | + |
| 194 | + GLuint program = glCreateProgram(); |
| 195 | + glAttachShader(program, vs); |
| 196 | + glAttachShader(program, fs); |
| 197 | + glLinkProgram(program); |
| 198 | + |
| 199 | + int params = -1; |
| 200 | + glGetProgramiv(program, GL_LINK_STATUS, ¶ms); |
| 201 | + |
| 202 | + if (params != GL_TRUE) { |
| 203 | + fprintf(stderr, "Program did not link!\n"); |
| 204 | + } |
| 205 | + |
| 206 | + glUseProgram(program); |
| 207 | + |
| 208 | + Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); |
| 209 | + XSetWMProtocols(display, window, &wmDeleteMessage, 1); |
| 210 | + |
| 211 | + // Animation loop |
| 212 | + while (1) { |
| 213 | + if (XCheckTypedWindowEvent(display, window, Expose, &event) == True) { |
| 214 | + XGetWindowAttributes(display, window, &xWinAtt); |
| 215 | + glViewport(0, 0, xWinAtt.width, xWinAtt.height); |
| 216 | + } |
| 217 | + |
| 218 | + if (XCheckTypedWindowEvent(display, window, ClientMessage, &event) == True) { |
| 219 | + if (event.xclient.data.l[0] == (long) wmDeleteMessage) { |
| 220 | + break; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + glClear(GL_COLOR_BUFFER_BIT); |
| 225 | + glDrawArrays(GL_TRIANGLES, 0, 3); |
| 226 | + |
| 227 | + glXSwapBuffers(display, window); |
| 228 | + }; |
| 229 | + |
| 230 | + // Teardown |
| 231 | + XDestroyWindow(display, window); |
| 232 | + XCloseDisplay(display); |
| 233 | +} |
0 commit comments