refraction-effect-example.cpp 20.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <dali/dali.h>
#include <dali-toolkit/dali-toolkit.h>
#include "shared/view.h"

#include <fstream>
#include <sstream>

using namespace Dali;

namespace
{
const char * const APPLICATION_TITLE( "Refraction Effect" );
const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
const char * const CHANGE_TEXTURE_ICON( DALI_IMAGE_DIR "icon-change.png" );
const char * const CHANGE_MESH_ICON( DALI_IMAGE_DIR "icon-replace.png" );

const char* MESH_FILES[] =
{
 DALI_MODEL_DIR "surface_pattern_v01.obj",
 DALI_MODEL_DIR "surface_pattern_v02.obj"
};
const unsigned int NUM_MESH_FILES( sizeof( MESH_FILES ) / sizeof( MESH_FILES[0] ) );

const char* TEXTURE_IMAGES[]=
{
  DALI_IMAGE_DIR "background-1.jpg",
  DALI_IMAGE_DIR "background-2.jpg",
  DALI_IMAGE_DIR "background-3.jpg",
  DALI_IMAGE_DIR "background-4.jpg"
};
const unsigned int NUM_TEXTURE_IMAGES( sizeof( TEXTURE_IMAGES ) / sizeof( TEXTURE_IMAGES[0] ) );

#define MAKE_SHADER(A)#A

struct LightOffsetConstraint
{
  LightOffsetConstraint( float radius )
  : mRadius( radius )
  {
  }

  Vector2 operator()( const Vector2& current, const PropertyInput& spinAngleProperty)
  {
    float spinAngle = spinAngleProperty.GetFloat();
    return Vector2( cos(spinAngle ), sin( spinAngle ) ) * mRadius;
  }

  float mRadius;
};

/**
 * @brief Load an image, scaled-down to no more than the stage dimensions.
 *
 * Uses image scaling mode ImageAttributes::ScaleToFill to resize the image at
 * load time to cover the entire stage with pixels with no borders,
 * and filter mode ImageAttributes::BoxThenLinear to sample the image with
 * maximum quality.
 */
ResourceImage LoadStageFillingImage( const char * const imagePath )
{
  Size stageSize = Stage::GetCurrent().GetSize();
  ImageAttributes attributes;
  attributes.SetSize( stageSize.x, stageSize.y );
  attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
  attributes.SetScalingMode( ImageAttributes::ScaleToFill );
  return ResourceImage::New( imagePath, attributes );
}

} // namespace

/************************************************************************************************
 *** This shader is used when the MeshActor is not touched***
 ************************************************************************************************/
class NoEffect : public ShaderEffect
{
public:
  /**
   * Create an empty handle.
   */
  NoEffect()
  {
  }

  /**
   * Virtual destructor
   */
  virtual ~NoEffect()
  {
  }

  /**
   * Create a NoEffect object.
   * @return A handle to a newly allocated NoEffect
   */
  static NoEffect New()
  {
    std::string vertexShader = MAKE_SHADER(
        precision mediump float;\n
        uniform mediump vec4 uTextureRect;\n
        void main()\n
        {\n
          gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n
          vTexCoord = aTexCoord.xy;\n
        }\n
    );
    std::string fragmentShader = MAKE_SHADER(
        precision mediump float;\n
        void main()\n
        {\n
          gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
        }\n
    );
    ShaderEffect shaderEffect = ShaderEffect::New( vertexShader, fragmentShader,
                                                   GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
                                                   ShaderEffect::GeometryHints( ShaderEffect::HINT_NONE ) );
    NoEffect handle( shaderEffect );
    return handle;
  }

private:
  /**
   * Helper for New()
   */
  NoEffect( ShaderEffect handle )
  : ShaderEffect( handle )
  {
  }
};

/************************************************************/
/* Custom refraction effect shader******************************/
/************************************************************/

class RefractionEffect : public ShaderEffect
{
public:

  /**
   * Create an empty RefractionEffect handle.
   */
  RefractionEffect()
  {
  }

  /**
   * Virtual destructor
   */
  virtual ~RefractionEffect()
  {
  }

