1 //========================================================================
2 // OpenGL ES 2.0 triangle example
3 // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 // claim that you wrote the original software. If you use this software
15 // in a product, an acknowledgment in the product documentation would
16 // be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 // be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 // distribution.
23 //
24 //========================================================================
25
26 #define GLAD_GLES2_IMPLEMENTATION
27 #include <glad/gles2.h>
28 #define GLFW_INCLUDE_NONE
29 #include <GLFW/glfw3.h>
30
31 #include "linmath.h"
32
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <stdio.h>
36
37 typedef struct Vertex
38 {
39 vec2 pos;
40 vec3 col;
41 } Vertex;
42
43 static const Vertex vertices[3] =
44 {
45 { { -0.6f, -0.4f }, { 1.f, 0.f, 0.f } },
46 { { 0.6f, -0.4f }, { 0.f, 1.f, 0.f } },
47 { { 0.f, 0.6f }, { 0.f, 0.f, 1.f } }
48 };
49
50 static const char* vertex_shader_text =
51 "#version 100\n"
52 "precision mediump float;\n"
53 "uniform mat4 MVP;\n"
54 "attribute vec3 vCol;\n"
55 "attribute vec2 vPos;\n"
56 "varying vec3 color;\n"
57 "void main()\n"
58 "{\n"
59 " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
60 " color = vCol;\n"
61 "}\n";
62
63 static const char* fragment_shader_text =
64 "#version 100\n"
65 "precision mediump float;\n"
66 "varying vec3 color;\n"
67 "void main()\n"
68 "{\n"
69 " gl_FragColor = vec4(color, 1.0);\n"
70 "}\n";
71
error_callback(int error, const char* description)72 static void error_callback(int error, const char* description)
73 {
74 fprintf(stderr, "GLFW Error: %s\n", description);
75 }
76
key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)77 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
78 {
79 if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
80 glfwSetWindowShouldClose(window, GLFW_TRUE);
81 }
82
main(void)83 int main(void)
84 {
85 glfwSetErrorCallback(error_callback);
86
87 if (!glfwInit())
88 exit(EXIT_FAILURE);
89
90 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
91 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
92 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
93 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
94
95 GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL ES 2.0 Triangle (EGL)", NULL, NULL);
96 if (!window)
97 {
98 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_NATIVE_CONTEXT_API);
99 window = glfwCreateWindow(640, 480, "OpenGL ES 2.0 Triangle", NULL, NULL);
100 if (!window)
101 {
102 glfwTerminate();
103 exit(EXIT_FAILURE);
104 }
105 }
106
107 glfwSetKeyCallback(window, key_callback);
108
109 glfwMakeContextCurrent(window);
110 gladLoadGLES2(glfwGetProcAddress);
111 glfwSwapInterval(1);
112
113 GLuint vertex_buffer;
114 glGenBuffers(1, &vertex_buffer);
115 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
116 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
117
118 const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
119 glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
120 glCompileShader(vertex_shader);
121
122 const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
123 glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
124 glCompileShader(fragment_shader);
125
126 const GLuint program = glCreateProgram();
127 glAttachShader(program, vertex_shader);
128 glAttachShader(program, fragment_shader);
129 glLinkProgram(program);
130
131 const GLint mvp_location = glGetUniformLocation(program, "MVP");
132 const GLint vpos_location = glGetAttribLocation(program, "vPos");
133 const GLint vcol_location = glGetAttribLocation(program, "vCol");
134
135 glEnableVertexAttribArray(vpos_location);
136 glEnableVertexAttribArray(vcol_location);
137 glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
138 sizeof(Vertex), (void*) offsetof(Vertex, pos));
139 glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
140 sizeof(Vertex), (void*) offsetof(Vertex, col));
141
142 while (!glfwWindowShouldClose(window))
143 {
144 int width, height;
145 glfwGetFramebufferSize(window, &width, &height);
146 const float ratio = width / (float) height;
147
148 glViewport(0, 0, width, height);
149 glClear(GL_COLOR_BUFFER_BIT);
150
151 mat4x4 m, p, mvp;
152 mat4x4_identity(m);
153 mat4x4_rotate_Z(m, m, (float) glfwGetTime());
154 mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
155 mat4x4_mul(mvp, p, m);
156
157 glUseProgram(program);
158 glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) &mvp);
159 glDrawArrays(GL_TRIANGLES, 0, 3);
160
161 glfwSwapBuffers(window);
162 glfwPollEvents();
163 }
164
165 glfwDestroyWindow(window);
166
167 glfwTerminate();
168 exit(EXIT_SUCCESS);
169 }
170
171