transparent-cube

Minimal cross-platform native/wasm graphics example.
git clone git://git.amin.space/transparent-cube.git
Log | Files | Refs | README | LICENSE

webgl.h (8121B)


      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 inline 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 inline void glAttachShader(GLuint program, GLuint shader)
     89 {
     90     webglAttachShader(WEBGL_CAST_I32(program), WEBGL_CAST_I32(shader));
     91 }
     92 
     93 inline void glBindBuffer(GLenum target, GLuint buffer)
     94 {
     95     webglBindBuffer(WEBGL_CAST_I32(target), WEBGL_CAST_I32(buffer));
     96 }
     97 
     98 inline void glBindVertexArray(GLuint array)
     99 {
    100     webglBindVertexArray(WEBGL_CAST_I32(array));
    101 }
    102 
    103 inline void glBlendColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
    104 {
    105     webglBlendColor((f32)red, (f32)green, (f32)blue, (f32)alpha);
    106 }
    107 
    108 inline void glBlendFunc(GLenum sfactor, GLenum dfactor)
    109 {
    110     webglBlendFunc(WEBGL_CAST_I32(sfactor), WEBGL_CAST_I32(dfactor));
    111 }
    112 
    113 inline 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 inline void glClear(GLbitfield mask)
    119 {
    120     webglClear(WEBGL_CAST_I32(mask));
    121 }
    122 
    123 inline void glClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
    124 {
    125     webglClearColor((f32)red, (f32)green, (f32)blue, (f32)alpha);
    126 }
    127 
    128 inline void glCompileShader(GLuint shader)
    129 {
    130     webglCompileShader(WEBGL_CAST_I32(shader));
    131 }
    132 
    133 inline GLuint glCreateProgram(void)
    134 {
    135     i32 program_id = webglCreateProgram();
    136     return (GLuint)program_id;
    137 }
    138 
    139 inline GLuint glCreateShader(GLenum type)
    140 {
    141     return webglCreateShader(WEBGL_CAST_I32(type));
    142 }
    143 
    144 inline void glDeleteBuffers(GLsizei n, const GLuint *buffers)
    145 {
    146     assert(n == 1);
    147     i32 the_buffer = WEBGL_CAST_I32(buffers[0]);
    148     webglDeleteBuffer(the_buffer);
    149 }
    150 
    151 inline void glDeleteShader(GLuint shader)
    152 {
    153     webglDeleteShader(WEBGL_CAST_I32(shader));
    154 }
    155 
    156 inline void glDeleteVertexArrays(GLsizei n, const GLuint *arrays)
    157 {
    158     assert(n == 1);
    159     i32 the_array = WEBGL_CAST_I32(arrays[0]);
    160     webglDeleteVertexArray(the_array);
    161 }
    162 
    163 inline void glDepthMask(GLboolean flag)
    164 {
    165     webglDepthMask(WEBGL_CAST_I32(flag));
    166 }
    167 
    168 inline void glDisable(GLenum cap)
    169 {
    170     webglDisable(WEBGL_CAST_I32(cap));
    171 }
    172 
    173 inline void glDrawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
    174 {
    175     webglDrawElements(WEBGL_CAST_I32(mode), WEBGL_CAST_I32(count), WEBGL_CAST_I32(type), WEBGL_CAST_I32(indices));
    176 }
    177 
    178 inline void glEnable(GLenum cap)
    179 {
    180     webglEnable(WEBGL_CAST_I32(cap));
    181 }
    182 
    183 inline void glEnableVertexAttribArray(GLuint index)
    184 {
    185     webglEnableVertexAttribArray(WEBGL_CAST_I32(index));
    186 }
    187 
    188 inline void glGenBuffers(GLsizei n, GLuint *buffers)
    189 {
    190     assert(n == 1);
    191     i32 buffer_id = webglCreateBuffer();
    192     assert(buffer_id >= 0);
    193     *buffers = (GLuint)buffer_id;
    194 }
    195 
    196 inline void glGenVertexArrays(GLsizei n, GLuint *arrays)
    197 {
    198     assert(n == 1);
    199     i32 vao_id = webglCreateVertexArray();
    200     assert(vao_id >= 0);
    201     *arrays = (GLuint)vao_id;
    202 }
    203 
    204 inline void glGetProgramInfoLog(GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog)
    205 {
    206     // TODO: implement
    207     //webglGetProgramInfoLog(WEBGL_CAST_I32(program), WEBGL_CAST_I32(bufsize), WEBGL_CAST_I32(*length), WEBGL_CAST_I32(*infoLog));
    208 }
    209 
    210 inline void glGetProgramiv(GLuint program, GLenum pname, GLint *params)
    211 {
    212     *params = webglGetProgramParameter(WEBGL_CAST_I32(program), WEBGL_CAST_I32(pname));
    213 }
    214 
    215 inline void glGetShaderInfoLog(GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog)
    216 {
    217     webglGetShaderInfoLog(WEBGL_CAST_I32(shader), infoLog);
    218 }
    219 
    220 inline void glGetShaderiv(GLuint shader, GLenum pname, GLint *params)
    221 {
    222     *params = webglGetShaderParameter(WEBGL_CAST_I32(shader), WEBGL_CAST_I32(pname));
    223 }
    224 
    225 inline GLint glGetUniformLocation(GLuint program, const GLchar *name)
    226 {
    227     i32 name_len = _webgl_strlen(name);
    228     return webglGetUniformLocation(WEBGL_CAST_I32(program), name, name_len);
    229 }
    230 
    231 inline void glLinkProgram(GLuint program)
    232 {
    233     webglLinkProgram(WEBGL_CAST_I32(program));
    234 }
    235 
    236 inline void glShaderSource(GLuint shader, GLsizei count, const GLchar *const *string, const GLint *length)
    237 {
    238     const GLchar *s = (void *)*string;
    239     i32 l = _webgl_strlen(s);
    240     webglShaderSource(WEBGL_CAST_I32(shader), s, l);
    241 }
    242 
    243 inline void glUniform1f(GLint location, GLfloat v0)
    244 {
    245     webglUniform1f(WEBGL_CAST_I32(location), (f32)v0);
    246 }
    247 
    248 inline void glUniform1i(GLint location, GLint v0)
    249 {
    250     webglUniform1i(WEBGL_CAST_I32(location), WEBGL_CAST_I32(v0));
    251 }
    252 
    253 inline void glUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2)
    254 {
    255     webglUniform3f(WEBGL_CAST_I32(location), (f32)v0, (f32)v1, (f32)v2);
    256 }
    257 
    258 inline void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
    259 {
    260     webglUniformMatrix4fv(WEBGL_CAST_I32(location), WEBGL_CAST_I32(transpose), value);
    261 }
    262 
    263 inline void glUseProgram(GLuint program)
    264 {
    265     webglUseProgram(WEBGL_CAST_I32(program));
    266 }
    267 
    268 inline void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer)
    269 {
    270     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));
    271 }
    272 
    273 inline void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
    274 {
    275     webglViewport(x, y, WEBGL_CAST_I32(width), WEBGL_CAST_I32(height));
    276 }
    277 
    278 #undef WEBGL_CAST_I32