Implemented a primitive shape renderer to display simple shapes like cubes. 95/72595/28
authorAndrew Poor <andrew.poor@samsung.com>
Wed, 1 Jun 2016 15:49:24 +0000 (16:49 +0100)
committerAndrew Poor <andrew.poor@samsung.com>
Wed, 20 Jul 2016 13:39:49 +0000 (14:39 +0100)
Change-Id: I2c26b0c56e69d033fcb282903dc3819716a410b8

21 files changed:
automated-tests/src/dali-toolkit/utc-Dali-ControlRenderer.cpp
automated-tests/src/dali-toolkit/utc-Dali-RendererFactory.cpp
dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.cpp
dali-toolkit/internal/controls/renderers/primitive/primitive-renderer.cpp [new file with mode: 0644]
dali-toolkit/internal/controls/renderers/primitive/primitive-renderer.h [new file with mode: 0644]
dali-toolkit/internal/controls/renderers/renderer-factory-impl.cpp
dali-toolkit/internal/controls/renderers/renderer-factory-impl.h
dali-toolkit/internal/controls/renderers/renderer-string-constants.cpp
dali-toolkit/internal/controls/renderers/renderer-string-constants.h
dali-toolkit/internal/file.list
docs/content/images/renderers/bevelled-cube-high.png [new file with mode: 0644]
docs/content/images/renderers/bevelled-cube-low.png [new file with mode: 0644]
docs/content/images/renderers/cone.png [new file with mode: 0644]
docs/content/images/renderers/conical-frustrum.png [new file with mode: 0644]
docs/content/images/renderers/cube.png [new file with mode: 0644]
docs/content/images/renderers/cylinder.png [new file with mode: 0644]
docs/content/images/renderers/octahedron.png [new file with mode: 0644]
docs/content/images/renderers/slices.png [new file with mode: 0644]
docs/content/images/renderers/sphere.png [new file with mode: 0644]
docs/content/images/renderers/stacks.png [new file with mode: 0644]
docs/content/shared-javascript-and-cpp-documentation/control-renderers.md

index 80608a1..8b3989f 100644 (file)
@@ -664,23 +664,110 @@ int UtcDaliControlRendererGetPropertyMap8(void)
   //Check values in the result map are identical to the initial map's values.
   Property::Value* value = resultMap.Find( "rendererType", Property::STRING );
   DALI_TEST_CHECK( value );
   //Check values in the result map are identical to the initial map's values.
   Property::Value* value = resultMap.Find( "rendererType", Property::STRING );
   DALI_TEST_CHECK( value );
-  DALI_TEST_CHECK( value->Get<std::string>() == "MESH" );
+  DALI_TEST_EQUALS( value->Get<std::string>(), "MESH", TEST_LOCATION );
 
   value = resultMap.Find( "objectUrl", Property::STRING );
   DALI_TEST_CHECK( value );
 
   value = resultMap.Find( "objectUrl", Property::STRING );
   DALI_TEST_CHECK( value );
-  DALI_TEST_CHECK( value->Get<std::string>() == TEST_OBJ_FILE_NAME );
+  DALI_TEST_EQUALS( value->Get<std::string>(), TEST_OBJ_FILE_NAME, TEST_LOCATION );
 
   value = resultMap.Find( "materialUrl", Property::STRING );
   DALI_TEST_CHECK( value );
 
   value = resultMap.Find( "materialUrl", Property::STRING );
   DALI_TEST_CHECK( value );
-  DALI_TEST_CHECK( value->Get<std::string>() == TEST_MTL_FILE_NAME );
+  DALI_TEST_EQUALS( value->Get<std::string>(), TEST_MTL_FILE_NAME, TEST_LOCATION );
 
   value = resultMap.Find( "texturesPath", Property::STRING );
   DALI_TEST_CHECK( value );
 
   value = resultMap.Find( "texturesPath", Property::STRING );
   DALI_TEST_CHECK( value );
-  DALI_TEST_CHECK( value->Get<std::string>() == TEST_RESOURCE_LOCATION );
+  DALI_TEST_EQUALS( value->Get<std::string>(), TEST_RESOURCE_LOCATION, TEST_LOCATION );
 
   value = resultMap.Find( "shaderType", Property::STRING );
   DALI_TEST_CHECK( value );
 
   value = resultMap.Find( "shaderType", Property::STRING );
   DALI_TEST_CHECK( value );
-  DALI_TEST_CHECK( value->Get<std::string>() == "TEXTURELESS" );
+  DALI_TEST_EQUALS( value->Get<std::string>(), "TEXTURELESS", TEST_LOCATION );
+
+  END_TEST;
+}
+
+//Primitive shape renderer
+int UtcDaliControlRendererGetPropertyMap9(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline( "UtcDaliControlRendererGetPropertyMap9: PrimitiveRenderer" );
+
+  Vector4 color = Vector4( 1.0, 0.8, 0.6, 1.0);
+  Vector3 dimensions = Vector3( 1.0, 2.0, 3.0 );
+
+  //Request PrimitiveRenderer using a property map.
+  RendererFactory factory = RendererFactory::Get();
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "CUBE" );
+  propertyMap.Insert( "color", color );
+  propertyMap.Insert( "slices", 10 );
+  propertyMap.Insert( "stacks", 20 );
+  propertyMap.Insert( "scaleTopRadius", 30.0f );
+  propertyMap.Insert( "scaleBottomRadius", 40.0f );
+  propertyMap.Insert( "scaleHeight", 50.0f );
+  propertyMap.Insert( "scaleRadius", 60.0f );
+  propertyMap.Insert( "scaleDimensions", dimensions );
+  propertyMap.Insert( "bevelPercentage", 0.3f );
+  propertyMap.Insert( "bevelSmoothness", 0.6f );
+  propertyMap.Insert( "uLightPosition", Vector3( 5.0f, 10.0f, 15.0f) );
+  ControlRenderer primitiveRenderer = factory.CreateControlRenderer( propertyMap );
+
+  Property::Map resultMap;
+  primitiveRenderer.CreatePropertyMap( resultMap );
+
+  //Check values in the result map are identical to the initial map's values.
+  Property::Value* value = resultMap.Find( "rendererType", Property::STRING );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<std::string>(), "PRIMITIVE", TEST_LOCATION );
+
+  value = resultMap.Find( "shape", Property::STRING );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<std::string>(), "CUBE", TEST_LOCATION );
+
+  value = resultMap.Find( "color", Property::VECTOR4 );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_CHECK( value->Get<Vector4>() == color );
+  DALI_TEST_EQUALS( value->Get<Vector4>(), color, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "slices", Property::INTEGER );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<int>(), 10, TEST_LOCATION );
+
+  value = resultMap.Find( "stacks", Property::INTEGER );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<int>(), 20, TEST_LOCATION );
+
+  value = resultMap.Find( "scaleTopRadius", Property::FLOAT );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<float>(), 30.0f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "scaleBottomRadius", Property::FLOAT );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<float>(), 40.0f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "scaleHeight", Property::FLOAT );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<float>(), 50.0f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "scaleRadius", Property::FLOAT );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<float>(), 60.0f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "scaleDimensions", Property::VECTOR3 );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<Vector3>(), dimensions, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "bevelPercentage", Property::FLOAT );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<float>(), 0.3f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "bevelSmoothness", Property::FLOAT );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<float>(), 0.6f, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  value = resultMap.Find( "uLightPosition", Property::VECTOR3 );
+  DALI_TEST_CHECK( value );
+  DALI_TEST_EQUALS( value->Get<Vector3>(), Vector3( 5.0f, 10.0f, 15.0f), Math::MACHINE_EPSILON_100, TEST_LOCATION );
 
   END_TEST;
 }
 
   END_TEST;
 }
index f132ae0..19df59d 100644 (file)
@@ -1323,3 +1323,235 @@ int UtcDaliRendererFactoryGetMeshRendererN3(void)
 
   END_TEST;
 }
 
   END_TEST;
 }
