| 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 "mojo/examples/pepper_container_app/pepper_spinning_cube_demo/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> | 22 |
| 23 #include <GLES2/gl2ext.h> | 23 #include <algorithm> |
| 24 |
| 25 #include "ppapi/lib/gl/include/GLES2/gl2.h" |
| 24 | 26 |
| 25 namespace mojo { | 27 namespace mojo { |
| 26 namespace examples { | 28 namespace examples { |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 const float kPi = 3.14159265359f; | 32 const float kPi = 3.14159265359f; |
| 31 | 33 |
| 32 int GenerateCube(GLuint *vbo_vertices, | 34 int GenerateCube(GLuint *vbo_vertices, |
| 33 GLuint *vbo_indices) { | 35 GLuint *vbo_indices) { |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 | 348 |
| 347 SpinningCube::~SpinningCube() { | 349 SpinningCube::~SpinningCube() { |
| 348 if (!initialized_) | 350 if (!initialized_) |
| 349 return; | 351 return; |
| 350 if (state_->vbo_vertices_) | 352 if (state_->vbo_vertices_) |
| 351 glDeleteBuffers(1, &state_->vbo_vertices_); | 353 glDeleteBuffers(1, &state_->vbo_vertices_); |
| 352 if (state_->vbo_indices_) | 354 if (state_->vbo_indices_) |
| 353 glDeleteBuffers(1, &state_->vbo_indices_); | 355 glDeleteBuffers(1, &state_->vbo_indices_); |
| 354 if (state_->program_object_) | 356 if (state_->program_object_) |
| 355 glDeleteProgram(state_->program_object_); | 357 glDeleteProgram(state_->program_object_); |
| 358 |
| 359 delete state_; |
| 356 } | 360 } |
| 357 | 361 |
| 358 void SpinningCube::Init(uint32_t width, uint32_t height) { | 362 void SpinningCube::Init(uint32_t width, uint32_t height) { |
| 359 width_ = width; | 363 width_ = width; |
| 360 height_ = height; | 364 height_ = height; |
| 361 | 365 |
| 362 const char vertext_shader_source[] = | 366 if (!initialized_) { |
| 363 "uniform mat4 u_mvpMatrix; \n" | 367 initialized_ = true; |
| 364 "attribute vec4 a_position; \n" | 368 const char vertext_shader_source[] = |
| 365 "void main() \n" | 369 "uniform mat4 u_mvpMatrix; \n" |
| 366 "{ \n" | 370 "attribute vec4 a_position; \n" |
| 367 " gl_Position = u_mvpMatrix * a_position; \n" | 371 "void main() \n" |
| 368 "} \n"; | 372 "{ \n" |
| 373 " gl_Position = u_mvpMatrix * a_position; \n" |
| 374 "} \n"; |
| 369 | 375 |
| 370 const char fragment_shader_source[] = | 376 const char fragment_shader_source[] = |
| 371 "precision mediump float; \n" | 377 "precision mediump float; \n" |
| 372 "void main() \n" | 378 "void main() \n" |
| 373 "{ \n" | 379 "{ \n" |
| 374 " gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 ); \n" | 380 " gl_FragColor = vec4( 0.0, 0.0, 1.0, 1.0 ); \n" |
| 375 "} \n"; | 381 "} \n"; |
| 376 | 382 |
| 377 state_->program_object_ = LoadProgram( | 383 state_->program_object_ = LoadProgram( |
| 378 vertext_shader_source, fragment_shader_source); | 384 vertext_shader_source, fragment_shader_source); |
| 379 state_->position_location_ = glGetAttribLocation( | 385 state_->position_location_ = glGetAttribLocation( |
| 380 state_->program_object_, "a_position"); | 386 state_->program_object_, "a_position"); |
| 381 state_->mvp_location_ = glGetUniformLocation( | 387 state_->mvp_location_ = glGetUniformLocation( |
| 382 state_->program_object_, "u_mvpMatrix"); | 388 state_->program_object_, "u_mvpMatrix"); |
| 383 state_->num_indices_ = GenerateCube( | 389 state_->num_indices_ = GenerateCube( |
| 384 &state_->vbo_vertices_, &state_->vbo_indices_); | 390 &state_->vbo_vertices_, &state_->vbo_indices_); |
| 385 | 391 |
| 386 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | 392 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 387 initialized_ = true; | 393 } |
| 388 } | 394 } |
| 389 | 395 |
| 390 void SpinningCube::OnGLContextLost() { | 396 void SpinningCube::OnGLContextLost() { |
| 397 // TODO(yzshen): Is it correct that in this case we don't need to do cleanup |
| 398 // for program and buffers? |
| 391 initialized_ = false; | 399 initialized_ = false; |
| 392 height_ = 0; | 400 height_ = 0; |
| 393 width_ = 0; | 401 width_ = 0; |
| 394 state_->OnGLContextLost(); | 402 state_->OnGLContextLost(); |
| 395 } | 403 } |
| 396 | 404 |
| 397 void SpinningCube::SetFlingMultiplier(float drag_distance, | 405 void SpinningCube::SetFlingMultiplier(float drag_distance, |
| 398 float drag_time) { | 406 float drag_time) { |
| 399 fling_multiplier_ = RotationForDragDistance(drag_distance) / | 407 fling_multiplier_ = RotationForDragDistance(drag_distance) / |
| 400 RotationForTimeDelta(drag_time); | 408 RotationForTimeDelta(drag_time); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 ESMatrix modelview; | 463 ESMatrix modelview; |
| 456 modelview.LoadIdentity(); | 464 modelview.LoadIdentity(); |
| 457 modelview.Translate(0.0, 0.0, -2.0); | 465 modelview.Translate(0.0, 0.0, -2.0); |
| 458 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); | 466 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); |
| 459 | 467 |
| 460 state_->mvp_matrix_.Multiply(&modelview, &perspective); | 468 state_->mvp_matrix_.Multiply(&modelview, &perspective); |
| 461 } | 469 } |
| 462 | 470 |
| 463 } // namespace examples | 471 } // namespace examples |
| 464 } // namespace mojo | 472 } // namespace mojo |
| OLD | NEW |