(Reflection Example) Decrease class references in main class body 88/239088/1
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 21 Jul 2020 16:44:42 +0000 (17:44 +0100)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 21 Jul 2020 16:44:42 +0000 (17:44 +0100)
Change-Id: I6d9f96f762aa955764ff56d496a751c4bf1f2f61

examples/reflection-demo/reflection-example.cpp

index ecd635d..0900d80 100644 (file)
@@ -26,6 +26,9 @@
 using namespace Dali;
 using Dali::Toolkit::TextLabel;
 
+namespace
+{
+
 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
   attribute mediump vec3 aPosition;\n
   attribute mediump vec3 aNormal;\n
@@ -165,6 +168,115 @@ struct Model
   Geometry  geometry;
 };
 
+template<class T>
+bool LoadFile( const std::string& filename, std::vector<T>& bytes )
+{
+  Dali::FileStream fileStream( filename, Dali::FileStream::READ | Dali::FileStream::BINARY );
+  FILE* fin = fileStream.GetFile();
+
+  if( fin )
+  {
+    if( fseek( fin, 0, SEEK_END ) )
+    {
+      return false;
+    }
+    bytes.resize( uint32_t(ftell( fin )) );
+    std::fill( bytes.begin(), bytes.end(), 0 );
+    if( fseek( fin, 0, SEEK_SET ) )
+    {
+      return false;
+    }
+    size_t result = fread( bytes.data(), 1, bytes.size(), fin );
+    return ( result != 0 );
+  }
+
+  return false;
+}
+
+Shader CreateShader( const std::string& vsh, const std::string& fsh )
+{
+  std::vector<char> vshShaderSource;
+  std::vector<char> fshShaderSource;
+
+  // VSH
+  if(vsh[0] == '/')
+  {
+    std::string vshPath( DEMO_GAME_DIR );
+    vshPath += '/';
+    vshPath += vsh;
+    LoadFile( vshPath, vshShaderSource );
+  }
+  else
+  {
+    vshShaderSource.insert(vshShaderSource.end(), vsh.begin(), vsh.end());
+  }
+
+  // FSH
+  if(fsh[0] == '/')
+  {
+    std::string fshPath( DEMO_GAME_DIR );
+    fshPath += '/';
+    fshPath += fsh;
+    LoadFile( fshPath, fshShaderSource );
+  }
+  else
+  {
+    fshShaderSource.insert(fshShaderSource.end(), fsh.begin(), fsh.end());
+  }
+
+  vshShaderSource.emplace_back(0);
+  fshShaderSource.emplace_back(0);
+  return Shader::New( std::string(vshShaderSource.data()), std::string(fshShaderSource.data()) );
+}
+
+std::unique_ptr<Model> CreateModel( glTF& gltf,
+                                    const glTF_Mesh* mesh,
+                                    const std::string& vertexShaderSource,
+                                    const std::string& fragmentShaderSource )
+{
+  /*
+   * Obtain interleaved buffer for first mesh with position and normal attributes
+   */
+  auto positionBuffer = gltf.GetMeshAttributeBuffer( *mesh,
+                                                     {
+                                                       glTFAttributeType::POSITION,
+                                                       glTFAttributeType::NORMAL,
+                                                       glTFAttributeType::TEXCOORD_0
+                                                     } );
+
+  auto attributeCount = gltf.GetMeshAttributeCount( mesh );
+  /**
+   * Create matching property buffer
+   */
+  auto vertexBuffer = PropertyBuffer::New( Property::Map()
+                                             .Add("aPosition", Property::VECTOR3 )
+                                             .Add("aNormal", Property::VECTOR3)
+                                             .Add("aTexCoord", Property::VECTOR2)
+  );
+
+  // set vertex data
+  vertexBuffer.SetData( positionBuffer.data(), attributeCount );
+
+  auto geometry = Geometry::New();
+  geometry.AddVertexBuffer( vertexBuffer );
+  auto indexBuffer = gltf.GetMeshIndexBuffer( mesh );
+  geometry.SetIndexBuffer( indexBuffer.data(), indexBuffer.size() );
+  geometry.SetType( Geometry::Type::TRIANGLES );
+  std::unique_ptr<Model> retval( new Model() );
+  retval->shader = CreateShader( vertexShaderSource, fragmentShaderSource );
+  retval->geometry = geometry;
+  return retval;
+}
+
+void ReplaceShader( Actor& actor, const std::string& vsh, const std::string& fsh )
+{
+  auto renderer = actor.GetRendererAt(0);
+  auto shader = CreateShader(vsh, fsh);
+  renderer.SetShader( shader );
+}
+
+} // unnamed namespace
+
 // This example shows how to create and display mirrored reflection using CameraActor
 //
 class ReflectionExample : public ConnectionTracker