+
+//Creates a primitive renderer with the given property map and tests to see if it correctly loads in the given application.
+void TestPrimitiveRendererWithProperties( Property::Map& propertyMap, ToolkitTestApplication& application )
+{
+  RendererFactory factory = RendererFactory::Get();
+  DALI_TEST_CHECK( factory );
+
+  //Create a primitive renderer.
+  ControlRenderer controlRenderer = factory.CreateControlRenderer( propertyMap );
+  DALI_TEST_CHECK( controlRenderer );
+
+  //Create an actor on stage to house the renderer.
+  Actor actor = Actor::New();
+  actor.SetSize( 200.f, 200.f );
+  Stage::GetCurrent().Add( actor );
+  controlRenderer.SetSize( Vector2( 200.f, 200.f ) );
+  controlRenderer.SetOnStage( actor );
+
+  //Ensure set on stage.
+  DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
+
+  //Tell test application to load the renderer.
+  application.SendNotification();
+  application.Render(0);
+
+  Matrix testScaleMatrix;
+  testScaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
+  Matrix actualScaleMatrix;
+
+  //Test to see if the object has been successfully loaded.
+  DALI_TEST_CHECK( application.GetGlAbstraction().GetUniformValue<Matrix>( "uObjectMatrix", actualScaleMatrix ) );
+  DALI_TEST_EQUALS( actualScaleMatrix, testScaleMatrix, Math::MACHINE_EPSILON_100, TEST_LOCATION );
+
+  //Finish by setting off stage, and ensuring this was successful.
+  controlRenderer.SetOffStage( actor );
+  DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
+}
+
+//Test if primitive shape loads correctly when supplied with only the bare minimum requirements, the shape to use.
+int UtcDaliRendererFactoryGetPrimitiveRenderer1(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer1:  Request primitive renderer with a shape only" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "CUBE" );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape loads correctly when supplied with all possible parameters
+int UtcDaliRendererFactoryGetPrimitiveRenderer2(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer2:  Request primitive renderer with everything" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "CUBE" );
+  propertyMap.Insert( "color", Vector4( 0.5, 0.5, 0.5, 1.0 ) );
+  propertyMap.Insert( "slices", 10 );
+  propertyMap.Insert( "stacks", 20 );
+  propertyMap.Insert( "scaleTopRadius", 30.0f );
+  propertyMap.Insert( "scaleBottomRadius", 40.0f );
+  propertyMap.Insert( "scaleHeight", 50.0f );
+  propertyMap.Insert( "scaleRadius", 60.0f );
+  propertyMap.Insert( "bevelPercentage", 0.7f );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape loads a sphere correctly.
+int UtcDaliRendererFactoryGetPrimitiveRenderer3(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer3:  Request primitive renderer to display a sphere" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "SPHERE" );
+  propertyMap.Insert( "color", Vector4( 0.5, 0.5, 0.5, 1.0 ) );
+  propertyMap.Insert( "slices", 10 );
+  propertyMap.Insert( "stacks", 20 );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape loads a conic section correctly.
+int UtcDaliRendererFactoryGetPrimitiveRenderer4(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer4:  Request primitive renderer to display a conic section" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "CONICAL_FRUSTRUM" );
+  propertyMap.Insert( "color", Vector4( 0.5, 0.5, 0.5, 1.0 ) );
+  propertyMap.Insert( "slices", 10 );
+  propertyMap.Insert( "scaleTopRadius", 30.0f );
+  propertyMap.Insert( "scaleBottomRadius", 40.0f );
+  propertyMap.Insert( "scaleHeight", 50.0f );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape loads a bevelled cube correctly.
+int UtcDaliRendererFactoryGetPrimitiveRenderer5(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer5:  Request primitive renderer to display a bevelled cube" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "BEVELLED_CUBE" );
+  propertyMap.Insert( "color", Vector4( 0.5, 0.5, 0.5, 1.0 ) );
+  propertyMap.Insert( "bevelPercentage", 0.7f );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape loads an octahedron correctly.
+int UtcDaliRendererFactoryGetPrimitiveRenderer6(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer6:  Request primitive renderer to display an octahedron" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "OCTAHEDRON" );
+  propertyMap.Insert( "color", Vector4( 0.5, 0.5, 0.5, 1.0 ) );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape loads a cone correctly.
+int UtcDaliRendererFactoryGetPrimitiveRenderer7(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer7:  Request primitive renderer to display a cone" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "CONE" );
+  propertyMap.Insert( "color", Vector4( 0.5, 0.5, 0.5, 1.0 ) );
+  propertyMap.Insert( "slices", 10 );
+  propertyMap.Insert( "scaleTopRadius", 30.0f );
+  propertyMap.Insert( "scaleHeight", 50.0f );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape loads correctly when light position is manually set.
+int UtcDaliRendererFactoryGetPrimitiveRenderer8(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRenderer8:  Request primitive renderer with set light position" );
+
+  //Set up renderer properties.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+  propertyMap.Insert( "shape", "SPHERE" );
+  propertyMap.Insert( "color", Vector4( 0.5, 0.5, 0.5, 1.0 ) );
+  propertyMap.Insert( "uLightPosition", Vector3( 0.0, 1.0, 2.0 ) );
+
+  //Test to see if shape loads correctly.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
+
+//Test if primitive shape renderer handles the case of not being passed a specific shape to use.
+int UtcDaliRendererFactoryGetPrimitiveRendererN1(void)
+{
+  //Set up test application first, so everything else can be handled.
+  ToolkitTestApplication application;
+
+  tet_infoline( "UtcDaliRendererFactoryGetPrimitiveRendererN1:  Request primitive renderer without shape" );
+
+  //Set up renderer properties, without supplying shape.
+  Property::Map propertyMap;
+  propertyMap.Insert( "rendererType", "PRIMITIVE" );
+
+  //Test to see if shape loads regardless of missing input.
+  TestPrimitiveRendererWithProperties( propertyMap, application );
+
+  END_TEST;
+}
index 717c00b..acc248c 100644 (file)
@@ -28,8 +28,6 @@
 
 //INTERNAL INCLUDES
 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
 
 //INTERNAL INCLUDES
 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
-#include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
-#include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
 
 namespace Dali
 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
 
 namespace Dali
@@ -115,8 +113,8 @@ const char* SIMPLE_VERTEX_SHADER = DALI_COMPOSE_SHADER(
     //Illumination in Model-View space - Transform attributes and uniforms\n
     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
     vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
     //Illumination in Model-View space - Transform attributes and uniforms\n
     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
     vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
-    vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
-    vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
+    vec4 center = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
+    vec4 lightPos = vec4( center.x, center.y, uLightPosition.z, 1.0 );\n
     vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
 
     float lightDiffuse = max( dot( vecToLight, normal ), 0.0 );\n
     vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
 
     float lightDiffuse = max( dot( vecToLight, normal ), 0.0 );\n
@@ -161,8 +159,8 @@ const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
 
     //Illumination in Model-View space - Transform attributes and uniforms\n
     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
 
     //Illumination in Model-View space - Transform attributes and uniforms\n
     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
-    vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
-    vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
+    vec4 center = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
+    vec4 lightPos = vec4( center.x, center.y, uLightPosition.z, 1.0 );\n
     vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );\n
 
     vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
     vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );\n
 
     vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
@@ -223,8 +221,8 @@ const char* NORMAL_MAP_VERTEX_SHADER = DALI_COMPOSE_SHADER(
     vertexPosition = uMvpMatrix * vertexPosition;\n
 
     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
     vertexPosition = uMvpMatrix * vertexPosition;\n
 
     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
-    vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
-    vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
+    vec4 center = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
+    vec4 lightPos = vec4( center.x, center.y, uLightPosition.z, 1.0 );\n
 
     vec3 tangent = normalize( uNormalMatrix * aTangent );
     vec3 binormal = normalize( uNormalMatrix * aBiNormal );
 
     vec3 tangent = normalize( uNormalMatrix * aTangent );
     vec3 binormal = normalize( uNormalMatrix * aBiNormal );
diff --git a/dali-toolkit/internal/controls/renderers/primitive/primitive-renderer.cpp b/dali-toolkit/internal/controls/renderers/primitive/primitive-renderer.cpp
new file mode 100644 (file)
index 0000000..1ed6404
--- /dev/null
@@ -0,0 +1,1464 @@
+/*
+ * Copyright (c) 2016 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 "primitive-renderer.h"
+
+// EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
+#include <dali/public-api/common/stage.h>
+#include <dali/public-api/common/constants.h>
+
+//INTERNAL INCLUDES
+#include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
+#include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace Internal
+{
+
+namespace
+{
+
+//Primitive property defaults
+const int     DEFAULT_SLICES =              128; ///< For spheres and conics
+const int     DEFAULT_STACKS =              128; ///< For spheres and conics
+const float   DEFAULT_SCALE_TOP_RADIUS =    1.0; ///< For conical frustrums
+const float   DEFAULT_SCALE_BOTTOM_RADIUS = 1.5; ///< For cones and conical frustrums
+const float   DEFAULT_SCALE_HEIGHT =        3.0; ///< For all conics
+const float   DEFAULT_SCALE_RADIUS =        1.0; ///< For cylinders
+const float   DEFAULT_BEVEL_PERCENTAGE =    0.0; ///< For bevelled cubes
+const float   DEFAULT_BEVEL_SMOOTHNESS =    0.0; ///< For bevelled cubes
+const Vector4 DEFAULT_COLOR =               Vector4( 0.5, 0.5, 0.5, 0.0 ); ///< Grey, for all.
+
+//Property limits
+const int   MIN_SLICES =           1;   ///< Minimum number of slices for spheres and conics
+const int   MIN_STACKS =           1;   ///< Minimum number of stacks for spheres and conics
+const int   MAX_PARTITIONS =       255; ///< Maximum number of slices or stacks for spheres and conics
+const float MIN_BEVEL_PERCENTAGE = 0.0; ///< Minimum bevel percentage for bevelled cubes
+const float MAX_BEVEL_PERCENTAGE = 1.0; ///< Maximum bevel percentage for bevelled cubes
+const float MIN_SMOOTHNESS =       0.0; ///< Minimum bevel smoothness for bevelled cubes
+const float MAX_SMOOTHNESS =       1.0; ///< Maximum bevel smoothness for bevelled cubes
+
+const char * const RENDERER_TYPE_VALUE( "PRIMITIVE" );
+
+//Specific shape labels.
+const char * const SPHERE_LABEL( "SPHERE" );
+const char * const CONE_LABEL( "CONE" );
+const char * const CONICAL_FRUSTRUM_LABEL( "CONICAL_FRUSTRUM" );
+const char * const CYLINDER_LABEL( "CYLINDER" );
+const char * const CUBE_LABEL( "CUBE" );
+const char * const OCTAHEDRON_LABEL( "OCTAHEDRON" );
+const char * const BEVELLED_CUBE_LABEL( "BEVELLED_CUBE" );
+
+//Shader properties
+const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
+const char * const COLOR_UNIFORM_NAME( "uColor" );
+const char * const OBJECT_DIMENSIONS_UNIFORM_NAME( "uObjectDimensions" );
+const char * const STAGE_SIZE_UNIFORM_NAME( "uStageSize" );
+
+//Vertex properties
+const char * const POSITION( "aPosition");
+const char * const NORMAL( "aNormal" );
+const char * const INDICES( "aIndices" );
+
+//A simple shader that applies diffuse lighting to a mono-coloured object.
+const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
+  attribute highp   vec3 aPosition;\n
+  attribute highp   vec2 aTexCoord;\n
+  attribute highp   vec3 aNormal;\n
+  varying   mediump vec3 vIllumination;\n
+  uniform   mediump vec3 uSize;\n
+  uniform   mediump vec3 uObjectDimensions;\n
+  uniform   mediump mat4 uMvpMatrix;\n
+  uniform   mediump mat4 uModelView;\n
+  uniform   mediump mat4 uViewMatrix;\n
+  uniform   mediump mat3 uNormalMatrix;\n
+  uniform   mediump mat4 uObjectMatrix;\n
+  uniform   mediump vec3 uLightPosition;\n
+  uniform   mediump vec3 uStageSize;\n
+
+  void main()\n
+  {\n
+    float xRatio = uSize.x / uObjectDimensions.x;\n
+    float yRatio = uSize.y / uObjectDimensions.y;\n
+    float scaleFactor = min( xRatio, yRatio );\n
+
+    vec4 normalisedVertexPosition = vec4( aPosition * scaleFactor, 1.0 );\n
+    vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
+    vertexPosition = uMvpMatrix * vertexPosition;\n
+
+    //Illumination in Model-View space - Transform attributes and uniforms\n
+    vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
+    vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
+
+    vec3 stageOffset = vec3( uStageSize.xy, 0 ) / 2.0;\n
+    vec4 lightPosition = vec4( ( uLightPosition - stageOffset ), 1.0 );\n
+    lightPosition = uViewMatrix * lightPosition;\n
+    vec3 vectorToLight = normalize( lightPosition.xyz - mvVertexPosition.xyz );\n
+
+    float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
+    vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
+
+    gl_Position = vertexPosition;\n
+  }\n
+);
+
+//Very simple fragment shader that merely applies the vertex shading to the color at each fragment.
+const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
+  precision mediump float;\n
+  varying   mediump vec3  vIllumination;\n
+  uniform   lowp    vec4  uColor;\n
+
+  void main()\n
+  {\n
+    gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a );\n
+  }\n
+);
+
+} // namespace
+
+PrimitiveRenderer::PrimitiveRenderer( RendererFactoryCache& factoryCache )
+: ControlRenderer( factoryCache ),
+  mColor( DEFAULT_COLOR ),
+  mScaleDimensions( Vector3::ONE ),
+  mScaleTopRadius( DEFAULT_SCALE_TOP_RADIUS ),
+  mScaleBottomRadius( DEFAULT_SCALE_BOTTOM_RADIUS ),
+  mScaleHeight( DEFAULT_SCALE_HEIGHT ),
+  mScaleRadius( DEFAULT_SCALE_RADIUS ),
+  mBevelPercentage( DEFAULT_BEVEL_PERCENTAGE ),
+  mBevelSmoothness( DEFAULT_BEVEL_SMOOTHNESS ),
+  mSlices( DEFAULT_SLICES ),
+  mStacks( DEFAULT_STACKS ),
+  mPrimitiveType( SPHERE )
+{
+}
+
+PrimitiveRenderer::~PrimitiveRenderer()
+{
+}
+
+void PrimitiveRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
+{
+  //Find out which shape to renderer.
+  Property::Value* primitiveType = propertyMap.Find( PRIMITIVE_SHAPE );
+  if( primitiveType )
+  {
+    if( primitiveType->Get( mShape ) )
+    {
+      //Set property type as an enum.
+      if( mShape == SPHERE_LABEL )
+      {
+        mPrimitiveType = SPHERE;
+      }
+      else if( mShape == CONE_LABEL )
+      {
+        mPrimitiveType = CONE;
+      }
+      else if( mShape == CONICAL_FRUSTRUM_LABEL )
+      {
+        mPrimitiveType = CONICAL_FRUSTRUM;
+      }
+      else if( mShape == CYLINDER_LABEL )
+      {
+        mPrimitiveType = CYLINDER;
+      }
+      else if( mShape == CUBE_LABEL )
+      {
+        mPrimitiveType = CUBE;
+      }
+      else if( mShape == OCTAHEDRON_LABEL )
+      {
+        mPrimitiveType = OCTAHEDRON;
+      }
+      else if( mShape == BEVELLED_CUBE_LABEL )
+      {
+        mPrimitiveType = BEVELLED_CUBE;
+      }
+      else
+      {
+        DALI_LOG_ERROR( "No known shape in PrimitiveRenderer.\n" );
+      }
+    }
+    else
+    {
+      DALI_LOG_ERROR( "Invalid type for shape in PrimitiveRenderer.\n" );
+    }
+  }
+  else
+  {
+    DALI_LOG_ERROR( "Fail to provide shape to the PrimitiveRenderer object.\n" );
+  }
+
+  //Read in other potential properties.
+
+  Property::Value* color = propertyMap.Find( SHAPE_COLOR );
+  if( color && !color->Get( mColor ) )
+  {
+    DALI_LOG_ERROR( "Invalid type for color in PrimitiveRenderer.\n" );
+  }
+
+  Property::Value* slices = propertyMap.Find( SLICES );
+  if( slices )
+  {
+    if( slices->Get( mSlices ) )
+    {
+      //Clamp value.
+      if( mSlices > MAX_PARTITIONS )
+      {
+        mSlices = MAX_PARTITIONS;
+      }
+      else if ( mSlices < MIN_SLICES )
+      {
+        mSlices = MIN_SLICES;
+      }
+    }
+    else
+    {
+      DALI_LOG_ERROR( "Invalid type for slices in PrimitiveRenderer.\n" );
+    }
+  }
+
+  Property::Value* stacks = propertyMap.Find( STACKS );
+  if( stacks )
+  {
+    if( stacks->Get( mStacks ) )
+    {
+      //Clamp value.
+      if( mStacks > MAX_PARTITIONS )
+      {
+        mStacks = MAX_PARTITIONS;
+      }
+      else if ( mStacks < MIN_STACKS )
+      {
+        mStacks = MIN_STACKS;
+      }
+    }
+    else
+    {
+      DALI_LOG_ERROR( "Invalid type for stacks in PrimitiveRenderer.\n" );
+    }
+  }
+
+  Property::Value* scaleTop = propertyMap.Find( SCALE_TOP_RADIUS );
+  if( scaleTop && !scaleTop->Get( mScaleTopRadius ) )
+  {
+    DALI_LOG_ERROR( "Invalid type for scale top radius in PrimitiveRenderer.\n" );
+  }
+
+  Property::Value* scaleBottom = propertyMap.Find( SCALE_BOTTOM_RADIUS );
+  if( scaleBottom && !scaleBottom->Get( mScaleBottomRadius ) )
+  {
+    DALI_LOG_ERROR( "Invalid type for scale bottom radius in PrimitiveRenderer.\n" );
+  }
+
+  Property::Value* scaleHeight = propertyMap.Find( SCALE_HEIGHT );
+  if( scaleHeight && !scaleHeight->Get( mScaleHeight ) )
+  {
+    DALI_LOG_ERROR( "Invalid type for scale height in PrimitiveRenderer.\n" );
+  }
+
+  Property::Value* scaleRadius = propertyMap.Find( SCALE_RADIUS );
+  if( scaleRadius && !scaleRadius->Get( mScaleRadius ) )
+  {
+    DALI_LOG_ERROR( "Invalid type for scale radius in PrimitiveRenderer.\n" );
+  }
+
+  Property::Value* dimensions = propertyMap.Find( SCALE_DIMENSIONS );
+  if( dimensions )
+  {
+    if( dimensions->Get( mScaleDimensions ) )
+    {
+      //If any dimension is invalid, set it to a sensible default.
+      if( mScaleDimensions.x <= 0.0 )
+      {
+        mScaleDimensions.x = 1.0;
+      }
+      if( mScaleDimensions.y <= 0.0 )
+      {
+        mScaleDimensions.y = 1.0;
+      }
+      if( mScaleDimensions.z <= 0.0 )
+      {
+        mScaleDimensions.z = 1.0;
+      }
+    }
+    else
+    {
+      DALI_LOG_ERROR( "Invalid type for scale dimensions in PrimitiveRenderer.\n" );
+    }
+  }
+
+  Property::Value* bevel = propertyMap.Find( BEVEL_PERCENTAGE );
+  if( bevel )
+  {
+    if( bevel->Get( mBevelPercentage ) )
+    {
+      //Clamp value.
+      if( mBevelPercentage < MIN_BEVEL_PERCENTAGE )
+      {
+        mBevelPercentage = MIN_BEVEL_PERCENTAGE;
+      }
+      else if( mBevelPercentage > MAX_BEVEL_PERCENTAGE )
+      {
+        mBevelPercentage = MAX_BEVEL_PERCENTAGE;
+      }
+    }
+    else
+    {
+      DALI_LOG_ERROR( "Invalid type for bevel percentage in PrimitiveRenderer.\n" );
+    }
+  }
+
+  Property::Value* smoothness = propertyMap.Find( BEVEL_SMOOTHNESS );
+  if( smoothness )
+  {
+    if( smoothness->Get( mBevelSmoothness ) )
+    {
+      //Clamp value.
+      if( mBevelSmoothness < MIN_SMOOTHNESS )
+      {
+        mBevelSmoothness = MIN_SMOOTHNESS;
+      }
+      else if( mBevelSmoothness > MAX_SMOOTHNESS )
+      {
+        mBevelSmoothness = MAX_SMOOTHNESS;
+      }
+    }
+    else
+    {
+      DALI_LOG_ERROR( "Invalid type for bevel smoothness in PrimitiveRenderer.\n" );
+    }
+  }
+
+  //Read in light position.
+  Property::Value* lightPosition = propertyMap.Find( LIGHT_POSITION_UNIFORM_NAME );
+  if( lightPosition )
+  {
+    if( !lightPosition->Get( mLightPosition ) )
+    {
+      DALI_LOG_ERROR( "Invalid value passed for light position in MeshRenderer object.\n" );
+      mLightPosition = Vector3::ZERO;
+    }
+  }
+  else
+  {
+    //Default behaviour is to place the light directly in front of the object,
+    // at a reasonable distance to light everything on screen.
+    Stage stage = Stage::GetCurrent();
+
+    mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
+  }
+}
+
+void PrimitiveRenderer::SetSize( const Vector2& size )
+{
+  ControlRenderer::SetSize( size );
+
+  // ToDo: renderer responds to the size change
+}
+
+void PrimitiveRenderer::SetClipRect( const Rect<int>& clipRect )
+{
+  ControlRenderer::SetClipRect( clipRect );
+
+  //ToDo: renderer responds to the clipRect change
+}
+
+void PrimitiveRenderer::SetOffset( const Vector2& offset )
+{
+  //ToDo: renderer applies the offset
+}
+
+void PrimitiveRenderer::DoSetOnStage( Actor& actor )
+{
+  InitializeRenderer();
+}
+
+void PrimitiveRenderer::DoCreatePropertyMap( Property::Map& map ) const
+{
+  map.Clear();
+  map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
+  map.Insert( PRIMITIVE_SHAPE, mShape );
+  map.Insert( SHAPE_COLOR, mColor );
+  map.Insert( SLICES, mSlices );
+  map.Insert( STACKS, mStacks );
+  map.Insert( SCALE_TOP_RADIUS, mScaleTopRadius );
+  map.Insert( SCALE_BOTTOM_RADIUS, mScaleBottomRadius );
+  map.Insert( SCALE_HEIGHT, mScaleHeight );
+  map.Insert( SCALE_RADIUS, mScaleRadius );
+  map.Insert( SCALE_DIMENSIONS, mScaleDimensions );
+  map.Insert( BEVEL_PERCENTAGE, mBevelPercentage );
+  map.Insert( BEVEL_SMOOTHNESS, mBevelSmoothness );
+  map.Insert( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
+}
+
+void PrimitiveRenderer::InitializeRenderer()
+{
+  if( !mGeometry )
+  {
+    CreateGeometry();
+  }
+
+  if( !mShader )
+  {
+    CreateShader();
+  }
+
+  mImpl->mRenderer = Renderer::New( mGeometry, mShader );
+}
+
+void PrimitiveRenderer::UpdateShaderUniforms()
+{
+  Stage stage = Stage::GetCurrent();
+  float width = stage.GetSize().width;
+  float height = stage.GetSize().height;
+
+  //Flip model to account for DALi starting with (0, 0) at the top left.
+  Matrix scaleMatrix;
+  scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
+
+  mShader.RegisterProperty( STAGE_SIZE_UNIFORM_NAME, Vector3( width, height, std::min( width, height ) ) );
+  mShader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
+  mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
+  mShader.RegisterProperty( COLOR_UNIFORM_NAME, mColor );
+  mShader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions );
+}
+
+void PrimitiveRenderer::CreateShader()
+{
+  mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
+  UpdateShaderUniforms();
+}
+
+void PrimitiveRenderer::CreateGeometry()
+{
+  Dali::Vector<Vertex> vertices;
+  Dali::Vector<unsigned short> indices;
+
+  switch( mPrimitiveType )
+  {
+    case SPHERE:
+    {
+      CreateSphere( vertices, indices, mSlices, mStacks );
+      break;
+    }
+    case CONE:
+    {
+      //Create a conic with zero top radius.
+      CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
+      break;
+    }
+    case CONICAL_FRUSTRUM:
+    {
+      CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
+      break;
+    }
+    case CYLINDER:
+    {
+      //Create a conic with equal radii on the top and bottom.
+      CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
+      break;
+    }
+    case CUBE:
+    {
+      //Create a cube by creating a bevelled cube with minimum bevel.
+      CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
+      break;
+    }
+    case OCTAHEDRON:
+    {
+      //Create an octahedron by creating a bevelled cube with maximum bevel.
+      CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
+      break;
+    }
+    case BEVELLED_CUBE:
+    {
+      CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
+      break;
+    }
+  }
+
+  mGeometry = Geometry::New();
+
+  //Vertices
+  Property::Map vertexFormat;
+  vertexFormat[POSITION] = Property::VECTOR3;
+  vertexFormat[NORMAL] = Property::VECTOR3;
+  PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
+  surfaceVertices.SetData( &vertices[0], vertices.Size() );
+
+  mGeometry.AddVertexBuffer( surfaceVertices );
+
+  //Indices for triangle formulation
+  mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
+}
+
+void PrimitiveRenderer::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
+{
+  ComputeSphereVertices( vertices, slices, stacks );
+  FormSphereTriangles( indices, slices, stacks );
+
+  mObjectDimensions = Vector3::ONE;
+}
+
+void PrimitiveRenderer::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
+                                     float scaleBottomRadius, float scaleHeight, int slices )
+{
+  ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
+  FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
+
+  //Determine object dimensions, and scale them to be between 0.0 and 1.0.
+  float xDimension = std::max( scaleTopRadius, scaleBottomRadius ) * 2.0f;
+  float yDimension = scaleHeight;
+  float largestDimension = std::max( xDimension, yDimension );
+
+  mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
+                               xDimension / largestDimension );
+}
+
+void PrimitiveRenderer::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
+                                            Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
+{
+  dimensions.Normalize();
+
+  if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
+  {
+    ComputeCubeVertices( vertices, dimensions );
+    FormCubeTriangles( indices );
+  }
+  else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
+  {
+    ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
+    FormOctahedronTriangles( indices );
+  }
+  else //In between, form a bevelled cube.
+  {
+    ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
+    FormBevelledCubeTriangles( indices );
+  }
+
+  mObjectDimensions = dimensions;
+}
+
+void PrimitiveRenderer::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
+                                             bool halfCircle )
+{
+  if( divisions < 0 )
+  {
+    return;
+  }
+
+  const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
+
+  sinTable.Resize( divisions );
+  cosTable.Resize( divisions );
+
+  for( int i = 0; i < divisions; i++ )
+  {
+    sinTable[i] = sin( angleDivision * i );
+    cosTable[i] = cos( angleDivision * i );
+  }
+}
+
+void PrimitiveRenderer::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
+{
+  //Tables for calculating slices angles and stacks angles, respectively.
+  Vector<float> sinTable1;
+  Vector<float> cosTable1;
+  Vector<float> sinTable2;
+  Vector<float> cosTable2;
+
+  ComputeCircleTables( sinTable1, cosTable1, slices, false );
+  ComputeCircleTables( sinTable2, cosTable2, stacks, true );
+
+  int numVertices = slices * ( stacks - 1 ) + 2;
+  vertices.Resize( numVertices );
+
+  int vertexIndex = 0;  //Track progress through vertices.
+  float x;
+  float y;
+  float z;
+
+  //Top stack.
+  vertices[vertexIndex].position = Vector3( 0.0, 0.0, 0.5 );
+  vertices[vertexIndex].normal = Vector3( 0.0, 0.0, 1.0 );
+  vertexIndex++;
+
+  //Middle stacks.
+  for( int i = 1; i < stacks; i++ )
+  {
+    for( int j = 0; j < slices; j++, vertexIndex++ )
+    {
+      x = cosTable1[j] * sinTable2[i];
+      y = sinTable1[j] * sinTable2[i];
+      z = cosTable2[i];
+
+      vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
+      vertices[vertexIndex].normal = Vector3( x, y, z );
+    }
+  }
+
+  //Bottom stack.
+  vertices[vertexIndex].position = Vector3( 0.0, 0.0, -0.5 );
+  vertices[vertexIndex].normal = Vector3( 0.0, 0.0, -1.0 );
+}
+
+void PrimitiveRenderer::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
+{
+  if( stacks <= 1 )
+  {
+    //Set indices to placeholder "error" values.
+    //This will display nothing, which is the expected behaviour for this edge case.
+    indices.Resize( 3 );
+    return;
+  }
+
+  int numTriangles = 2 * slices * ( stacks - 1 );
+
+  indices.Resize( 3 * numTriangles );
+
+  int indiceIndex = 0;  //Used to keep track of progress through indices.
+  int previousCycleBeginning = 1;  //Stores the index of the vertex that started the cycle of the previous stack.
+  int currentCycleBeginning = 1 + slices;
+
+  //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
+  for( int i = 1; i <= slices; i++, indiceIndex += 3 )
+  {
+    indices[indiceIndex] = 0;
+    indices[indiceIndex + 1] = i;
+    if( i == slices )
+    {
+      //End, so loop around.
+      indices[indiceIndex + 2] = 1;
+    }
+    else
+    {
+      indices[indiceIndex + 2] = i + 1;
+    }
+  }
+
+  //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
+  for( int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices )
+  {
+    for( int j = 0; j < slices; j++, indiceIndex += 6 )
+    {
+      if( j == slices - 1 )
+      {
+        //End, so loop around.
+        indices[indiceIndex] =     previousCycleBeginning + j;
+        indices[indiceIndex + 1] = currentCycleBeginning + j;
+        indices[indiceIndex + 2] = previousCycleBeginning;
+        indices[indiceIndex + 3] = currentCycleBeginning + j;
+        indices[indiceIndex + 4] = currentCycleBeginning;
+        indices[indiceIndex + 5] = previousCycleBeginning;
+      }
+      else
+      {
+        indices[indiceIndex] =     previousCycleBeginning + j;
+        indices[indiceIndex + 1] = currentCycleBeginning + j;
+        indices[indiceIndex + 2] = previousCycleBeginning + 1 + j;
+        indices[indiceIndex + 3] = currentCycleBeginning + j;
+        indices[indiceIndex + 4] = currentCycleBeginning + 1 + j;
+        indices[indiceIndex + 5] = previousCycleBeginning + 1 + j;
+      }
+    }
+  }
+
+  //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
+  for( int i = 0; i < slices; i++, indiceIndex += 3 )
+  {
+    indices[indiceIndex] = previousCycleBeginning + slices;
+    indices[indiceIndex + 1] = previousCycleBeginning + i;
+    if( i == slices - 1 )
+    {
+      //End, so loop around.
+      indices[indiceIndex + 2] = previousCycleBeginning;
+    }
+    else
+    {
+      indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
+    }
+  }
+}
+
+void PrimitiveRenderer::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
+                                                     float scaleBottomRadius, float scaleHeight, int slices )
+{
+  int vertexIndex = 0;  //Track progress through vertices.
+  Vector<float> sinTable;
+  Vector<float> cosTable;
+
+  ComputeCircleTables( sinTable, cosTable, slices, false );
+
+  int numVertices = 2;  //Always will have one at the top and one at the bottom.
+
+  //Add vertices for each circle. Need two per point for different face normals.
+  if( scaleTopRadius > 0.0 )
+  {
+    numVertices += 2 * slices;
+  }
+  if( scaleBottomRadius > 0.0 )
+  {
+    numVertices += 2 * slices;
+  }
+
+  vertices.Resize( numVertices );
+
+
+  //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
+  float biggestObjectDimension = std::max( std::max( scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f ), scaleHeight );
+  scaleTopRadius = scaleTopRadius / biggestObjectDimension;
+  scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
+
+  //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
+  float x;
+  float y = scaleHeight / biggestObjectDimension / 2.0f;
+  float z;
+
+  //Top center.
+  vertices[0].position = Vector3( 0, y, 0 );
+  vertices[0].normal = Vector3( 0, 1, 0 );
+  vertexIndex++;
+
+  //Top circle.
+  if( scaleTopRadius > 0.0 )
+  {
+    //Loop around the circle.
+    for( int i = 0; i < slices; i++, vertexIndex++ )
+    {
+      x = sinTable[i] * scaleTopRadius;
+      z = cosTable[i] * scaleTopRadius;
+
+      //Upward-facing normal.
+      vertices[vertexIndex].position = Vector3( x, y, z );
+      vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
+
+      //Outward-facing normal.
+      vertices[vertexIndex + slices].position = Vector3( x, y, z );
+      vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
+    }
+
+    vertexIndex += slices;
+  }
+
+  //Bottom circle.
+  if( scaleBottomRadius > 0.0 )
+  {
+    //Loop around the circle.
+    for( int i = 0; i < slices; i++, vertexIndex++ )
+    {
+      x = sinTable[i] * scaleBottomRadius;
+      z = cosTable[i] * scaleBottomRadius;
+
+      //Outward-facing normal.
+      vertices[vertexIndex].position = Vector3( x, -y, z );
+      vertices[vertexIndex].normal = Vector3( x, 0, z );
+
+      //Downward-facing normal.
+      vertices[vertexIndex + slices].position = Vector3( x, -y, z );
+      vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
+    }
+
+    vertexIndex += slices;
+  }
+
+  //Bottom center.
+  vertices[vertexIndex].position = Vector3( 0, -y, 0 );
+  vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
+  vertexIndex++;
+}
+
+void PrimitiveRenderer::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
+                                                   float scaleBottomRadius, int slices )
+{
+  int  indiceIndex = 0;  //Track progress through indices.
+  int  numTriangles = 0;
+  bool coneTop = scaleTopRadius <= 0.0;
+  bool coneBottom = scaleBottomRadius <= 0.0;
+
+  if( coneTop && coneBottom )
+  {
+    //Set indices to placeholder "error" values.
+    //This will display nothing, which is the expected behaviour for this edge case.
+    indices.Resize( 3 );
+    return;
+  }
+
+  if( !coneTop )
+  {
+    numTriangles += 2 * slices;
+  }
+  if( !coneBottom )
+  {
+    numTriangles += 2 * slices;
+  }
+
+  indices.Resize( 3 * numTriangles );
+
+  //Switch on the type of conic we have.
+  if( !coneTop && !coneBottom )
+  {
+    //Top circle. Start at index of first outer point and go around.
+    for( int i = 1; i <= slices; i++, indiceIndex += 3 )
+    {
+      indices[indiceIndex] = 0;
+      indices[indiceIndex + 1] = i;
+      if( i == slices )
+      {
+        //End, so loop around.
+        indices[indiceIndex + 2] = 1;
+      }
+      else
+      {
+        indices[indiceIndex + 2] = i + 1;
+      }
+    }
+
+    int topCycleBeginning = slices + 1;
+    int bottomCycleBeginning = topCycleBeginning + slices;
+
+    //Vertical edges.
+    for( int i = 0; i < slices; i++, indiceIndex += 6 )
+    {
+      if( i == slices - 1 )
+      {
+        //End, so loop around.
+        indices[indiceIndex] =     topCycleBeginning + i;
+        indices[indiceIndex + 1] = bottomCycleBeginning + i;
+        indices[indiceIndex + 2] = topCycleBeginning;
+        indices[indiceIndex + 3] = bottomCycleBeginning + i;
+        indices[indiceIndex + 4] = bottomCycleBeginning;
+        indices[indiceIndex + 5] = topCycleBeginning;
+      }
+      else
+      {
+        indices[indiceIndex] =     topCycleBeginning + i;
+        indices[indiceIndex + 1] = bottomCycleBeginning + i;
+        indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
+        indices[indiceIndex + 3] = bottomCycleBeginning + i;
+        indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
+        indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
+      }
+    }
+
+    int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
+
+    //Bottom circle.
+    for( int i = 0; i < slices; i++, indiceIndex += 3 )
+    {
+      indices[indiceIndex] = bottomFaceCycleBeginning;
+      indices[indiceIndex + 1] = bottomFaceCycleBeginning + i;
+      if( i == slices - 1 )
+      {
+        //End, so loop around.
+        indices[indiceIndex + 2] = bottomFaceCycleBeginning;
+      }
+      else
+      {
+        indices[indiceIndex + 2] = bottomFaceCycleBeginning + i + 1;
+      }
+    }
+  }
+  else if( !coneTop || !coneBottom )
+  {
+    //Top circle/edges. Start at index of first outer point and go around.
+    for( int i = 1; i <= slices; i++, indiceIndex += 3 )
+    {
+      indices[indiceIndex] = 0;
+      indices[indiceIndex + 1] = i;
+      if( i == slices )
+      {
+        //End, so loop around.
+        indices[indiceIndex + 2] = 1;
+      }
+      else
+      {
+        indices[indiceIndex + 2] = i + 1;
+      }
+    }
+
+    //Bottom circle/edges. Start at index of first outer point and go around.
+    for( int i = 1; i <= slices; i++, indiceIndex += 3 )
+    {
+      indices[indiceIndex] = 2 * slices + 1;
+      indices[indiceIndex + 1] = slices + i;
+      if( i == slices )
+      {
+        //End, so loop around.
+        indices[indiceIndex + 2] = slices + 1;
+      }
+      else
+      {
+        indices[indiceIndex + 2] = slices + i + 1;
+      }
+    }
+  }
+}
+
+void PrimitiveRenderer::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
+{
+  int numVertices = 4 * 6; //Four per face.
+  int vertexIndex = 0; //Tracks progress through vertices.
+  float scaledX = 0.5 * dimensions.x;
+  float scaledY = 0.5 * dimensions.y;
+  float scaledZ = 0.5 * dimensions.z;
+
+  vertices.Resize( numVertices );
+
+  Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
+  positions.Resize(8);
+  Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
+  normals.Resize(6);
+
+  positions[0] = Vector3( -scaledX,  scaledY, -scaledZ );
+  positions[1] = Vector3(  scaledX,  scaledY, -scaledZ );
+  positions[2] = Vector3(  scaledX,  scaledY,  scaledZ );
+  positions[3] = Vector3( -scaledX,  scaledY,  scaledZ );
+  positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
+  positions[5] = Vector3(  scaledX, -scaledY, -scaledZ );
+  positions[6] = Vector3(  scaledX, -scaledY,  scaledZ );
+  positions[7] = Vector3( -scaledX, -scaledY,  scaledZ );
+
+  normals[0] = Vector3(  0,  1,  0 );
+  normals[1] = Vector3(  0,  0, -1 );
+  normals[2] = Vector3(  1,  0,  0 );
+  normals[3] = Vector3(  0,  0,  1 );
+  normals[4] = Vector3( -1,  0,  0 );
+  normals[5] = Vector3(  0, -1,  0 );
+
+  //Top face, upward normals.
+  for( int i = 0; i < 4; i++, vertexIndex++ )
+  {
+    vertices[vertexIndex].position = positions[i];
+    vertices[vertexIndex].normal = normals[0];
+  }
+
+  //Top face, outward normals.
+  for( int i = 0; i < 4; i++, vertexIndex += 2 )
+  {
+    vertices[vertexIndex].position = positions[i];
+    vertices[vertexIndex].normal = normals[i + 1];
+
+    if( i == 3 )
+    {
+      //End, so loop around.
+      vertices[vertexIndex + 1].position = positions[0];
+    }
+    else
+    {
+      vertices[vertexIndex + 1].position = positions[i + 1];
+    }
+    vertices[vertexIndex + 1].normal = normals[i + 1];
+  }
+
+  //Bottom face, outward normals.
+  for( int i = 0; i < 4; i++, vertexIndex += 2 )
+  {
+    vertices[vertexIndex].position = positions[i + 4];
+    vertices[vertexIndex].normal = normals[i + 1];
+
+    if( i == 3 )
+    {
+      //End, so loop around.
+      vertices[vertexIndex + 1].position = positions[4];
+    }
+    else
+    {
+      vertices[vertexIndex + 1].position = positions[i + 5];
+    }
+    vertices[vertexIndex + 1].normal = normals[i + 1];
+  }
+
+  //Bottom face, downward normals.
+  for( int i = 0; i < 4; i++, vertexIndex++ )
+  {
+    vertices[vertexIndex].position = positions[i + 4];
+    vertices[vertexIndex].normal = normals[5];
+  }
+
+}
+
+void PrimitiveRenderer::FormCubeTriangles( Vector<unsigned short>& indices )
+{
+  int numTriangles = 12;
+  int triangleIndex = 0;  //Track progress through indices.
+
+  indices.Resize( 3 * numTriangles );
+
+  //Top face.
+  indices[triangleIndex] =     0;
+  indices[triangleIndex + 1] = 1;
+  indices[triangleIndex + 2] = 2;
+  indices[triangleIndex + 3] = 2;
+  indices[triangleIndex + 4] = 3;
+  indices[triangleIndex + 5] = 0;
+  triangleIndex += 6;
+
+  int topFaceStart = 4;
+  int bottomFaceStart = 12;
+
+  //Side faces.
+  for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
+  {
+    indices[triangleIndex    ] = i + topFaceStart;
+    indices[triangleIndex + 1] = i + bottomFaceStart + 1;
+    indices[triangleIndex + 2] = i + topFaceStart + 1;
+    indices[triangleIndex + 3] = i + topFaceStart;
+    indices[triangleIndex + 4] = i + bottomFaceStart;
+    indices[triangleIndex + 5] = i + bottomFaceStart + 1;
+  }
+
+  //Bottom face.
+  indices[triangleIndex] =     20;
+  indices[triangleIndex + 1] = 21;
+  indices[triangleIndex + 2] = 22;
+  indices[triangleIndex + 3] = 22;
+  indices[triangleIndex + 4] = 23;
+  indices[triangleIndex + 5] = 20;
+}
+
+void PrimitiveRenderer::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
+{
+  int numVertices = 3 * 8; //Three per face
+  int vertexIndex = 0; //Tracks progress through vertices.
+  float scaledX = 0.5 * dimensions.x;
+  float scaledY = 0.5 * dimensions.y;
+  float scaledZ = 0.5 * dimensions.z;
+
+  vertices.Resize( numVertices );
+
+  Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
+  positions.Resize(6);
+  Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
+  normals.Resize(8);
+  Vector<Vector3> outerNormals;  //Holds normals that point outwards at each vertex.
+  outerNormals.Resize( 6 );
+
+  positions[0] = Vector3(  0.0,  scaledY,  0.0 );
+  positions[1] = Vector3( -scaledX,  0.0,  0.0 );
+  positions[2] = Vector3(  0.0,  0.0, -scaledZ );
+  positions[3] = Vector3(  scaledX,  0.0,  0.0 );
+  positions[4] = Vector3(  0.0,  0.0,  scaledZ );
+  positions[5] = Vector3(  0.0, -scaledY,  0.0 );
+
+  normals[0] = Vector3( -1,  1, -1 );
+  normals[1] = Vector3(  1,  1, -1 );
+  normals[2] = Vector3(  1,  1,  1 );
+  normals[3] = Vector3( -1,  1,  1 );
+  normals[4] = Vector3( -1, -1, -1 );
+  normals[5] = Vector3(  1, -1, -1 );
+  normals[6] = Vector3(  1, -1,  1 );
+  normals[7] = Vector3( -1, -1,  1 );
+
+  outerNormals[0] = Vector3(  0,  1,  0 );
+  outerNormals[1] = Vector3( -1,  0,  0 );
+  outerNormals[2] = Vector3(  0,  0, -1 );
+  outerNormals[3] = Vector3(  1,  0,  0 );
+  outerNormals[4] = Vector3(  0,  0,  1 );
+  outerNormals[5] = Vector3(  0, -1,  0 );
+
+  //Loop through top faces.
+  for( int i = 0; i < 4; i++, vertexIndex += 3 )
+  {
+    if( i == 3 )
+    {
+      //End, so loop around.
+      vertices[vertexIndex    ].position = positions[0];
+      vertices[vertexIndex    ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
+      vertices[vertexIndex + 1].position = positions[i + 1];
+      vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
+      vertices[vertexIndex + 2].position = positions[1];
+      vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
+    }
+    else
+    {
+      vertices[vertexIndex    ].position = positions[0];
+      vertices[vertexIndex    ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
+      vertices[vertexIndex + 1].position = positions[i + 1];
+      vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
+      vertices[vertexIndex + 2].position = positions[i + 2];
+      vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
+    }
+  }
+
+  //Loop through bottom faces.
+  for( int i = 0; i < 4; i++, vertexIndex += 3 )
+  {
+    if( i == 3 )
+    {
+      //End, so loop around.
+      vertices[vertexIndex    ].position = positions[5];
+      vertices[vertexIndex    ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
+      vertices[vertexIndex + 1].position = positions[i + 1];
+      vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
+      vertices[vertexIndex + 2].position = positions[1];
+      vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
+    }
+    else
+    {
+      vertices[vertexIndex    ].position = positions[5];
+      vertices[vertexIndex    ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
+      vertices[vertexIndex + 1].position = positions[i + 1];
+      vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
+      vertices[vertexIndex + 2].position = positions[i + 2];
+      vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
+    }
+  }
+}
+
+void PrimitiveRenderer::FormOctahedronTriangles( Vector<unsigned short>& indices )
+{
+  int numTriangles = 8;
+  int numIndices = numTriangles * 3;
+
+  indices.Resize( numIndices );
+
+  for( unsigned short i = 0; i < numIndices; i++ )
+  {
+    indices[i] = i;
+  }
+}
+
+void PrimitiveRenderer::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
+                                                     float bevelPercentage, float bevelSmoothness )
+{
+  int numPositions = 24;
+  int numFaces = 26;
+  int numOuterFaces = 6;
+  int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
+  int vertexIndex = 0;  //Track progress through vertices.
+  int normalIndex = 0;  //Track progress through normals, as vertices are calculated per face.
+
+  float minDimension = std::min( std::min( dimensions.x, dimensions.y ), dimensions.z );
+  float bevelScale = 1.0 - bevelPercentage;
+  float bevelAmount = 0.5 * bevelScale * minDimension;
+
+  float outerX = 0.5 * dimensions.x;
+  float outerY = 0.5 * dimensions.y;
+  float outerZ = 0.5 * dimensions.z;
+
+  float bevelX = outerX - ( 0.5 * minDimension - bevelAmount );
+  float bevelY = outerY - ( 0.5 * minDimension - bevelAmount );
+  float bevelZ = outerZ - ( 0.5 * minDimension - bevelAmount );
+
+  Vector<Vector3> positions;  //Holds object points, to be shared between vertexes.
+  positions.Resize( numPositions );
+  Vector<Vector3> normals;  //Holds face normals, to be shared between vertexes.
+  normals.Resize( numFaces );
+  Vector<Vector3> outerNormals;  //Holds normals of the outermost faces specifically.
+  outerNormals.Resize( numOuterFaces );
+  vertices.Resize( numVertices );
+
+  //Topmost face positions.
+  positions[0 ] = Vector3( -bevelX,  outerY, -bevelZ );
+  positions[1 ] = Vector3(  bevelX,  outerY, -bevelZ );
+  positions[2 ] = Vector3(  bevelX,  outerY,  bevelZ );
+  positions[3 ] = Vector3( -bevelX,  outerY,  bevelZ );
+
+  //Second layer positions.
+  positions[4 ] = Vector3( -outerX,  bevelY, -bevelZ );
+  positions[5 ] = Vector3( -bevelX,  bevelY, -outerZ );
+  positions[6 ] = Vector3(  bevelX,  bevelY, -outerZ );
+  positions[7 ] = Vector3(  outerX,  bevelY, -bevelZ );
+  positions[8 ] = Vector3(  outerX,  bevelY,  bevelZ );
+  positions[9 ] = Vector3(  bevelX,  bevelY,  outerZ );
+  positions[10] = Vector3( -bevelX,  bevelY,  outerZ );
+  positions[11] = Vector3( -outerX,  bevelY,  bevelZ );
+
+  //Third layer positions.
+  positions[12] = Vector3( -outerX, -bevelY, -bevelZ );
+  positions[13] = Vector3( -bevelX, -bevelY, -outerZ );
+  positions[14] = Vector3(  bevelX, -bevelY, -outerZ );
+  positions[15] = Vector3(  outerX, -bevelY, -bevelZ );
+  positions[16] = Vector3(  outerX, -bevelY,  bevelZ );
+  positions[17] = Vector3(  bevelX, -bevelY,  outerZ );
+  positions[18] = Vector3( -bevelX, -bevelY,  outerZ );
+  positions[19] = Vector3( -outerX, -bevelY,  bevelZ );
+
+  //Bottom-most face positions.
+  positions[20] = Vector3( -bevelX, -outerY, -bevelZ );
+  positions[21] = Vector3(  bevelX, -outerY, -bevelZ );
+  positions[22] = Vector3(  bevelX, -outerY,  bevelZ );
+  positions[23] = Vector3( -bevelX, -outerY,  bevelZ );
+
+  //Top face normal.
+  normals[0 ] = Vector3(  0,  1,  0 );
+
+  //Top slope normals.
+  normals[1 ] = Vector3( -1,  1, -1 );
+  normals[2 ] = Vector3(  0,  1, -1 );
+  normals[3 ] = Vector3(  1,  1, -1 );
+  normals[4 ] = Vector3(  1,  1,  0 );
+  normals[5 ] = Vector3(  1,  1,  1 );
+  normals[6 ] = Vector3(  0,  1,  1 );
+  normals[7 ] = Vector3( -1,  1,  1 );
+  normals[8 ] = Vector3( -1,  1,  0 );
+
+  //Side normals.
+  normals[9 ] = Vector3( -1,  0, -1 );
+  normals[10] = Vector3(  0,  0, -1 );
+  normals[11] = Vector3(  1,  0, -1 );
+  normals[12] = Vector3(  1,  0,  0 );
+  normals[13] = Vector3(  1,  0,  1 );
+  normals[14] = Vector3(  0,  0,  1 );
+  normals[15] = Vector3( -1,  0,  1 );
+  normals[16] = Vector3( -1,  0,  0 );
+
+  //Bottom slope normals.
+  normals[17] = Vector3( -1, -1, -1 );
+  normals[18] = Vector3(  0, -1, -1 );
+  normals[19] = Vector3(  1, -1, -1 );
+  normals[20] = Vector3(  1, -1,  0 );
+  normals[21] = Vector3(  1, -1,  1 );
+  normals[22] = Vector3(  0, -1,  1 );
+  normals[23] = Vector3( -1, -1,  1 );
+  normals[24] = Vector3( -1, -1,  0 );
+
+  //Bottom face normal.
+  normals[25] = Vector3(  0, -1,  0 );
+
+  //Top, back, right, front, left and bottom faces, respectively.
+  outerNormals[0] = Vector3(  0,  1,  0 );
+  outerNormals[1] = Vector3(  0,  0, -1 );
+  outerNormals[2] = Vector3(  1,  0,  0 );
+  outerNormals[3] = Vector3(  0,  0,  1 );
+  outerNormals[4] = Vector3( -1,  0,  0 );
+  outerNormals[5] = Vector3(  0, -1,  0 );
+
+  //Topmost face vertices.
+  for( int i = 0; i < 4; i++, vertexIndex++ )
+  {
+    vertices[vertexIndex].position = positions[i];
+    vertices[vertexIndex].normal = normals[normalIndex];
+  }
+
+  normalIndex++;
+
+  //Top slope vertices.
+  for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
+  {
+    //Triangle part
+    vertices[vertexIndex    ].position = positions[i];
+    vertices[vertexIndex    ].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+    vertices[vertexIndex + 1].position = positions[2 * i + 4];
+    vertices[vertexIndex + 1].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness  + normals[normalIndex] * (1 - bevelSmoothness);
+    vertices[vertexIndex + 2].position = positions[2 * i + 5];
+    vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+
+    //Rectangle part
+    if( i == 3 )
+    {
+      //End, so loop around.
+      vertices[vertexIndex + 3].position = positions[i];
+      vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 4].position = positions[0];
+      vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 5].position = positions[2 * i + 5];
+      vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 6].position = positions[4];
+      vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+    }
+    else
+    {
+      vertices[vertexIndex + 3].position = positions[i];
+      vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 4].position = positions[i + 1];
+      vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 5].position = positions[2 * i + 5];
+      vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 6].position = positions[2 * i + 6];
+      vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+    }
+  }
+
+  int secondCycleBeginning = 4;
+  int thirdCycleBeginning = secondCycleBeginning + 8;
+  int bottomCycleBeginning = thirdCycleBeginning + 8;
+
+  //Side vertices.
+  for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
+  {
+    if( i == 7 )
+    {
+      //End, so loop around.
+      vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
+      vertices[vertexIndex    ].normal = normals[normalIndex];
+      vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
+      vertices[vertexIndex + 1].normal = normals[normalIndex];
+      vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
+      vertices[vertexIndex + 2].normal = normals[normalIndex];
+      vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
+      vertices[vertexIndex + 3].normal = normals[normalIndex];
+    }
+    else if( (i % 2) == 0 )
+    {
+      //'even' faces are corner ones, and need smoothing.
+      vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
+      vertices[vertexIndex    ].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
+      vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
+      vertices[vertexIndex + 2].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
+      vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+    }
+    else
+    {
+      //'odd' faces are outer ones, and so don't need smoothing.
+      vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
+      vertices[vertexIndex    ].normal = normals[normalIndex];
+      vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
+      vertices[vertexIndex + 1].normal = normals[normalIndex];
+      vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
+      vertices[vertexIndex + 2].normal = normals[normalIndex];
+      vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
+      vertices[vertexIndex + 3].normal = normals[normalIndex];
+    }
+  }
+
+  //Bottom slope vertices.
+  for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
+  {
+    //Triangle part
+    vertices[vertexIndex    ].position = positions[thirdCycleBeginning + 2 * i];
+    vertices[vertexIndex    ].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+    vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
+    vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+    vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
+    vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
+
+    //Rectangle part
+    if( i == 3 )
+    {
+      //End, so loop around.
+      vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
+      vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
+      vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
+      vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
+      vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+    }
+    else
+    {
+      vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
+      vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
+      vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
+      vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+      vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
+      vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
+    }
+  }
+
+  //Bottom-most face vertices.
+  for( int i = 0; i < 4; i++, vertexIndex++ )
+  {
+    vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
+    vertices[vertexIndex].normal = normals[normalIndex];
+  }
+
+  normalIndex++;
+}
+
+void PrimitiveRenderer::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
+{
+  int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
+  int indiceIndex = 0;  //Track progress through indices.
+  int vertexIndex = 0;  //Track progress through vertices as they're processed.
+
+  indices.Resize( 3 * numTriangles );
+
+  //Top face.
+  indices[indiceIndex    ] = vertexIndex;
+  indices[indiceIndex + 1] = vertexIndex + 1;
+  indices[indiceIndex + 2] = vertexIndex + 2;
+  indices[indiceIndex + 3] = vertexIndex + 0;
+  indices[indiceIndex + 4] = vertexIndex + 2;
+  indices[indiceIndex + 5] = vertexIndex + 3;
+  indiceIndex += 6;
+  vertexIndex += 4;
+
+  //Top slopes.
+  for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
+  {
+    //Triangle part.
+    indices[indiceIndex    ] = vertexIndex;
+    indices[indiceIndex + 1] = vertexIndex + 1;
+    indices[indiceIndex + 2] = vertexIndex + 2;
+
+    //Rectangle part.
+    indices[indiceIndex + 3] = vertexIndex + 3;
+    indices[indiceIndex + 4] = vertexIndex + 4;
+    indices[indiceIndex + 5] = vertexIndex + 5;
+    indices[indiceIndex + 6] = vertexIndex + 4;
+    indices[indiceIndex + 7] = vertexIndex + 5;
+    indices[indiceIndex + 8] = vertexIndex + 6;
+  }
+
+  //Side faces.
+  for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
+  {
+    indices[indiceIndex    ] = vertexIndex;
+    indices[indiceIndex + 1] = vertexIndex + 1;
+    indices[indiceIndex + 2] = vertexIndex + 2;
+    indices[indiceIndex + 3] = vertexIndex + 1;
+    indices[indiceIndex + 4] = vertexIndex + 2;
+    indices[indiceIndex + 5] = vertexIndex + 3;
+  }
+
+  //Bottom slopes.
+  for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
+  {
+    //Triangle part.
+    indices[indiceIndex    ] = vertexIndex;
+    indices[indiceIndex + 1] = vertexIndex + 1;
+    indices[indiceIndex + 2] = vertexIndex + 2;
+
+    //Rectangle part.
+    indices[indiceIndex + 3] = vertexIndex + 3;
+    indices[indiceIndex + 4] = vertexIndex + 4;
+    indices[indiceIndex + 5] = vertexIndex + 5;
+    indices[indiceIndex + 6] = vertexIndex + 4;
+    indices[indiceIndex + 7] = vertexIndex + 5;
+    indices[indiceIndex + 8] = vertexIndex + 6;
+  }
+
+  //Bottom face.
+  indices[indiceIndex    ] = vertexIndex;
+  indices[indiceIndex + 1] = vertexIndex + 1;
+  indices[indiceIndex + 2] = vertexIndex + 2;
+  indices[indiceIndex + 3] = vertexIndex + 0;
+  indices[indiceIndex + 4] = vertexIndex + 2;
+  indices[indiceIndex + 5] = vertexIndex + 3;
+  indiceIndex += 6;
+  vertexIndex += 4;
+}
+
+} // namespace Internal
+
+} // namespace Toolkit
+
+} // namespace Dali
diff --git a/dali-toolkit/internal/controls/renderers/primitive/primitive-renderer.h b/dali-toolkit/internal/controls/renderers/primitive/primitive-renderer.h
new file mode 100644 (file)
index 0000000..273bb36
--- /dev/null
@@ -0,0 +1,352 @@
+#ifndef DALI_TOOLKIT_INTERNAL_PRIMITIVE_RENDERER_H
+#define DALI_TOOLKIT_INTERNAL_PRIMITIVE_RENDERER_H
+
+/*
+ * Copyright (c) 2016 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.
+ *
+ */
+
+/*
+ * The geometry creation logic was based off similar methods provided by freeGLUT.
+ * Original copyright and licence information:
+ *
+ * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
+ * Written by Pawel W. Olszta, <olszta@sourceforge.net>
+ * Creation date: Fri Dec 3 1999
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/internal/controls/renderers/control-renderer-impl.h>
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace Internal
+{
+
+/**
+ * The renderer which renders a simple 3D shape to the control's quad
+ *
+ * The following properties are required to create a PrimitiveRender
+ *
+ * | %Property Name  | Type        |
+ * |-----------------|-------------|
+ * | shape           | STRING      |
+ *
+ * In addition, the following properties can be (optionally) supplied to modify the shape's parameters
+ *
+ * | %Property Name    | Type        | Shapes Affected                          |
+ * |-------------------|-------------|------------------------------------------|
+ * | color             | VECTOR4     | all                                      |
+ * | slices            | INTEGER     | sphere, cone, conical frustrum, cylinder |
+ * | stacks            | INTEGER     | sphere                                   |
+ * | scaleTopRadius    | FLOAT       | conical frustrum                         |
+ * | scaleBottomRadius | FLOAT       | cone, conical frustrum                   |
+ * | scaleHeight       | FLOAT       | cone, conical frustrum, cylinder         |
+ * | scaleRadius       | FLOAT       | cylinder                                 |
+ * | scaleDimensions   | VECTOR3     | cube, octahedron, bevelled cube          |
+ * | bevelPercentage   | FLOAT       | bevelled cube                            |
+ * | bevelSmoothness   | FLOAT       | bevelled cube                            |
+ *
+ * Note: slices and stacks both have an upper limit of 255.
+ *
+ * Finally, the following can be used to affect the renderer's shader
+ *
+ * | %Property Name  | Type        | Representing                            |
+ * |-----------------|-------------|-----------------------------------------|
+ * | uLightPosition  | VECTOR3     | The position (on stage) of the light    |
+ */
+class PrimitiveRenderer: public ControlRenderer
+{
+public:
+
+  /**
+   * @brief Constructor.
+   *
+   * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
+   */
+  PrimitiveRenderer( RendererFactoryCache& factoryCache );
+
+  /**
+   * @brief A reference counted object may only be deleted by calling Unreference().
+   */
+  virtual ~PrimitiveRenderer();
+
+public:  // from ControlRenderer
+
+  /**
+   * @copydoc ControlRenderer::SetSize
+   */
+  virtual void SetSize( const Vector2& size );
+
+  /**
+   * @copydoc ControlRenderer::SetClipRect
+   */
+  virtual void SetClipRect( const Rect<int>& clipRect );
+
+  /**
+   * @copydoc ControlRenderer::SetOffset
+   */
+  virtual void SetOffset( const Vector2& offset );
+
+  /**
+   * @copydoc ControlRenderer::CreatePropertyMap
+   */
+  virtual void DoCreatePropertyMap( Property::Map& map ) const;
+
+protected:
+
+  /**
+   * @copydoc ControlRenderer::DoInitialize
+   */
+  virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
+
+  /**
+   * @copydoc ControlRenderer::DoSetOnStage
+   */
+  virtual void DoSetOnStage( Actor& actor );
+
+private:
+
+  enum PrimitiveType
+  {
+    SPHERE,
+    CONE,
+    CONICAL_FRUSTRUM,
+    CYLINDER,
+    CUBE,
+    OCTAHEDRON,
+    BEVELLED_CUBE
+  };
+
+  //Simple struct to store the position and normal of a single vertex.
+  struct Vertex
+  {
+    Vertex()
+    {}
+
+    Vertex( const Vector3& position, const Vector3& normal, const Vector2& textureCoord )
+    : position( position ), normal( normal )
+    {}
+
+    Vector3 position;
+    Vector3 normal;
+  };
+
+  /**
+   * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
+   */
+  void InitializeRenderer();
+
+  /**
+     * @brief Create a shader for the object to use.
+     */
+  void CreateShader();
+
+  /**
+   * @brief Update shader related info, uniforms, etc. for the new shader.
+   */
+  void UpdateShaderUniforms();
+
+  /**
+   * @brief Create the geometry of the given primitive type.
+   */
+  void CreateGeometry();
+
+  /**
+   * @brief Compute the vertices and the triangles for a sphere.
+   * @param[in, out] vertices The vector of vertices.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
+   * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
+   */
+  void CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks );
+
+  /**
+   * @brief Compute the vertices and the triangles for a conic shape.
+   * @param[in, out] vertices The vector of vertices.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
+   * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
+   * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
+   * @param[in] slices The number of slices as you go around the conic shape. Affects the smoothness of the surface.
+   */
+  void CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
+                           float scaleBottomRadius, float scaleHeight, int slices );
+
+  /**
+   * @brief Compute the vertices and the triangles for a bevelled cube.
+   * @param[in, out] vertices The vector of vertices.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
+   * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
+   * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
+   */
+  void CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices, Vector3 dimensions,
+                           float bevelPercentage, float bevelSmoothness );
+
+  /**
+   * @brief Computes look-up tables for sin and cos, over angle divisions of (2 * Pi) / divisions
+   * @param[in, out] sinTable The table of sin values.
+   * @param[in, out] cosTable The table of cos values.
+   * @param[in] divisions Determines the angle coverage of the table. E.g divisions of '4' will have the sin values 0 = sin(0), 1 = sin(Pi/2), 2 = sin(Pi), 3 = sin(3Pi/2)
+   * @Param[in] halfCircle If true, go from 0 to Pi instead of 0 to 2Pi.
+   */
+  void ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions, bool halfCircle );
+
+  /**
+   * @brief Compute the vertices for a sphere.
+   * @param[in, out] vertices The vector of vertices.
+   * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
+   * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
+   */
+  void ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks );
+
+  /**
+   * @brief Compute the triangles for a sphere.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
+   * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
+   */
+  void FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks );
+
+  /**
+   * @brief Compute the vertices for a conical.
+   * @param[in, out] vertices The vector of vertices.
+   * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
+   * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
+   * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
+   * @param[in] slices The number of slices as you go around the conical. Affects the smoothness of the surface.
+   */
+  void ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius, float scaleBottomRadius,
+                                    float scaleHeight, int slices );
+
+  /**
+   * @brief Compute the triangles for a conic.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   * @param[in] coneTop True if the top circle has a radius of zero, i.e. the object is a complete cone.
+   * @param[in] coneBottom True if the bottom circle has a radius of zero, i.e. the object is an inverted complete cone.
+   * @param[in] slices The number of slices as you go around the conic. Affects the smoothness of the surface.
+   */
+  void FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius,
+                                  int slices );
+
+  /**
+   * @brief Compute the vertices for a cube.
+   * @param[in, out] vertices The vector of vertices.
+   * @Param[in] dimensions The dimensions of the object.
+   */
+  void ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions );
+
+  /**
+   * @brief Compute the triangles for a cube.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   */
+  void FormCubeTriangles( Vector<unsigned short>& indices );
+
+  /**
+   * @brief Compute the vertices for an octahedron (maximumly bevelled cube).
+   * @param[in, out] vertices The vector of vertices.
+   * @Param[in] dimensions The dimensions of the object.
+   * @Param[in] smoothness Defines how rounded the edges appear under lighting. Between 0.0 and 1.0.
+   */
+  void ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness );
+
+  /**
+   * @brief Compute the triangles for an octahedron.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   */
+  void FormOctahedronTriangles( Vector<unsigned short>& indices );
+
+  /**
+   * @brief Compute the vertices for a bevelled cube.
+   * @param[in, out] vertices The vector of vertices.
+   * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
+   * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
+   * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
+   */
+  void ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions, float bevelPercentage,
+                                    float bevelSmoothness );
+
+  /**
+   * @brief Compute the triangles for a bevelled cube.
+   * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
+   */
+  void FormBevelledCubeTriangles( Vector<unsigned short>& indices );
+
+private:
+
+  // Undefined
+  PrimitiveRenderer( const PrimitiveRenderer& PrimitiveRenderer );
+
+  // Undefined
+  PrimitiveRenderer& operator=( const PrimitiveRenderer& PrimitiveRenderer );
+
+private:
+  Shader mShader;
+  Geometry mGeometry;
+
+  std::string mShape;            //Shape to render, as string.
+  Vector4 mColor;                //Color of shape.
+  Vector3 mObjectDimensions;     //Dimensions of shape, scaled to be between 0.0 and 1.0.
+
+  Vector3 mSceneCenter;
+  Vector3 mSceneSize;
+
+  //Shader properties.
+  Vector3 mLightPosition;
+
+  //Shape properties.
+  Vector3 mScaleDimensions;      ///< Scale of dimensions of bevelled cube and sub-shapes.
+  float   mScaleTopRadius;       ///< Scale of radius of top circle, to use when creating certain objects.
+  float   mScaleBottomRadius;    ///< Scale of radius of bottom circle, to use when creating certain objects.
+  float   mScaleHeight;          ///< Scale of height, to use when creating certain objects.
+  float   mScaleRadius;          ///< Scale of radius, to use when creating certain objects.
+  float   mBevelPercentage;      ///< Used to determine bevel amount when creating certain objects.
+  float   mBevelSmoothness;      ///< Used to determine the smoothness of bevelled edges.
+  int     mSlices;               ///< Number of slices to use when creating certain objects.
+  int     mStacks;               ///< Number of stacks to use when creating certain objects.
+
+  PrimitiveType mPrimitiveType;  //Shape to render, as enum.
+};
+
+} // namespace Internal
+
+} // namespace Toolkit
+
+} // namespace Dali
+
+#endif /* DALI_TOOLKIT_INTERNAL_PRIMITIVE_RENDERER_H */
index 6cb0aa0..e1c75a2 100644 (file)
@@ -33,6 +33,7 @@
 #include <dali-toolkit/internal/controls/renderers/image/image-renderer.h>
 #include <dali-toolkit/internal/controls/renderers/svg/svg-renderer.h>
 #include <dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.h>
 #include <dali-toolkit/internal/controls/renderers/image/image-renderer.h>
 #include <dali-toolkit/internal/controls/renderers/svg/svg-renderer.h>
 #include <dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.h>
