|
/* |
|
This is free and unencumbered software released into the public domain. |
|
|
|
Anyone is free to copy, modify, publish, use, compile, sell, or |
|
distribute this software, either in source code form or as a compiled |
|
binary, for any purpose, commercial or non-commercial, and by any |
|
means. |
|
|
|
In jurisdictions that recognize copyright laws, the author or authors |
|
of this software dedicate any and all copyright interest in the |
|
software to the public domain. We make this dedication for the benefit |
|
of the public at large and to the detriment of our heirs and |
|
successors. We intend this dedication to be an overt act of |
|
relinquishment in perpetuity of all present and future rights to this |
|
software under copyright law. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
OTHER DEALINGS IN THE SOFTWARE. |
|
|
|
For more information, please refer to <http://unlicense.org/> |
|
*/ |
|
|
|
#include <emscripten/emscripten.h> |
|
#include <emscripten/html5.h> |
|
|
|
#define GL_GLEXT_PROTOTYPES |
|
#define EGL_EGLEXT_PROTOTYPES |
|
#include <GL/gl.h> |
|
#include <GLES2/gl2.h> |
|
#include <math.h> |
|
|
|
static const char* vertex_shader_text = |
|
"precision lowp float;" |
|
"uniform mat4 uMVP;" |
|
"attribute vec4 aPos;" |
|
"attribute vec3 aCol;" |
|
"varying vec3 vCol;" |
|
"void main()" |
|
"{" |
|
"vCol = aCol;" |
|
"gl_Position = uMVP * aPos;" |
|
"}"; |
|
|
|
static const char* fragment_shader_text = |
|
"precision lowp float;" |
|
"varying vec3 vCol;" |
|
"void main()" |
|
"{" |
|
"gl_FragColor = vec4(vCol, 1.0);" |
|
"}"; |
|
|
|
typedef struct Vertex { float x, y, r, g, b; } Vertex; |
|
static GLuint program, vertex_buffer; |
|
static GLint uMVP_location, aPos_location, aCol_location; |
|
|
|
int WAFNDraw(double f, void*); |
|
|
|
// This function is called at startup |
|
int main(int argc, char *argv[]) |
|
{ |
|
emscripten_set_canvas_element_size("canvas", 640, 480); |
|
|
|
EmscriptenWebGLContextAttributes attrs; |
|
emscripten_webgl_init_context_attributes(&attrs); |
|
attrs.alpha = 0; |
|
auto glContext = emscripten_webgl_create_context("canvas", &attrs); |
|
emscripten_webgl_make_context_current(glContext); |
|
|
|
glViewport(0, 0, 640, 480); |
|
|
|
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); |
|
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); |
|
glCompileShader(vertex_shader); |
|
|
|
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); |
|
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); |
|
glCompileShader(fragment_shader); |
|
|
|
program = glCreateProgram(); |
|
glAttachShader(program, vertex_shader); |
|
glAttachShader(program, fragment_shader); |
|
glLinkProgram(program); |
|
|
|
uMVP_location = glGetUniformLocation(program, "uMVP"); |
|
aPos_location = glGetAttribLocation(program, "aPos"); |
|
aCol_location = glGetAttribLocation(program, "aCol"); |
|
|
|
glGenBuffers(1, &vertex_buffer); |
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); |
|
|
|
glEnableVertexAttribArray(aPos_location); |
|
glVertexAttribPointer(aPos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0); |
|
glEnableVertexAttribArray(aCol_location); |
|
glVertexAttribPointer(aCol_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float) * 2)); |
|
|
|
emscripten_request_animation_frame_loop(&WAFNDraw, 0); |
|
|
|
return 0; |
|
} |
|
|
|
// This function is called by loader.js every frame |
|
int WAFNDraw(double f, void*) |
|
{ |
|
f /= 1000.0; |
|
|
|
glClear(GL_COLOR_BUFFER_BIT); |
|
|
|
Vertex vertices[3] = |
|
{ |
|
{ -0.6f, -0.4f, 1.f, 0.f, 0.f }, |
|
{ 0.6f, -0.4f, 0.f, 0.f, 1.f }, |
|
{ 0.f, 0.6f, 1.f, 1.f, 1.f }, |
|
}; |
|
vertices[0].r = 0.5f + sinf(f * 3.14159f * 2.0f) * 0.5f; |
|
vertices[1].b = 0.5f + cosf(f * 3.14159f * 2.0f) * 0.5f; |
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); |
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
|
|
|
GLfloat mvp[4*4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 }; |
|
glUseProgram(program); |
|
glUniformMatrix4fv(uMVP_location, 1, GL_FALSE, mvp); |
|
glDrawArrays(GL_TRIANGLES, 0, 3); |
|
|
|
return EM_TRUE; |
|
} |
Hello Alon
I'm the author of the article and as mentioned in my twitter response I'm very happy you took the time to look at it and respond!
Maybe I should try to clarify my intent with the article some more. It is intended for people who want to treat the web platform as an important build target and who aren't afraid of customizing the JavaScript layer to get full control over the application. People who don't want to engage in JavaScript and are happy with the SDL2 emulation of Emscripten are not the intended audience.
Even though file-size is not the main/only reason, I do mention about minifying the JavaScript code in the article in the last (advanced) demo. Taking the WebGL demo from the article used in this gist as is and just running the JavaScript file through the online tool UglifyJS 3, results in html+js+wasm at 19,962 bytes.
In 2012 I started using Emscripten a few months after adding NaCl target support to my game framework. Ever since I struggled with keeping up with all the changes on it and the web (damn you webaudio!) and kept hoping for NaCl to not die.
This lower level article from 2019 from @surma (https://surma.dev/things/c-to-webassembly/) showed me that wasm can be built with just two EXE files (clang and wasm-ld). A month later I deprecated Emscripten and NaCl targets and finally I could embrace WebAssembly as a platform by understanding most of what is going on while it executes my code.
This somehow became a blog post on its own... anyway, as much as I hoped that Emscripten would go away when llvm added wasm as officially supported, I'm glad for what it did and does and where we are today. Thank you Alon!