Primitive visual bevel fix.
[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/public-api/visuals/primitive-visual-properties.h>
50 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
51
52 namespace Dali
53 {
54
55 namespace Toolkit
56 {
57
58 namespace Internal
59 {
60
61 /**
62  * The visual which renders a simple 3D shape to the control's quad
63  *
64  * Primitives are created with clockwise winding and back-face culling by default.
65  *
66  * The following properties are required to create a PrimitiveRender
67  *
68  * | %Property Name  | Type        |
69  * |-----------------|-------------|
70  * | shape           | STRING      |
71  *
72  * In addition, the following properties can be (optionally) supplied to modify the shape's parameters
73  *
74  * | %Property Name    | Type        | Shapes Affected                          |
75  * |-------------------|-------------|------------------------------------------|
76  * | shapeColor        | VECTOR4     | all                                      |
77  * | slices            | INTEGER     | sphere, cone, conical frustrum, cylinder |
78  * | stacks            | INTEGER     | sphere                                   |
79  * | scaleTopRadius    | FLOAT       | conical frustrum                         |
80  * | scaleBottomRadius | FLOAT       | cone, conical frustrum                   |
81  * | scaleHeight       | FLOAT       | cone, conical frustrum, cylinder         |
82  * | scaleRadius       | FLOAT       | cylinder                                 |
83  * | scaleDimensions   | VECTOR3     | cube, octahedron, bevelled cube          |
84  * | bevelPercentage   | FLOAT       | bevelled cube                            |
85  * | bevelSmoothness   | FLOAT       | bevelled cube                            |
86  *
87  * Note: slices and stacks both have an upper limit of 255.
88  *
89  * Finally, the following can be used to affect the visual's shader
90  *
91  * | %Property Name  | Type        | Representing                            |
92  * |-----------------|-------------|-----------------------------------------|
93  * | lightPosition   | VECTOR3     | The position (on stage) of the light    |
94  */
95 class PrimitiveVisual: public Visual::Base
96 {
97 public:
98
99   /**
100    * @brief Constructor.
101    *
102    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
103    */
104   PrimitiveVisual( VisualFactoryCache& factoryCache );
105
106   /**
107    * @brief A reference counted object may only be deleted by calling Unreference().
108    */
109   virtual ~PrimitiveVisual();
110
111 public:  // from Visual
112
113   /**
114    * @copydoc Visual::Base::SetSize
115    */
116   virtual void SetSize( const Vector2& size );
117
118   /**
119    * @copydoc Visual::Base::GetNaturalSize
120    */
121   virtual void GetNaturalSize( Vector2& naturalSize ) const;
122
123   /**
124    * @copydoc Visual::Base::CreatePropertyMap
125    */
126   virtual void DoCreatePropertyMap( Property::Map& map ) const;
127
128 protected:
129
130   /**
131    * @copydoc Visual::Base::DoInitialize
132    */
133   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
134
135   /**
136    * @copydoc Visual::Base::DoSetOnStage
137    */
138   virtual void DoSetOnStage( Actor& actor );
139
140 private:
141
142   //Simple struct to store the position and normal of a single vertex.
143   struct Vertex
144   {
145     Vertex()
146     {}
147
148     Vertex( const Vector3& position, const Vector3& normal, const Vector2& textureCoord )
149     : position( position ), normal( normal )
150     {}
151
152     Vector3 position;
153     Vector3 normal;
154   };
155
156   /**
157    * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
158    */
159   void InitializeRenderer();
160
161   /**
162      * @brief Create a shader for the object to use.
163      */
164   void CreateShader();
165
166   /**
167    * @brief Update shader related info, uniforms, etc. for the new shader.
168    */
169   void UpdateShaderUniforms();
170
171   /**
172    * @brief Create the geometry of the given primitive type.
173    */
174   void CreateGeometry();
175
176   /**
177    * @brief Compute the vertices and the triangles for a sphere.
178    * @param[in, out] vertices The vector of vertices.
179    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
180    * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
181    * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
182    */
183   void CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks );
184
185   /**
186    * @brief Compute the vertices and the triangles for a conic shape.
187    * @param[in, out] vertices The vector of vertices.
188    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
189    * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
190    * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
191    * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
192    * @param[in] slices The number of slices as you go around the conic shape. Affects the smoothness of the surface.
193    */
194   void CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
195                            float scaleBottomRadius, float scaleHeight, int slices );
196
197   /**
198    * @brief Compute the vertices and the triangles for a bevelled cube.
199    * @param[in, out] vertices The vector of vertices.
200    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
201    * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
202    * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
203    * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
204    */
205   void CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices, Vector3 dimensions,
206                            float bevelPercentage, float bevelSmoothness );
207
208   /**
209    * @brief Computes look-up tables for sin and cos, over angle divisions of (2 * Pi) / divisions
210    * @param[in, out] sinTable The table of sin values.
211    * @param[in, out] cosTable The table of cos values.
212    * @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)
213    * @Param[in] halfCircle If true, go from 0 to Pi instead of 0 to 2Pi.
214    */
215   void ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions, bool halfCircle );
216
217   /**
218    * @brief Compute the vertices for a sphere.
219    * @param[in, out] vertices The vector of vertices.
220    * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
221    * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
222    */
223   void ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks );
224
225   /**
226    * @brief Compute the triangles for a sphere.
227    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
228    * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
229    * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
230    */
231   void FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks );
232
233   /**
234    * @brief Compute the vertices for a conical.
235    * @param[in, out] vertices The vector of vertices.
236    * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
237    * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
238    * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
239    * @param[in] slices The number of slices as you go around the conical. Affects the smoothness of the surface.
240    */
241   void ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius, float scaleBottomRadius,
242                                     float scaleHeight, int slices );
243
244   /**
245    * @brief Compute the triangles for a conic.
246    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
247    * @param[in] coneTop True if the top circle has a radius of zero, i.e. the object is a complete cone.
248    * @param[in] coneBottom True if the bottom circle has a radius of zero, i.e. the object is an inverted complete cone.
249    * @param[in] slices The number of slices as you go around the conic. Affects the smoothness of the surface.
250    */
251   void FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius,
252                                   int slices );
253
254   /**
255    * @brief Compute the vertices for a cube.
256    * @param[in, out] vertices The vector of vertices.
257    * @Param[in] dimensions The dimensions of the object.
258    */
259   void ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions );
260
261   /**
262    * @brief Compute the triangles for a cube.
263    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
264    */
265   void FormCubeTriangles( Vector<unsigned short>& indices );
266
267   /**
268    * @brief Compute the vertices for an octahedron (maximumly bevelled cube).
269    * @param[in, out] vertices The vector of vertices.
270    * @Param[in] dimensions The dimensions of the object.
271    * @Param[in] smoothness Defines how rounded the edges appear under lighting. Between 0.0 and 1.0.
272    */
273   void ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness );
274
275   /**
276    * @brief Compute the triangles for an octahedron.
277    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
278    */
279   void FormOctahedronTriangles( Vector<unsigned short>& indices );
280
281   /**
282    * @brief Compute the vertices for a bevelled cube.
283    * @param[in, out] vertices The vector of vertices.
284    * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
285    * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
286    * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
287    */
288   void ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions, float bevelPercentage,
289                                     float bevelSmoothness );
290
291   /**
292    * @brief Compute the triangles for a bevelled cube.
293    * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
294    */
295   void FormBevelledCubeTriangles( Vector<unsigned short>& indices );
296
297 private:
298
299   // Undefined
300   PrimitiveVisual( const PrimitiveVisual& PrimitiveVisual );
301
302   // Undefined
303   PrimitiveVisual& operator=( const PrimitiveVisual& PrimitiveVisual );
304
305 private:
306   Shader mShader;
307   Geometry mGeometry;
308
309   Vector4 mColor;                //Color of shape.
310   Vector3 mObjectDimensions;     //Dimensions of shape, scaled to be between 0.0 and 1.0.
311
312   Vector3 mSceneCenter;
313   Vector3 mSceneSize;
314
315   //Shader properties.
316   Vector3 mLightPosition;
317
318   //Shape properties.
319   Vector3 mScaleDimensions;      ///< Scale of dimensions of bevelled cube and sub-shapes.
320   float   mScaleTopRadius;       ///< Scale of radius of top circle, to use when creating certain objects.
321   float   mScaleBottomRadius;    ///< Scale of radius of bottom circle, to use when creating certain objects.
322   float   mScaleHeight;          ///< Scale of height, to use when creating certain objects.
323   float   mScaleRadius;          ///< Scale of radius, to use when creating certain objects.
324   float   mBevelPercentage;      ///< Used to determine bevel amount when creating certain objects.
325   float   mBevelSmoothness;      ///< Used to determine the smoothness of bevelled edges.
326   int     mSlices;               ///< Number of slices to use when creating certain objects.
327   int     mStacks;               ///< Number of stacks to use when creating certain objects.
328
329   Toolkit::PrimitiveVisual::Shape::Type mPrimitiveType;  //Shape to render, as enum.
330 };
331
332 } // namespace Internal
333
334 } // namespace Toolkit
335
336 } // namespace Dali
337
338 #endif /* DALI_TOOLKIT_INTERNAL_PRIMITIVE_VISUAL_H */