+#include <dali-toolkit/internal/controls/renderers/primitive/primitive-renderer.h>
 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
 #include <dali-toolkit/internal/controls/renderers/image-atlas-manager.h>
 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
 #include <dali-toolkit/internal/controls/renderers/image-atlas-manager.h>
@@ -103,6 +104,10 @@ RendererFactory::RendererType RendererFactory::GetRendererType( const Property::
     {
       rendererType = MESH;
     }
     {
       rendererType = MESH;
     }
+    else if( typeValue == PRIMITIVE_RENDERER )
+    {
+      rendererType = PRIMITIVE;
+    }
   }
 
   // check the url if exist, to decide the renderer type
   }
 
   // check the url if exist, to decide the renderer type
@@ -187,6 +192,11 @@ Toolkit::ControlRenderer RendererFactory::CreateControlRenderer( const Property:
       rendererPtr = new MeshRenderer( *( mFactoryCache.Get() ) );
       break;
     }
       rendererPtr = new MeshRenderer( *( mFactoryCache.Get() ) );
       break;
     }
+    case PRIMITIVE:
+    {
+      rendererPtr = new PrimitiveRenderer( *( mFactoryCache.Get() ) );
+      break;
+    }
     case UNDEFINED:
     default:
     {
     case UNDEFINED:
     default:
     {
index 671c752..57f5f59 100644 (file)
@@ -55,6 +55,7 @@ public:
     N_PATCH,
     SVG,
     MESH,
     N_PATCH,
     SVG,
     MESH,
+    PRIMITIVE,
     UNDEFINED
   };
 
     UNDEFINED
   };
 
