• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	if (s_verbose) {
148 		printf("GL_VENDOR:       %s\n", glGetString(GL_VENDOR));
149 		printf("GL_VERSION:      %s\n", glGetString(GL_VERSION));
150 		printf("GL_RENDERER:     %s\n", glGetString(GL_RENDERER));
151 		printf("GL_EXTENSIONS:   %s\n", glGetString(GL_EXTENSIONS));
152 	}
153 
154 	vertex_shader = glCreateShader(GL_VERTEX_SHADER);
155 
156 	glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
157 	glCompileShader(vertex_shader);
158 
159 	glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &ret);
160 	FAIL_IF(!ret, "vertex shader compilation failed!");
161 
162 	fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
163 
164 	glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
165 	glCompileShader(fragment_shader);
166 
167 	glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &ret);
168 	FAIL_IF(!ret, "fragment shader compilation failed!");
169 
170 	GLuint program = glCreateProgram();
171 
172 	glAttachShader(program, vertex_shader);
173 	glAttachShader(program, fragment_shader);
174 
175 	glBindAttribLocation(program, 0, "in_position");
176 	glBindAttribLocation(program, 1, "in_normal");
177 	glBindAttribLocation(program, 2, "in_color");
178 
179 	glLinkProgram(program);
180 
181 	glGetProgramiv(program, GL_LINK_STATUS, &ret);
182 	FAIL_IF(!ret, "program linking failed!");
183 
184 	glUseProgram(program);
185 
186 	m_modelviewmatrix = glGetUniformLocation(program, "modelviewMatrix");
187 	m_modelviewprojectionmatrix = glGetUniformLocation(program, "modelviewprojectionMatrix");
188 	m_normalmatrix = glGetUniformLocation(program, "normalMatrix");
189 
190 	glEnable(GL_CULL_FACE);
191 
192 	GLintptr positionsoffset = 0;
193 	GLintptr colorsoffset = sizeof(vVertices);
194 	GLintptr normalsoffset = sizeof(vVertices) + sizeof(vColors);
195 	GLuint vbo;
196 
197 	glGenBuffers(1, &vbo);
198 	glBindBuffer(GL_ARRAY_BUFFER, vbo);
199 	glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices) + sizeof(vColors) + sizeof(vNormals), 0, GL_STATIC_DRAW);
200 	glBufferSubData(GL_ARRAY_BUFFER, positionsoffset, sizeof(vVertices), &vVertices[0]);
201 	glBufferSubData(GL_ARRAY_BUFFER, colorsoffset, sizeof(vColors), &vColors[0]);
202 	glBufferSubData(GL_ARRAY_BUFFER, normalsoffset, sizeof(vNormals), &vNormals[0]);
203 	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)positionsoffset);
204 	glEnableVertexAttribArray(0);
205 	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)normalsoffset);
206 	glEnableVertexAttribArray(1);
207 	glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)colorsoffset);
208 	glEnableVertexAttribArray(2);
209 }
210 
set_viewport(uint32_t width,uint32_t height)211 void GlScene::set_viewport(uint32_t width, uint32_t height)
212 {
213 	m_width = width;
214 	m_height = height;
215 }
216 
draw(uint32_t framenum)217 void GlScene::draw(uint32_t framenum)
218 {
219 	glViewport(0, 0, m_width, m_height);
220 
221 	glClearColor(0.5, 0.5, 0.5, 1.0);
222 	glClear(GL_COLOR_BUFFER_BIT);
223 
224 	ESMatrix modelview;
225 
226 	esMatrixLoadIdentity(&modelview);
227 	esTranslate(&modelview, 0.0f, 0.0f, -8.0f);
228 	esRotate(&modelview, 45.0f + (0.75f * framenum), 1.0f, 0.0f, 0.0f);
229 	esRotate(&modelview, 45.0f - (0.5f * framenum), 0.0f, 1.0f, 0.0f);
230 	esRotate(&modelview, 10.0f + (0.45f * framenum), 0.0f, 0.0f, 1.0f);
231 
232 	GLfloat aspect = (float)m_height / m_width;
233 
234 	ESMatrix projection;
235 	esMatrixLoadIdentity(&projection);
236 	esFrustum(&projection, -2.8f, +2.8f, -2.8f * aspect, +2.8f * aspect, 6.0f, 10.0f);
237 
238 	ESMatrix modelviewprojection;
239 	esMatrixLoadIdentity(&modelviewprojection);
240 	esMatrixMultiply(&modelviewprojection, &modelview, &projection);
241 
242 	float normal[9];
243 	normal[0] = modelview.m[0][0];
244 	normal[1] = modelview.m[0][1];
245 	normal[2] = modelview.m[0][2];
246 	normal[3] = modelview.m[1][0];
247 	normal[4] = modelview.m[1][1];
248 	normal[5] = modelview.m[1][2];
249 	normal[6] = modelview.m[2][0];
250 	normal[7] = modelview.m[2][1];
251 	normal[8] = modelview.m[2][2];
252 
253 	glUniformMatrix4fv(m_modelviewmatrix, 1, GL_FALSE, &modelview.m[0][0]);
254 	glUniformMatrix4fv(m_modelviewprojectionmatrix, 1, GL_FALSE, &modelviewprojection.m[0][0]);
255 	glUniformMatrix3fv(m_normalmatrix, 1, GL_FALSE, normal);
256 
257 	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
258 	glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
259 	glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
260 	glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
261 	glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
262 	glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
263 }
264