  /**
   * Create a RefractionEffect object.
   * @return A handle to a newly allocated RefractionEffect
   */
  static RefractionEffect New()
  {
    std::string vertexShader = MAKE_SHADER(
      precision mediump float;\n
      varying mediump vec2 vTextureOffset;\n
      void main()\n
      {\n
        gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n
        vTexCoord = aTexCoord.xy;\n

        vNormal = aNormal;\n
        vVertex = vec4( aPosition, 1.0 );\n
        float length = max(0.01, length(aNormal.xy)) * 40.0;\n
        vTextureOffset = aNormal.xy / length;\n
      }\n
    );

    std::string fragmentShader = MAKE_SHADER(
      precision mediump float;\n
      uniform mediump float uEffectStrength;\n
      uniform mediump vec3 uLightPosition;\n
      uniform mediump vec2 uLightXYOffset;\n
      uniform mediump vec2 uLightSpinOffset;\n
      uniform mediump float uLightIntensity;\n
      varying mediump vec2 vTextureOffset;\n

      vec3 rgb2hsl(vec3 rgb)\n
      {\n
        float epsilon = 1.0e-10;\n
        vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n
        vec4 P = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));\n
        vec4 Q = mix(vec4(P.xyw, rgb.r), vec4(rgb.r, P.yzx), step(P.x, rgb.r));\n
        \n
        // RGB -> HCV
        float value = Q.x;\n
        float chroma = Q.x - min(Q.w, Q.y);\n
        float hue = abs(Q.z + (Q.w-Q.y) / (6.0*chroma+epsilon));\n
        // HCV -> HSL
        float lightness = value - chroma*0.5;\n
        return vec3( hue, chroma/max( 1.0-abs(lightness*2.0-1.0), 1.0e-1 ), lightness );\n
      }\n

      vec3 hsl2rgb( vec3 hsl )
      {
        // pure hue->RGB
        vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n
        vec3 p = abs(fract(hsl.xxx + K.xyz) * 6.0 - K.www);\n
        vec3 RGB = clamp(p - K.xxx, 0.0, 1.0);\n
        \n
        float chroma = ( 1.0 - abs( hsl.z*2.0-1.0 ) ) * hsl.y;\n
        return ( RGB - 0.5 ) * chroma + hsl.z;
      }

      void main()\n
      {\n
        vec3 normal = normalize( vNormal);\n

        vec3 lightPosition = uLightPosition + vec3(uLightXYOffset+uLightSpinOffset, 0.0);\n
        mediump vec3 vecToLight = normalize( (lightPosition - vVertex.xyz) * 0.01 );\n
        mediump float spotEffect = pow( max(0.05, vecToLight.z ) - 0.05, 8.0);\n

        spotEffect = spotEffect * uEffectStrength;\n
        mediump float lightDiffuse = ( ( dot( vecToLight, normal )-0.75 ) *uLightIntensity  ) * spotEffect;\n

        lowp vec4 color = texture2D( sTexture, vTexCoord + vTextureOffset * spotEffect );\n
        vec3 lightedColor =  hsl2rgb( rgb2hsl(color.rgb) + vec3(0.0,0.0,lightDiffuse) );\n

        gl_FragColor = vec4( lightedColor, color.a ) * uColor;\n
      }\n
    );

    ShaderEffect shaderEffect = ShaderEffect::New( vertexShader, fragmentShader,
                                                   GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
                                                   ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
    RefractionEffect handle( shaderEffect );

    Vector2 stageSize = Stage::GetCurrent().GetSize();
    handle.SetLightPosition( Vector2(stageSize.x, 0.f) );
    handle.SetUniform( "uLightXYOffset",  Vector2::ZERO );
    handle.SetUniform( "uLightSpinOffset",  Vector2::ZERO );
    handle.SetUniform( "uEffectStrength", 0.f );
    handle.SetUniform( "uLightIntensity",  2.5f );

    Dali::Property::Index index = handle.RegisterProperty( "uSpinAngle", 0.f );
    Constraint constraint = Constraint::New<Vector2>( handle.GetPropertyIndex("uLightSpinOffset"),
                                                      LocalSource(index),
                                                      LightOffsetConstraint(stageSize.x*0.1f));
    handle.ApplyConstraint( constraint );

    return handle;
  }

  void SetLightPosition( const Vector2& position )
  {
    Vector2 stageHalfSize = Stage::GetCurrent().GetSize() * 0.5f;
    SetUniform( "uLightPosition", Vector3( position.x - stageHalfSize.x, position.y - stageHalfSize.y, stageHalfSize.x ) );
  }

  void SetLightXYOffset( const Vector2& offset )
  {
    SetUniform( "uLightXYOffset",  offset );
  }

  void SetEffectStrength( float strength )
  {
    SetUniform( "uEffectStrength", strength );
  }

  void SetLightIntensity( float intensity )
  {
    SetUniform( "uLightIntensity", intensity );
  }