index 6f55536..baca920 100644 (file)
@@ -33,10 +33,12 @@ const char * const BORDER_RENDERER("BORDER");
 const char * const GRADIENT_RENDERER("GRADIENT");
 const char * const IMAGE_RENDERER("IMAGE");
 const char * const MESH_RENDERER("MESH");
 const char * const GRADIENT_RENDERER("GRADIENT");
 const char * const IMAGE_RENDERER("IMAGE");
 const char * const MESH_RENDERER("MESH");
+const char * const PRIMITIVE_RENDERER( "PRIMITIVE" );
 const char * const DEBUG_RENDERER("DEBUG");
 
 const char * const IMAGE_URL_NAME("url");
 const char * const ATLAS_RECT_UNIFORM_NAME ( "uAtlasRect" );
 const char * const DEBUG_RENDERER("DEBUG");
 
 const char * const IMAGE_URL_NAME("url");
 const char * const ATLAS_RECT_UNIFORM_NAME ( "uAtlasRect" );
+const char * const LIGHT_POSITION_UNIFORM_NAME( "uLightPosition" );
 
 //Mesh properties
 const char * const OBJECT_URL( "objectUrl" );
 
 //Mesh properties
 const char * const OBJECT_URL( "objectUrl" );
@@ -45,6 +47,19 @@ const char * const TEXTURES_PATH( "texturesPath" );
 const char * const SHADER_TYPE( "shaderType" );
 const char * const USE_MIPMAPPING( "useMipmapping" );
 
 const char * const SHADER_TYPE( "shaderType" );
 const char * const USE_MIPMAPPING( "useMipmapping" );
 
