Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: ppapi/examples/compositor/spinning_cube.cc

Issue 298023004: [PPAPI] Compositor API implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@compositor_api_def_new
Patch Set: Fix review issue Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 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 "ppapi/examples/gles2_spinning_cube/spinning_cube.h" 17 #include "ppapi/examples/compositor/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 22
23 #include <algorithm> 23 #include <algorithm>
24 24
25 #include "ppapi/lib/gl/include/GLES2/gl2.h" 25 #include "ppapi/lib/gl/include/GLES2/gl2.h"
26 26
27 namespace { 27 namespace {
28 28
29 const float kPi = 3.14159265359f; 29 const float kPi = 3.14159265359f;
30 30
31 int GenerateCube(GLuint *vbo_vertices, 31 int GenerateCube(GLuint *vbo_vertices,
32 GLuint *vbo_indices) { 32 GLuint *vbo_indices) {
33 const int num_indices = 36; 33 const int num_indices = 36;
34 34
35 const GLfloat cube_vertices[] = { 35 const GLfloat cube_vertices[] = {
36 -0.5f, -0.5f, -0.5f, 36 -0.5f, -0.5f, -0.5f,
37 0.5f, -0.5f, -0.5f,
38 0.5f, -0.5f, 0.5f,
37 -0.5f, -0.5f, 0.5f, 39 -0.5f, -0.5f, 0.5f,
38 0.5f, -0.5f, 0.5f,
39 0.5f, -0.5f, -0.5f,
40 -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,
41 -0.5f, 0.5f, 0.5f, 43 -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 }; 44 };
61 45
62 const GLushort cube_indices[] = { 46 const GLushort cube_indices[] = {
63 0, 2, 1, 47 0, 2, 1,
64 0, 3, 2, 48 0, 3, 2,
65 4, 5, 6, 49 4, 5, 6,
66 4, 6, 7, 50 4, 6, 7,
67 8, 9, 10, 51 3, 6, 2,
68 8, 10, 11, 52 3, 7, 6,
69 12, 15, 14, 53 0, 1, 5,
70 12, 14, 13, 54 0, 5, 4,
71 16, 17, 18, 55 0, 7, 3,
72 16, 18, 19, 56 0, 4, 7,
73 20, 23, 22, 57 1, 2, 6,
74 20, 22, 21 58 1, 6, 5,
75 }; 59 };
76 60
77 if (vbo_vertices) { 61 if (vbo_vertices) {
78 glGenBuffers(1, vbo_vertices); 62 glGenBuffers(1, vbo_vertices);
79 glBindBuffer(GL_ARRAY_BUFFER, *vbo_vertices); 63 glBindBuffer(GL_ARRAY_BUFFER, *vbo_vertices);
80 glBufferData(GL_ARRAY_BUFFER, 64 glBufferData(GL_ARRAY_BUFFER,
81 sizeof(cube_vertices), 65 sizeof(cube_vertices),
82 cube_vertices, 66 cube_vertices,
83 GL_STATIC_DRAW); 67 GL_STATIC_DRAW);
84 glBindBuffer(GL_ARRAY_BUFFER, 0); 68 glBindBuffer(GL_ARRAY_BUFFER, 0);
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 342
359 void SpinningCube::Init(uint32_t width, uint32_t height) { 343 void SpinningCube::Init(uint32_t width, uint32_t height) {
360 width_ = width; 344 width_ = width;
361 height_ = height; 345 height_ = height;
362 346
363 if (!initialized_) { 347 if (!initialized_) {
364 initialized_ = true; 348 initialized_ = true;
365 const char vertext_shader_source[] = 349 const char vertext_shader_source[] =
366 "uniform mat4 u_mvpMatrix; \n" 350 "uniform mat4 u_mvpMatrix; \n"
367 "attribute vec4 a_position; \n" 351 "attribute vec4 a_position; \n"
352 "varying vec4 v_color; \n"
368 "void main() \n" 353 "void main() \n"
369 "{ \n" 354 "{ \n"
370 " gl_Position = u_mvpMatrix * a_position; \n" 355 " gl_Position = u_mvpMatrix * a_position; \n"
356 " v_color = vec4(a_position.x + 0.5, \n"
357 " a_position.y + 0.5, \n"
358 " a_position.z + 0.5, \n"
359 " 0.8); \n"
371 "} \n"; 360 "} \n";
372 361
373 const char fragment_shader_source[] = 362 const char fragment_shader_source[] =
374 "precision mediump float; \n" 363 "precision mediump float; \n"
375 "void main() \n" 364 "varying vec4 v_color; \n"
376 "{ \n" 365 "void main() \n"
377 " gl_FragColor = vec4( 0.0, 0.0, 1.0, 1.0 ); \n" 366 "{ \n"
378 "} \n"; 367 " gl_FragColor = v_color; \n"
368 "} \n";
379 369
380 state_->program_object_ = LoadProgram( 370 state_->program_object_ = LoadProgram(
381 vertext_shader_source, fragment_shader_source); 371 vertext_shader_source, fragment_shader_source);
382 state_->position_location_ = glGetAttribLocation( 372 state_->position_location_ = glGetAttribLocation(
383 state_->program_object_, "a_position"); 373 state_->program_object_, "a_position");
384 state_->mvp_location_ = glGetUniformLocation( 374 state_->mvp_location_ = glGetUniformLocation(
385 state_->program_object_, "u_mvpMatrix"); 375 state_->program_object_, "u_mvpMatrix");
386 state_->num_indices_ = GenerateCube( 376 state_->num_indices_ = GenerateCube(&state_->vbo_vertices_,
387 &state_->vbo_vertices_, &state_->vbo_indices_); 377 &state_->vbo_indices_);
388 378
389 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 379 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
390 } 380 }
391 } 381 }
392 382
393 void SpinningCube::OnGLContextLost() { 383 void SpinningCube::OnGLContextLost() {
394 // TODO(yzshen): Is it correct that in this case we don't need to do cleanup 384 // TODO(yzshen): Is it correct that in this case we don't need to do cleanup
395 // for program and buffers? 385 // for program and buffers?
396 initialized_ = false; 386 initialized_ = false;
397 height_ = 0; 387 height_ = 0;
(...skipping 25 matching lines...) Expand all
423 void SpinningCube::UpdateForDragDistance(float distance) { 413 void SpinningCube::UpdateForDragDistance(float distance) {
424 state_->angle_ += RotationForDragDistance(distance); 414 state_->angle_ += RotationForDragDistance(distance);
425 if (state_->angle_ >= 360.0f ) 415 if (state_->angle_ >= 360.0f )
426 state_->angle_ -= 360.0f; 416 state_->angle_ -= 360.0f;
427 417
428 Update(); 418 Update();
429 } 419 }
430 420
431 void SpinningCube::Draw() { 421 void SpinningCube::Draw() {
432 glViewport(0, 0, width_, height_); 422 glViewport(0, 0, width_, height_);
433 glClear(GL_COLOR_BUFFER_BIT); 423 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
424 glEnable(GL_DEPTH_TEST);
434 glUseProgram(state_->program_object_); 425 glUseProgram(state_->program_object_);
426
435 glBindBuffer(GL_ARRAY_BUFFER, state_->vbo_vertices_); 427 glBindBuffer(GL_ARRAY_BUFFER, state_->vbo_vertices_);
428 glVertexAttribPointer(state_->position_location_,
429 3,
430 GL_FLOAT,
431 GL_FALSE, 3 * sizeof(GLfloat),
432 0);
433 glEnableVertexAttribArray(state_->position_location_);
434
435 glUniformMatrix4fv(state_->mvp_location_,
436 1,
437 GL_FALSE,
438 (GLfloat*) &state_->mvp_matrix_.m[0][0]);
436 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state_->vbo_indices_); 439 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state_->vbo_indices_);
437 glVertexAttribPointer(state_->position_location_,
438 3,
439 GL_FLOAT,
440 GL_FALSE, 3 * sizeof(GLfloat),
441 0);
442 glEnableVertexAttribArray(state_->position_location_);
443 glUniformMatrix4fv(state_->mvp_location_,
444 1,
445 GL_FALSE,
446 (GLfloat*) &state_->mvp_matrix_.m[0][0]);
447 glDrawElements(GL_TRIANGLES, 440 glDrawElements(GL_TRIANGLES,
448 state_->num_indices_, 441 state_->num_indices_,
449 GL_UNSIGNED_SHORT, 442 GL_UNSIGNED_SHORT,
450 0); 443 0);
451 } 444 }
452 445
453 void SpinningCube::Update() { 446 void SpinningCube::Update() {
454 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); 447 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_);
455 448
456 ESMatrix perspective; 449 ESMatrix perspective;
457 perspective.LoadIdentity(); 450 perspective.LoadIdentity();
458 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); 451 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f );
459 452
460 ESMatrix modelview; 453 ESMatrix modelview;
461 modelview.LoadIdentity(); 454 modelview.LoadIdentity();
462 modelview.Translate(0.0, 0.0, -2.0); 455 modelview.Translate(0.0, 0.0, -2.0);
463 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); 456 modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0);
464 457
465 state_->mvp_matrix_.Multiply(&modelview, &perspective); 458 state_->mvp_matrix_.Multiply(&modelview, &perspective);
466 } 459 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698