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

Side by Side Diff: mojo/examples/sample_app/spinning_cube.cc

Issue 297113002: Change the color of the spinning cube on click. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « mojo/examples/sample_app/spinning_cube.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 public: 307 public:
308 GLState(); 308 GLState();
309 309
310 void OnGLContextLost(); 310 void OnGLContextLost();
311 311
312 GLfloat angle_; // Survives losing the GL context. 312 GLfloat angle_; // Survives losing the GL context.
313 313
314 GLuint program_object_; 314 GLuint program_object_;
315 GLint position_location_; 315 GLint position_location_;
316 GLint mvp_location_; 316 GLint mvp_location_;
317 GLint color_location_;
317 GLuint vbo_vertices_; 318 GLuint vbo_vertices_;
318 GLuint vbo_indices_; 319 GLuint vbo_indices_;
319 int num_indices_; 320 int num_indices_;
320 ESMatrix mvp_matrix_; 321 ESMatrix mvp_matrix_;
321 }; 322 };
322 323
323 SpinningCube::GLState::GLState() 324 SpinningCube::GLState::GLState()
324 : angle_(0) { 325 : angle_(0) {
325 OnGLContextLost(); 326 OnGLContextLost();
326 } 327 }
327 328
328 void SpinningCube::GLState::OnGLContextLost() { 329 void SpinningCube::GLState::OnGLContextLost() {
329 program_object_ = 0; 330 program_object_ = 0;
330 position_location_ = 0; 331 position_location_ = 0;
331 mvp_location_ = 0; 332 mvp_location_ = 0;
333 color_location_ = 0;
332 vbo_vertices_ = 0; 334 vbo_vertices_ = 0;
333 vbo_indices_ = 0; 335 vbo_indices_ = 0;
334 num_indices_ = 0; 336 num_indices_ = 0;
335 } 337 }
336 338
337 SpinningCube::SpinningCube() 339 SpinningCube::SpinningCube()
338 : initialized_(false), 340 : initialized_(false),
339 width_(0), 341 width_(0),
340 height_(0), 342 height_(0),
341 state_(new GLState()), 343 state_(new GLState()),
342 fling_multiplier_(1.0f), 344 fling_multiplier_(1.0f),
343 direction_(1) { 345 direction_(1),
346 color_() {
344 state_->angle_ = 45.0f; 347 state_->angle_ = 45.0f;
348 set_color(0.0, 1.0, 0.0);
345 } 349 }
346 350
347 SpinningCube::~SpinningCube() { 351 SpinningCube::~SpinningCube() {
348 if (!initialized_) 352 if (!initialized_)
349 return; 353 return;
350 if (state_->vbo_vertices_) 354 if (state_->vbo_vertices_)
351 glDeleteBuffers(1, &state_->vbo_vertices_); 355 glDeleteBuffers(1, &state_->vbo_vertices_);
352 if (state_->vbo_indices_) 356 if (state_->vbo_indices_)
353 glDeleteBuffers(1, &state_->vbo_indices_); 357 glDeleteBuffers(1, &state_->vbo_indices_);
354 if (state_->program_object_) 358 if (state_->program_object_)
355 glDeleteProgram(state_->program_object_); 359 glDeleteProgram(state_->program_object_);
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 const char vertext_shader_source[] =
363 "uniform mat4 u_mvpMatrix; \n" 367 "uniform mat4 u_mvpMatrix; \n"
364 "attribute vec4 a_position; \n" 368 "attribute vec4 a_position; \n"
365 "void main() \n" 369 "void main() \n"
366 "{ \n" 370 "{ \n"
367 " gl_Position = u_mvpMatrix * a_position; \n" 371 " gl_Position = u_mvpMatrix * a_position; \n"
368 "} \n"; 372 "} \n";
369 373
370 const char fragment_shader_source[] = 374 const char fragment_shader_source[] =
371 "precision mediump float; \n" 375 "precision mediump float; \n"
376 "uniform vec4 u_color; \n"
372 "void main() \n" 377 "void main() \n"
373 "{ \n" 378 "{ \n"
374 " gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 ); \n" 379 " gl_FragColor = u_color; \n"
375 "} \n"; 380 "} \n";
376 381
377 state_->program_object_ = LoadProgram( 382 state_->program_object_ = LoadProgram(
378 vertext_shader_source, fragment_shader_source); 383 vertext_shader_source, fragment_shader_source);
379 state_->position_location_ = glGetAttribLocation( 384 state_->position_location_ = glGetAttribLocation(
380 state_->program_object_, "a_position"); 385 state_->program_object_, "a_position");
381 state_->mvp_location_ = glGetUniformLocation( 386 state_->mvp_location_ = glGetUniformLocation(
382 state_->program_object_, "u_mvpMatrix"); 387 state_->program_object_, "u_mvpMatrix");
388 state_->color_location_ = glGetUniformLocation(
389 state_->program_object_, "u_color");
383 state_->num_indices_ = GenerateCube( 390 state_->num_indices_ = GenerateCube(
384 &state_->vbo_vertices_, &state_->vbo_indices_); 391 &state_->vbo_vertices_, &state_->vbo_indices_);
385 392
386 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 393 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
387 initialized_ = true; 394 initialized_ = true;
388 } 395 }
389 396
390 void SpinningCube::OnGLContextLost() { 397 void SpinningCube::OnGLContextLost() {
391 initialized_ = false; 398 initialized_ = false;
392 height_ = 0; 399 height_ = 0;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 glVertexAttribPointer(state_->position_location_, 439 glVertexAttribPointer(state_->position_location_,
433 3, 440 3,
434 GL_FLOAT, 441 GL_FLOAT,
435 GL_FALSE, 3 * sizeof(GLfloat), 442 GL_FALSE, 3 * sizeof(GLfloat),
436 0); 443 0);
437 glEnableVertexAttribArray(state_->position_location_); 444 glEnableVertexAttribArray(state_->position_location_);
438 glUniformMatrix4fv(state_->mvp_location_, 445 glUniformMatrix4fv(state_->mvp_location_,
439 1, 446 1,
440 GL_FALSE, 447 GL_FALSE,
441 (GLfloat*) &state_->mvp_matrix_.m[0][0]); 448 (GLfloat*) &state_->mvp_matrix_.m[0][0]);
449 glUniform4f(state_->color_location_, color_[0], color_[1], color_[2], 1.0);
442 glDrawElements(GL_TRIANGLES, 450 glDrawElements(GL_TRIANGLES,
443 state_->num_indices_, 451 state_->num_indices_,
444 GL_UNSIGNED_SHORT, 452 GL_UNSIGNED_SHORT,
445 0); 453 0);
446 } 454 }
447 455
448 void SpinningCube::Update() { 456 void SpinningCube::Update() {
449 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); 457 float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_);
450 458
451 ESMatrix perspective; 459 ESMatrix perspective;
452 perspective.LoadIdentity(); 460 perspective.LoadIdentity();
453 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); 461 perspective.Perspective(60.0f, aspect, 1.0f, 20.0f );
454 462
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
OLDNEW
« no previous file with comments | « mojo/examples/sample_app/spinning_cube.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698