https://juejin.cn/post/7362528152777162793 这篇文章中给出了一种使用 WASM 在浏览器中使用 OpenGL API 的方式,但是却没有给出代码参考

这里参考 https://github.com/aliabbas299792/openglWASMTemplate 使用现代 OpenGL 环境复现了上述功能,并 https://github.com/takashikumagai/wasm-opengl-keyboard-input/blob/master/MyApp.cpp 找到了如何处理输入的方式

OpenGL 本地运行

这里参考 https://learnopengl-cn.github.io/01 Getting started/04 Hello Triangle/ 其中的绘制三角形的代码

02_triangle.cpp

#include <functional>
#include <unistd.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <emscripten/emscripten.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

const char *vertexShaderSource = "#version 300 es\n"
    "layout (location = 0) in vec3 aPos;\n"
    "void main()\n"
    "{\n"
    "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
    "}\0";
const char *fragmentShaderSource = "#version 300 es\n"
    "precision mediump float\n;"
    "out vec4 FragColor;\n"
    "void main()\n"
    "{\n"
    "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
    "}\n\0";


std::function<void()> lambdaDrawLoop;
void drawLoop() { lambdaDrawLoop(); }

float r = 0.2f, g = 0.3f, b = 0.3f;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }


    // build and compile our shader program
    // ------------------------------------
    // vertex shader
    unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    // check for shader compile errors
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // fragment shader
    unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    // check for shader compile errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // link shaders
    unsigned int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    // check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    // set up vertex data (and buffer(s)) and configure vertex attributes
    // ------------------------------------------------------------------
    float vertices[] = {
        -0.5f, -0.5f, 0.0f, // left  
         0.5f, -0.5f, 0.0f, // right 
         0.0f,  0.5f, 0.0f  // top   
    }; 

    unsigned int VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
    glBindBuffer(GL_ARRAY_BUFFER, 0); 

    // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
    // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
    glBindVertexArray(0); 


    // uncomment this call to draw in wireframe polygons.
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    lambdaDrawLoop = [&] {
        glClearColor(r, g, b, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // draw our first triangle
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
        glDrawArrays(GL_TRIANGLES, 0, 3);
        // glBindVertexArray(0); // no need to unbind it every time 
 
        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    };

    emscripten_set_main_loop(drawLoop, -1, true); //sets the emscripten main loop
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);

    if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE) {
        r = float(rand() % 255) / 255.0f;
        g = float(rand() % 255) / 255.0f;
        b = float(rand() % 255) / 255.0f;
    }
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}

编译


emcc example/02_triangle.cpp D:/Dev/OpenglProject/GLProj/deps/glad/src/glad.c -s WASM=1 -s USE_GLFW=3 -s FULL_ES3=1 -s USE_WEBGL2=1 -s MIN_WEBGL_VERSION=2 -O3 -I"D:/Dev/OpenglProject/GLProj/deps/glad/include/" -I"D:/Apps/glfw3/include" -L"D:/Apps/glfw3/lib/" -lglfw3 -o index.js 

index.html

<style>
    html{ overflow: hidden; }

    *{
        box-sizing: border-box;
        padding: 0;
        margin: 0;
        background:#000;
    }

    canvas{
        position: absolute;
        top:50%;
        left:50%;
        transform: translate(-50%, -50%);
        max-width: 100vw;
        max-height: 100vh;
    }
</style>
<canvas id="canvas" oncontextmenu="event.preventDefault()" height="0" width="0"></canvas>
<!-- to start off with the height and width are set to 0 -->
<script>
    // const files = ["assets/fragshader.glsl", "assets/vertexshader.glsl"];
    // const data = {};

    // for(const file of files){
    //     fetch(file)
    //         .then(res => res.text())
    //         .then(res => data[file] = res);
    // }
