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