Added support for setting light position in mesh renderer.
[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  * | lightPosition   | VECTOR3     | The position (on stage) of the light                                  |
52  */
53 class MeshRenderer: public ControlRenderer
54 {
55 public:
56
57   /**
58    * @brief Constructor.
59    *
60    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
61    */
62   MeshRenderer( RendererFactoryCache& factoryCache );
63
64   /**
65    * @brief A reference counted object may only be deleted by calling Unreference().
66    */
67   virtual ~MeshRenderer();
68
69 public:  // from ControlRenderer
70
71   /**
72    * @copydoc ControlRenderer::SetSize
73    */
74   virtual void SetSize( const Vector2& size );
75
76   /**
77    * @copydoc ControlRenderer::SetClipRect
78    */
79   virtual void SetClipRect( const Rect<int>& clipRect );
80
81   /**
82    * @copydoc ControlRenderer::SetOffset
83    */
84   virtual void SetOffset( const Vector2& offset );
85
86   /**
87    * @copydoc ControlRenderer::CreatePropertyMap
88    */
89   virtual void DoCreatePropertyMap( Property::Map& map ) const;
90
91 protected:
92
93   /**
94    * @copydoc ControlRenderer::DoInitialize
95    */
96   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
97
98   /**
99    * @copydoc ControlRenderer::DoSetOnStage
100    */
101   virtual void DoSetOnStage( Actor& actor );
102
103 public:
104
105   /**
106    * Declare whether a texture map should be used for the object, if it's present. Defaults to true.
107    * @param[in] useTexture boolean declaration.
108    */
109   void SetUseTexture( bool useTexture );
110
111   /**
112    * Declare whether a normal map should be used for the object, if it's present. Defaults to true.
113    * @param[in] useNormalMap boolean declaration.
114    */
115   void SetUseNormalMap( bool useNormalMap );
116
117 private:
118
119   //Corresponds to the shader that will be used by the mesh renderer.
120   enum ShaderType
121   {
122     TEXTURELESS,
123     DIFFUSE_TEXTURE,
124     ALL_TEXTURES
125   };
126
127   /**
128    * @brief Provide an empty geometry for the renderer to use.
129    * @details For use in error cases where the initialisation has failed for varying reasons.
130    */
131   void SupplyEmptyGeometry();
132
133   /**
134    * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
135    */
136   void InitializeRenderer();
137
138   /**
139    * @brief Create a shader for the object to use.
140    */
141   void CreateShader();
142
143   /**
144    * @brief Update shader related info, uniforms, etc. for the new shader.
145    */
146   void UpdateShaderUniforms();
147
148   /**
149    * @brief Use the object URL stored in the renderer to load and create the geometry of the object.
150    * @return Boolean of success of operation.
151    */
152   bool CreateGeometry();
153
154   /**
155    * @brief Use the object URL stored in the renderer to load the geometry of the object.
156    * @return Boolean of success of operation.
157    */
158   bool LoadGeometry();
159
160   /**
161    * @brief Use the material URL stored in the renderer to load the material of the object.
162    * @return Boolean of success of operation.
163    */
164   bool LoadMaterial();
165
166   /**
167    * @brief Use the image and texture URL components to load the different types of texture.
168    * @return Boolean of success of operation. Returns false if any texture fails to load from a url.
169    */
170   bool LoadTextures();
171
172 private:
173
174   // Undefined
175   MeshRenderer( const MeshRenderer& meshRenderer );
176
177   // Undefined
178   MeshRenderer& operator=( const MeshRenderer& meshRenderer );
179
180 private:
181
182   std::string mObjectUrl;
183   std::string mMaterialUrl;
184
185   std::string mDiffuseTextureUrl;
186   std::string mNormalTextureUrl;
187   std::string mGlossTextureUrl;
188   std::string mTexturesPath;
189
190   Shader mShader;
191   Geometry mGeometry;
192   TextureSet mTextureSet;
193
194   ObjLoader mObjLoader;
195   Vector3 mSceneCenter;
196   Vector3 mSceneSize;
197
198   Vector3 mLightPosition;
199   ShaderType mShaderType;
200
201   bool mUseTexture;
202   bool mUseMipmapping;
203   bool mUseSoftNormals;
204 };
205
206 } // namespace Internal
207
208 } // namespace Toolkit
209
210 } // namespace Dali
211
212 #endif /* __DALI_TOOLKIT_INTERNAL_MESH_RENDERER_H__ */