Commit b1d4995d authored by aa@chromium.org's avatar aa@chromium.org

Change the color of the spinning cube on click.

Review URL: https://codereview.chromium.org/297113002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272684 0039d316-1c4b-4281-b951-d872f2087c98
parent 2d059551
......@@ -7,6 +7,7 @@
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <math.h>
#include <stdlib.h>
#include "mojo/public/c/gles2/gles2.h"
#include "ui/events/event_constants.h"
......@@ -19,6 +20,10 @@ float CalculateDragDistance(const gfx::PointF& start, const Point& end) {
return hypot(start.x() - end.x(), start.y() - end.y());
}
float GetRandomColor() {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
}
GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe)
......@@ -74,6 +79,8 @@ void GLES2ClientImpl::HandleInputEvent(const Event& event) {
CalculateDragDistance(capture_point_, event.location()),
delta);
cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor());
capture_point_ = last_drag_point_ = gfx::PointF();
RequestAnimationFrames();
}
......
......@@ -314,6 +314,7 @@ class SpinningCube::GLState {
GLuint program_object_;
GLint position_location_;
GLint mvp_location_;
GLint color_location_;
GLuint vbo_vertices_;
GLuint vbo_indices_;
int num_indices_;
......@@ -329,6 +330,7 @@ void SpinningCube::GLState::OnGLContextLost() {
program_object_ = 0;
position_location_ = 0;
mvp_location_ = 0;
color_location_ = 0;
vbo_vertices_ = 0;
vbo_indices_ = 0;
num_indices_ = 0;
......@@ -340,8 +342,10 @@ SpinningCube::SpinningCube()
height_(0),
state_(new GLState()),
fling_multiplier_(1.0f),
direction_(1) {
direction_(1),
color_() {
state_->angle_ = 45.0f;
set_color(0.0, 1.0, 0.0);
}
SpinningCube::~SpinningCube() {
......@@ -369,9 +373,10 @@ void SpinningCube::Init(uint32_t width, uint32_t height) {
const char fragment_shader_source[] =
"precision mediump float; \n"
"uniform vec4 u_color; \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 ); \n"
" gl_FragColor = u_color; \n"
"} \n";
state_->program_object_ = LoadProgram(
......@@ -380,6 +385,8 @@ void SpinningCube::Init(uint32_t width, uint32_t height) {
state_->program_object_, "a_position");
state_->mvp_location_ = glGetUniformLocation(
state_->program_object_, "u_mvpMatrix");
state_->color_location_ = glGetUniformLocation(
state_->program_object_, "u_color");
state_->num_indices_ = GenerateCube(
&state_->vbo_vertices_, &state_->vbo_indices_);
......@@ -439,6 +446,7 @@ void SpinningCube::Draw() {
1,
GL_FALSE,
(GLfloat*) &state_->mvp_matrix_.m[0][0]);
glUniform4f(state_->color_location_, color_[0], color_[1], color_[2], 1.0);
glDrawElements(GL_TRIANGLES,
state_->num_indices_,
GL_UNSIGNED_SHORT,
......
......@@ -19,6 +19,11 @@ class SpinningCube {
void Init(uint32_t width, uint32_t height);
void set_direction(int direction) { direction_ = direction; }
void set_color(float r, float g, float b) {
color_[0] = r;
color_[1] = g;
color_[2] = b;
}
void SetFlingMultiplier(float drag_distance, float drag_time);
void UpdateForTimeDelta(float delta_time);
void UpdateForDragDistance(float distance);
......@@ -37,6 +42,7 @@ class SpinningCube {
scoped_ptr<GLState> state_;
float fling_multiplier_;
int direction_;
float color_[3];
};
} // namespace examples
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment