diff options
Diffstat (limited to 'mojo/examples/sample_app/spinning_cube.cc')
-rw-r--r-- | mojo/examples/sample_app/spinning_cube.cc | 61 |
1 files changed, 48 insertions, 13 deletions
diff --git a/mojo/examples/sample_app/spinning_cube.cc b/mojo/examples/sample_app/spinning_cube.cc index 3a09132..7983563 100644 --- a/mojo/examples/sample_app/spinning_cube.cc +++ b/mojo/examples/sample_app/spinning_cube.cc @@ -293,8 +293,16 @@ class ESMatrix { } }; +float RotationForTimeDelta(float delta_time) { + return delta_time * 40.0f; } +float RotationForDragDistance(float drag_distance) { + return drag_distance / 5; // Arbitrary damping. +} + +} // namespace + class SpinningCube::GLState { public: GLState(); @@ -330,7 +338,9 @@ SpinningCube::SpinningCube() : initialized_(false), width_(0), height_(0), - state_(new GLState()) { + state_(new GLState()), + fling_multiplier_(1.0f), + direction_(1) { state_->angle_ = 45.0f; } @@ -384,23 +394,33 @@ void SpinningCube::OnGLContextLost() { state_->OnGLContextLost(); } -void SpinningCube::Update(float delta_time) { - state_->angle_ += ( delta_time * 40.0f ); - if (state_->angle_ >= 360.0f ) +void SpinningCube::SetFlingMultiplier(float drag_distance, + float drag_time) { + fling_multiplier_ = RotationForDragDistance(drag_distance) / + RotationForTimeDelta(drag_time); + +} + +void SpinningCube::UpdateForTimeDelta(float delta_time) { + state_->angle_ += RotationForTimeDelta(delta_time) * fling_multiplier_; + if (state_->angle_ >= 360.0f) state_->angle_ -= 360.0f; - float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); + // Arbitrary 50-step linear reduction in spin speed. + if (fling_multiplier_ > 1.0f) { + fling_multiplier_ = + std::max(1.0f, fling_multiplier_ - (fling_multiplier_ - 1.0f) / 50); + } - ESMatrix perspective; - perspective.LoadIdentity(); - perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); + Update(); +} - ESMatrix modelview; - modelview.LoadIdentity(); - modelview.Translate(0.0, 0.0, -2.0); - modelview.Rotate(state_->angle_, 1.0, 0.0, 1.0); +void SpinningCube::UpdateForDragDistance(float distance) { + state_->angle_ += RotationForDragDistance(distance); + if (state_->angle_ >= 360.0f ) + state_->angle_ -= 360.0f; - state_->mvp_matrix_.Multiply(&modelview, &perspective); + Update(); } void SpinningCube::Draw() { @@ -425,5 +445,20 @@ void SpinningCube::Draw() { 0); } +void SpinningCube::Update() { + float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); + + ESMatrix perspective; + perspective.LoadIdentity(); + perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); + + ESMatrix modelview; + modelview.LoadIdentity(); + modelview.Translate(0.0, 0.0, -2.0); + modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); + + state_->mvp_matrix_.Multiply(&modelview, &perspective); +} + } // namespace examples } // namespace mojo |