private:
  /**
   * Helper for New()
   */
  RefractionEffect( ShaderEffect handle )
  : ShaderEffect( handle )
  {
  }
};

/*************************************************/
/*Demo using RefractionEffect*****************/
/*************************************************/
class RefractionEffectExample : public ConnectionTracker
{
public:
  RefractionEffectExample( Application &application )
  : mApplication( application ),
    mIsDown( false ),
    mCurrentTextureId( 1 ),
    mCurrentMeshId( 0 )
  {
    // Connect to the Application's Init signal
    application.InitSignal().Connect(this, &RefractionEffectExample::Create);
  }

  ~RefractionEffectExample()
  {
  }

private:

  // The Init signal is received once (only) during the Application lifetime
  void Create(Application& application)
  {
    Stage stage = Stage::GetCurrent();
    mStageHalfSize = stage.GetSize() * 0.5f;

    stage.KeyEventSignal().Connect(this, &RefractionEffectExample::OnKeyEvent);

    // Creates a default view with a default tool bar.
    // The view is added to the stage.
    Toolkit::ToolBar toolBar;
    Toolkit::View    view;
    mContent = DemoHelper::CreateView( application,
        view,
        toolBar,
        "",
        TOOLBAR_IMAGE,
        APPLICATION_TITLE );

    // Add a button to change background. (right of toolbar)
    mChangeTextureButton = Toolkit::PushButton::New();
    mChangeTextureButton.SetBackgroundImage( ResourceImage::New( CHANGE_TEXTURE_ICON ) );
    mChangeTextureButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeTexture );
    toolBar.AddControl( mChangeTextureButton,
                        DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
                        Toolkit::Alignment::HorizontalRight,
                        DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
    // Add a button to change mesh pattern. ( left of bar )
    mChangeMeshButton = Toolkit::PushButton::New();
    mChangeMeshButton.SetBackgroundImage( ResourceImage::New( CHANGE_MESH_ICON ) );
    mChangeMeshButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeMesh );
    toolBar.AddControl( mChangeMeshButton,
                        DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
                        Toolkit::Alignment::HorizontalLeft,
                        DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );

    // creates the shader effects applied on the mesh actor
    mRefractionEffect = RefractionEffect::New(); // used when the finger is touching the screen
    mNoEffect = NoEffect::New(); // used in the other situations, basic render shader
    // Create the mesh from the obj file and add to stage
    mMaterial =  Material::New( "Material" ) ;
    mMaterial.SetDiffuseTexture( LoadStageFillingImage( TEXTURE_IMAGES[mCurrentTextureId] ) );
    CreateSurface( MESH_FILES[mCurrentMeshId] );

    // Connect the callback to the touch signal on the mesh actor
    mContent.TouchedSignal().Connect( this, &RefractionEffectExample::OnTouch );

