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