MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / init_webgl

Function init_webgl

test/minimal_webgl/webgl.c:45–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43}
44
45void init_webgl(int width, int height)
46{
47 double dpr = emscripten_get_device_pixel_ratio();
48 emscripten_set_element_css_size("canvas", width / dpr, height / dpr);
49 emscripten_set_canvas_element_size("canvas", width, height);
50
51 EmscriptenWebGLContextAttributes attrs;
52 emscripten_webgl_init_context_attributes(&attrs);
53 attrs.alpha = 0;
54#if MAX_WEBGL_VERSION >= 2
55 attrs.majorVersion = 2;
56#endif
57 glContext = emscripten_webgl_create_context("canvas", &attrs);
58 assert(glContext);
59 emscripten_webgl_make_context_current(glContext);
60
61 pixelWidth = 2.f / width;
62 pixelHeight = 2.f / height;
63
64 static const char vertex_shader[] =
65 "attribute vec4 pos;"
66 "varying vec2 uv;"
67 "uniform mat4 mat;"
68 "void main(){"
69 "uv=pos.xy;"
70 "gl_Position=mat*pos;"
71 "}";
72 GLuint vs = compile_shader(GL_VERTEX_SHADER, vertex_shader);
73
74 static const char fragment_shader[] =
75 "precision lowp float;"
76 "uniform sampler2D tex;"
77 "varying vec2 uv;"
78 "uniform vec4 color;"
79 "void main(){"
80 "gl_FragColor=color*texture2D(tex,uv);"
81 "}";
82 GLuint fs = compile_shader(GL_FRAGMENT_SHADER, fragment_shader);
83
84 GLuint program = create_program(vs, fs);
85 colorPos = glGetUniformLocation(program, "color");
86 matPos = glGetUniformLocation(program, "mat");
87 glEnable(GL_BLEND);
88 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
89
90 glGenBuffers(1, &quad);
91 glBindBuffer(GL_ARRAY_BUFFER, quad);
92 const float pos[] = { 0, 0, 1, 0, 0, 1, 1, 1 };
93 glBufferData(GL_ARRAY_BUFFER, sizeof(pos), pos, GL_STATIC_DRAW);
94 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
95 glEnableVertexAttribArray(0);
96
97 solidColor = create_texture();
98 unsigned int whitePixel = 0xFFFFFFFFu;
99 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &whitePixel);
100}
101
102typedef void (*tick_func)(double t, double dt);

Callers 1

mainFunction · 0.85

Calls 9

glBufferDataFunction · 0.85
create_textureFunction · 0.85
glTexImage2DFunction · 0.85
compile_shaderFunction · 0.70
create_programFunction · 0.70
assertFunction · 0.50

Tested by

no test coverage detected