Added shader effect to render quadratic bezier curves and bounded regions on the GPU 96/35696/4
authorFerran Sole <ferran.sole@samsung.com>
Mon, 23 Feb 2015 11:57:43 +0000 (11:57 +0000)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 24 Feb 2015 16:00:25 +0000 (08:00 -0800)
Change-Id: I246162665cbbdf438d601e337c830dea7cdf19c6

dali-toolkit/dali-toolkit.h
dali-toolkit/public-api/file.list
dali-toolkit/public-api/shader-effects/quadratic-bezier.cpp [new file with mode: 0644]
dali-toolkit/public-api/shader-effects/quadratic-bezier.h [new file with mode: 0644]

index 942e62d7f8637a2cd44a431bf65fe066189ac1c5..70f9652575ccb805a615f8c1f0660b5b55da696a 100644 (file)
 #include <dali-toolkit/public-api/shader-effects/nine-patch-mask-effect.h>
 #include <dali-toolkit/public-api/shader-effects/page-turn-book-spine-effect.h>
 #include <dali-toolkit/public-api/shader-effects/page-turn-effect.h>
+#include <dali-toolkit/public-api/shader-effects/quadratic-bezier.h>
 #include <dali-toolkit/public-api/shader-effects/ripple-effect.h>
 #include <dali-toolkit/public-api/shader-effects/ripple2d-effect.h>
 #include <dali-toolkit/public-api/shader-effects/swirl-effect.h>
index 1b9307af9c5cf276e8e942621b979df1d8e18b7c..85a370209f9bd63ad7b0e44a76023a0114109497 100755 (executable)
@@ -85,6 +85,7 @@ public_api_src_files = \
   $(public_api_src_dir)/shader-effects/overlay-effect.cpp \
   $(public_api_src_dir)/shader-effects/page-turn-book-spine-effect.cpp \
   $(public_api_src_dir)/shader-effects/page-turn-effect.cpp \
+  $(public_api_src_dir)/shader-effects/quadratic-bezier.cpp \
   $(public_api_src_dir)/shader-effects/ripple-effect.cpp \
   $(public_api_src_dir)/shader-effects/ripple2d-effect.cpp \
   $(public_api_src_dir)/shader-effects/shear-effect.cpp \
@@ -250,6 +251,7 @@ public_api_shader_effects_header_files = \
   $(public_api_src_dir)/shader-effects/overlay-effect.h \
   $(public_api_src_dir)/shader-effects/page-turn-book-spine-effect.h \
   $(public_api_src_dir)/shader-effects/page-turn-effect.h \
+  $(public_api_src_dir)/shader-effects/quadratic-bezier.h \
   $(public_api_src_dir)/shader-effects/ripple-effect.h \
   $(public_api_src_dir)/shader-effects/ripple2d-effect.h \
   $(public_api_src_dir)/shader-effects/shear-effect.h \
