James Kuszmaul | 9a05bfd | 2019-08-03 17:03:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Emscripten Authors. All rights reserved. |
| 3 | * Emscripten is available under two separate licenses, the MIT license and the |
| 4 | * University of Illinois/NCSA Open Source License. Both these licenses can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <emscripten/emscripten.h> |
| 11 | #include <emscripten/html5.h> |
| 12 | #include <GLES2/gl2.h> |
| 13 | |
| 14 | GLuint compile_shader(GLenum shaderType, const char *src) |
| 15 | { |
| 16 | GLuint shader = glCreateShader(shaderType); |
| 17 | glShaderSource(shader, 1, &src, NULL); |
| 18 | glCompileShader(shader); |
| 19 | |
| 20 | GLint isCompiled = 0; |
| 21 | glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled); |
| 22 | if (!isCompiled) |
| 23 | { |
| 24 | GLint maxLength = 0; |
| 25 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); |
| 26 | char *buf = (char*)malloc(maxLength+1); |
| 27 | glGetShaderInfoLog(shader, maxLength, &maxLength, buf); |
| 28 | printf("%s\n", buf); |
| 29 | free(buf); |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | return shader; |
| 34 | } |
| 35 | |
| 36 | GLuint create_program(GLuint vertexShader, GLuint fragmentShader) |
| 37 | { |
| 38 | GLuint program = glCreateProgram(); |
| 39 | glAttachShader(program, vertexShader); |
| 40 | glAttachShader(program, fragmentShader); |
| 41 | glBindAttribLocation(program, 0, "apos"); |
| 42 | glBindAttribLocation(program, 1, "acolor"); |
| 43 | glLinkProgram(program); |
| 44 | return program; |
| 45 | } |
| 46 | |
| 47 | int main() |
| 48 | { |
| 49 | EmscriptenWebGLContextAttributes attr; |
| 50 | emscripten_webgl_init_context_attributes(&attr); |
| 51 | #ifdef EXPLICIT_SWAP |
| 52 | attr.explicitSwapControl = 1; |
| 53 | #endif |
| 54 | #ifdef DRAW_FROM_CLIENT_MEMORY |
| 55 | // This test verifies that drawing from client-side memory when enableExtensionsByDefault==false works. |
| 56 | attr.enableExtensionsByDefault = 0; |
| 57 | #endif |
| 58 | |
| 59 | EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr); |
| 60 | emscripten_webgl_make_context_current(ctx); |
| 61 | |
| 62 | static const char vertex_shader[] = |
| 63 | "attribute vec4 apos;" |
| 64 | "attribute vec4 acolor;" |
| 65 | "varying vec4 color;" |
| 66 | "void main() {" |
| 67 | "color = acolor;" |
| 68 | "gl_Position = apos;" |
| 69 | "}"; |
| 70 | GLuint vs = compile_shader(GL_VERTEX_SHADER, vertex_shader); |
| 71 | |
| 72 | static const char fragment_shader[] = |
| 73 | "precision lowp float;" |
| 74 | "varying vec4 color;" |
| 75 | "void main() {" |
| 76 | "gl_FragColor = color;" |
| 77 | "}"; |
| 78 | GLuint fs = compile_shader(GL_FRAGMENT_SHADER, fragment_shader); |
| 79 | |
| 80 | GLuint program = create_program(vs, fs); |
| 81 | glUseProgram(program); |
| 82 | |
| 83 | static const float pos_and_color[] = { |
| 84 | // x, y, r, g, b |
| 85 | -0.6f, -0.6f, 1, 0, 0, |
| 86 | 0.6f, -0.6f, 0, 1, 0, |
| 87 | 0.f, 0.6f, 0, 0, 1, |
| 88 | }; |
| 89 | |
| 90 | #ifdef DRAW_FROM_CLIENT_MEMORY |
| 91 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 20, pos_and_color); |
| 92 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 20, (void*)(pos_and_color+2)); |
| 93 | #else |
| 94 | GLuint vbo; |
| 95 | glGenBuffers(1, &vbo); |
| 96 | glBindBuffer(GL_ARRAY_BUFFER, vbo); |
| 97 | glBufferData(GL_ARRAY_BUFFER, sizeof(pos_and_color), pos_and_color, GL_STATIC_DRAW); |
| 98 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 20, 0); |
| 99 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 20, (void*)8); |
| 100 | #endif |
| 101 | glEnableVertexAttribArray(0); |
| 102 | glEnableVertexAttribArray(1); |
| 103 | |
| 104 | glClearColor(0.3f,0.3f,0.3f,1); |
| 105 | glClear(GL_COLOR_BUFFER_BIT); |
| 106 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 107 | |
| 108 | #ifdef EXPLICIT_SWAP |
| 109 | emscripten_webgl_commit_frame(); |
| 110 | #endif |
| 111 | |
| 112 | #ifdef REPORT_RESULT |
| 113 | REPORT_RESULT(0); |
| 114 | #endif |
| 115 | } |