+//Primitive shape properties
+const char * const PRIMITIVE_SHAPE( "shape" );
+const char * const SHAPE_COLOR( "color" );
+const char * const SLICES( "slices" );
+const char * const STACKS( "stacks" );
+const char * const SCALE_TOP_RADIUS( "scaleTopRadius" );
+const char * const SCALE_BOTTOM_RADIUS( "scaleBottomRadius" );
+const char * const SCALE_HEIGHT( "scaleHeight" );
+const char * const SCALE_RADIUS( "scaleRadius" );
+const char * const SCALE_DIMENSIONS( "scaleDimensions" );
+const char * const BEVEL_PERCENTAGE( "bevelPercentage" );
+const char * const BEVEL_SMOOTHNESS( "bevelSmoothness" );
+
 } // namespace Internal
 
 } // namespace Toolkit
 } // namespace Internal
 
 } // namespace Toolkit
index 5154691..ec3e161 100644 (file)
@@ -33,10 +33,12 @@ extern const char * const BORDER_RENDERER;
 extern const char * const GRADIENT_RENDERER;
 extern const char * const IMAGE_RENDERER;
 extern const char * const MESH_RENDERER;
 extern const char * const GRADIENT_RENDERER;
 extern const char * const IMAGE_RENDERER;
 extern const char * const MESH_RENDERER;
