2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include "shared/view.h"
29 const char * const APPLICATION_TITLE( "Refraction Effect" );
30 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
31 const char * const CHANGE_TEXTURE_ICON( DALI_IMAGE_DIR "icon-change.png" );
32 const char * const CHANGE_MESH_ICON( DALI_IMAGE_DIR "icon-replace.png" );
34 const char* MESH_FILES[] =
36 DALI_MODEL_DIR "surface_pattern_v01.obj",
37 DALI_MODEL_DIR "surface_pattern_v02.obj"
39 const unsigned int NUM_MESH_FILES( sizeof( MESH_FILES ) / sizeof( MESH_FILES[0] ) );
41 const char* TEXTURE_IMAGES[]=
43 DALI_IMAGE_DIR "background-1.jpg",
44 DALI_IMAGE_DIR "background-2.jpg",
45 DALI_IMAGE_DIR "background-3.jpg",
46 DALI_IMAGE_DIR "background-4.jpg"
48 const unsigned int NUM_TEXTURE_IMAGES( sizeof( TEXTURE_IMAGES ) / sizeof( TEXTURE_IMAGES[0] ) );
50 #define MAKE_SHADER(A)#A
52 struct LightOffsetConstraint
54 LightOffsetConstraint( float radius )
59 void operator()( Vector2& current, const PropertyInputContainer& inputs )
61 float spinAngle = inputs[0]->GetFloat();
62 current.x = cos( spinAngle );
63 current.y = sin( spinAngle );
72 * @brief Load an image, scaled-down to no more than the stage dimensions.
74 * Uses image scaling mode ImageAttributes::ScaleToFill to resize the image at
75 * load time to cover the entire stage with pixels with no borders,
76 * and filter mode ImageAttributes::BoxThenLinear to sample the image with
79 ResourceImage LoadStageFillingImage( const char * const imagePath )
81 Size stageSize = Stage::GetCurrent().GetSize();
82 ImageAttributes attributes;
83 attributes.SetSize( stageSize.x, stageSize.y );
84 attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
85 attributes.SetScalingMode( ImageAttributes::ScaleToFill );
86 return ResourceImage::New( imagePath, attributes );
91 /************************************************************************************************
92 *** This shader is used when the MeshActor is not touched***
93 ************************************************************************************************/
94 class NoEffect : public ShaderEffect
98 * Create an empty handle.
112 * Create a NoEffect object.
113 * @return A handle to a newly allocated NoEffect
115 static NoEffect New()
117 std::string vertexShader = MAKE_SHADER(
118 precision mediump float;\n
119 uniform mediump vec4 uTextureRect;\n
122 gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n
123 vTexCoord = aTexCoord.xy;\n
126 std::string fragmentShader = MAKE_SHADER(
127 precision mediump float;\n
130 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
133 ShaderEffect shaderEffect = ShaderEffect::New( vertexShader, fragmentShader,
134 GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
135 ShaderEffect::GeometryHints( ShaderEffect::HINT_NONE ) );
136 NoEffect handle( shaderEffect );
144 NoEffect( ShaderEffect handle )
145 : ShaderEffect( handle )
150 /************************************************************/
151 /* Custom refraction effect shader******************************/
152 /************************************************************/
154 class RefractionEffect : public ShaderEffect
159 * Create an empty RefractionEffect handle.
168 virtual ~RefractionEffect()
173 * Create a RefractionEffect object.
174 * @return A handle to a newly allocated RefractionEffect
176 static RefractionEffect New()
178 std::string vertexShader = MAKE_SHADER(
179 precision mediump float;\n
180 varying mediump vec2 vTextureOffset;\n
183 gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n
184 vTexCoord = aTexCoord.xy;\n
187 vVertex = vec4( aPosition, 1.0 );\n
188 float length = max(0.01, length(aNormal.xy)) * 40.0;\n
189 vTextureOffset = aNormal.xy / length;\n
193 std::string fragmentShader = MAKE_SHADER(
194 precision mediump float;\n
195 uniform mediump float uEffectStrength;\n
196 uniform mediump vec3 uLightPosition;\n
197 uniform mediump vec2 uLightXYOffset;\n
198 uniform mediump vec2 uLightSpinOffset;\n
199 uniform mediump float uLightIntensity;\n
200 varying mediump vec2 vTextureOffset;\n
202 vec3 rgb2hsl(vec3 rgb)\n
204 float epsilon = 1.0e-10;\n
205 vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n
206 vec4 P = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));\n
207 vec4 Q = mix(vec4(P.xyw, rgb.r), vec4(rgb.r, P.yzx), step(P.x, rgb.r));\n
211 float chroma = Q.x - min(Q.w, Q.y);\n
212 float hue = abs(Q.z + (Q.w-Q.y) / (6.0*chroma+epsilon));\n
214 float lightness = value - chroma*0.5;\n
215 return vec3( hue, chroma/max( 1.0-abs(lightness*2.0-1.0), 1.0e-1 ), lightness );\n
218 vec3 hsl2rgb( vec3 hsl )
221 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n
222 vec3 p = abs(fract(hsl.xxx + K.xyz) * 6.0 - K.www);\n
223 vec3 RGB = clamp(p - K.xxx, 0.0, 1.0);\n
225 float chroma = ( 1.0 - abs( hsl.z*2.0-1.0 ) ) * hsl.y;\n
226 return ( RGB - 0.5 ) * chroma + hsl.z;
231 vec3 normal = normalize( vNormal);\n
233 vec3 lightPosition = uLightPosition + vec3(uLightXYOffset+uLightSpinOffset, 0.0);\n
234 mediump vec3 vecToLight = normalize( (lightPosition - vVertex.xyz) * 0.01 );\n
235 mediump float spotEffect = pow( max(0.05, vecToLight.z ) - 0.05, 8.0);\n
237 spotEffect = spotEffect * uEffectStrength;\n
238 mediump float lightDiffuse = ( ( dot( vecToLight, normal )-0.75 ) *uLightIntensity ) * spotEffect;\n
240 lowp vec4 color = texture2D( sTexture, vTexCoord + vTextureOffset * spotEffect );\n
241 vec3 lightedColor = hsl2rgb( rgb2hsl(color.rgb) + vec3(0.0,0.0,lightDiffuse) );\n
243 gl_FragColor = vec4( lightedColor, color.a ) * uColor;\n
247 ShaderEffect shaderEffect = ShaderEffect::New( vertexShader, fragmentShader,
248 GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
249 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
250 RefractionEffect handle( shaderEffect );
252 Vector2 stageSize = Stage::GetCurrent().GetSize();
253 handle.SetLightPosition( Vector2(stageSize.x, 0.f) );
254 handle.SetUniform( "uLightXYOffset", Vector2::ZERO );
255 handle.SetUniform( "uLightSpinOffset", Vector2::ZERO );
256 handle.SetUniform( "uEffectStrength", 0.f );
257 handle.SetUniform( "uLightIntensity", 2.5f );
259 Dali::Property::Index index = handle.RegisterProperty( "uSpinAngle", 0.f );
260 Constraint constraint = Constraint::New<Vector2>( handle, handle.GetPropertyIndex("uLightSpinOffset"), LightOffsetConstraint(stageSize.x*0.1f) );
261 constraint.AddSource( LocalSource(index) );
267 void SetLightPosition( const Vector2& position )
269 Vector2 stageHalfSize = Stage::GetCurrent().GetSize() * 0.5f;
270 SetUniform( "uLightPosition", Vector3( position.x - stageHalfSize.x, position.y - stageHalfSize.y, stageHalfSize.x ) );
273 void SetLightXYOffset( const Vector2& offset )
275 SetUniform( "uLightXYOffset", offset );
278 void SetEffectStrength( float strength )
280 SetUniform( "uEffectStrength", strength );
283 void SetLightIntensity( float intensity )
285 SetUniform( "uLightIntensity", intensity );
292 RefractionEffect( ShaderEffect handle )
293 : ShaderEffect( handle )
298 /*************************************************/
299 /*Demo using RefractionEffect*****************/
300 /*************************************************/
301 class RefractionEffectExample : public ConnectionTracker
304 RefractionEffectExample( Application &application )
305 : mApplication( application ),
307 mCurrentTextureId( 1 ),
310 // Connect to the Application's Init signal
311 application.InitSignal().Connect(this, &RefractionEffectExample::Create);
314 ~RefractionEffectExample()
320 // The Init signal is received once (only) during the Application lifetime
321 void Create(Application& application)
323 DemoHelper::RequestThemeChange();
325 Stage stage = Stage::GetCurrent();
326 mStageHalfSize = stage.GetSize() * 0.5f;
328 stage.KeyEventSignal().Connect(this, &RefractionEffectExample::OnKeyEvent);
330 // Creates a default view with a default tool bar.
331 // The view is added to the stage.
332 Toolkit::ToolBar toolBar;
334 mContent = DemoHelper::CreateView( application,
341 // Add a button to change background. (right of toolbar)
342 mChangeTextureButton = Toolkit::PushButton::New();
343 mChangeTextureButton.SetBackgroundImage( ResourceImage::New( CHANGE_TEXTURE_ICON ) );
344 mChangeTextureButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeTexture );
345 toolBar.AddControl( mChangeTextureButton,
346 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
347 Toolkit::Alignment::HorizontalRight,
348 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
349 // Add a button to change mesh pattern. ( left of bar )
350 mChangeMeshButton = Toolkit::PushButton::New();
351 mChangeMeshButton.SetBackgroundImage( ResourceImage::New( CHANGE_MESH_ICON ) );
352 mChangeMeshButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeMesh );
353 toolBar.AddControl( mChangeMeshButton,
354 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
355 Toolkit::Alignment::HorizontalLeft,
356 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
358 // creates the shader effects applied on the mesh actor
359 mRefractionEffect = RefractionEffect::New(); // used when the finger is touching the screen
360 mNoEffect = NoEffect::New(); // used in the other situations, basic render shader
361 // Create the mesh from the obj file and add to stage
362 mMaterial = Material::New( "Material" ) ;
363 mMaterial.SetDiffuseTexture( LoadStageFillingImage( TEXTURE_IMAGES[mCurrentTextureId] ) );
364 CreateSurface( MESH_FILES[mCurrentMeshId] );
366 // Connect the callback to the touch signal on the mesh actor
367 mContent.TouchedSignal().Connect( this, &RefractionEffectExample::OnTouch );
369 // the animation which spin the light around the finger touch position
370 mLightPosition = Vector2( mStageHalfSize.x*2.f, 0.f);
371 mLightAnimation = Animation::New(2.f);
372 mLightAnimation.AnimateTo( Property( mRefractionEffect, "uSpinAngle" ), Math::PI*2.f );
373 mLightAnimation.SetLooping( true );
374 mLightAnimation.Pause();
378 * Create a mesh actor with different geometry to replace the current one
380 bool OnChangeMesh( Toolkit::Button button )
384 UnparentAndReset( mMeshActor );
387 mCurrentMeshId = ( mCurrentMeshId + 1 ) % NUM_MESH_FILES;
388 CreateSurface( MESH_FILES[mCurrentMeshId] );
393 bool OnChangeTexture( Toolkit::Button button )
395 mCurrentTextureId = ( mCurrentTextureId + 1 ) % NUM_TEXTURE_IMAGES;
396 mMaterial.SetDiffuseTexture( LoadStageFillingImage( TEXTURE_IMAGES[mCurrentTextureId] ) );
401 bool OnTouch( Actor actor , const TouchEvent& event )
403 const TouchPoint &point = event.GetPoint(0);
407 case TouchPoint::Down:
410 mDownPosition = point.screen;
412 mLightAnimation.Play();
414 if( mStrenghAnimation )
416 mStrenghAnimation.Clear();
419 mRefractionEffect.SetLightXYOffset( point.screen - mLightPosition );
420 mMeshActor.SetShaderEffect( mRefractionEffect );
421 mStrenghAnimation= Animation::New(0.5f);
422 mStrenghAnimation.AnimateTo( Property( mRefractionEffect, "uEffectStrength" ), 1.f );
423 mStrenghAnimation.Play();
427 case TouchPoint::Motion:
431 // make the light position following the finger movement
432 mRefractionEffect.SetLightXYOffset( point.screen - mLightPosition );
437 case TouchPoint::Leave:
438 case TouchPoint::Interrupted:
442 mLightAnimation.Pause();
444 if( mStrenghAnimation )
446 mStrenghAnimation.Clear();
448 mStrenghAnimation = Animation::New(0.5f);
449 mStrenghAnimation.AnimateTo( Property( mRefractionEffect, "uEffectStrength" ), 0.f );
450 mStrenghAnimation.FinishedSignal().Connect( this, &RefractionEffectExample::OnTouchFinished );
451 mStrenghAnimation.Play();
457 case TouchPoint::Stationary:
458 case TouchPoint::Last:
467 void OnTouchFinished( Animation& source )
469 mMeshActor.SetShaderEffect( mNoEffect );
470 mRefractionEffect.SetLightXYOffset( Vector2::ZERO );
473 void CreateSurface( const std::string& objFileName )
475 MeshData::VertexContainer vertices;
476 MeshData::FaceIndices faces;
479 std::vector<float> boundingBox;
480 std::vector<Vector3> vertexPositions;
481 std::vector<int> faceIndices;
482 // read the vertice and faces from the .obj file, and record the bounding box
483 ReadObjFile( objFileName, boundingBox, vertexPositions, faceIndices );
485 std::vector<Vector2> textureCoordinates;
486 // align the mesh, scale it to fit the screen size, and calculate the texture coordinate for each vertex
487 ShapeResizeAndTexureCoordinateCalculation( boundingBox, vertexPositions, textureCoordinates );
489 // re-organize the mesh, the vertices are duplicated, each vertex only belongs to one triangle.
490 // Without sharing vertex between triangle, so we can manipulate the texture offset on each triangle conveniently.
491 for( std::size_t i=0; i<faceIndices.size(); i=i+3 )
493 Vector3 edge1 = vertexPositions[ faceIndices[i+2] ] - vertexPositions[ faceIndices[i] ];
494 Vector3 edge2 = vertexPositions[ faceIndices[i+1] ] - vertexPositions[ faceIndices[i] ];
495 Vector3 normal = edge1.Cross(edge2);
500 faces.push_back( i );
501 faces.push_back( i+1 );
502 faces.push_back( i+2 );
507 faces.push_back( i );
508 faces.push_back( i+2 );
509 faces.push_back( i+1 );
512 vertices.push_back( MeshData::Vertex( vertexPositions[ faceIndices[i] ], textureCoordinates[ faceIndices[i] ], normal ) );
513 vertices.push_back( MeshData::Vertex( vertexPositions[ faceIndices[i+1] ], textureCoordinates[ faceIndices[i+1] ], normal ) );
514 vertices.push_back( MeshData::Vertex( vertexPositions[ faceIndices[i+2] ], textureCoordinates[ faceIndices[i+2] ], normal ) );
518 // Now ready to construct the mesh actor
519 meshData.SetMaterial( mMaterial );
520 meshData.SetVertices( vertices );
521 meshData.SetFaceIndices( faces );
522 meshData.SetHasTextureCoords(true);
523 meshData.SetHasNormals(true);
524 mMeshActor = MeshActor::New( Mesh::New( meshData ) );
525 mMeshActor.SetParentOrigin(ParentOrigin::CENTER);
526 mMeshActor.SetShaderEffect( mNoEffect );
527 mContent.Add( mMeshActor );
530 void ReadObjFile( const std::string& objFileName,
531 std::vector<float>& boundingBox,
532 std::vector<Vector3>& vertexPositions,
533 std::vector<int>& faceIndices)
535 std::ifstream ifs( objFileName.c_str(), std::ios::in );
537 boundingBox.resize( 6 );
538 boundingBox[0]=boundingBox[2]=boundingBox[4] = std::numeric_limits<float>::max();
539 boundingBox[1]=boundingBox[3]=boundingBox[5] = -std::numeric_limits<float>::max();
542 while( std::getline( ifs, line ) )
544 if( line[0] == 'v' && std::isspace(line[1])) // vertex
546 std::istringstream iss(line.substr(2), std::istringstream::in);
549 while( iss >> vertex[i++] && i < 3);
550 if( vertex.x < boundingBox[0] ) boundingBox[0] = vertex.x;
551 if( vertex.x > boundingBox[1] ) boundingBox[1] = vertex.x;
552 if( vertex.y < boundingBox[2] ) boundingBox[2] = vertex.y;
553 if( vertex.y > boundingBox[3] ) boundingBox[3] = vertex.y;
554 if( vertex.z < boundingBox[4] ) boundingBox[4] = vertex.z;
555 if( vertex.z > boundingBox[5] ) boundingBox[5] = vertex.z;
556 vertexPositions.push_back( vertex );
558 else if( line[0] == 'f' ) //face
560 unsigned int numOfInt = 3;
563 std::size_t found = line.find('/');
564 if( found == std::string::npos )
572 std::istringstream iss(line.substr(2), std::istringstream::in);
573 int indices[ numOfInt ];
575 while( iss >> indices[i++] && i < numOfInt);
576 unsigned int step = (i+1) / 3;
577 faceIndices.push_back( indices[0]-1 );
578 faceIndices.push_back( indices[step]-1 );
579 faceIndices.push_back( indices[2*step]-1 );
586 void ShapeResizeAndTexureCoordinateCalculation( const std::vector<float>& boundingBox,
587 std::vector<Vector3>& vertexPositions,
588 std::vector<Vector2>& textureCoordinates)
590 Vector3 bBoxSize( boundingBox[1] - boundingBox[0], boundingBox[3] - boundingBox[2], boundingBox[5] - boundingBox[4]);
591 Vector3 bBoxMinCorner( boundingBox[0], boundingBox[2], boundingBox[4] );
593 Vector2 stageSize = Stage::GetCurrent().GetSize();
594 Vector3 scale( stageSize.x / bBoxSize.x, stageSize.y / bBoxSize.y, 1.f );
595 scale.z = (scale.x + scale.y)/2.f;
597 for( std::vector<Vector3>::iterator iter = vertexPositions.begin(); iter != vertexPositions.end(); iter++ )
599 Vector3 newPosition( (*iter) - bBoxMinCorner ) ;
601 Vector2 textureCoord( newPosition.x / bBoxSize.x, newPosition.y / bBoxSize.y );
602 textureCoordinates.push_back( textureCoord );
604 newPosition -= bBoxSize * 0.5f;
605 (*iter) = newPosition * scale;
610 * Main key event handler
612 void OnKeyEvent(const KeyEvent& event)
614 if(event.state == KeyEvent::Down)
616 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
625 Application& mApplication;
629 Vector2 mDownPosition;
630 Vector2 mLightPosition;
631 Vector2 mStageHalfSize;
634 MeshActor mMeshActor;
636 RefractionEffect mRefractionEffect;
638 Animation mLightAnimation;
639 Animation mStrenghAnimation;
641 Toolkit::PushButton mChangeTextureButton;
642 Toolkit::PushButton mChangeMeshButton;
643 unsigned int mCurrentTextureId;
644 unsigned int mCurrentMeshId;
647 /*****************************************************************************/
650 RunTest(Application& app)
652 RefractionEffectExample theApp(app);
656 /*****************************************************************************/
659 main(int argc, char **argv)
661 Application app = Application::New(&argc, &argv);