Normal-less objects now have their normals calculated so that they can be displayed...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / mesh / mesh-renderer.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_MESH_RENDERER_H__
2 #define __DALI_TOOLKIT_INTERNAL_MESH_RENDERER_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 // EXTERNAL INCLUDES
22 #include <fstream>
23 #include <string.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/controls/renderers/control-renderer-impl.h>
27 #include <dali-toolkit/internal/controls/model3d-view/obj-loader.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 /**
39  * The renderer which renders a 3D object to the control's quad
40  *
41  * The following Property::Map keys are required to create a MeshRender
42  *
43  * | %Property Name  | Type        | Representing                                                          |
44  * |-----------------|-------------|-----------------------------------------------------------------------|
45  * | objectUrl       | STRING      | A URL to the .obj file                                                |
46  * | materialUrl     | STRING      | A URL to the .mtl file                                                |
47  * | texturesPath    | STRING      | A URL of the path to the texture images                               |
48  * | shaderType      | STRING      | An enum of shader types                                               |
49  * | useMipmapping   | BOOLEAN     | If true, use mipmaps for textures. Default true.                      |
50  * | useSoftNormals  | BOOLEAN     | If true, average normals at points for smooth textures. Default true. |
51  */
52 class MeshRenderer: public ControlRenderer
53 {
54 public:
55
56   /**
57    * @brief Constructor.
58    *
59    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
60    */
61   MeshRenderer( RendererFactoryCache& factoryCache );
62
63   /**
64    * @brief A reference counted object may only be deleted by calling Unreference().
65    */
66   virtual ~MeshRenderer();
67
68 public:  // from ControlRenderer
69
70   /**
71    * @copydoc ControlRenderer::SetSize
72    */
73   virtual void SetSize( const Vector2& size );
74
75   /**
76    * @copydoc ControlRenderer::SetClipRect
77    */
78   virtual void SetClipRect( const Rect<int>& clipRect );
79
80   /**
81    * @copydoc ControlRenderer::SetOffset
82    */
83   virtual void SetOffset( const Vector2& offset );
84
85   /**
86    * @copydoc ControlRenderer::CreatePropertyMap
87    */
88   virtual void DoCreatePropertyMap( Property::Map& map ) const;
89
90 protected:
91
92   /**
93    * @copydoc ControlRenderer::DoInitialize
94    */
95   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
96
97   /**
98    * @copydoc ControlRenderer::DoSetOnStage
99    */
100   virtual void DoSetOnStage( Actor& actor );
101
102 public:
103
104   /**
105    * Declare whether a texture map should be used for the object, if it's present. Defaults to true.
106    * @param[in] useTexture boolean declaration.
107    */
108   void SetUseTexture( bool useTexture );
109
110   /**
111    * Declare whether a normal map should be used for the object, if it's present. Defaults to true.
112    * @param[in] useNormalMap boolean declaration.
113    */
114   void SetUseNormalMap( bool useNormalMap );
115
116 private:
117
118   //Corresponds to the shader that will be used by the mesh renderer.
119   enum ShaderType
120   {
121     TEXTURELESS,
122     DIFFUSE_TEXTURE,
123     ALL_TEXTURES
124   };
125
126   /**
127    * @brief Provide an empty geometry for the renderer to use.
128    * @details For use in error cases where the initialisation has failed for varying reasons.
129    */
130   void SupplyEmptyGeometry();
131
132   /**
133    * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
134    */
135   void InitializeRenderer();
136
137   /**
138    * @brief Create a shader for the object to use.
139    */
140   void CreateShader();
141
142   /**
143    * @brief Update shader related info, uniforms, etc. for the new shader.
144    */
145   void UpdateShaderUniforms();
146
147   /**
148    * @brief Use the object URL stored in the renderer to load and create the geometry of the object.
149    * @return Boolean of success of operation.
150    */
151   bool CreateGeometry();
152
153   /**
154    * @brief Use the object URL stored in the renderer to load the geometry of the object.
155    * @return Boolean of success of operation.
156    */
157   bool LoadGeometry();
158
159   /**
160    * @brief Use the material URL stored in the renderer to load the material of the object.
161    * @return Boolean of success of operation.
162    */
163   bool LoadMaterial();
164
165   /**
166    * @brief Use the image and texture URL components to load the different types of texture.
167    * @return Boolean of success of operation. Returns false if any texture fails to load from a url.
168    */
169   bool LoadTextures();
170
171 private:
172
173   // Undefined
174   MeshRenderer( const MeshRenderer& meshRenderer );
175
176   // Undefined
177   MeshRenderer& operator=( const MeshRenderer& meshRenderer );
178
179 private:
180
181   std::string mObjectUrl;
182   std::string mMaterialUrl;
183
184   std::string mDiffuseTextureUrl;
185   std::string mNormalTextureUrl;
186   std::string mGlossTextureUrl;
187   std::string mTexturesPath;
188
189   Shader mShader;
190   Geometry mGeometry;
191   TextureSet mTextureSet;
192
193   ObjLoader mObjLoader;
194   Vector3 mSceneCenter;
195   Vector3 mSceneSize;
196   ShaderType mShaderType;
197
198   bool mUseTexture;
199   bool mUseMipmapping;
200   bool mUseSoftNormals;
201 };
202
203 } // namespace Internal
204
205 } // namespace Toolkit
206
207 } // namespace Dali
208
209 #endif /* __DALI_TOOLKIT_INTERNAL_MESH_RENDERER_H__ */