1 Star 0 Fork 0

almpazel/opengl-canvas-wasm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 3.52 KB
一键复制 编辑 原始数据 按行查看 历史
#include <functional>
#include <emscripten.h>
#include <SDL.h>
#define GL_GLEXT_PROTOTYPES 1
#include <SDL_opengles2.h>
// Shader sources
const GLchar* vertexSource =
"attribute vec4 position; \n"
"void main() \n"
"{ \n"
" gl_Position = vec4(position.xyz, 1.0); \n"
"} \n";
const GLchar* fragmentSource =
"precision mediump float;\n"
"void main() \n"
"{ \n"
" gl_FragColor[0] = gl_FragCoord.x/640.0; \n"
" gl_FragColor[1] = gl_FragCoord.y/480.0; \n"
" gl_FragColor[2] = 0.5; \n"
"} \n";
// an example of something we will control from the javascript side
bool background_is_black = true;
// the function called by the javascript code
extern "C" void toggle_background_color() { background_is_black = !background_is_black; }
std::function<void()> loop;
void main_loop() { loop(); }
int main(int argc, char** argv)
{
SDL_Window *window;
SDL_CreateWindowAndRenderer(640, 480, 0, &window, nullptr);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetSwapInterval(0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers(1, &vbo);
GLfloat vertices[] = {0.0f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Create and compile the vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, nullptr);
glCompileShader(vertexShader);
// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
glCompileShader(fragmentShader);
// Link the vertex and fragment shader into a shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
// Specify the layout of the vertex data
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
loop = [&]
{
// move a vertex
const uint32_t milliseconds_since_start = SDL_GetTicks();
const uint32_t milliseconds_per_loop = 3000;
vertices[0] = ( milliseconds_since_start % milliseconds_per_loop ) / float(milliseconds_per_loop) - 0.5f;
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Clear the screen
if( background_is_black )
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
else
glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw a triangle from the 3 vertices
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window);
};
emscripten_set_main_loop(main_loop, 0, true);
return EXIT_SUCCESS;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/almpazel/opengl-canvas-wasm.git
[email protected]:almpazel/opengl-canvas-wasm.git
almpazel
opengl-canvas-wasm
opengl-canvas-wasm
gh-pages

搜索帮助