+extern const char * const PRIMITIVE_RENDERER;
 extern const char * const DEBUG_RENDERER;
 
 extern const char * const IMAGE_URL_NAME;
 extern const char * const ATLAS_RECT_UNIFORM_NAME;
 extern const char * const DEBUG_RENDERER;
 
 extern const char * const IMAGE_URL_NAME;
 extern const char * const ATLAS_RECT_UNIFORM_NAME;
+extern const char * const LIGHT_POSITION_UNIFORM_NAME;
 
 //Mesh properties
 extern const char * const OBJECT_URL;
 
 //Mesh properties
 extern const char * const OBJECT_URL;
@@ -45,6 +47,19 @@ extern const char * const TEXTURES_PATH;
 extern const char * const SHADER_TYPE;
 extern const char * const USE_MIPMAPPING;
 
 extern const char * const SHADER_TYPE;
 extern const char * const USE_MIPMAPPING;
 
+//Primitive shape properties
+extern const char * const PRIMITIVE_SHAPE;
+extern const char * const SHAPE_COLOR;
+extern const char * const SLICES;
+extern const char * const STACKS;
+extern const char * const SCALE_TOP_RADIUS;
+extern const char * const SCALE_BOTTOM_RADIUS;
+extern const char * const SCALE_HEIGHT;
+extern const char * const SCALE_RADIUS;
+extern const char * const SCALE_DIMENSIONS;
+extern const char * const BEVEL_PERCENTAGE;
+extern const char * const BEVEL_SMOOTHNESS;
+
 } // namespace Internal
 
 } // namespace Toolkit
 } // namespace Internal
 
 } // namespace Toolkit