@@ -485,113 +597,6 @@ private:
     }
   }
 
-  template<class T>
-  bool LoadFile( const std::string& filename, std::vector<T>& bytes )
-  {
-    Dali::FileStream fileStream( filename, Dali::FileStream::READ | Dali::FileStream::BINARY );
-    FILE* fin = fileStream.GetFile();
-
-    if( fin )
-    {
-      if( fseek( fin, 0, SEEK_END ) )
-      {
-        return false;
-      }
-      bytes.resize( uint32_t(ftell( fin )) );
-      std::fill( bytes.begin(), bytes.end(), 0 );
-      if( fseek( fin, 0, SEEK_SET ) )
-      {
-        return false;
-      }
-      size_t result = fread( bytes.data(), 1, bytes.size(), fin );
-      return ( result != 0 );
-    }
-
-    return false;
-  }
-
-  Shader CreateShader( const std::string& vsh, const std::string& fsh )
-  {
-    std::vector<char> vshShaderSource;
-    std::vector<char> fshShaderSource;
-
-    // VSH
-    if(vsh[0] == '/')
-    {
-      std::string vshPath( DEMO_GAME_DIR );
-      vshPath += '/';
-      vshPath += vsh;
-      LoadFile( vshPath, vshShaderSource );
-    }
-    else
-    {
-      vshShaderSource.insert(vshShaderSource.end(), vsh.begin(), vsh.end());
-    }
-
-    // FSH
-    if(fsh[0] == '/')
-    {
-      std::string fshPath( DEMO_GAME_DIR );
-      fshPath += '/';
-      fshPath += fsh;
-      LoadFile( fshPath, fshShaderSource );
-    }
-    else
-    {
-      fshShaderSource.insert(fshShaderSource.end(), fsh.begin(), fsh.end());
-    }
-
-    vshShaderSource.emplace_back(0);
-    fshShaderSource.emplace_back(0);
-    return Shader::New( std::string(vshShaderSource.data()), std::string(fshShaderSource.data()) );
-  }
-
-  std::unique_ptr<Model> CreateModel( glTF& gltf,
-                                      const glTF_Mesh* mesh,
-                                      const std::string& vertexShaderSource,
-                                      const std::string& fragmentShaderSource )
-  {
-    /*
-     * Obtain interleaved buffer for first mesh with position and normal attributes
-     */
-    auto positionBuffer = gltf.GetMeshAttributeBuffer( *mesh,
-                                                       {
-                                                         glTFAttributeType::POSITION,
-                                                         glTFAttributeType::NORMAL,
-                                                         glTFAttributeType::TEXCOORD_0
-                                                       } );
-
-    auto attributeCount = gltf.GetMeshAttributeCount( mesh );
-    /**
-     * Create matching property buffer
-     */
-    auto vertexBuffer = PropertyBuffer::New( Property::Map()
-                                               .Add("aPosition", Property::VECTOR3 )
-                                               .Add("aNormal", Property::VECTOR3)
-                                               .Add("aTexCoord", Property::VECTOR2)
-    );
-
-    // set vertex data
-    vertexBuffer.SetData( positionBuffer.data(), attributeCount );
-
-    auto geometry = Geometry::New();
-    geometry.AddVertexBuffer( vertexBuffer );
-    auto indexBuffer = gltf.GetMeshIndexBuffer( mesh );
-    geometry.SetIndexBuffer( indexBuffer.data(), indexBuffer.size() );
-    geometry.SetType( Geometry::Type::TRIANGLES );
-    std::unique_ptr<Model> retval( new Model() );
-    retval->shader = CreateShader( vertexShaderSource, fragmentShaderSource );
-    retval->geometry = geometry;
-    return retval;
-  }
-
-  void ReplaceShader( Actor& actor, const std::string& vsh, const std::string& fsh )
-  {
-    auto renderer = actor.GetRendererAt(0);
-    auto shader = CreateShader(vsh, fsh);
-    renderer.SetShader( shader );
-  }
-
   void OnPan( Actor actor, const PanGesture& panGesture )
   {
     auto displacement = panGesture.screenDisplacement;