1 #include "esTransform.h"
2 #include "cube-gles2.h"
3 #include "cube.h"
4
5 #include <kms++util/kms++util.h>
6
7 using namespace std;
8
GlScene()9 GlScene::GlScene()
10 {
11 GLuint vertex_shader, fragment_shader;
12 GLint ret;
13
14 static const GLfloat vVertices[] = {
15 // front
16 -1.0f, -1.0f, +1.0f, // point blue
17 +1.0f, -1.0f, +1.0f, // point magenta
18 -1.0f, +1.0f, +1.0f, // point cyan
19 +1.0f, +1.0f, +1.0f, // point white
20 // back
21 +1.0f, -1.0f, -1.0f, // point red
22 -1.0f, -1.0f, -1.0f, // point black
23 +1.0f, +1.0f, -1.0f, // point yellow
24 -1.0f, +1.0f, -1.0f, // point green
25 // right
26 +1.0f, -1.0f, +1.0f, // point magenta
27 +1.0f, -1.0f, -1.0f, // point red
28 +1.0f, +1.0f, +1.0f, // point white
29 +1.0f, +1.0f, -1.0f, // point yellow
30 // left
31 -1.0f, -1.0f, -1.0f, // point black
32 -1.0f, -1.0f, +1.0f, // point blue
33 -1.0f, +1.0f, -1.0f, // point green
34 -1.0f, +1.0f, +1.0f, // point cyan
35 // top
36 -1.0f, +1.0f, +1.0f, // point cyan
37 +1.0f, +1.0f, +1.0f, // point white
38 -1.0f, +1.0f, -1.0f, // point green
39 +1.0f, +1.0f, -1.0f, // point yellow
40 // bottom
41 -1.0f, -1.0f, -1.0f, // point black
42 +1.0f, -1.0f, -1.0f, // point red
43 -1.0f, -1.0f, +1.0f, // point blue
44 +1.0f, -1.0f, +1.0f // point magenta
45 };
46
47 static const GLfloat vColors[] = {
48 // front
49 0.0f, 0.0f, 1.0f, // blue
50 1.0f, 0.0f, 1.0f, // magenta
51 0.0f, 1.0f, 1.0f, // cyan
52 1.0f, 1.0f, 1.0f, // white
53 // back
54 1.0f, 0.0f, 0.0f, // red
55 0.0f, 0.0f, 0.0f, // black
56 1.0f, 1.0f, 0.0f, // yellow
57 0.0f, 1.0f, 0.0f, // green
58 // right
59 1.0f, 0.0f, 1.0f, // magenta
60 1.0f, 0.0f, 0.0f, // red
61 1.0f, 1.0f, 1.0f, // white
62 1.0f, 1.0f, 0.0f, // yellow
63 // left
64 0.0f, 0.0f, 0.0f, // black
65 0.0f, 0.0f, 1.0f, // blue
66 0.0f, 1.0f, 0.0f, // green
67 0.0f, 1.0f, 1.0f, // cyan
68 // top
69 0.0f, 1.0f, 1.0f, // cyan
70 1.0f, 1.0f, 1.0f, // white
71 0.0f, 1.0f, 0.0f, // green
72 1.0f, 1.0f, 0.0f, // yellow
73 // bottom
74 0.0f, 0.0f, 0.0f, // black
75 1.0f, 0.0f, 0.0f, // red
76 0.0f, 0.0f, 1.0f, // blue
77 1.0f, 0.0f, 1.0f // magenta
78 };
79
80 static const GLfloat vNormals[] = {
81 // front
82 +0.0f, +0.0f, +1.0f, // forward
83 +0.0f, +0.0f, +1.0f, // forward
84 +0.0f, +0.0f, +1.0f, // forward
85 +0.0f, +0.0f, +1.0f, // forward
86 // back
87 +0.0f, +0.0f, -1.0f, // backbard
88 +0.0f, +0.0f, -1.0f, // backbard
89 +0.0f, +0.0f, -1.0f, // backbard
90 +0.0f, +0.0f, -1.0f, // backbard
91 // right
92 +1.0f, +0.0f, +0.0f, // right
93 +1.0f, +0.0f, +0.0f, // right
94 +1.0f, +0.0f, +0.0f, // right
95 +1.0f, +0.0f, +0.0f, // right
96 // left
97 -1.0f, +0.0f, +0.0f, // left
98 -1.0f, +0.0f, +0.0f, // left
99 -1.0f, +0.0f, +0.0f, // left
100 -1.0f, +0.0f, +0.0f, // left
101 // top
102 +0.0f, +1.0f, +0.0f, // up
103 +0.0f, +1.0f, +0.0f, // up
104 +0.0f, +1.0f, +0.0f, // up
105 +0.0f, +1.0f, +0.0f, // up
106 // bottom
107 +0.0f, -1.0f, +0.0f, // down
108 +0.0f, -1.0f, +0.0f, // down
109 +0.0f, -1.0f, +0.0f, // down
110 +0.0f, -1.0f, +0.0f // down
111 };
112
113 static const char *vertex_shader_source =
114 "uniform mat4 modelviewMatrix; \n"
115 "uniform mat4 modelviewprojectionMatrix;\n"
116 "uniform mat3 normalMatrix; \n"
117 " \n"
118 "attribute vec4 in_position; \n"
119 "attribute vec3 in_normal; \n"
120 "attribute vec4 in_color; \n"
121 "\n"
122 "vec4 lightSource = vec4(2.0, 2.0, 20.0, 0.0);\n"
123 " \n"
124 "varying vec4 vVaryingColor; \n"
125 " \n"
126 "void main() \n"
127 "{ \n"
128 " gl_Position = modelviewprojectionMatrix * in_position;\n"
129 " vec3 vEyeNormal = normalMatrix * in_normal;\n"
130 " vec4 vPosition4 = modelviewMatrix * in_position;\n"
131 " vec3 vPosition3 = vPosition4.xyz / vPosition4.w;\n"
132 " vec3 vLightDir = normalize(lightSource.xyz - vPosition3);\n"
133 " float diff = max(0.0, dot(vEyeNormal, vLightDir));\n"
134 " vVaryingColor = vec4(diff * in_color.rgb, 1.0);\n"
135 "} \n";
136
137 static const char *fragment_shader_source =
138 "precision mediump float; \n"
139 " \n"
140 "varying vec4 vVaryingColor; \n"
141 " \n"
142 "void main() \n"
143 "{ \n"
144 " gl_FragColor = vVaryingColor; \n"
145 "} \n";
146
147
148 if (s_verbose) {
149 printf("GL_VENDOR: %s\n", glGetString(GL_VENDOR));
150 printf("GL_VERSION: %s\n", glGetString(GL_VERSION));
151 printf("GL_RENDERER: %s\n", glGetString(GL_RENDERER));
152 printf("GL_EXTENSIONS: %s\n", glGetString(GL_EXTENSIONS));
153 }
154
155 vertex_shader = glCreateShader(GL_VERTEX_SHADER);
156
157 glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
158 glCompileShader(vertex_shader);
159
160 glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &ret);
161 FAIL_IF(!ret, "vertex shader compilation failed!");
162
163 fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
164
165 glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
166 glCompileShader(fragment_shader);
167
168 glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &ret);
169 FAIL_IF(!ret, "fragment shader compilation failed!");
170
171 GLuint program = glCreateProgram();
172
173 glAttachShader(program, vertex_shader);
174 glAttachShader(program, fragment_shader);
175
176 glBindAttribLocation(program, 0, "in_position");
177 glBindAttribLocation(program, 1, "in_normal");
178 glBindAttribLocation(program, 2, "in_color");
179
180 glLinkProgram(program);
181
182 glGetProgramiv(program, GL_LINK_STATUS, &ret);
183 FAIL_IF(!ret, "program linking failed!");
184
185 glUseProgram(program);
186
187 m_modelviewmatrix = glGetUniformLocation(program, "modelviewMatrix");
188 m_modelviewprojectionmatrix = glGetUniformLocation(program, "modelviewprojectionMatrix");
189 m_normalmatrix = glGetUniformLocation(program, "normalMatrix");
190
191 glEnable(GL_CULL_FACE);
192
193 GLintptr positionsoffset = 0;
194 GLintptr colorsoffset = sizeof(vVertices);
195 GLintptr normalsoffset = sizeof(vVertices) + sizeof(vColors);
196 GLuint vbo;
197
198 glGenBuffers(1, &vbo);
199 glBindBuffer(GL_ARRAY_BUFFER, vbo);
200 glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices) + sizeof(vColors) + sizeof(vNormals), 0, GL_STATIC_DRAW);
201 glBufferSubData(GL_ARRAY_BUFFER, positionsoffset, sizeof(vVertices), &vVertices[0]);
202 glBufferSubData(GL_ARRAY_BUFFER, colorsoffset, sizeof(vColors), &vColors[0]);
203 glBufferSubData(GL_ARRAY_BUFFER, normalsoffset, sizeof(vNormals), &vNormals[0]);
204 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)positionsoffset);
205 glEnableVertexAttribArray(0);
206 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)normalsoffset);
207 glEnableVertexAttribArray(1);
208 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)colorsoffset);
209 glEnableVertexAttribArray(2);
210 }
211
set_viewport(uint32_t width,uint32_t height)212 void GlScene::set_viewport(uint32_t width, uint32_t height)
213 {
214 m_width = width;
215 m_height = height;
216 }
217
draw(uint32_t framenum)218 void GlScene::draw(uint32_t framenum)
219 {
220 glViewport(0, 0, m_width, m_height);
221
222 glClearColor(0.5, 0.5, 0.5, 1.0);
223 glClear(GL_COLOR_BUFFER_BIT);
224
225 ESMatrix modelview;
226
227 esMatrixLoadIdentity(&modelview);
228 esTranslate(&modelview, 0.0f, 0.0f, -8.0f);
229 esRotate(&modelview, 45.0f + (0.75f * framenum), 1.0f, 0.0f, 0.0f);
230 esRotate(&modelview, 45.0f - (0.5f * framenum), 0.0f, 1.0f, 0.0f);
231 esRotate(&modelview, 10.0f + (0.45f * framenum), 0.0f, 0.0f, 1.0f);
232
233 GLfloat aspect = (float)m_height / m_width;
234
235 ESMatrix projection;
236 esMatrixLoadIdentity(&projection);
237 esFrustum(&projection, -2.8f, +2.8f, -2.8f * aspect, +2.8f * aspect, 6.0f, 10.0f);
238
239 ESMatrix modelviewprojection;
240 esMatrixLoadIdentity(&modelviewprojection);
241 esMatrixMultiply(&modelviewprojection, &modelview, &projection);
242
243 float normal[9];
244 normal[0] = modelview.m[0][0];
245 normal[1] = modelview.m[0][1];
246 normal[2] = modelview.m[0][2];
247 normal[3] = modelview.m[1][0];
248 normal[4] = modelview.m[1][1];
249 normal[5] = modelview.m[1][2];
250 normal[6] = modelview.m[2][0];
251 normal[7] = modelview.m[2][1];
252 normal[8] = modelview.m[2][2];
253
254 glUniformMatrix4fv(m_modelviewmatrix, 1, GL_FALSE, &modelview.m[0][0]);
255 glUniformMatrix4fv(m_modelviewprojectionmatrix, 1, GL_FALSE, &modelviewprojection.m[0][0]);
256 glUniformMatrix3fv(m_normalmatrix, 1, GL_FALSE, normal);
257
258 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
259 glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
260 glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
261 glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
262 glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
263 glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
264 }
265