index a33845e..b8730a6 100644 (file)
@@ -28,6 +28,7 @@ toolkit_src_files = \
    $(toolkit_src_dir)/controls/renderers/svg/svg-rasterize-thread.cpp \
    $(toolkit_src_dir)/controls/renderers/svg/svg-renderer.cpp \
    $(toolkit_src_dir)/controls/renderers/mesh/mesh-renderer.cpp \
    $(toolkit_src_dir)/controls/renderers/svg/svg-rasterize-thread.cpp \
    $(toolkit_src_dir)/controls/renderers/svg/svg-renderer.cpp \
    $(toolkit_src_dir)/controls/renderers/mesh/mesh-renderer.cpp \
+   $(toolkit_src_dir)/controls/renderers/primitive/primitive-renderer.cpp \
    $(toolkit_src_dir)/controls/alignment/alignment-impl.cpp \
    $(toolkit_src_dir)/controls/bloom-view/bloom-view-impl.cpp \
    $(toolkit_src_dir)/controls/bubble-effect/bubble-emitter-impl.cpp \
    $(toolkit_src_dir)/controls/alignment/alignment-impl.cpp \
    $(toolkit_src_dir)/controls/bloom-view/bloom-view-impl.cpp \
    $(toolkit_src_dir)/controls/bubble-effect/bubble-emitter-impl.cpp \