diff --git a/dali-toolkit/public-api/shader-effects/quadratic-bezier.cpp b/dali-toolkit/public-api/shader-effects/quadratic-bezier.cpp
new file mode 100644 (file)
index 0000000..7c8965c
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * 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.
+ *
+ */
+
+//CLASS HEADER
+#include <dali-toolkit/public-api/shader-effects/quadratic-bezier.h>
+
+//EXTERNAL HEADERS
+#include <sstream>
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace
+{
+const char* POINT_PROPERTY_NAME( "uPoint" );
+const char* LINEWIDTH_PROPERTY_NAME( "uLineWidth" );
+const char* COLOR_PROPERTY_NAME( "uColor" );
+} // namespace
+
+QuadraticBezier::QuadraticBezier()
+{
+}
+
+//Call the Parent copy constructor to add reference to the implementation for this object
+QuadraticBezier::QuadraticBezier(ShaderEffect handle)
+:ShaderEffect(handle)
+{
+}
+
+QuadraticBezier::~QuadraticBezier()
+{
+}
+
+QuadraticBezier QuadraticBezier::New(unsigned int pointCount, bool filled )
+{
+  std::string vertexShader = DALI_COMPOSE_SHADER
+  (
+      uniform mediump vec3 uPoint[MAX_POINT_COUNT];\n
+      varying highp vec2 vCoefficient;
+      void main()\n
+      {\n
+        int vertexId = int(aNormal.z);\n
+        gl_Position = uMvpMatrix * vec4(uPoint[vertexId], 1.0);\n
+        vCoefficient = aNormal.xy;\n
+      }\n
+  );
+
+  std::string fragmentShader;
+
+  if( filled )
+  {
+    fragmentShader = DALI_COMPOSE_SHADER
+    (
+        varying highp vec2 vCoefficient;\n
+
+        void main()\n
+        {\n
+          highp float C = (vCoefficient.x*vCoefficient.x-vCoefficient.y);\n
+          highp float Cdx = dFdx(C);\n
+          highp float Cdy = dFdy(C);\n
+
+          highp float distance = float(C / sqrt(Cdx*Cdx + Cdy*Cdy));\n
+
+          gl_FragColor = uColor;\n
+          highp float alpha = 0.5 - distance;\n
+          if( alpha < 0.0 )\n
+            discard;\n
+
+          gl_FragColor.w = alpha;\n
+        }\n
+    );
+  }
+  else
+  {
+    fragmentShader = DALI_COMPOSE_SHADER
+    (
+        varying highp vec2 vCoefficient;\n
+        uniform lowp float uLineWidth;\n
+
+        void main()\n
+        {\n
+          highp float C = (vCoefficient.x*vCoefficient.x-vCoefficient.y);\n
+          highp float Cdx = dFdx(C);\n
+          highp float Cdy = dFdy(C);\n
+          highp float distance = abs(float(C / sqrt(Cdx*Cdx + Cdy*Cdy)));\n
+          gl_FragColor = uColor*(uLineWidth-distance);\n
+        }\n
+    );
+  }
+
+  std::ostringstream vertexShaderPrefix;
+  vertexShaderPrefix << "#define MAX_POINT_COUNT "<< pointCount << "\n";
+
+  Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::NewWithPrefix( vertexShaderPrefix.str(),vertexShader,
+                                                                             "#extension GL_OES_standard_derivatives:enable\n", fragmentShader,
+                                                                             GEOMETRY_TYPE_UNTEXTURED_MESH );
+
+  Dali::Toolkit::QuadraticBezier handle( shaderEffectCustom );
+
+  //Set default uniform values
+  handle.SetUniform( COLOR_PROPERTY_NAME, Vector4(1.0f,1.0f,1.0f,1.0f) );
+  if( !filled )
+  {
+    //Set default line widht to 1 pixel
+    handle.SetUniform( LINEWIDTH_PROPERTY_NAME, 1.0f );
+  }
+
+  return handle;
+}
+
+void QuadraticBezier::SetPoint(unsigned int index, const Vector3& position)
+{
+  SetUniform( GetPointPropertyName(index), position );
+}
+
+void QuadraticBezier::SetLineWidth( float width )
+{
+  SetUniform( LINEWIDTH_PROPERTY_NAME, width );
+}
+
+void QuadraticBezier::SetColor( const Vector4& color )
+{
+  SetUniform( COLOR_PROPERTY_NAME, color );
+}
+
+std::string QuadraticBezier::GetPointPropertyName(unsigned int index) const
+{
+  std::ostringstream propertyName;
+  propertyName<<POINT_PROPERTY_NAME<<"["<<index<<"]";
+  return propertyName.str();
+}
+
+std::string QuadraticBezier::GetLineWidthPropertyName( ) const
+{
+  return LINEWIDTH_PROPERTY_NAME;
+}
+
+std::string QuadraticBezier::GetColorPropertyName( ) const
+{
+  return COLOR_PROPERTY_NAME;
+}
+
+} // namespace Toolkit
+
+} // namespace Dali
+
diff --git a/dali-toolkit/public-api/shader-effects/quadratic-bezier.h b/dali-toolkit/public-api/shader-effects/quadratic-bezier.h
new file mode 100644 (file)
index 0000000..7d5e199
--- /dev/null
@@ -0,0 +1,122 @@
+#ifndef __DALI_TOOLKIT_QUADRATIC_BEZIER_H__
+#define __DALI_TOOLKIT_QUADRATIC_BEZIER_H__
+
+/*
+ * 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.
+ *
+ */
+
+// EXTERNAL INCLUDES
+#include <dali/public-api/shader-effects/shader-effect.h>
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+/**
+ * @brief QuadraticBezier is a custom shader to render quadratic bezier curves and bounded regions.
+ *
+ * Implementation based on the paper "Resolution Independent Curve Rendering using Programmable Graphics Hardware"
+ * by Charles Loop and Jim Blinn.
+ *
+ * The coordinates of the control points is passed to the shader via uniforms.
+ * aNormal attribute is used to pass the coefficients of each control point (xy coordinates) as well as the vertex ID (z coordinate).
+ * A quadratic curve should have as normal for the first control point (0.0,0.0), (0.5,0.0) for the second and (1.0,1.0) for the third.
+ * Triangles that do not contain curves should have coordinates (0.0,1.0) for each control point in order to be filled properly.
+ */
+
+class DALI_IMPORT_API QuadraticBezier : public ShaderEffect
+{
+public:
+
+  /**
+   * @brief Create an uninitialized QuadraticBezier; this can be initialized with QuadraticBezier::New().
+   *
+   * Calling member functions with an uninitialized Dali::Object is not allowed.
+   */
+  QuadraticBezier();
+
+  /**
+   * @brief Destructor
+   *
+   * This is non-virtual since derived Handle types must not contain data or virtual methods.
+   */
+  ~QuadraticBezier();
+
+  /**
+   * @brief Create an initialized QuadraticBezier.
+   *
+   * @param[in] pointCount The maximum number of vertices
+   * @param[in] filled Specify whether the the bounded region should be filled or not
+   * @return A handle to a newly allocated Dali resource.
+   */
+  static QuadraticBezier New(unsigned int pointCount, bool filled );
+
+  /**
+   * @brief Set position coordinates for a point in the curve
+   *
+   * @param[in] index The index of the vertex
+   * @param[in] position The new position
+   */
+  void SetPoint( unsigned int index, const Vector3& position );
+
+  /**
+   * @brief Set the width of the pathThis is only for not filled curves
+   *
+   * @param[in] width Width of the line in pixels
+   */
+  void SetLineWidth( float width );
+
+  /**
+   * @brief Sets the color of the curve
+   *
+   * @param[in] color The new color
+   */
+  void SetColor( const Vector4& color );
+
+  /**
+   * @brief Get the name of a the point property given its index
+   *
+   * @param[in] index Index of the vertex
+   * @return A std::string containing the property name
+   */
+  std::string GetPointPropertyName( unsigned int index ) const;
+
+  /**
+   * @brief Get the name of the line width property
+   *
+   * @return A std::string containing the property name
+   */
+  std::string GetLineWidthPropertyName( ) const;
+
+  /**
+   * @brief Get the name of the color property
+   *
+   * @return A std::string containing the property name
+   */
+  std::string GetColorPropertyName( ) const;
+
+private: // Not intended for application developers
+
+  DALI_INTERNAL QuadraticBezier(ShaderEffect handle);
+
+};
+
+} // namespace Toolkit
+
+} // namespace Dali
+
+#endif // __DALI_TOOLKIT_IMAGE_REGION_EFFECT_H__