Updated primitive shape generation to allow for back face culling.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / primitive / primitive-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_PRIMITIVE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_PRIMITIVE_VISUAL_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 /*
22  * The geometry creation logic was based off similar methods provided by freeGLUT.
23  * Original copyright and licence information:
24  *
25  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
26  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
27  * Creation date: Fri Dec 3 1999
28  *
29  * Permission is hereby granted, free of charge, to any person obtaining a
30  * copy of this software and associated documentation files (the "Software"),
31  * to deal in the Software without restriction, including without limitation
32  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
33  * and/or sell copies of the Software, and to permit persons to whom the
34  * Software is furnished to do so, subject to the following conditions:
35  *
36  * The above copyright notice and this permission notice shall be included
37  * in all copies or substantial portions of the Software.
38  *
39  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
42  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
43  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45  */
46
47
48 // INTERNAL INCLUDES
49 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
50
51 namespace Dali
52 {
53
54 namespace Toolkit
55 {
56
57 namespace Internal
58 {
59
60 /**
61  * The visual which renders a simple 3D shape to the control's quad
62  *
63  * Primitives are created with clockwise winding and back-face culling by default.
64  *
65  * The following properties are required to create a PrimitiveRender
66  *
67  * | %Property Name  | Type        |
68  * |-----------------|-------------|
69  * | shape           | STRING      |
70  *
71  * In addition, the following properties can be (optionally) supplied to modify the shape's parameters
72  *
73  * | %Property Name    | Type        | Shapes Affected                          |
74  * |-------------------|-------------|------------------------------------------|
75  * | shapeColor        | VECTOR4     | all                                      |
76  * | slices            | INTEGER     | sphere, cone, conical frustrum, cylinder |
77  * | stacks            | INTEGER     | sphere                                   |
78  * | scaleTopRadius    | FLOAT       | conical frustrum                         |
79  * | scaleBottomRadius | FLOAT       | cone, conical frustrum                   |
80  * | scaleHeight       | FLOAT       | cone, conical frustrum, cylinder         |
81  * | scaleRadius       | FLOAT       | cylinder                                 |
82  * | scaleDimensions   | VECTOR3     | cube, octahedron, bevelled cube          |
83  * | bevelPercentage   | FLOAT       | bevelled cube                            |
84  * | bevelSmoothness   | FLOAT       | bevelled cube                            |
85  *
86  * Note: slices and stacks both have an upper limit of 255.
87  *
88  * Finally, the following can be used to affect the visual's shader
89  *
90  * | %Property Name  | Type        | Representing                            |
91  * |-----------------|-------------|-----------------------------------------|
92  * | lightPosition   | VECTOR3     | The position (on stage) of the light    |
93  */
94 class PrimitiveVisual: public Visual::Base
95 {
96 public:
97
98   /**
99    * @brief Constructor.
100    *
101    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
102    */
103   PrimitiveVisual( VisualFactoryCache& factoryCache );
104
105   /**
106    * @brief A reference counted object may only be deleted by calling Unreference().
107    */
108   virtual ~PrimitiveVisual();
109
110 public:  // from Visual
111
112   /**
113    * @copydoc Visual::SetSize
114    */
115   virtual void SetSize( const Vector2& size );
116
117   /**
118    * @copydoc Visual::SetClipRect
119    */
120   virtual void SetClipRect( const Rect<int>& clipRect );
121
122   /**
123    * @copydoc Visual::SetOffset
124    */
125   virtual void SetOffset( const Vector2& offset );
126
127   /**
128    * @copydoc Visual::CreatePropertyMap
129    */
130   virtual void DoCreatePropertyMap( Property::Map& map ) const;
131
132 protected:
133
134   /**
135    * @copydoc Visual::DoInitialize
136    */
137   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
138
139   /**
140    * @copydoc Visual::DoSetOnStage
141    */
142   virtual void DoSetOnStage( Actor& actor );
143
144 private:
145
146   enum PrimitiveType
147   {
148     SPHERE,
149     CONE,
150     CONICAL_FRUSTRUM,
151     CYLINDER,
152     CUBE,
153     OCTAHEDRON,
154     BEVELLED_CUBE
155   };
156
157   //Simple struct to store the position and normal of a single vertex.
158   struct Vertex
159   {
160     Vertex()
161     {}
162
163     Vertex( const Vector3& position, const Vector3& normal, const Vector2& textureCoord )
164     : position( position ), normal( normal )
165     {}
166
167     Vector3 position;
168     Vector3 normal;
169   };
170
171   /**
172    * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
173    */
174   void InitializeRenderer();
175
176   /**
177      * @brief Create a shader for the object to use.
178      */
179   void CreateShader();
180
181   /**
182    * @brief Update shader related info, uniforms, etc. for the new shader.
183    */
184   void UpdateShaderUniforms();
185
186   /**
187    * @brief Create the geometry of the given primitive type.
188    */
189   void CreateGeometry();
190
191   /**
192    * @brief Compute the vertices and the triangles for a sphere.
193    * @param[in, out] vertices The vector of vertices.
194    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
195    * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
196    * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
197    */
198   void CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks );
199
200   /**
201    * @brief Compute the vertices and the triangles for a conic shape.
202    * @param[in, out] vertices The vector of vertices.
203    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
204    * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
205    * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
206    * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
207    * @param[in] slices The number of slices as you go around the conic shape. Affects the smoothness of the surface.
208    */
209   void CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
210                            float scaleBottomRadius, float scaleHeight, int slices );
211
212   /**
213    * @brief Compute the vertices and the triangles for a bevelled cube.
214    * @param[in, out] vertices The vector of vertices.
215    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
216    * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
217    * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
218    * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
219    */
220   void CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices, Vector3 dimensions,
221                            float bevelPercentage, float bevelSmoothness );
222
223   /**
224    * @brief Computes look-up tables for sin and cos, over angle divisions of (2 * Pi) / divisions
225    * @param[in, out] sinTable The table of sin values.
226    * @param[in, out] cosTable The table of cos values.
227    * @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)
228    * @Param[in] halfCircle If true, go from 0 to Pi instead of 0 to 2Pi.
229    */
230   void ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions, bool halfCircle );
231
232   /**
233    * @brief Compute the vertices for a sphere.
234    * @param[in, out] vertices The vector of vertices.
235    * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
236    * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
237    */
238   void ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks );
239
240   /**
241    * @brief Compute the triangles for a sphere.
242    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
243    * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
244    * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
245    */
246   void FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks );
247
248   /**
249    * @brief Compute the vertices for a conical.
250    * @param[in, out] vertices The vector of vertices.
251    * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
252    * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
253    * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
254    * @param[in] slices The number of slices as you go around the conical. Affects the smoothness of the surface.
255    */
256   void ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius, float scaleBottomRadius,
257                                     float scaleHeight, int slices );
258
259   /**
260    * @brief Compute the triangles for a conic.
261    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
262    * @param[in] coneTop True if the top circle has a radius of zero, i.e. the object is a complete cone.
263    * @param[in] coneBottom True if the bottom circle has a radius of zero, i.e. the object is an inverted complete cone.
264    * @param[in] slices The number of slices as you go around the conic. Affects the smoothness of the surface.
265    */
266   void FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius,
267                                   int slices );
268
269   /**
270    * @brief Compute the vertices for a cube.
271    * @param[in, out] vertices The vector of vertices.
272    * @Param[in] dimensions The dimensions of the object.
273    */
274   void ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions );
275
276   /**
277    * @brief Compute the triangles for a cube.
278    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
279    */
280   void FormCubeTriangles( Vector<unsigned short>& indices );
281
282   /**
283    * @brief Compute the vertices for an octahedron (maximumly bevelled cube).
284    * @param[in, out] vertices The vector of vertices.
285    * @Param[in] dimensions The dimensions of the object.
286    * @Param[in] smoothness Defines how rounded the edges appear under lighting. Between 0.0 and 1.0.
287    */
288   void ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness );
289
290   /**
291    * @brief Compute the triangles for an octahedron.
292    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
293    */
294   void FormOctahedronTriangles( Vector<unsigned short>& indices );
295
296   /**
297    * @brief Compute the vertices for a bevelled cube.
298    * @param[in, out] vertices The vector of vertices.
299    * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
300    * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
301    * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
302    */
303   void ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions, float bevelPercentage,
304                                     float bevelSmoothness );
305
306   /**
307    * @brief Compute the triangles for a bevelled cube.
308    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
309    */
310   void FormBevelledCubeTriangles( Vector<unsigned short>& indices );
311
312 private:
313
314   // Undefined
315   PrimitiveVisual( const PrimitiveVisual& PrimitiveVisual );
316
317   // Undefined
318   PrimitiveVisual& operator=( const PrimitiveVisual& PrimitiveVisual );
319
320 private:
321   Shader mShader;
322   Geometry mGeometry;
323
324   std::string mShape;            //Shape to render, as string.
325   Vector4 mColor;                //Color of shape.
326   Vector3 mObjectDimensions;     //Dimensions of shape, scaled to be between 0.0 and 1.0.
327
328   Vector3 mSceneCenter;
329   Vector3 mSceneSize;
330
331   //Shader properties.
332   Vector3 mLightPosition;
333
334   //Shape properties.
335   Vector3 mScaleDimensions;      ///< Scale of dimensions of bevelled cube and sub-shapes.
336   float   mScaleTopRadius;       ///< Scale of radius of top circle, to use when creating certain objects.
337   float   mScaleBottomRadius;    ///< Scale of radius of bottom circle, to use when creating certain objects.
338   float   mScaleHeight;          ///< Scale of height, to use when creating certain objects.
339   float   mScaleRadius;          ///< Scale of radius, to use when creating certain objects.
340   float   mBevelPercentage;      ///< Used to determine bevel amount when creating certain objects.
341   float   mBevelSmoothness;      ///< Used to determine the smoothness of bevelled edges.
342   int     mSlices;               ///< Number of slices to use when creating certain objects.
343   int     mStacks;               ///< Number of stacks to use when creating certain objects.
344
345   PrimitiveType mPrimitiveType;  //Shape to render, as enum.
346 };
347
348 } // namespace Internal
349
350 } // namespace Toolkit
351
352 } // namespace Dali
353
354 #endif /* DALI_TOOLKIT_INTERNAL_PRIMITIVE_VISUAL_H */