首页 > 解决方案 > Emscripten:如何在运行时检测 webgl 上下文版本?

问题描述

我在 Emscripten 中使用 GLFW3 和 GLEW 包装器,所以我不emscripten_webgl_create_context手动调用也不设置上下文的属性。上下文版本仅由 JS 代码确定,这超出了我的范围。在我的 C++ 代码中,我需要知道我们是在 WebGL1 还是 WebGL2 上下文中运行。有没有一种独立于文件的方式来做到这一点?就像是:

const auto ctx = emscripten_webgl_get_current_context();
emscripten_webgl_get_context_version(ctx);// Should return 1 or 2.

标签: webglemscripten

解决方案


在 C++ 中

const char ES_VERSION_2_0[] = "OpenGL ES 2.0";
const char ES_VERSION_3_0[] = "OpenGL ES 3.0";

const char* version = glGetString(GL_VERSION);
if (strncmp(version, ES_VERSION_2_0, sizeof(ES_VERSION_2_0)) == 0) {
  // it's WebGL1
} else if (strncmp(version, ES_VERSION_3_0, sizeof(ES_VERSION_3_0)) == 0) {
  // it's WebGL2
} else {
  // it's something else
}

WebGL 中的版本字符串具有必需的非硬件相关的起始格式。请参阅WebGL2 的规范

VERSION:返回 WebGL<space>2.0<optional><space><vendor-specific information></optional> 形式的版本或发行版号。

对于WebGL1

VERSION:返回 WebGL<space>1.0<space><vendor-specific information> 形式的版本或发行版号。

Emscripten 还返回固定字符串。查看源代码

https://github.com/kripken/emscripten/blob/ec764ace634f13bab5ae932912da53fe93ee1b69/src/library_gl.js#L923


推荐阅读