2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "mesh-renderer.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
26 #include <dali/devel-api/adaptor-framework/file-loader.h>
30 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
31 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
32 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
33 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
41 * @brief Loads a texture from a file
42 * @param[in] imageUrl The url of the file
43 * @param[in] generateMipmaps Indicates whether to generate mipmaps for the texture
44 * @return A texture if loading succeeds, an empty handle otherwise
46 Texture LoadTexture( const char* imageUrl, bool generateMipmaps )
49 Dali::BitmapLoader loader = Dali::BitmapLoader::New( imageUrl );
51 PixelData pixelData = loader.GetPixelData();
54 texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() );
55 texture.Upload( pixelData );
59 texture.GenerateMipmaps();
76 //Defines ordering of textures for shaders.
77 //All shaders, if including certain texture types, must include them in the same order.
78 //Within the texture set for the renderer, textures are ordered in the same manner.
86 const char * const RENDERER_TYPE_VALUE( "mesh" ); //String label for which type of control renderer this is.
87 const char * const LIGHT_POSITION( "uLightPosition" ); //Shader property
88 const char * const OBJECT_MATRIX( "uObjectMatrix" ); //Shader property
90 const char * const SHADER_TYPE_TEXTURELESS( "TEXTURELESS" );
91 const char * const SHADER_TYPE_DIFFUSE_TEXTURE( "DIFFUSE_TEXTURE" );
92 const char * const SHADER_TYPE_ALL_TEXTURES( "ALL_TEXTURES" );
95 //If a shader requires certain textures, they must be listed in order,
96 //as detailed in the TextureIndex enum documentation.
98 //A basic shader that doesn't use textures at all.
99 const char* SIMPLE_VERTEX_SHADER = DALI_COMPOSE_SHADER(
100 attribute highp vec3 aPosition;\n
101 attribute highp vec3 aNormal;\n
102 varying mediump vec3 vIllumination;\n
103 uniform mediump vec3 uSize;\n
104 uniform mediump mat4 uMvpMatrix;\n
105 uniform mediump mat4 uModelView;\n
106 uniform mediump mat3 uNormalMatrix;
107 uniform mediump mat4 uObjectMatrix;\n
108 uniform mediump vec3 uLightPosition;\n
112 vec4 vertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
113 vertexPosition = uObjectMatrix * vertexPosition;\n
114 vertexPosition = uMvpMatrix * vertexPosition;\n
116 //Illumination in Model-View space - Transform attributes and uniforms\n
117 vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
118 vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
119 vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
120 vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
121 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
123 float lightDiffuse = max( dot( vecToLight, normal ), 0.0 );\n
124 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
126 gl_Position = vertexPosition;\n
130 //Fragment shader corresponding to the texture-less shader.
131 const char* SIMPLE_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
132 precision mediump float;\n
133 varying mediump vec3 vIllumination;\n
134 uniform lowp vec4 uColor;\n
138 gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a );\n
142 //Diffuse and specular illumination shader with albedo texture. Texture is index 0.
143 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
144 attribute highp vec3 aPosition;\n
145 attribute highp vec2 aTexCoord;\n
146 attribute highp vec3 aNormal;\n
147 varying mediump vec2 vTexCoord;\n
148 varying mediump vec3 vIllumination;\n
149 varying mediump float vSpecular;\n
150 uniform mediump vec3 uSize;\n
151 uniform mediump mat4 uMvpMatrix;\n
152 uniform mediump mat4 uModelView;
153 uniform mediump mat3 uNormalMatrix;
154 uniform mediump mat4 uObjectMatrix;\n
155 uniform mediump vec3 uLightPosition;\n
159 vec4 vertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
160 vertexPosition = uObjectMatrix * vertexPosition;\n
161 vertexPosition = uMvpMatrix * vertexPosition;\n
163 //Illumination in Model-View space - Transform attributes and uniforms\n
164 vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
165 vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
166 vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
167 vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );\n
169 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
170 vec3 viewDir = normalize( -vertPos.xyz );
172 vec3 halfVector = normalize( viewDir + vecToLight );
174 float lightDiffuse = dot( vecToLight, normal );\n
175 lightDiffuse = max( 0.0,lightDiffuse );\n
176 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
178 vec3 reflectDir = reflect( -vecToLight, normal );
179 vSpecular = pow( max( dot( reflectDir, viewDir ), 0.0 ), 4.0 );
181 vTexCoord = aTexCoord;\n
182 gl_Position = vertexPosition;\n
186 //Fragment shader corresponding to the diffuse and specular illumination shader with albedo texture
187 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
188 precision mediump float;\n
189 varying mediump vec2 vTexCoord;\n
190 varying mediump vec3 vIllumination;\n
191 varying mediump float vSpecular;\n
192 uniform sampler2D sDiffuse;\n
193 uniform lowp vec4 uColor;\n
197 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
198 gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb + vSpecular * 0.3, texture.a * uColor.a );\n
202 //Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader.
203 //Diffuse (albedo) texture is index 0, normal is 1, gloss is 2. They must be declared in this order.
204 const char* NORMAL_MAP_VERTEX_SHADER = DALI_COMPOSE_SHADER(
205 attribute highp vec3 aPosition;\n
206 attribute highp vec2 aTexCoord;\n
207 attribute highp vec3 aNormal;\n
208 attribute highp vec3 aTangent;\n
209 attribute highp vec3 aBiNormal;\n
210 varying mediump vec2 vTexCoord;\n
211 varying mediump vec3 vLightDirection;\n
212 varying mediump vec3 vHalfVector;\n
213 uniform mediump vec3 uSize;\n
214 uniform mediump mat4 uMvpMatrix;\n
215 uniform mediump mat4 uModelView;
216 uniform mediump mat3 uNormalMatrix;
217 uniform mediump mat4 uObjectMatrix;\n
218 uniform mediump vec3 uLightPosition;\n
222 vec4 vertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
223 vertexPosition = uObjectMatrix * vertexPosition;\n
224 vertexPosition = uMvpMatrix * vertexPosition;\n
226 vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
227 vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
228 vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
230 vec3 tangent = normalize( uNormalMatrix * aTangent );
231 vec3 binormal = normalize( uNormalMatrix * aBiNormal );
232 vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );
234 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
235 vLightDirection.x = dot( vecToLight, tangent );
236 vLightDirection.y = dot( vecToLight, binormal );
237 vLightDirection.z = dot( vecToLight, normal );
239 vec3 viewDir = normalize( -vertPos.xyz );
240 vec3 halfVector = normalize( viewDir + vecToLight );
241 vHalfVector.x = dot( halfVector, tangent );
242 vHalfVector.y = dot( halfVector, binormal );
243 vHalfVector.z = dot( halfVector, normal );
245 vTexCoord = aTexCoord;\n
246 gl_Position = vertexPosition;\n
250 //Fragment shader corresponding to the shader that uses all textures (diffuse, normal and gloss maps)
251 const char* NORMAL_MAP_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
252 precision mediump float;\n
253 varying mediump vec2 vTexCoord;\n
254 varying mediump vec3 vLightDirection;\n
255 varying mediump vec3 vHalfVector;\n
256 uniform sampler2D sDiffuse;\n
257 uniform sampler2D sNormal;\n
258 uniform sampler2D sGloss;\n
259 uniform lowp vec4 uColor;\n
263 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
264 vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n
265 vec4 glossMap = texture2D( sGloss, vTexCoord );\n
267 float lightDiffuse = max( 0.0, dot( normal, normalize( vLightDirection ) ) );\n
268 lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
270 float shininess = pow ( max ( dot ( normalize( vHalfVector ), normal ), 0.0 ), 16.0 ) ;
272 gl_FragColor = vec4( texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a );\n
278 MeshRenderer::MeshRenderer( RendererFactoryCache& factoryCache )
279 : ControlRenderer( factoryCache ),
280 mShaderType( ALL_TEXTURES ),
282 mUseMipmapping( true )
286 MeshRenderer::~MeshRenderer()
290 void MeshRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
292 Property::Value* objectUrl = propertyMap.Find( OBJECT_URL );
293 if( !objectUrl || !objectUrl->Get( mObjectUrl ) )
295 DALI_LOG_ERROR( "Fail to provide object URL to the MeshRenderer object.\n" );
298 Property::Value* materialUrl = propertyMap.Find( MATERIAL_URL );
300 if( !materialUrl || !materialUrl->Get( mMaterialUrl ) || mMaterialUrl.empty() )
305 Property::Value* imagesUrl = propertyMap.Find( TEXTURES_PATH );
306 if( !imagesUrl || !imagesUrl->Get( mTexturesPath ) )
308 //Default behaviour is to assume files are in the same directory,
309 // or have their locations detailed in full when supplied.
310 mTexturesPath.clear();
313 Property::Value* useMipmapping = propertyMap.Find( USE_MIPMAPPING );
316 useMipmapping->Get( mUseMipmapping );
319 Property::Value* shaderType = propertyMap.Find( SHADER_TYPE );
322 std::string shaderTypeString;
323 if( shaderType->Get( shaderTypeString ) )
325 if( shaderTypeString == SHADER_TYPE_TEXTURELESS )
327 mShaderType = TEXTURELESS;
329 else if( shaderTypeString == SHADER_TYPE_DIFFUSE_TEXTURE )
331 mShaderType = DIFFUSE_TEXTURE;
333 else if( shaderTypeString == SHADER_TYPE_ALL_TEXTURES )
335 mShaderType = ALL_TEXTURES;
339 DALI_LOG_ERROR( "Unknown shader type provided to the MeshRenderer object.\n");
345 void MeshRenderer::SetSize( const Vector2& size )
347 ControlRenderer::SetSize( size );
349 // ToDo: renderer responds to the size change
352 void MeshRenderer::SetClipRect( const Rect<int>& clipRect )
354 ControlRenderer::SetClipRect( clipRect );
356 //ToDo: renderer responds to the clipRect change
359 void MeshRenderer::SetOffset( const Vector2& offset )
361 //ToDo: renderer applies the offset
364 void MeshRenderer::DoSetOnStage( Actor& actor )
366 InitializeRenderer();
369 void MeshRenderer::DoCreatePropertyMap( Property::Map& map ) const
372 map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
373 map.Insert( OBJECT_URL, mObjectUrl );
374 map.Insert( MATERIAL_URL, mMaterialUrl );
375 map.Insert( TEXTURES_PATH, mTexturesPath );
377 std::string shaderTypeString;
378 switch( mShaderType )
382 shaderTypeString = SHADER_TYPE_ALL_TEXTURES;
386 case DIFFUSE_TEXTURE:
388 shaderTypeString = SHADER_TYPE_DIFFUSE_TEXTURE;
394 shaderTypeString = SHADER_TYPE_TEXTURELESS;
398 map.Insert( SHADER_TYPE, shaderTypeString );
401 void MeshRenderer::InitializeRenderer()
403 //Try to load the geometry from the file.
404 if( !LoadGeometry() )
406 SupplyEmptyGeometry();
410 //If a texture is used by the obj file, load the supplied material file.
411 if( mObjLoader.IsTexturePresent() && !mMaterialUrl.empty() )
413 if( !LoadMaterial() )
415 SupplyEmptyGeometry();
420 //Now that the required parts are loaded, create the geometry for the object.
421 if( !CreateGeometry() )
423 SupplyEmptyGeometry();
429 //Load the various texture files supplied by the material file.
430 if( !LoadTextures() )
432 SupplyEmptyGeometry();
436 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
437 mImpl->mRenderer.SetTextures( mTextureSet );
438 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
441 void MeshRenderer::SupplyEmptyGeometry()
443 mGeometry = Geometry::New();
444 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
445 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
447 DALI_LOG_ERROR( "Initialisation error in mesh renderer.\n" );
450 void MeshRenderer::UpdateShaderUniforms()
452 Stage stage = Stage::GetCurrent();
454 Vector3 lightPosition( 0, 0, stage.GetSize().width );
455 mShader.RegisterProperty( LIGHT_POSITION, lightPosition );
458 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
459 mShader.RegisterProperty( OBJECT_MATRIX, scaleMatrix );
462 void MeshRenderer::CreateShader()
464 if( mShaderType == ALL_TEXTURES )
466 mShader = Shader::New( NORMAL_MAP_VERTEX_SHADER, NORMAL_MAP_FRAGMENT_SHADER );
468 else if( mShaderType == DIFFUSE_TEXTURE )
470 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
474 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
477 UpdateShaderUniforms();
480 bool MeshRenderer::CreateGeometry()
482 //Determine if we need to use a simpler shader to handle the provided data
483 if( !mUseTexture || !mObjLoader.IsDiffuseMapPresent() )
485 mShaderType = TEXTURELESS;
487 else if( mShaderType == ALL_TEXTURES && (!mObjLoader.IsNormalMapPresent() || !mObjLoader.IsSpecularMapPresent()) )
489 mShaderType = DIFFUSE_TEXTURE;
492 int objectProperties = 0;
494 if( mShaderType == DIFFUSE_TEXTURE ||
495 mShaderType == ALL_TEXTURES )
497 objectProperties |= ObjLoader::TEXTURE_COORDINATES;
500 if( mShaderType == ALL_TEXTURES )
502 objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
505 //Create geometry with attributes required by shader.
506 mGeometry = mObjLoader.CreateGeometry( objectProperties );
513 DALI_LOG_ERROR( "Failed to load geometry in mesh renderer.\n" );
517 bool MeshRenderer::LoadGeometry()
519 std::streampos fileSize;
520 Dali::Vector<char> fileContent;
522 if( FileLoader::ReadFile( mObjectUrl, fileSize, fileContent, FileLoader::TEXT ) )
524 mObjLoader.ClearArrays();
525 mObjLoader.LoadObject( fileContent.Begin(), fileSize );
527 //Get size information from the obj loaded
528 mSceneCenter = mObjLoader.GetCenter();
529 mSceneSize = mObjLoader.GetSize();
534 DALI_LOG_ERROR( "Failed to find object to load in mesh renderer.\n" );
538 bool MeshRenderer::LoadMaterial()
540 std::streampos fileSize;
541 Dali::Vector<char> fileContent;
543 if( FileLoader::ReadFile( mMaterialUrl, fileSize, fileContent, FileLoader::TEXT ) )
545 //Load data into obj (usable) form
546 mObjLoader.LoadMaterial( fileContent.Begin(), fileSize, mDiffuseTextureUrl, mNormalTextureUrl, mGlossTextureUrl );
550 DALI_LOG_ERROR( "Failed to find texture set to load in mesh renderer.\n" );
555 bool MeshRenderer::LoadTextures()
557 mTextureSet = TextureSet::New();
559 if( mShaderType != TEXTURELESS )
561 Sampler sampler = Sampler::New();
564 sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
567 if( !mDiffuseTextureUrl.empty() )
569 std::string imageUrl = mTexturesPath + mDiffuseTextureUrl;
572 Texture diffuseTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
575 mTextureSet.SetTexture( DIFFUSE_INDEX, diffuseTexture );
576 mTextureSet.SetSampler( DIFFUSE_INDEX, sampler );
580 DALI_LOG_ERROR( "Failed to load diffuse map texture in mesh renderer.\n");
585 if( !mNormalTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
587 std::string imageUrl = mTexturesPath + mNormalTextureUrl;
590 Texture normalTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
593 mTextureSet.SetTexture( NORMAL_INDEX, normalTexture );
594 mTextureSet.SetSampler( NORMAL_INDEX, sampler );
598 DALI_LOG_ERROR( "Failed to load normal map texture in mesh renderer.\n");
603 if( !mGlossTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
605 std::string imageUrl = mTexturesPath + mGlossTextureUrl;
608 Texture glossTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
611 mTextureSet.SetTexture( GLOSS_INDEX, glossTexture );
612 mTextureSet.SetSampler( GLOSS_INDEX, sampler );
616 DALI_LOG_ERROR( "Failed to load gloss map texture in mesh renderer.\n");
624 } // namespace Internal
626 } // namespace Toolkit