    // the animation which spin the light around the finger touch position
    mLightPosition = Vector2( mStageHalfSize.x*2.f, 0.f);
    mLightAnimation = Animation::New(2.f);
    mLightAnimation.AnimateTo( Property( mRefractionEffect, "uSpinAngle" ), Math::PI*2.f );
    mLightAnimation.SetLooping( true );
    mLightAnimation.Pause();
  }

  /**
   * Create a mesh actor with different geometry to replace the current one
   */
  bool OnChangeMesh( Toolkit::Button button  )
  {
    if( mMeshActor )
    {
      UnparentAndReset( mMeshActor );
    }

    mCurrentMeshId = ( mCurrentMeshId + 1 ) % NUM_MESH_FILES;
    CreateSurface( MESH_FILES[mCurrentMeshId] );

    return true;
  }

  bool OnChangeTexture( Toolkit::Button button )
  {
    mCurrentTextureId = ( mCurrentTextureId + 1 ) % NUM_TEXTURE_IMAGES;
    mMaterial.SetDiffuseTexture( LoadStageFillingImage( TEXTURE_IMAGES[mCurrentTextureId] ) );

    return true;
  }

  bool OnTouch( Actor actor , const TouchEvent& event )
  {
    const TouchPoint &point = event.GetPoint(0);

    switch(point.state)
    {
      case TouchPoint::Down:
      {
        mIsDown = true;
        mDownPosition = point.screen;

        mLightAnimation.Play();

        if( mStrenghAnimation )
        {
          mStrenghAnimation.Clear();
        }

        mRefractionEffect.SetLightXYOffset( point.screen - mLightPosition );
        mMeshActor.SetShaderEffect( mRefractionEffect );
        mStrenghAnimation= Animation::New(0.5f);
        mStrenghAnimation.AnimateTo( Property( mRefractionEffect, "uEffectStrength" ), 1.f );
        mStrenghAnimation.Play();

        break;
      }
      case TouchPoint::Motion:
      {
        if(mIsDown)
        {
          // make the light position following the finger movement
          mRefractionEffect.SetLightXYOffset( point.screen - mLightPosition );
        }
        break;
      }
      case TouchPoint::Up:
      case TouchPoint::Leave:
      case TouchPoint::Interrupted:
      {
        if(mIsDown)
        {
          mLightAnimation.Pause();

          if( mStrenghAnimation )
          {
            mStrenghAnimation.Clear();
          }
          mStrenghAnimation = Animation::New(0.5f);
          mStrenghAnimation.AnimateTo( Property( mRefractionEffect, "uEffectStrength" ), 0.f );
          mStrenghAnimation.FinishedSignal().Connect( this, &RefractionEffectExample::OnTouchFinished );
          mStrenghAnimation.Play();
        }

        mIsDown = false;
        break;
      }
      case TouchPoint::Stationary:
      case TouchPoint::Last:
      default:
      {
        break;
      }
    }
    return true;
  }

  void OnTouchFinished( Animation& source )
  {
    mMeshActor.SetShaderEffect( mNoEffect );
    mRefractionEffect.SetLightXYOffset( Vector2::ZERO );
  }

  void CreateSurface( const std::string& objFileName )
  {
    MeshData::VertexContainer    vertices;
    MeshData::FaceIndices        faces;
    MeshData                     meshData;

    std::vector<float> boundingBox;
    std::vector<Vector3> vertexPositions;
    std::vector<int> faceIndices;
    // read the vertice and faces from the .obj file, and record the bounding box
    ReadObjFile( objFileName, boundingBox, vertexPositions, faceIndices );

    std::vector<Vector2> textureCoordinates;
    // align the mesh, scale it to fit the screen size, and calculate the texture coordinate for each vertex
    ShapeResizeAndTexureCoordinateCalculation( boundingBox, vertexPositions, textureCoordinates );

    // re-organize the mesh, the vertices are duplicated, each vertex only belongs to one triangle.
    // Without sharing vertex between triangle, so we can manipulate the texture offset on each triangle conveniently.
    for( std::size_t i=0; i<faceIndices.size(); i=i+3 )
    {
      Vector3 edge1 = vertexPositions[ faceIndices[i+2] ] - vertexPositions[ faceIndices[i] ];
      Vector3 edge2 = vertexPositions[ faceIndices[i+1] ] - vertexPositions[ faceIndices[i] ];
      Vector3 normal = edge1.Cross(edge2);
      normal.Normalize();

      if( normal.z > 0 )
      {
        faces.push_back( i );
        faces.push_back( i+1 );
        faces.push_back( i+2 );
      }
      else
      {
        normal *= -1.f;
        faces.push_back( i );
        faces.push_back( i+2 );
        faces.push_back( i+1 );
      }

      vertices.push_back( MeshData::Vertex( vertexPositions[ faceIndices[i] ], textureCoordinates[ faceIndices[i] ], normal ) );
      vertices.push_back( MeshData::Vertex( vertexPositions[ faceIndices[i+1] ], textureCoordinates[ faceIndices[i+1] ], normal ) );
      vertices.push_back( MeshData::Vertex( vertexPositions[ faceIndices[i+2] ], textureCoordinates[ faceIndices[i+2] ], normal ) );

    }

    // Now ready to construct the mesh actor
    meshData.SetMaterial( mMaterial );
    meshData.SetVertices( vertices );
    meshData.SetFaceIndices( faces );
    meshData.SetHasTextureCoords(true);
    meshData.SetHasNormals(true);
    mMeshActor = MeshActor::New( Mesh::New( meshData ) );
    mMeshActor.SetParentOrigin(ParentOrigin::CENTER);
    mMeshActor.SetShaderEffect( mNoEffect );
    mContent.Add( mMeshActor );
  }

  void ReadObjFile( const std::string& objFileName,
      std::vector<float>& boundingBox,
      std::vector<Vector3>& vertexPositions,
      std::vector<int>& faceIndices)
  {
    std::ifstream ifs( objFileName.c_str(), std::ios::in );

    boundingBox.resize( 6 );
    boundingBox[0]=boundingBox[2]=boundingBox[4] = std::numeric_limits<float>::max();
    boundingBox[1]=boundingBox[3]=boundingBox[5] = -std::numeric_limits<float>::max();

    std::string line;
    while( std::getline( ifs, line ) )
    {
      if( line[0] == 'v' && std::isspace(line[1]))  // vertex
      {
        std::istringstream iss(line.substr(2), std::istringstream::in);
        unsigned int i = 0;
        Vector3 vertex;
        while( iss >> vertex[i++] && i < 3);
        if( vertex.x < boundingBox[0] )  boundingBox[0] = vertex.x;
        if( vertex.x > boundingBox[1] )  boundingBox[1] = vertex.x;
        if( vertex.y < boundingBox[2] )  boundingBox[2] = vertex.y;
        if( vertex.y > boundingBox[3] )  boundingBox[3] = vertex.y;
        if( vertex.z < boundingBox[4] )  boundingBox[4] = vertex.z;
        if( vertex.z > boundingBox[5] )  boundingBox[5] = vertex.z;
        vertexPositions.push_back( vertex );
      }
      else if( line[0] == 'f' ) //face
      {
        unsigned int numOfInt = 3;
        while( true )
        {
          std::size_t found  = line.find('/');
          if( found == std::string::npos )
          {
            break;
          }
          line[found] = ' ';
          numOfInt++;
        }

        std::istringstream iss(line.substr(2), std::istringstream::in);
        int indices[ numOfInt ];
        unsigned int i=0;
        while( iss >> indices[i++] && i < numOfInt);
        unsigned int step = (i+1) / 3;
        faceIndices.push_back( indices[0]-1 );
        faceIndices.push_back( indices[step]-1 );
        faceIndices.push_back( indices[2*step]-1 );
      }
    }

    ifs.close();
  }

  void ShapeResizeAndTexureCoordinateCalculation( const std::vector<float>& boundingBox,
      std::vector<Vector3>& vertexPositions,
      std::vector<Vector2>& textureCoordinates)
  {
    Vector3 bBoxSize( boundingBox[1] - boundingBox[0], boundingBox[3] - boundingBox[2], boundingBox[5] - boundingBox[4]);
    Vector3 bBoxMinCorner( boundingBox[0], boundingBox[2], boundingBox[4] );

    Vector2 stageSize = Stage::GetCurrent().GetSize();
    Vector3 scale( stageSize.x / bBoxSize.x, stageSize.y / bBoxSize.y, 1.f );
    scale.z = (scale.x + scale.y)/2.f;

    for( std::vector<Vector3>::iterator iter = vertexPositions.begin(); iter != vertexPositions.end(); iter++ )
    {
      Vector3 newPosition(  (*iter) - bBoxMinCorner ) ;

      Vector2 textureCoord( newPosition.x / bBoxSize.x, newPosition.y / bBoxSize.y );
      textureCoordinates.push_back( textureCoord );

      newPosition -= bBoxSize * 0.5f;
      (*iter) = newPosition * scale;
    }
  }

  /**
   * Main key event handler
   */
  void OnKeyEvent(const KeyEvent& event)
  {
    if(event.state == KeyEvent::Down)
    {
      if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
      {
        mApplication.Quit();
      }
    }
  }

private:

  Application&   mApplication;
  Layer          mContent;

  bool           mIsDown;
  Vector2        mDownPosition;
  Vector2        mLightPosition;
  Vector2        mStageHalfSize;

  Material       mMaterial;
  MeshActor      mMeshActor;

  RefractionEffect  mRefractionEffect;
  NoEffect          mNoEffect;
  Animation         mLightAnimation;
  Animation         mStrenghAnimation;

  Toolkit::PushButton        mChangeTextureButton;
  Toolkit::PushButton        mChangeMeshButton;
  unsigned int               mCurrentTextureId;
  unsigned int               mCurrentMeshId;
};

/*****************************************************************************/

static void
RunTest(Application& app)
{
  RefractionEffectExample theApp(app);
  app.MainLoop();
}

/*****************************************************************************/

int
main(int argc, char **argv)
{
  Application app = Application::New(&argc, &argv);

  RunTest(app);

  return 0;
}