diff --git a/docs/content/images/renderers/bevelled-cube-high.png b/docs/content/images/renderers/bevelled-cube-high.png
new file mode 100644 (file)
index 0000000..d6676db
Binary files /dev/null and b/docs/content/images/renderers/bevelled-cube-high.png differ
diff --git a/docs/content/images/renderers/bevelled-cube-low.png b/docs/content/images/renderers/bevelled-cube-low.png
new file mode 100644 (file)
index 0000000..d76c836
Binary files /dev/null and b/docs/content/images/renderers/bevelled-cube-low.png differ
diff --git a/docs/content/images/renderers/cone.png b/docs/content/images/renderers/cone.png
new file mode 100644 (file)
index 0000000..6437a0e
Binary files /dev/null and b/docs/content/images/renderers/cone.png differ
diff --git a/docs/content/images/renderers/conical-frustrum.png b/docs/content/images/renderers/conical-frustrum.png
new file mode 100644 (file)
index 0000000..000c74b
Binary files /dev/null and b/docs/content/images/renderers/conical-frustrum.png differ
diff --git a/docs/content/images/renderers/cube.png b/docs/content/images/renderers/cube.png
new file mode 100644 (file)
index 0000000..af04b30
Binary files /dev/null and b/docs/content/images/renderers/cube.png differ
diff --git a/docs/content/images/renderers/cylinder.png b/docs/content/images/renderers/cylinder.png
new file mode 100644 (file)
index 0000000..d4efd00
Binary files /dev/null and b/docs/content/images/renderers/cylinder.png differ
diff --git a/docs/content/images/renderers/octahedron.png b/docs/content/images/renderers/octahedron.png
new file mode 100644 (file)
index 0000000..9ad5704
Binary files /dev/null and b/docs/content/images/renderers/octahedron.png differ
diff --git a/docs/content/images/renderers/slices.png b/docs/content/images/renderers/slices.png
new file mode 100644 (file)
index 0000000..8747577
Binary files /dev/null and b/docs/content/images/renderers/slices.png differ
diff --git a/docs/content/images/renderers/sphere.png b/docs/content/images/renderers/sphere.png
new file mode 100644 (file)
index 0000000..3efd4a0
Binary files /dev/null and b/docs/content/images/renderers/sphere.png differ
diff --git a/docs/content/images/renderers/stacks.png b/docs/content/images/renderers/stacks.png
new file mode 100644 (file)
index 0000000..fb3cb84
Binary files /dev/null and b/docs/content/images/renderers/stacks.png differ
index 4581cb0..010a385 100644 (file)
@@ -15,6 +15,7 @@ DALi provides the following renderers:
  + [Image](@ref image-renderers)
  + [Border](@ref border-renderer)
  + [Mesh](@ref mesh-renderer)
  + [Image](@ref image-renderers)
  + [Border](@ref border-renderer)
  + [Mesh](@ref mesh-renderer)
+ + [Primitive](@ref primitive-renderer)
  
 Controls can provide properties that allow users to specify the renderer type.
 Setting renderer properties are done via a property map.
  
 Controls can provide properties that allow users to specify the renderer type.
 Setting renderer properties are done via a property map.
@@ -472,6 +473,134 @@ map[ "texturesPath" ] = "home/images/";
 control.SetProperty( Dali::Toolkit::Control::Property::BACKGROUND, map );
 ~~~
 
 control.SetProperty( Dali::Toolkit::Control::Property::BACKGROUND, map );
 ~~~
 
+___________________________________________________________________________________________________
+
+## Primitive Renderer {#primitive-renderer}
+
+Renders a simple 3D shape, such as a cube or sphere. Scaled to fit the control.
+
+![ ](../assets/img/renderers/cube.png)
+![ ](renderers/cube.png)
+
+### Properties Supported
+
+**RendererType** "PRIMITIVE"
+
+| Property Name                         | Type    | Description                                                                     | Default Value        | Range                          |
+|---------------------------------------|:-------:|---------------------------------------------------------------------------------|:--------------------:|:------------------------------:|
+| [shape](@ref shape-details)           | STRING  | The specific shape to render.                                                   | "SPHERE"             | [See list](@ref shape-details) |
+| color                                 | VECTOR4 | The color of the shape.                                                         | (0.5, 0.5, 0.5, 1.0) | 0.0 - 1.0 for each             |
+| [slices](@ref slices-details)         | INT     | The number of slices as you go around the shape.                                | 128                  | 1 - 255                        |
+| [stacks](@ref stacks-details)         | INT     | The number of stacks as you go down the shape.                                  | 128                  | 1 - 255                        |
+| scaleTopRadius                        | FLOAT   | The scale of the radius of the top circle of a conical frustrum.                | 1.0                  | ≥ 0.0                          |
+| scaleBottomRadius                     | FLOAT   | The scale of the radius of the bottom circle of a conical frustrum.             | 1.5                  | ≥ 0.0                          |
+| scaleHeight                           | FLOAT   | The scale of the height of a conic.                                             | 3.0                  | > 0.0                          |
+| scaleRadius                           | FLOAT   | The scale of the radius of a cylinder.                                          | 1.0                  | > 0.0                          |
+| scaleDimensions                       | VECTOR3 | The dimensions of a cuboid. Scales in the same fashion as a 9-patch image.      | (1.0, 1.0, 1.0)      | > 0.0 for each                 |
+| [bevelPercentage](@ref bevel-details) | FLOAT   | Determines how bevelled the cuboid should be, based off the smallest dimension. | 0.0 (no bevel)       | 0.0 - 1.0                      |
+| bevelSmoothness                       | FLOAT   | Defines how smooth the bevelled edges should be.                                | 0.0 (sharp edges)    | 0.0 - 1.0                      |
+| uLightPosition                        | VECTOR3 | The position, in stage space, of the point light that applies lighting to the model. This is based off the stage's dimensions, so using the width and height of the stage halved will correspond to the center, and using all zeroes will place the light at the upper left back corner. Note that this corresponds to a shader property, so it can be registered and set in the actor as well. | (Offset outwards from the center of the screen.) | Unlimited |
+
+### Shapes {#shape-details}
+
+There are six shapes that can be chosen, some of which are simplified specialisations of another.
+
+| Value            | Description                                                                       | Parameters                                                    |
+|------------------|-----------------------------------------------------------------------------------|---------------------------------------------------------------|
+| SPHERE           | *Default*.                                                                        | color, slices, stacks                                         |
+| CONICAL_FRUSTRUM | The area bound between two circles, i.e. a cone with the tip removed.             | color, scaleTopRadius, scaleBottomRadius, scaleHeight, slices |
+| CONE             | Equivalent to a conical frustrum with top radius of zero.                         | color, scaleBottomRadius, scaleHeight, slices                 |
+| CYLINDER         | Equivalent to a conical frustrum with equal radii for the top and bottom circles. | color, scaleRadius, scaleHeight, slices                       |
+| CUBE             | Equivalent to a bevelled cube with a bevel percentage of zero.                    | color, scaleDimensions                                        |
+| OCTAHEDRON       | Equivalent to a bevelled cube with a bevel percentage of one.                     | color, scaleDimensions                                        |
+| BEVELLED_CUBE    | A cube/cuboid with all edges flattened to some degree.                            | color, scaleDimensions, bevelPercentage, bevelSmoothness      |
+
+Examples below:
+
+**sphere:**
+
+![ ](../assets/img/renderers/sphere.png)
+![ ](renderers/sphere.png)
+
+**conics:**
+
+| Frustrum | Cone | Cylinder |
+|----------|------|----------|
+| ![ ](../assets/img/renderers/conical-frustrum.png) ![ ](renderers/conical-frustrum.png) | ![ ](../assets/img/renderers/cone.png) ![ ](renderers/cone.png) | ![ ](../assets/img/renderers/cylinder.png) ![ ](renderers/cylinder.png) |
+
+### Bevel {#bevel-details}
+
+Bevel percentage ranges from 0.0 to 1.0. It affects the ratio of the outer face widths to the width of the overall cube, as shown:
+
+| 0.0 ( cube) | 0.3 | 0.7 | 1.0 (octahedron) |
+|-------------|-----|-----|------------------|
+| ![ ](../assets/img/renderers/cube.png) ![ ](renderers/cube.png) | ![ ](../assets/img/renderers/bevelled-cube-low.png) ![ ](renderers/bevelled-cube-low.png) | ![ ](../assets/img/renderers/bevelled-cube-high.png) ![ ](renderers/bevelled-cube-high.png) | ![ ](../assets/img/renderers/octahedron.png) ![ ](renderers/octahedron.png) |
+
+### Slices {#slices-details}
+
+For spheres and conical frustrums, 'slices' determines how many divisions there are as you go around the object. Note that spheres are rendered along the Z-axis, and so will appear rotated.
+
+![ ](../assets/img/renderers/slices.png)
+![ ](renderers/slices.png)
+
+### Stacks {#stacks-details}
+
+For spheres, 'stacks' determines how many layers there are as you go down the object. Note that spheres are rendered along the Z-axis, and so will appear rotated.
+
+![ ](../assets/img/renderers/stacks.png)
+![ ](renderers/stacks.png)
+
+### Usage
+
+**sphere**
+
+~~~{.cpp}
+// C++
+Dali::Toolkit::Control control = Dali::Toolkit::Control::New();
+
+Dali::Property::Map map;
+
+map[ "rendererType" ] = "PRIMITIVE";
+map[ "shape"        ] = "SPHERE";
+map[ "color"        ] = Vector4( 1.0, 0.5, 0.0, 1.0 );
+
+control.SetProperty( Dali::Toolkit::Control::Property::BACKGROUND, map );
+~~~
+
+**conical frustrum**
+
+~~~{.cpp}
+// C++
+Dali::Toolkit::Control control = Dali::Toolkit::Control::New();
+
+Dali::Property::Map map;
+
+map[ "rendererType"      ] = "PRIMITIVE";
+map[ "shape"             ] = "CONICAL_FRUSTRUM";
+map[ "color"             ] = Vector4( 1.0, 0.5, 0.0, 1.0 );
+map[ "scaleTopRadius"    ] = 1.0f;
+map[ "scaleBottomRadius" ] = 1.5f;
+map[ "scaleHeight"       ] = 3.0f;
+
+control.SetProperty( Dali::Toolkit::Control::Property::BACKGROUND, map );
+~~~
+
+**bevelled cube**
+
+~~~{.cpp}
+// C++
+Dali::Toolkit::Control control = Dali::Toolkit::Control::New();
+
+Dali::Property::Map map;
+
+map[ "rendererType"    ] = "PRIMITIVE";
+map[ "shape"           ] = "BEVELLED_CUBE";
+map[ "color"           ] = Vector4( 1.0, 0.5, 0.0, 1.0 );
+map[ "bevelPercentage" ] = 0.4f;
+
+control.SetProperty( Dali::Toolkit::Control::Property::BACKGROUND, map );
+~~~
+
 @class _Guide_Control_Renderers
 
 */
 @class _Guide_Control_Renderers
 
 */