webgl.h (7981B)
1 #pragma once 2 3 typedef int i32; 4 typedef float f32; 5 typedef double f64; 6 7 // NOTE(amin): Since these functions will be implemented in javascript, we can 8 // only use i32, f32, and f64 params. 9 void webglAttachShader(i32 program, i32 shader); 10 void webglBindBuffer(i32 target, i32 buffer); 11 void webglBindVertexArray(i32 vao); 12 void webglBlendColor(f32 r, f32 g, f32 b, f32 a); 13 void webglBlendFunc(i32 sfactor, i32 dfactor); 14 void webglBufferData(i32 target, i32 size, i32 data, i32 usage); 15 void webglClear(i32 mask); 16 void webglClearColor(f32 r, f32 g, f32 b, f32 a); 17 void webglCompileShader(i32 shader); 18 i32 webglCreateBuffer(void); 19 i32 webglCreateProgram(void); 20 i32 webglCreateShader(i32 type); 21 i32 webglCreateVertexArray(void); 22 void webglDeleteBuffer(i32 bo); 23 void webglDeleteShader(i32 shader); 24 void webglDeleteVertexArray(i32 vao); 25 void webglDepthMask(i32 flag); 26 void webglDisable(i32 cap); 27 void webglDrawElements(i32 mode, i32 count, i32 type, i32 offset); 28 void webglEnable(i32 cap); 29 void webglEnableVertexAttribArray(i32 index); 30 void webglGetProgramInfoLog(void); 31 int webglGetProgramParameter(i32 program, i32 param); 32 void webglGetShaderInfoLog(i32 shader, char *out_buf); 33 int webglGetShaderParameter(i32 shader, i32 param); 34 i32 webglGetUniformLocation(i32 program, const char name[static 1], i32 name_len); 35 void webglLinkProgram(i32 program); 36 void webglShaderSource(i32 shader, const char source[static 1], i32 source_len); 37 void webglUniform1f(i32 location, f32 value); 38 void webglUniform1i(i32 location, i32 value); 39 void webglUniform3f(i32 location, f32 x, f32 y, f32 z); 40 void webglUniformMatrix4fv(i32 location, i32 transpose, const f32 data[static 16]); 41 void webglUseProgram(i32 program); 42 void webglVertexAttribPointer(i32 index, i32 size, i32 type, i32 normalized, i32 stride, i32 offset); 43 void webglViewport(i32 x, i32 y, i32 width, i32 height); 44 45 #define GL_DEPTH_BUFFER_BIT 0x00000100 46 #define GL_COLOR_BUFFER_BIT 0x00004000 47 #define GL_FALSE 0 48 #define GL_TRUE 1 49 #define GL_TRIANGLES 0x0004 50 #define GL_SRC_ALPHA 0x0302 51 #define GL_DEPTH_TEST 0x0B71 52 #define GL_BLEND 0x0BE2 53 #define GL_UNSIGNED_INT 0x1405 54 #define GL_FLOAT 0x1406 55 #define GL_CONSTANT_ALPHA 0x8003 56 #define GL_ARRAY_BUFFER 0x8892 57 #define GL_ELEMENT_ARRAY_BUFFER 0x8893 58 #define GL_STATIC_DRAW 0x88E4 59 #define GL_FRAGMENT_SHADER 0x8B30 60 #define GL_VERTEX_SHADER 0x8B31 61 #define GL_COMPILE_STATUS 0x8B81 62 #define GL_LINK_STATUS 0x8B82 63 64 typedef unsigned int GLenum; 65 typedef unsigned char GLboolean; 66 typedef unsigned int GLbitfield; 67 typedef void GLvoid; 68 typedef int GLint; 69 typedef unsigned int GLuint; 70 typedef int GLsizei; 71 typedef float GLfloat; 72 typedef char GLchar; 73 typedef long GLsizeiptr; 74 75 // TODO: add a version with safety checks 76 #define WEBGL_CAST_I32(x) (i32)(x) 77 78 static i32 _webgl_strlen(const char *str) 79 { 80 i32 len = 0; 81 while(str[len] != '\0') 82 { 83 len++; 84 } 85 return len; 86 } 87 88 static void glAttachShader(GLuint program, GLuint shader) 89 { 90 webglAttachShader(WEBGL_CAST_I32(program), WEBGL_CAST_I32(shader)); 91 } 92 93 static void glBindBuffer(GLenum target, GLuint buffer) 94 { 95 webglBindBuffer(WEBGL_CAST_I32(target), WEBGL_CAST_I32(buffer)); 96 } 97 98 static void glBindVertexArray(GLuint array) 99 { 100 webglBindVertexArray(WEBGL_CAST_I32(array)); 101 } 102 103 static void glBlendColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) 104 { 105 webglBlendColor((f32)red, (f32)green, (f32)blue, (f32)alpha); 106 } 107 108 static void glBlendFunc(GLenum sfactor, GLenum dfactor) 109 { 110 webglBlendFunc(WEBGL_CAST_I32(sfactor), WEBGL_CAST_I32(dfactor)); 111 } 112 113 static void glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage) 114 { 115 webglBufferData(WEBGL_CAST_I32(target), WEBGL_CAST_I32(size), WEBGL_CAST_I32(data), WEBGL_CAST_I32(usage)); 116 } 117 118 static void glClear(GLbitfield mask) 119 { 120 webglClear(WEBGL_CAST_I32(mask)); 121 } 122 123 static void glClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) 124 { 125 webglClearColor((f32)red, (f32)green, (f32)blue, (f32)alpha); 126 } 127 128 static void glCompileShader(GLuint shader) 129 { 130 webglCompileShader(WEBGL_CAST_I32(shader)); 131 } 132 133 static GLuint glCreateProgram(void) 134 { 135 i32 program_id = webglCreateProgram(); 136 return (GLuint)program_id; 137 } 138 139 static GLuint glCreateShader(GLenum type) 140 { 141 return webglCreateShader(WEBGL_CAST_I32(type)); 142 } 143 144 static void glDeleteBuffers(GLsizei n, const GLuint *buffers) 145 { 146 i32 the_buffer = WEBGL_CAST_I32(buffers[0]); 147 webglDeleteBuffer(the_buffer); 148 } 149 150 static void glDeleteShader(GLuint shader) 151 { 152 webglDeleteShader(WEBGL_CAST_I32(shader)); 153 } 154 155 static void glDeleteVertexArrays(GLsizei n, const GLuint *arrays) 156 { 157 i32 the_array = WEBGL_CAST_I32(arrays[0]); 158 webglDeleteVertexArray(the_array); 159 } 160 161 static void glDepthMask(GLboolean flag) 162 { 163 webglDepthMask(WEBGL_CAST_I32(flag)); 164 } 165 166 static void glDisable(GLenum cap) 167 { 168 webglDisable(WEBGL_CAST_I32(cap)); 169 } 170 171 static void glDrawElements(GLenum mode, GLsizei count, GLenum type, const void *indices) 172 { 173 webglDrawElements(WEBGL_CAST_I32(mode), WEBGL_CAST_I32(count), WEBGL_CAST_I32(type), WEBGL_CAST_I32(indices)); 174 } 175 176 static void glEnable(GLenum cap) 177 { 178 webglEnable(WEBGL_CAST_I32(cap)); 179 } 180 181 static void glEnableVertexAttribArray(GLuint index) 182 { 183 webglEnableVertexAttribArray(WEBGL_CAST_I32(index)); 184 } 185 186 static void glGenBuffers(GLsizei n, GLuint *buffers) 187 { 188 i32 buffer_id = webglCreateBuffer(); 189 *buffers = (GLuint)buffer_id; 190 } 191 192 static void glGenVertexArrays(GLsizei n, GLuint *arrays) 193 { 194 i32 vao_id = webglCreateVertexArray(); 195 *arrays = (GLuint)vao_id; 196 } 197 198 static void glGetProgramInfoLog(GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog) 199 { 200 // TODO: implement 201 //webglGetProgramInfoLog(WEBGL_CAST_I32(program), WEBGL_CAST_I32(bufsize), WEBGL_CAST_I32(*length), WEBGL_CAST_I32(*infoLog)); 202 } 203 204 static void glGetProgramiv(GLuint program, GLenum pname, GLint *params) 205 { 206 *params = webglGetProgramParameter(WEBGL_CAST_I32(program), WEBGL_CAST_I32(pname)); 207 } 208 209 static void glGetShaderInfoLog(GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog) 210 { 211 webglGetShaderInfoLog(WEBGL_CAST_I32(shader), infoLog); 212 } 213 214 static void glGetShaderiv(GLuint shader, GLenum pname, GLint *params) 215 { 216 *params = webglGetShaderParameter(WEBGL_CAST_I32(shader), WEBGL_CAST_I32(pname)); 217 } 218 219 static GLint glGetUniformLocation(GLuint program, const GLchar *name) 220 { 221 i32 name_len = _webgl_strlen(name); 222 return webglGetUniformLocation(WEBGL_CAST_I32(program), name, name_len); 223 } 224 225 static void glLinkProgram(GLuint program) 226 { 227 webglLinkProgram(WEBGL_CAST_I32(program)); 228 } 229 230 static void glShaderSource(GLuint shader, GLsizei count, const GLchar *const *string, const GLint *length) 231 { 232 const GLchar *s = (void *)*string; 233 i32 l = _webgl_strlen(s); 234 webglShaderSource(WEBGL_CAST_I32(shader), s, l); 235 } 236 237 static void glUniform1f(GLint location, GLfloat v0) 238 { 239 webglUniform1f(WEBGL_CAST_I32(location), (f32)v0); 240 } 241 242 static void glUniform1i(GLint location, GLint v0) 243 { 244 webglUniform1i(WEBGL_CAST_I32(location), WEBGL_CAST_I32(v0)); 245 } 246 247 static void glUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2) 248 { 249 webglUniform3f(WEBGL_CAST_I32(location), (f32)v0, (f32)v1, (f32)v2); 250 } 251 252 static void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) 253 { 254 webglUniformMatrix4fv(WEBGL_CAST_I32(location), WEBGL_CAST_I32(transpose), value); 255 } 256 257 static void glUseProgram(GLuint program) 258 { 259 webglUseProgram(WEBGL_CAST_I32(program)); 260 } 261 262 static void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer) 263 { 264 webglVertexAttribPointer(WEBGL_CAST_I32(index), WEBGL_CAST_I32(size), WEBGL_CAST_I32(type), WEBGL_CAST_I32(normalized), WEBGL_CAST_I32(stride), WEBGL_CAST_I32(pointer)); 265 } 266 267 static void glViewport(GLint x, GLint y, GLsizei width, GLsizei height) 268 { 269 webglViewport(x, y, WEBGL_CAST_I32(width), WEBGL_CAST_I32(height)); 270 } 271 272 #undef WEBGL_CAST_I32