</script>
<!-- <script src="library.js"></script> -->
<script src="index.js"></script>
<script type='text/javascript'>
  const canvas = document.getElementById('canvas');

  Module.canvas = canvas;
  Module.onRuntimeInitialized = () => {
    setTimeout(() => {
        resize()
    }, 50); //delays the resize by 50ms to allow the initial sizing to occur
  }

  window.addEventListener('resize', resize);

  function resize(){
    const width = window.innerWidth;
    const height = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight;

    _setWindowSize(width*2, height*2); //sets the content to the correct size
    //setting it to be 2x bigger than the actual viewport seems to get rid of the aliasing effect well

    canvas.setAttribute("style", `width:${width}px; height:${height}px`); //sets canvas to be as big as the content inside it
  }
</script>

处理输入


    lambdaDrawLoop = [&] {
        processInput(window);
        
        
        // ...
        glfwSwapBuffers(window);
        glfwPollEvents();
    };
    return 0;
}


void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
    }
    
    if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE) {
        r = float(rand() % 255) / 255.0f;
        g = float(rand() % 255) / 255.0f;
        b = float(rand() % 255) / 255.0f;
    }
}

打印 cout

默认操作

在生成的 index.js 文件中配置如下代码:

Module['print'] = function(text) {
  console.log('[C++ Output] ' + text);
};

Module['printErr'] = function(text) {
  console.error('[C++ Error] ' + text);
};

注意,默认情况下 emcc 4.0 生成 index.js 中就会包含类似的上述代码

强制使用 emscripten_run_script

你可以在 C++ 中直接调用 JS 函数(性能差,仅用于调试):

EM_ASM_({console.log(UTF8ToString($0));}, "opengl in wasm");

或者可以将其封装成函数

#include <emscripten.h>
void log_to_console(const std::string& msg) {
    EM_ASM_({
        console.log(UTF8ToString($0));
    }, msg.c_str());
}

读取文件

index.data

在 emcc 编译过程中使用 --preload-file "D:/Dev/OpenglProject/GLProj/assets/textures" 可以将该目录打包进 index.data 中,

index.data 文件是 Emscripten 工具链自动生成和管理的,其内容在 WebAssembly 模块启动时会被自动解压并加载到一个虚拟的 Emscripten 文件系统(通常是 MEMFS)中。

你的 C/C++ 代码(编译成 WebAssembly)会像访问本地文件系统一样,通过标准的 IO 函数来读取这些文件。Emscripten 的运行时环境会将这些调用重定向到 index.data 中预加载的内容。

fetch

<script>
    const files = ["assets/glsl/fragshader.glsl", "assets/glsl/vertexshader.glsl"];
    const data = {};

    for(const file of files){
        fetch(file)
            .then(res => res.text())
            .then(res => data[file] = res);
    }
</script>

导出函数给 JS 调用

以 setWindowSize 函数为例子,在 cpp 文件中这样编写:

// helper.hpp
#include <GLFW/glfw3.h>
#include <emscripten/emscripten.h>
extern GLFWwindow *window;
extern "C" {
  void EMSCRIPTEN_KEEPALIVE setWindowSize(int width, int height) {
    glfwSetWindowSize(window, width, height);
    glViewport(0, 0, width, height);
  }
}

在 index.html 中调用 _setWindowSize

<script type='text/javascript'>
  const canvas = document.getElementById('canvas');

  window.addEventListener('resize', resize);

  function resize(){
    const width = window.innerWidth;
    const height = window.innerHeight;
    // const height = window.innerWidth < window.innerHeight ? window.innerWidth : window.innerHeight;


    _setWindowSize(width*2, height*2); //sets the content to the correct size
    //setting it to be 2x bigger than the actual viewport seems to get rid of the aliasing effect well

    canvas.setAttribute("style", `width:${width}px; height:${height}px`); //sets canvas to be as big as the content inside it
  }
</script>

引用

https://github.com/aliabbas299792/openglWASMTemplate
https://developer.aliyun.com/article/609232
https://github.com/13rac1/starter-wasm-webgl-opengl
https://juejin.cn/post/7362528152777162793
https://github.com/Bitbloq/wasmGL

https://github.com/takashikumagai/wasm-opengl-keyboard-input/blob/master/MyApp.cpp