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