1 #ifndef DALI_TOOLKIT_INTERNAL_PRIMITIVE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_PRIMITIVE_VISUAL_H
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * The geometry creation logic was based off similar methods provided by freeGLUT.
23 * Original copyright and licence information:
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
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:
36 * The above copyright notice and this permission notice shall be included
37 * in all copies or substantial portions of the Software.
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.
48 #include <dali/public-api/common/intrusive-ptr.h>
51 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
52 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
63 class PrimitiveVisual;
64 typedef IntrusivePtr< PrimitiveVisual > PrimitiveVisualPtr;
67 * The visual which renders a simple 3D shape to the control's quad
69 * Primitives are created with clockwise winding and back-face culling by default.
71 * The following properties are required to create a PrimitiveRender
73 * | %Property Name | Type |
74 * |-----------------|-------------|
77 * In addition, the following properties can be (optionally) supplied to modify the shape's parameters
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 |
92 * Note: slices and stacks both have an upper limit of 255.
94 * Finally, the following can be used to affect the visual's shader
96 * | %Property Name | Type | Representing |
97 * |-----------------|-------------|-----------------------------------------|
98 * | lightPosition | VECTOR3 | The position (on stage) of the light |
100 class PrimitiveVisual: public Visual::Base
105 * @brief Create a new primitive visual.
107 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
108 * @param[in] properties A Property::Map containing settings for this visual
109 * @return A smart-pointer to the newly allocated visual.
111 static PrimitiveVisualPtr New( VisualFactoryCache& factoryCache, const Property::Map& properties );
113 public: // from Visual
116 * @copydoc Visual::Base::GetNaturalSize
118 virtual void GetNaturalSize( Vector2& naturalSize );
121 * @copydoc Visual::Base::CreatePropertyMap
123 virtual void DoCreatePropertyMap( Property::Map& map ) const;
128 * @brief Constructor.
130 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
132 PrimitiveVisual( VisualFactoryCache& factoryCache );
135 * @brief A reference counted object may only be deleted by calling Unreference().
137 virtual ~PrimitiveVisual();
140 * @copydoc Visual::Base::DoSetProperties
142 virtual void DoSetProperties( const Property::Map& propertyMap );
145 * @copydoc Visual::Base::DoSetOnStage
147 virtual void DoSetOnStage( Actor& actor );
150 * @copydoc Visual::Base::OnSetTransform
152 virtual void OnSetTransform();
156 //Simple struct to store the position and normal of a single vertex.
162 Vertex( const Vector3& position, const Vector3& normal, const Vector2& textureCoord )
163 : position( position ), normal( normal )
171 * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
173 void InitializeRenderer();
176 * @brief Create a shader for the object to use.
181 * @brief Update shader related info, uniforms, etc. for the new shader.
183 void UpdateShaderUniforms();
186 * @brief Create the geometry of the given primitive type.
188 void CreateGeometry();
191 * @brief Compute the vertices and the triangles for a sphere.
192 * @param[in, out] vertices The vector of vertices.
193 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
194 * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
195 * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
197 void CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks );
200 * @brief Compute the vertices and the triangles for a conic shape.
201 * @param[in, out] vertices The vector of vertices.
202 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
203 * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
204 * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
205 * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
206 * @param[in] slices The number of slices as you go around the conic shape. Affects the smoothness of the surface.
208 void CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
209 float scaleBottomRadius, float scaleHeight, int slices );
212 * @brief Compute the vertices and the triangles for a bevelled cube.
213 * @param[in, out] vertices The vector of vertices.
214 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
215 * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
216 * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
217 * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
219 void CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices, Vector3 dimensions,
220 float bevelPercentage, float bevelSmoothness );
223 * @brief Computes look-up tables for sin and cos, over angle divisions of (2 * Pi) / divisions
224 * @param[in, out] sinTable The table of sin values.
225 * @param[in, out] cosTable The table of cos values.
226 * @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)
227 * @Param[in] halfCircle If true, go from 0 to Pi instead of 0 to 2Pi.
229 void ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions, bool halfCircle );
232 * @brief Compute the vertices for a sphere.
233 * @param[in, out] vertices The vector of vertices.
234 * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
235 * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
237 void ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks );
240 * @brief Compute the triangles for a sphere.
241 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
242 * @param[in] slices The number of slices as you go around the sphere. Affects the smoothness of the surface.
243 * @param[in] stacks The number of stacks as you go down the sphere. Affects the smoothness of the surface.
245 void FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks );
248 * @brief Compute the vertices for a conical.
249 * @param[in, out] vertices The vector of vertices.
250 * @param[in] scaleTopRadius The scale of the radius of the top circle, compared to the other dimensions.
251 * @param[in] scaleBottomRadius The scale of the radius of the bottom circle, compared to the other dimensions.
252 * @param[in] scaleHeight The scale of the height of the object, compared to the other dimensions.
253 * @param[in] slices The number of slices as you go around the conical. Affects the smoothness of the surface.
255 void ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius, float scaleBottomRadius,
256 float scaleHeight, int slices );
259 * @brief Compute the triangles for a conic.
260 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
261 * @param[in] coneTop True if the top circle has a radius of zero, i.e. the object is a complete cone.
262 * @param[in] coneBottom True if the bottom circle has a radius of zero, i.e. the object is an inverted complete cone.
263 * @param[in] slices The number of slices as you go around the conic. Affects the smoothness of the surface.
265 void FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius,
269 * @brief Compute the vertices for a cube.
270 * @param[in, out] vertices The vector of vertices.
271 * @Param[in] dimensions The dimensions of the object.
273 void ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions );
276 * @brief Compute the triangles for a cube.
277 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
279 void FormCubeTriangles( Vector<unsigned short>& indices );
282 * @brief Compute the vertices for an octahedron (maximumly bevelled cube).
283 * @param[in, out] vertices The vector of vertices.
284 * @Param[in] dimensions The dimensions of the object.
285 * @Param[in] smoothness Defines how rounded the edges appear under lighting. Between 0.0 and 1.0.
287 void ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness );
290 * @brief Compute the triangles for an octahedron.
291 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
293 void FormOctahedronTriangles( Vector<unsigned short>& indices );
296 * @brief Compute the vertices for a bevelled cube.
297 * @param[in, out] vertices The vector of vertices.
298 * @Param[in] dimensions The dimensions of the object. Scales in the same fashion as a 9-patch image.
299 * @param[in] bevelPercentage The ratio of the outer face widths to the cube's width. Between 0.0 and 1.0.
300 * @param[in] bevelSmoothness The smoothness of the bevelled edges. Between 0.0 and 1.0.
302 void ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions, float bevelPercentage,
303 float bevelSmoothness );
306 * @brief Compute the triangles for a bevelled cube.
307 * @param[in, out] indices The vector of triangles, consisting of groups of three vertex indices.
309 void FormBevelledCubeTriangles( Vector<unsigned short>& indices );
314 PrimitiveVisual( const PrimitiveVisual& PrimitiveVisual );
317 PrimitiveVisual& operator=( const PrimitiveVisual& PrimitiveVisual );
323 Vector3 mObjectDimensions; //Dimensions of shape, scaled to be between 0.0 and 1.0.
325 Vector3 mSceneCenter;
329 Vector3 mLightPosition;
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.
342 Toolkit::PrimitiveVisual::Shape::Type mPrimitiveType; //Shape to render, as enum.
345 } // namespace Internal
347 } // namespace Toolkit
351 #endif /* DALI_TOOLKIT_INTERNAL_PRIMITIVE_VISUAL_H */