Path: blob/main/test/clientside_vertex_arrays_es3.c
4129 views
/*1* Copyright 2018 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include <GLFW/glfw3.h>8#include <GLES3/gl3.h>9#include <stdlib.h>10#ifdef __EMSCRIPTEN__11#include <emscripten.h>12#endif1314void onDraw(void* arg) {15GLFWwindow* window = *((GLFWwindow**)arg);16glClear(GL_COLOR_BUFFER_BIT);17glDrawArrays(GL_TRIANGLES, 0, 3);18glfwSwapBuffers(window);19glfwPollEvents();20#ifdef __EMSCRIPTEN__21EM_ASM({reftestUnblock()}); // All done, perform the JS side image comparison reftest.22#endif23}2425void onResize(GLFWwindow* window, int width, int height) {26glViewport(0, 0, width, height);27}2829void onClose(GLFWwindow* window) {30glfwDestroyWindow(window);31glfwTerminate();32exit(EXIT_SUCCESS);33}3435int main() {36GLFWwindow* window;3738if (!glfwInit()) {39return EXIT_FAILURE;40}41glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);42glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);43glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);44window = glfwCreateWindow(640, 480, "Client-side Vertex Arrays", NULL, NULL);45if (!window) {46glfwTerminate();47return EXIT_FAILURE;48}49glfwSetFramebufferSizeCallback(window, onResize);50glfwSetWindowCloseCallback(window, onClose);51glfwMakeContextCurrent(window);5253GLuint vertex_shader;54GLuint fragment_shader;55GLuint program;56{57const GLchar* source = "#version 300 es \n"58" \n"59"in vec2 aPosition; \n"60"in uvec3 aColor; \n"61"out vec4 color; \n"62" \n"63"void main() \n"64"{ \n"65" gl_Position = vec4(aPosition, 0.f, 1.f); \n"66" color = vec4(vec3(aColor) / 255.f, 1.f); \n"67"} \n";68vertex_shader = glCreateShader(GL_VERTEX_SHADER);69glShaderSource(vertex_shader, 1, &source, NULL);70glCompileShader(vertex_shader);71}72{73const GLchar* source = "#version 300 es \n"74" \n"75"precision mediump float; \n"76" \n"77"in vec4 color; \n"78"out vec4 FragColor; \n"79" \n"80"void main() \n"81"{ \n"82" FragColor = color; \n"83"} \n";84fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);85glShaderSource(fragment_shader, 1, &source, NULL);86glCompileShader(fragment_shader);87}88program = glCreateProgram();89glAttachShader(program, vertex_shader);90glAttachShader(program, fragment_shader);91glLinkProgram(program);92glUseProgram(program);93{94static const float position[] = {95-1.f, -1.f,961.f, -1.f,970.f, 1.f,98};99GLint location;100location = glGetAttribLocation(program, "aPosition");101glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, position);102glEnableVertexAttribArray(location);103}104{105static const unsigned int color[] = {106255, 0, 0,1070, 255, 0,1080, 0, 255,109};110GLint location;111location = glGetAttribLocation(program, "aColor");112glVertexAttribIPointer(location, 3, GL_UNSIGNED_INT, 0, color);113glEnableVertexAttribArray(location);114}115116#ifdef __EMSCRIPTEN__117// This test kicks off an asynchronous main loop, so do not perform a synchronous118// reftest immediately after falling out from main.119EM_ASM({reftestBlock()});120121emscripten_set_main_loop_arg(onDraw, &window, 0, 1);122#else123while (1) {124onDraw(&window);125}126#endif127128return EXIT_FAILURE; // not reached129}130131132