OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This example program is based on Simple_VertexShader.c from: | |
6 | |
7 // | |
8 // Book: OpenGL(R) ES 2.0 Programming Guide | |
9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | |
10 // ISBN-10: 0321502795 | |
11 // ISBN-13: 9780321502797 | |
12 // Publisher: Addison-Wesley Professional | |
13 // URLs: http://safari.informit.com/9780321563835 | |
14 // http://www.opengles-book.com | |
15 // | |
16 | |
17 #include "mojo/examples/sample_app/spinning_cube.h" | |
18 | |
19 #include <math.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 #include "gpu/command_buffer/client/gles2_interface.h" | |
23 #include "mojo/public/system/core.h" | |
24 #include "mojo/public/system/macros.h" | |
25 | |
26 namespace mojo { | |
27 namespace examples { | |
28 | |
29 namespace { | |
30 | |
31 int GenerateCube(gpu::gles2::GLES2Interface* gl, | |
32 GLuint *vbo_vertices, | |
33 GLuint *vbo_indices) { | |
34 // const int num_vertices = 24; | |
35 const int num_indices = 36; | |
36 | |
37 const GLfloat cube_vertices[] = { | |
38 -0.5f, -0.5f, -0.5f, | |
39 -0.5f, -0.5f, 0.5f, | |
40 0.5f, -0.5f, 0.5f, | |
41 0.5f, -0.5f, -0.5f, | |
42 -0.5f, 0.5f, -0.5f, | |
43 -0.5f, 0.5f, 0.5f, | |
44 0.5f, 0.5f, 0.5f, | |
45 0.5f, 0.5f, -0.5f, | |
46 -0.5f, -0.5f, -0.5f, | |
47 -0.5f, 0.5f, -0.5f, | |
48 0.5f, 0.5f, -0.5f, | |
49 0.5f, -0.5f, -0.5f, | |
50 -0.5f, -0.5f, 0.5f, | |
51 -0.5f, 0.5f, 0.5f, | |
52 0.5f, 0.5f, 0.5f, | |
53 0.5f, -0.5f, 0.5f, | |
54 -0.5f, -0.5f, -0.5f, | |
55 -0.5f, -0.5f, 0.5f, | |
56 -0.5f, 0.5f, 0.5f, | |
57 -0.5f, 0.5f, -0.5f, | |
58 0.5f, -0.5f, -0.5f, | |
59 0.5f, -0.5f, 0.5f, | |
60 0.5f, 0.5f, 0.5f, | |
61 0.5f, 0.5f, -0.5f, | |
62 }; | |
63 | |
64 const GLushort cube_indices[] = { | |
65 0, 2, 1, | |
66 0, 3, 2, | |
67 4, 5, 6, | |
68 4, 6, 7, | |
69 8, 9, 10, | |
70 8, 10, 11, | |
71 12, 15, 14, | |
72 12, 14, 13, | |
73 16, 17, 18, | |
74 16, 18, 19, | |
75 20, 23, 22, | |
76 20, 22, 21 | |
77 }; | |
78 | |
79 if (vbo_vertices) { | |
80 gl->GenBuffers(1, vbo_vertices); | |
81 gl->BindBuffer(GL_ARRAY_BUFFER, *vbo_vertices); | |
82 gl->BufferData( | |
83 GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW); | |
84 gl->BindBuffer(GL_ARRAY_BUFFER, 0); | |
85 } | |
86 | |
87 if (vbo_indices) { | |
88 gl->GenBuffers(1, vbo_indices); | |
89 gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, *vbo_indices); | |
90 gl->BufferData( | |
91 GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices, GL_STATIC_D RAW); | |
92 gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
93 } | |
94 | |
95 return num_indices; | |
96 } | |
97 | |
98 GLuint LoadShader(gpu::gles2::GLES2Interface* gl, | |
99 GLenum type, | |
100 const char* shader_source) { | |
101 GLuint shader = gl->CreateShader(type); | |
102 if (!shader) { | |
103 return 0; | |
104 } | |
105 | |
106 gl->ShaderSource(shader, 1, &shader_source, NULL); | |
107 gl->CompileShader(shader); | |
108 | |
109 GLint compiled = 0; | |
110 gl->GetShaderiv(shader, GL_COMPILE_STATUS, &compiled); | |
111 | |
112 if (!compiled) { | |
113 gl->DeleteShader(shader); | |
114 return 0; | |
115 } | |
116 | |
117 return shader; | |
118 } | |
119 | |
120 GLuint LoadProgram(gpu::gles2::GLES2Interface* gl, | |
121 const char* vertext_shader_source, | |
122 const char* fragment_shader_source) { | |
123 GLuint vertex_shader = LoadShader(gl, | |
124 GL_VERTEX_SHADER, | |
125 vertext_shader_source); | |
126 if (!vertex_shader) | |
127 return 0; | |
128 | |
129 GLuint fragment_shader = LoadShader(gl, | |
130 GL_FRAGMENT_SHADER, | |
131 fragment_shader_source); | |
132 if (!fragment_shader) { | |
133 gl->DeleteShader(vertex_shader); | |
134 return 0; | |
135 } | |
136 | |
137 GLuint program_object = gl->CreateProgram(); | |
138 if (!program_object) | |
139 return 0; // Why don't we need to delete the shaders? | |
jamesr
2013/11/06 01:42:45
this looks copied from cc/output/program_binding.h
abarth-chromium
2013/11/06 02:25:10
It's actually copied from Simple_VertexShader.c (a
| |
140 | |
141 gl->AttachShader(program_object, vertex_shader); | |
142 gl->AttachShader(program_object, fragment_shader); | |
143 | |
144 gl->LinkProgram(program_object); | |
145 | |
146 GLint linked = 0; | |
147 gl->GetProgramiv(program_object, GL_LINK_STATUS, &linked); | |
148 | |
149 if (!linked) { | |
150 gl->DeleteProgram(program_object); | |
151 return 0; // Why don't we need to delete the shaders? | |
152 } | |
153 | |
154 gl->DeleteShader(vertex_shader); | |
155 gl->DeleteShader(fragment_shader); | |
156 | |
157 return program_object; | |
158 } | |
159 | |
160 class ESMatrix { | |
161 public: | |
162 GLfloat m[4][4]; | |
163 | |
164 ESMatrix() { | |
165 LoadZero(); | |
166 } | |
167 | |
168 void LoadZero() { | |
169 memset(this, 0x0, sizeof(ESMatrix)); | |
170 } | |
171 | |
172 void LoadIdentity() { | |
173 LoadZero(); | |
174 m[0][0] = 1.0f; | |
175 m[1][1] = 1.0f; | |
176 m[2][2] = 1.0f; | |
177 m[3][3] = 1.0f; | |
178 } | |
179 | |
180 void Multiply(ESMatrix* a, ESMatrix* b) { | |
181 ESMatrix result; | |
182 for (int i = 0; i < 4; ++i) { | |
183 result.m[i][0] = (a->m[i][0] * b->m[0][0]) + | |
184 (a->m[i][1] * b->m[1][0]) + | |
185 (a->m[i][2] * b->m[2][0]) + | |
186 (a->m[i][3] * b->m[3][0]); | |
187 | |
188 result.m[i][1] = (a->m[i][0] * b->m[0][1]) + | |
189 (a->m[i][1] * b->m[1][1]) + | |
190 (a->m[i][2] * b->m[2][1]) + | |
191 (a->m[i][3] * b->m[3][1]); | |
192 | |
193 result.m[i][2] = (a->m[i][0] * b->m[0][2]) + | |
194 (a->m[i][1] * b->m[1][2]) + | |
195 (a->m[i][2] * b->m[2][2]) + | |
196 (a->m[i][3] * b->m[3][2]); | |
197 | |
198 result.m[i][3] = (a->m[i][0] * b->m[0][3]) + | |
199 (a->m[i][1] * b->m[1][3]) + | |
200 (a->m[i][2] * b->m[2][3]) + | |
201 (a->m[i][3] * b->m[3][3]); | |
202 } | |
203 *this = result; | |
204 } | |
205 | |
206 void Frustum(float left, | |
207 float right, | |
208 float bottom, | |
209 float top, | |
210 float near_z, | |
211 float far_z) { | |
212 float delta_x = right - left; | |
213 float delta_y = top - bottom; | |
214 float delta_z = far_z - near_z; | |
215 | |
216 if ((near_z <= 0.0f) || | |
217 (far_z <= 0.0f) || | |
218 (delta_z <= 0.0f) || | |
219 (delta_y <= 0.0f) || | |
220 (delta_y <= 0.0f)) | |
221 return; | |
222 | |
223 ESMatrix frust; | |
224 frust.m[0][0] = 2.0f * near_z / delta_x; | |
225 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f; | |
226 | |
227 frust.m[1][1] = 2.0f * near_z / delta_y; | |
228 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f; | |
229 | |
230 frust.m[2][0] = (right + left) / delta_x; | |
231 frust.m[2][1] = (top + bottom) / delta_y; | |
232 frust.m[2][2] = -(near_z + far_z) / delta_z; | |
233 frust.m[2][3] = -1.0f; | |
234 | |
235 frust.m[3][2] = -2.0f * near_z * far_z / delta_z; | |
236 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f; | |
237 | |
238 Multiply(&frust, this); | |
239 } | |
240 | |
241 void Perspective(float fov_y, float aspect, float near_z, float far_z) { | |
242 GLfloat frustum_h = tanf(fov_y / 360.0f * M_PI) * near_z; | |
243 GLfloat frustum_w = frustum_h * aspect; | |
244 Frustum(-frustum_w, frustum_w, -frustum_h, frustum_h, near_z, far_z); | |
245 } | |
246 | |
247 void Translate(GLfloat tx, GLfloat ty, GLfloat tz) { | |
248 m[3][0] += m[0][0] * tx + m[1][0] * ty + m[2][0] * tz; | |
249 m[3][1] += m[0][1] * tx + m[1][1] * ty + m[2][1] * tz; | |
250 m[3][2] += m[0][2] * tx + m[1][2] * ty + m[2][2] * tz; | |
251 m[3][3] += m[0][3] * tx + m[1][3] * ty + m[2][3] * tz; | |
252 } | |
253 | |
254 void Rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { | |
255 GLfloat mag = sqrtf(x * x + y * y + z * z); | |
256 | |
257 GLfloat sin_angle = sinf(angle * M_PI / 180.0f); | |
258 GLfloat cos_angle = cosf(angle * M_PI / 180.0f); | |
259 if (mag > 0.0f) { | |
260 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs; | |
261 GLfloat one_minus_cos; | |
262 ESMatrix rotation; | |
263 | |
264 x /= mag; | |
265 y /= mag; | |
266 z /= mag; | |
267 | |
268 xx = x * x; | |
269 yy = y * y; | |
270 zz = z * z; | |
271 xy = x * y; | |
272 yz = y * z; | |
273 zx = z * x; | |
274 xs = x * sin_angle; | |
275 ys = y * sin_angle; | |
276 zs = z * sin_angle; | |
277 one_minus_cos = 1.0f - cos_angle; | |
278 | |
279 rotation.m[0][0] = (one_minus_cos * xx) + cos_angle; | |
280 rotation.m[0][1] = (one_minus_cos * xy) - zs; | |
281 rotation.m[0][2] = (one_minus_cos * zx) + ys; | |
282 rotation.m[0][3] = 0.0F; | |
283 | |
284 rotation.m[1][0] = (one_minus_cos * xy) + zs; | |
285 rotation.m[1][1] = (one_minus_cos * yy) + cos_angle; | |
286 rotation.m[1][2] = (one_minus_cos * yz) - xs; | |
287 rotation.m[1][3] = 0.0F; | |
288 | |
289 rotation.m[2][0] = (one_minus_cos * zx) - ys; | |
290 rotation.m[2][1] = (one_minus_cos * yz) + xs; | |
291 rotation.m[2][2] = (one_minus_cos * zz) + cos_angle; | |
292 rotation.m[2][3] = 0.0F; | |
293 | |
294 rotation.m[3][0] = 0.0F; | |
295 rotation.m[3][1] = 0.0F; | |
296 rotation.m[3][2] = 0.0F; | |
297 rotation.m[3][3] = 1.0F; | |
298 | |
299 Multiply(&rotation, this); | |
300 } | |
301 } | |
302 }; | |
303 | |
304 } | |
305 | |
306 class SpinningCube::GLState { | |
307 public: | |
308 GLState(); | |
309 | |
310 GLuint program_object_; | |
311 GLint position_location_; | |
312 GLint mvp_location_; | |
313 GLuint vbo_vertices_; | |
314 GLuint vbo_indices_; | |
315 int num_indices_; | |
316 GLfloat angle_; | |
317 ESMatrix mvp_matrix_; | |
318 }; | |
319 | |
320 SpinningCube::GLState::GLState() | |
321 : program_object_(0), | |
322 position_location_(0), | |
323 mvp_location_(0), | |
324 vbo_vertices_(0), | |
325 vbo_indices_(0), | |
326 num_indices_(0), | |
327 angle_(0) { | |
328 } | |
329 | |
330 SpinningCube::SpinningCube(gpu::gles2::GLES2Interface* gl, | |
331 int width, | |
332 int height) | |
333 : gl_(gl), | |
334 width_(width), | |
335 height_(height), | |
336 state_(new GLState()) { | |
337 Initialize(); | |
338 } | |
339 | |
340 SpinningCube::~SpinningCube() { | |
341 if (state_->vbo_vertices_) | |
342 gl_->DeleteBuffers(1, &state_->vbo_vertices_); | |
343 if (state_->vbo_indices_) | |
344 gl_->DeleteBuffers(1, &state_->vbo_indices_); | |
345 if (state_->program_object_) | |
346 gl_->DeleteProgram(state_->program_object_); | |
347 } | |
348 | |
349 void SpinningCube::Initialize() { | |
350 const char vertext_shader_source[] = | |
351 "uniform mat4 u_mvpMatrix; \n" | |
352 "attribute vec4 a_position; \n" | |
353 "void main() \n" | |
354 "{ \n" | |
355 " gl_Position = u_mvpMatrix * a_position; \n" | |
356 "} \n"; | |
357 | |
358 const char fragment_shader_source[] = | |
359 "precision mediump float; \n" | |
360 "void main() \n" | |
361 "{ \n" | |
362 " gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 ); \n" | |
363 "} \n"; | |
364 | |
365 state_->program_object_ = LoadProgram( | |
366 gl_, vertext_shader_source, fragment_shader_source); | |
367 state_->position_location_ = gl_->GetAttribLocation( | |
368 state_->program_object_, "a_position"); | |
369 state_->mvp_location_ = gl_->GetUniformLocation( | |
370 state_->program_object_, "u_mvpMatrix"); | |
371 state_->num_indices_ = GenerateCube( | |
372 gl_, &state_->vbo_vertices_, &state_->vbo_indices_); | |
373 | |
374 gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
375 state_->angle_ = 45.0f; | |
376 } | |
377 | |
378 void SpinningCube::Update(float delta_time) { | |
379 state_->angle_ += ( delta_time * 40.0f ); | |
380 if (state_->angle_ >= 360.0f ) | |
381 state_->angle_ -= 360.0f; | |
382 | |
383 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); | |
384 | |
385 ESMatrix perspective; | |
386 perspective.LoadIdentity(); | |
387 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); | |
388 | |
389 ESMatrix modelview; | |
390 modelview.LoadIdentity(); | |
391 modelview.Translate(0.0, 0.0, -2.0); | |
392 modelview.Rotate(state_->angle_, 1.0, 0.0, 1.0); | |
393 | |
394 state_->mvp_matrix_.Multiply(&modelview, &perspective); | |
395 } | |
396 | |
397 void SpinningCube::Draw() { | |
398 gl_->Viewport(0, 0, width_, height_); | |
399 gl_->Clear(GL_COLOR_BUFFER_BIT); | |
400 gl_->UseProgram(state_->program_object_); | |
401 gl_->BindBuffer(GL_ARRAY_BUFFER, state_->vbo_vertices_); | |
402 gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, state_->vbo_indices_); | |
403 gl_->VertexAttribPointer(state_->position_location_, | |
404 3, | |
405 GL_FLOAT, | |
406 GL_FALSE, 3 * sizeof(GLfloat), | |
407 0); | |
408 gl_->EnableVertexAttribArray(state_->position_location_); | |
409 gl_->UniformMatrix4fv(state_->mvp_location_, | |
410 1, | |
411 GL_FALSE, | |
412 (GLfloat*) &state_->mvp_matrix_.m[0][0]); | |
413 gl_->DrawElements(GL_TRIANGLES, | |
414 state_->num_indices_, | |
415 GL_UNSIGNED_SHORT, | |
416 0); | |
417 gl_->SwapBuffers(); | |
418 } | |
419 | |
420 } // namespace examples | |
421 } // namespace mojo | |
OLD | NEW |