OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This example program is based on Simple_VertexShader.c from: | 5 // This example program is based on Simple_VertexShader.c from: |
6 | 6 |
7 // | 7 // |
8 // Book: OpenGL(R) ES 2.0 Programming Guide | 8 // Book: OpenGL(R) ES 2.0 Programming Guide |
9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | 9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner |
10 // ISBN-10: 0321502795 | 10 // ISBN-10: 0321502795 |
11 // ISBN-13: 9780321502797 | 11 // ISBN-13: 9780321502797 |
12 // Publisher: Addison-Wesley Professional | 12 // Publisher: Addison-Wesley Professional |
13 // URLs: http://safari.informit.com/9780321563835 | 13 // URLs: http://safari.informit.com/9780321563835 |
14 // http://www.opengles-book.com | 14 // http://www.opengles-book.com |
15 // | 15 // |
16 | 16 |
17 #include "mojo/examples/sample_app/spinning_cube.h" | 17 #include "ppapi/examples/gles2_spinning_cube/spinning_cube.h" |
18 | 18 |
19 #include <math.h> | 19 #include <math.h> |
20 #include <stdlib.h> | 20 #include <stdlib.h> |
21 #include <string.h> | 21 #include <string.h> |
22 #include <GLES2/gl2.h> | |
23 #include <GLES2/gl2ext.h> | |
24 | 22 |
25 namespace mojo { | 23 #include <algorithm> |
26 namespace examples { | 24 |
| 25 #include "ppapi/lib/gl/include/GLES2/gl2.h" |
27 | 26 |
28 namespace { | 27 namespace { |
29 | 28 |
30 const float kPi = 3.14159265359f; | 29 const float kPi = 3.14159265359f; |
31 | 30 |
32 int GenerateCube(GLuint *vbo_vertices, | 31 int GenerateCube(GLuint *vbo_vertices, |
33 GLuint *vbo_indices) { | 32 GLuint *vbo_indices) { |
34 const int num_indices = 36; | 33 const int num_indices = 36; |
35 | 34 |
36 const GLfloat cube_vertices[] = { | 35 const GLfloat cube_vertices[] = { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 | 345 |
347 SpinningCube::~SpinningCube() { | 346 SpinningCube::~SpinningCube() { |
348 if (!initialized_) | 347 if (!initialized_) |
349 return; | 348 return; |
350 if (state_->vbo_vertices_) | 349 if (state_->vbo_vertices_) |
351 glDeleteBuffers(1, &state_->vbo_vertices_); | 350 glDeleteBuffers(1, &state_->vbo_vertices_); |
352 if (state_->vbo_indices_) | 351 if (state_->vbo_indices_) |
353 glDeleteBuffers(1, &state_->vbo_indices_); | 352 glDeleteBuffers(1, &state_->vbo_indices_); |
354 if (state_->program_object_) | 353 if (state_->program_object_) |
355 glDeleteProgram(state_->program_object_); | 354 glDeleteProgram(state_->program_object_); |
| 355 |
| 356 delete state_; |
356 } | 357 } |
357 | 358 |
358 void SpinningCube::Init(uint32_t width, uint32_t height) { | 359 void SpinningCube::Init(uint32_t width, uint32_t height) { |
359 width_ = width; | 360 width_ = width; |
360 height_ = height; | 361 height_ = height; |
361 | 362 |
362 const char vertext_shader_source[] = | 363 if (!initialized_) { |
363 "uniform mat4 u_mvpMatrix; \n" | 364 initialized_ = true; |
364 "attribute vec4 a_position; \n" | 365 const char vertext_shader_source[] = |
365 "void main() \n" | 366 "uniform mat4 u_mvpMatrix; \n" |
366 "{ \n" | 367 "attribute vec4 a_position; \n" |
367 " gl_Position = u_mvpMatrix * a_position; \n" | 368 "void main() \n" |
368 "} \n"; | 369 "{ \n" |
| 370 " gl_Position = u_mvpMatrix * a_position; \n" |
| 371 "} \n"; |
369 | 372 |
370 const char fragment_shader_source[] = | 373 const char fragment_shader_source[] = |
371 "precision mediump float; \n" | 374 "precision mediump float; \n" |
372 "void main() \n" | 375 "void main() \n" |
373 "{ \n" | 376 "{ \n" |
374 " gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 ); \n" | 377 " gl_FragColor = vec4( 0.0, 0.0, 1.0, 1.0 ); \n" |
375 "} \n"; | 378 "} \n"; |
376 | 379 |
377 state_->program_object_ = LoadProgram( | 380 state_->program_object_ = LoadProgram( |
378 vertext_shader_source, fragment_shader_source); | 381 vertext_shader_source, fragment_shader_source); |
379 state_->position_location_ = glGetAttribLocation( | 382 state_->position_location_ = glGetAttribLocation( |
380 state_->program_object_, "a_position"); | 383 state_->program_object_, "a_position"); |
381 state_->mvp_location_ = glGetUniformLocation( | 384 state_->mvp_location_ = glGetUniformLocation( |
382 state_->program_object_, "u_mvpMatrix"); | 385 state_->program_object_, "u_mvpMatrix"); |
383 state_->num_indices_ = GenerateCube( | 386 state_->num_indices_ = GenerateCube( |
384 &state_->vbo_vertices_, &state_->vbo_indices_); | 387 &state_->vbo_vertices_, &state_->vbo_indices_); |
385 | 388 |
386 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | 389 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
387 initialized_ = true; | 390 } |
388 } | 391 } |
389 | 392 |
390 void SpinningCube::OnGLContextLost() { | 393 void SpinningCube::OnGLContextLost() { |
| 394 // TODO(yzshen): Is it correct that in this case we don't need to do cleanup |
| 395 // for program and buffers? |
391 initialized_ = false; | 396 initialized_ = false; |
392 height_ = 0; | 397 height_ = 0; |
393 width_ = 0; | 398 width_ = 0; |
394 state_->OnGLContextLost(); | 399 state_->OnGLContextLost(); |
395 } | 400 } |
396 | 401 |
397 void SpinningCube::SetFlingMultiplier(float drag_distance, | 402 void SpinningCube::SetFlingMultiplier(float drag_distance, |
398 float drag_time) { | 403 float drag_time) { |
399 fling_multiplier_ = RotationForDragDistance(drag_distance) / | 404 fling_multiplier_ = RotationForDragDistance(drag_distance) / |
400 RotationForTimeDelta(drag_time); | 405 RotationForTimeDelta(drag_time); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 perspective.LoadIdentity(); | 457 perspective.LoadIdentity(); |
453 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); | 458 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); |
454 | 459 |
455 ESMatrix modelview; | 460 ESMatrix modelview; |
456 modelview.LoadIdentity(); | 461 modelview.LoadIdentity(); |
457 modelview.Translate(0.0, 0.0, -2.0); | 462 modelview.Translate(0.0, 0.0, -2.0); |
458 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); | 463 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); |
459 | 464 |
460 state_->mvp_matrix_.Multiply(&modelview, &perspective); | 465 state_->mvp_matrix_.Multiply(&modelview, &perspective); |
461 } | 466 } |
462 | |
463 } // namespace examples | |
464 } // namespace mojo | |
OLD | NEW |