| 1 | /* |
| 2 | * C helper for loading OpenGL functions via GLAD and GLFW. |
| 3 | * This simplifies the Fortran binding since we can't easily pass |
| 4 | * glfwGetProcAddress as a function pointer from Fortran. |
| 5 | * |
| 6 | * Also provides wrapper functions for OpenGL calls, since GLAD uses |
| 7 | * function pointers that can't be directly called from Fortran bindings. |
| 8 | */ |
| 9 | |
| 10 | #include <glad/gl.h> |
| 11 | #include <GLFW/glfw3.h> |
| 12 | |
| 13 | /* Load OpenGL functions using GLFW's loader */ |
| 14 | int fortty_load_gl(void) { |
| 15 | return gladLoadGL((GLADloadfunc) glfwGetProcAddress); |
| 16 | } |
| 17 | |
| 18 | /* OpenGL wrapper functions for Fortran */ |
| 19 | void fortty_glViewport(int x, int y, int width, int height) { |
| 20 | glViewport(x, y, width, height); |
| 21 | } |
| 22 | |
| 23 | void fortty_glClear(unsigned int mask) { |
| 24 | glClear(mask); |
| 25 | } |
| 26 | |
| 27 | void fortty_glClearColor(float red, float green, float blue, float alpha) { |
| 28 | glClearColor(red, green, blue, alpha); |
| 29 | } |
| 30 | |
| 31 | /* ============ Texture functions ============ */ |
| 32 | |
| 33 | void fortty_glGenTextures(int n, unsigned int *textures) { |
| 34 | glGenTextures(n, textures); |
| 35 | } |
| 36 | |
| 37 | void fortty_glDeleteTextures(int n, unsigned int *textures) { |
| 38 | glDeleteTextures(n, textures); |
| 39 | } |
| 40 | |
| 41 | void fortty_glBindTexture(unsigned int target, unsigned int texture) { |
| 42 | glBindTexture(target, texture); |
| 43 | } |
| 44 | |
| 45 | void fortty_glTexImage2D(unsigned int target, int level, int internalformat, |
| 46 | int width, int height, int border, |
| 47 | unsigned int format, unsigned int type, const void *data) { |
| 48 | glTexImage2D(target, level, internalformat, width, height, border, format, type, data); |
| 49 | } |
| 50 | |
| 51 | void fortty_glTexSubImage2D(unsigned int target, int level, |
| 52 | int xoffset, int yoffset, int width, int height, |
| 53 | unsigned int format, unsigned int type, const void *data) { |
| 54 | glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, data); |
| 55 | } |
| 56 | |
| 57 | void fortty_glTexParameteri(unsigned int target, unsigned int pname, int param) { |
| 58 | glTexParameteri(target, pname, param); |
| 59 | } |
| 60 | |
| 61 | void fortty_glPixelStorei(unsigned int pname, int param) { |
| 62 | glPixelStorei(pname, param); |
| 63 | } |
| 64 | |
| 65 | void fortty_glActiveTexture(unsigned int texture) { |
| 66 | glActiveTexture(texture); |
| 67 | } |
| 68 | |
| 69 | /* ============ Shader functions ============ */ |
| 70 | |
| 71 | unsigned int fortty_glCreateShader(unsigned int type) { |
| 72 | return glCreateShader(type); |
| 73 | } |
| 74 | |
| 75 | void fortty_glDeleteShader(unsigned int shader) { |
| 76 | glDeleteShader(shader); |
| 77 | } |
| 78 | |
| 79 | void fortty_glShaderSource(unsigned int shader, const char *source) { |
| 80 | const char *sources[1] = { source }; |
| 81 | glShaderSource(shader, 1, sources, NULL); |
| 82 | } |
| 83 | |
| 84 | void fortty_glCompileShader(unsigned int shader) { |
| 85 | glCompileShader(shader); |
| 86 | } |
| 87 | |
| 88 | int fortty_glGetShaderCompileStatus(unsigned int shader) { |
| 89 | int success; |
| 90 | glGetShaderiv(shader, GL_COMPILE_STATUS, &success); |
| 91 | return success; |
| 92 | } |
| 93 | |
| 94 | void fortty_glGetShaderInfoLog(unsigned int shader, char *log, int maxlen) { |
| 95 | glGetShaderInfoLog(shader, maxlen, NULL, log); |
| 96 | } |
| 97 | |
| 98 | unsigned int fortty_glCreateProgram(void) { |
| 99 | return glCreateProgram(); |
| 100 | } |
| 101 | |
| 102 | void fortty_glDeleteProgram(unsigned int program) { |
| 103 | glDeleteProgram(program); |
| 104 | } |
| 105 | |
| 106 | void fortty_glAttachShader(unsigned int program, unsigned int shader) { |
| 107 | glAttachShader(program, shader); |
| 108 | } |
| 109 | |
| 110 | void fortty_glLinkProgram(unsigned int program) { |
| 111 | glLinkProgram(program); |
| 112 | } |
| 113 | |
| 114 | int fortty_glGetProgramLinkStatus(unsigned int program) { |
| 115 | int success; |
| 116 | glGetProgramiv(program, GL_LINK_STATUS, &success); |
| 117 | return success; |
| 118 | } |
| 119 | |
| 120 | void fortty_glGetProgramInfoLog(unsigned int program, char *log, int maxlen) { |
| 121 | glGetProgramInfoLog(program, maxlen, NULL, log); |
| 122 | } |
| 123 | |
| 124 | void fortty_glUseProgram(unsigned int program) { |
| 125 | glUseProgram(program); |
| 126 | } |
| 127 | |
| 128 | int fortty_glGetUniformLocation(unsigned int program, const char *name) { |
| 129 | return glGetUniformLocation(program, name); |
| 130 | } |
| 131 | |
| 132 | void fortty_glUniform1i(int location, int value) { |
| 133 | glUniform1i(location, value); |
| 134 | } |
| 135 | |
| 136 | void fortty_glUniform1f(int location, float value) { |
| 137 | glUniform1f(location, value); |
| 138 | } |
| 139 | |
| 140 | void fortty_glUniform3f(int location, float v0, float v1, float v2) { |
| 141 | glUniform3f(location, v0, v1, v2); |
| 142 | } |
| 143 | |
| 144 | void fortty_glUniform4f(int location, float v0, float v1, float v2, float v3) { |
| 145 | glUniform4f(location, v0, v1, v2, v3); |
| 146 | } |
| 147 | |
| 148 | void fortty_glUniformMatrix4fv(int location, int count, int transpose, const float *value) { |
| 149 | glUniformMatrix4fv(location, count, transpose, value); |
| 150 | } |
| 151 | |
| 152 | /* ============ VAO/VBO functions ============ */ |
| 153 | |
| 154 | void fortty_glGenVertexArrays(int n, unsigned int *arrays) { |
| 155 | glGenVertexArrays(n, arrays); |
| 156 | } |
| 157 | |
| 158 | void fortty_glDeleteVertexArrays(int n, unsigned int *arrays) { |
| 159 | glDeleteVertexArrays(n, arrays); |
| 160 | } |
| 161 | |
| 162 | void fortty_glBindVertexArray(unsigned int array) { |
| 163 | glBindVertexArray(array); |
| 164 | } |
| 165 | |
| 166 | void fortty_glGenBuffers(int n, unsigned int *buffers) { |
| 167 | glGenBuffers(n, buffers); |
| 168 | } |
| 169 | |
| 170 | void fortty_glDeleteBuffers(int n, unsigned int *buffers) { |
| 171 | glDeleteBuffers(n, buffers); |
| 172 | } |
| 173 | |
| 174 | void fortty_glBindBuffer(unsigned int target, unsigned int buffer) { |
| 175 | glBindBuffer(target, buffer); |
| 176 | } |
| 177 | |
| 178 | void fortty_glBufferData(unsigned int target, size_t size, const void *data, unsigned int usage) { |
| 179 | glBufferData(target, size, data, usage); |
| 180 | } |
| 181 | |
| 182 | void fortty_glBufferSubData(unsigned int target, size_t offset, size_t size, const void *data) { |
| 183 | glBufferSubData(target, offset, size, data); |
| 184 | } |
| 185 | |
| 186 | void fortty_glBufferSubData_floats(unsigned int target, size_t offset, size_t size, const float *data) { |
| 187 | glBufferSubData(target, offset, size, data); |
| 188 | } |
| 189 | |
| 190 | void fortty_glVertexAttribPointer(unsigned int index, int size, unsigned int type, |
| 191 | int normalized, int stride, size_t offset) { |
| 192 | glVertexAttribPointer(index, size, type, normalized, stride, (void*)offset); |
| 193 | } |
| 194 | |
| 195 | void fortty_glEnableVertexAttribArray(unsigned int index) { |
| 196 | glEnableVertexAttribArray(index); |
| 197 | } |
| 198 | |
| 199 | void fortty_glDisableVertexAttribArray(unsigned int index) { |
| 200 | glDisableVertexAttribArray(index); |
| 201 | } |
| 202 | |
| 203 | /* ============ Drawing functions ============ */ |
| 204 | |
| 205 | void fortty_glDrawArrays(unsigned int mode, int first, int count) { |
| 206 | glDrawArrays(mode, first, count); |
| 207 | } |
| 208 | |
| 209 | /* ============ State functions ============ */ |
| 210 | |
| 211 | void fortty_glEnable(unsigned int cap) { |
| 212 | glEnable(cap); |
| 213 | } |
| 214 | |
| 215 | void fortty_glDisable(unsigned int cap) { |
| 216 | glDisable(cap); |
| 217 | } |
| 218 | |
| 219 | void fortty_glBlendFunc(unsigned int sfactor, unsigned int dfactor) { |
| 220 | glBlendFunc(sfactor, dfactor); |
| 221 | } |
| 222 | |
| 223 | void fortty_glScissor(int x, int y, int width, int height) { |
| 224 | glScissor(x, y, width, height); |
| 225 | } |