Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/clientside_vertex_arrays_es3.c
4129 views
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 <GLFW/glfw3.h>
9
#include <GLES3/gl3.h>
10
#include <stdlib.h>
11
#ifdef __EMSCRIPTEN__
12
#include <emscripten.h>
13
#endif
14
15
void onDraw(void* arg) {
16
GLFWwindow* window = *((GLFWwindow**)arg);
17
glClear(GL_COLOR_BUFFER_BIT);
18
glDrawArrays(GL_TRIANGLES, 0, 3);
19
glfwSwapBuffers(window);
20
glfwPollEvents();
21
#ifdef __EMSCRIPTEN__
22
EM_ASM({reftestUnblock()}); // All done, perform the JS side image comparison reftest.
23
#endif
24
}
25
26
void onResize(GLFWwindow* window, int width, int height) {
27
glViewport(0, 0, width, height);
28
}
29
30
void onClose(GLFWwindow* window) {
31
glfwDestroyWindow(window);
32
glfwTerminate();
33
exit(EXIT_SUCCESS);
34
}
35
36
int main() {
37
GLFWwindow* window;
38
39
if (!glfwInit()) {
40
return EXIT_FAILURE;
41
}
42
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
43
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
44
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
45
window = glfwCreateWindow(640, 480, "Client-side Vertex Arrays", NULL, NULL);
46
if (!window) {
47
glfwTerminate();
48
return EXIT_FAILURE;
49
}
50
glfwSetFramebufferSizeCallback(window, onResize);
51
glfwSetWindowCloseCallback(window, onClose);
52
glfwMakeContextCurrent(window);
53
54
GLuint vertex_shader;
55
GLuint fragment_shader;
56
GLuint program;
57
{
58
const GLchar* source = "#version 300 es \n"
59
" \n"
60
"in vec2 aPosition; \n"
61
"in uvec3 aColor; \n"
62
"out vec4 color; \n"
63
" \n"
64
"void main() \n"
65
"{ \n"
66
" gl_Position = vec4(aPosition, 0.f, 1.f); \n"
67
" color = vec4(vec3(aColor) / 255.f, 1.f); \n"
68
"} \n";
69
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
70
glShaderSource(vertex_shader, 1, &source, NULL);
71
glCompileShader(vertex_shader);
72
}
73
{
74
const GLchar* source = "#version 300 es \n"
75
" \n"
76
"precision mediump float; \n"
77
" \n"
78
"in vec4 color; \n"
79
"out vec4 FragColor; \n"
80
" \n"
81
"void main() \n"
82
"{ \n"
83
" FragColor = color; \n"
84
"} \n";
85
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
86
glShaderSource(fragment_shader, 1, &source, NULL);
87
glCompileShader(fragment_shader);
88
}
89
program = glCreateProgram();
90
glAttachShader(program, vertex_shader);
91
glAttachShader(program, fragment_shader);
92
glLinkProgram(program);
93
glUseProgram(program);
94
{
95
static const float position[] = {
96
-1.f, -1.f,
97
1.f, -1.f,
98
0.f, 1.f,
99
};
100
GLint location;
101
location = glGetAttribLocation(program, "aPosition");
102
glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, position);
103
glEnableVertexAttribArray(location);
104
}
105
{
106
static const unsigned int color[] = {
107
255, 0, 0,
108
0, 255, 0,
109
0, 0, 255,
110
};
111
GLint location;
112
location = glGetAttribLocation(program, "aColor");
113
glVertexAttribIPointer(location, 3, GL_UNSIGNED_INT, 0, color);
114
glEnableVertexAttribArray(location);
115
}
116
117
#ifdef __EMSCRIPTEN__
118
// This test kicks off an asynchronous main loop, so do not perform a synchronous
119
// reftest immediately after falling out from main.
120
EM_ASM({reftestBlock()});
121
122
emscripten_set_main_loop_arg(onDraw, &window, 0, 1);
123
#else
124
while (1) {
125
onDraw(&window);
126
}
127
#endif
128
129
return EXIT_FAILURE; // not reached
130
}
131
132