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 "model3d-view-impl.h"
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/animation/constraint-source.h>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/public-api/object/type-registry-helper.h>
27 #include <dali-toolkit/public-api/controls/model3d-view/model3d-view.h>
28 #include <dali/public-api/images/resource-image.h>
29 #include <dali/devel-api/adaptor-framework/file-loader.h>
30 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
33 #include <dali-toolkit/internal/controls/model3d-view/obj-loader.h>
47 // Texture indices are constants.
50 DIFFUSE_TEXTURE_INDEX,
56 * @brief Loads a texture from a file.
57 * @param[in] imageUrl The URL of the file
58 * @return A texture if loading succeeds, an empty handle otherwise
60 Texture LoadTexture( const char* imageUrl )
63 Dali::BitmapLoader loader = Dali::BitmapLoader::New( imageUrl );
65 PixelData pixelData = loader.GetPixelData();
68 texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() );
69 texture.Upload( pixelData );
70 texture.GenerateMipmaps();
79 return Toolkit::Model3dView::New();
82 // Setup properties, signals and actions using the type-registry.
83 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Model3dView, Toolkit::Control, Create );
85 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "geometryUrl", STRING, GEOMETRY_URL)
86 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "materialUrl", STRING, MATERIAL_URL)
87 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "imagesUrl", STRING, IMAGES_URL)
88 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "illuminationType", INTEGER, ILLUMINATION_TYPE)
89 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture0Url", STRING, TEXTURE0_URL)
90 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture1Url", STRING, TEXTURE1_URL)
91 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture2Url", STRING, TEXTURE2_URL)
93 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Model3dView, "lightPosition", VECTOR3, LIGHT_POSITION)
95 DALI_TYPE_REGISTRATION_END()
98 #define MAKE_SHADER(A)#A
100 // Diffuse illumination shader
102 const char* SIMPLE_VERTEX_SHADER = MAKE_SHADER(
103 attribute highp vec3 aPosition;\n
104 attribute highp vec2 aTexCoord;\n
105 attribute highp vec3 aNormal;\n
106 varying mediump vec3 vIllumination;\n
107 uniform mediump vec3 uSize;\n
108 uniform mediump mat4 uMvpMatrix;\n
109 uniform mediump mat4 uModelView;\n
110 uniform mediump mat3 uNormalMatrix;
111 uniform mediump mat4 uObjectMatrix;\n
112 uniform mediump vec3 uLightPosition;\n
116 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
117 vertexPosition = uObjectMatrix * vertexPosition;\n
118 vertexPosition = uMvpMatrix * vertexPosition;\n
120 //Illumination in Model-View space - Transform attributes and uniforms\n
121 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
122 vec3 normal = uNormalMatrix * aNormal;\n
123 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
124 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
126 float lightDiffuse = max( dot( vecToLight, normal ), 0.0 );\n
127 vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
129 gl_Position = vertexPosition;\n
133 const char* SIMPLE_FRAGMENT_SHADER = MAKE_SHADER(
134 precision mediump float;\n
135 varying mediump vec3 vIllumination;\n
136 uniform lowp vec4 uColor;\n
140 gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a);\n
144 // Diffuse and specular illumination shader with albedo texture
146 const char* VERTEX_SHADER = MAKE_SHADER(
147 attribute highp vec3 aPosition;\n
148 attribute highp vec2 aTexCoord;\n
149 attribute highp vec3 aNormal;\n
150 varying mediump vec2 vTexCoord;\n
151 varying mediump vec3 vIllumination;\n
152 varying mediump float vSpecular;\n
153 uniform mediump vec3 uSize;\n
154 uniform mediump mat4 uMvpMatrix;\n
155 uniform mediump mat4 uModelView;
156 uniform mediump mat3 uNormalMatrix;
157 uniform mediump mat4 uObjectMatrix;\n
158 uniform mediump vec3 uLightPosition;\n
162 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
163 vertexPosition = uObjectMatrix * vertexPosition;\n
164 vertexPosition = uMvpMatrix * vertexPosition;\n
166 //Illumination in Model-View space - Transform attributes and uniforms\n
167 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
168 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
169 vec3 normal = normalize(uNormalMatrix * aNormal);\n
171 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
172 vec3 viewDir = normalize(-vertPos.xyz);
174 vec3 halfVector = normalize(viewDir + vecToLight);
176 float lightDiffuse = dot( vecToLight, normal );\n
177 lightDiffuse = max(0.0,lightDiffuse);\n
178 vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
180 vec3 reflectDir = reflect(-vecToLight, normal);
181 vSpecular = pow( max(dot(reflectDir, viewDir), 0.0), 4.0 );
183 vTexCoord = aTexCoord;\n
184 gl_Position = vertexPosition;\n
188 const char* FRAGMENT_SHADER = MAKE_SHADER(
189 precision mediump float;\n
190 varying mediump vec2 vTexCoord;\n
191 varying mediump vec3 vIllumination;\n
192 varying mediump float vSpecular;\n
193 uniform sampler2D sDiffuse;\n
194 uniform lowp vec4 uColor;\n
198 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
199 gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb + vSpecular * 0.3, texture.a * uColor.a);\n
203 // Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader
205 const char* NRMMAP_VERTEX_SHADER = MAKE_SHADER(
206 attribute highp vec3 aPosition;\n
207 attribute highp vec2 aTexCoord;\n
208 attribute highp vec3 aNormal;\n
209 attribute highp vec3 aTangent;\n
210 attribute highp vec3 aBiNormal;\n
211 varying mediump vec2 vTexCoord;\n
212 varying mediump vec3 vLightDirection;\n
213 varying mediump vec3 vHalfVector;\n
214 uniform mediump vec3 uSize;\n
215 uniform mediump mat4 uMvpMatrix;\n
216 uniform mediump mat4 uModelView;
217 uniform mediump mat3 uNormalMatrix;
218 uniform mediump mat4 uObjectMatrix;\n
219 uniform mediump vec3 uLightPosition;\n
223 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
224 vertexPosition = uObjectMatrix * vertexPosition;\n
225 vertexPosition = uMvpMatrix * vertexPosition;\n
227 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
228 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
230 vec3 tangent = normalize(uNormalMatrix * aTangent);
231 vec3 binormal = normalize(uNormalMatrix * aBiNormal);
232 vec3 normal = normalize(uNormalMatrix * 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 const char* NRMMAP_FRAGMENT_SHADER = MAKE_SHADER(
251 precision mediump float;\n
252 varying mediump vec2 vTexCoord;\n
253 varying mediump vec3 vLightDirection;\n
254 varying mediump vec3 vHalfVector;\n
255 uniform sampler2D sDiffuse;\n
256 uniform sampler2D sNormal;\n
257 uniform sampler2D sGloss;\n
258 uniform lowp vec4 uColor;\n
262 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
263 vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n
264 vec4 glossMap = texture2D( sGloss, vTexCoord );\n
266 float lightDiffuse = max( 0.0, dot( normal, normalize(vLightDirection) ) );\n
267 lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
269 float shininess = pow (max (dot (normalize( vHalfVector ), normal), 0.0), 16.0) ;
271 gl_FragColor = vec4( texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a);\n
276 } // anonymous namespace
278 using namespace Dali;
280 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
282 Vector3 vZ = target - eye;
285 Vector3 vX = up.Cross(vZ);
288 Vector3 vY = vZ.Cross(vX);
291 result.SetInverseTransformComponents(vX, vY, vZ, eye);
295 Model3dView::Model3dView()
296 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) )
298 mIlluminationType = Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP;
300 mCameraFOV = Math::PI_OVER_180 * 45.f;
302 mControlSize = Vector2(100.,100.);
305 Model3dView::~Model3dView()
309 Toolkit::Model3dView Model3dView::New()
311 Model3dView* impl = new Model3dView();
313 Dali::Toolkit::Model3dView handle = Dali::Toolkit::Model3dView( *impl );
315 // Second-phase init of the implementation
316 // This can only be done after the CustomActor connection has been made...
322 void Model3dView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
324 Toolkit::Model3dView model3dView = Toolkit::Model3dView::DownCast( Dali::BaseHandle( object ) );
328 Model3dView& impl( GetImpl( model3dView ) );
331 case Toolkit::Model3dView::Property::GEOMETRY_URL:
333 if( value.Get(impl.mObjUrl) )
336 impl.CreateGeometry();
340 case Toolkit::Model3dView::Property::MATERIAL_URL:
342 if( value.Get(impl.mTextureSetUrl) )
345 impl.CreateMaterial();
349 case Toolkit::Model3dView::Property::IMAGES_URL:
351 if( value.Get(impl.mImagesUrl) )
357 case Toolkit::Model3dView::Property::ILLUMINATION_TYPE:
359 int illuminationType;
360 if( value.Get(illuminationType) )
362 impl.mIlluminationType = Toolkit::Model3dView::IlluminationType(illuminationType);
363 impl.CreateGeometry();
364 impl.CreateMaterial();
369 case Toolkit::Model3dView::Property::TEXTURE0_URL:
371 value.Get(impl.mTexture0Url);
374 case Toolkit::Model3dView::Property::TEXTURE1_URL:
376 value.Get(impl.mTexture1Url);
379 case Toolkit::Model3dView::Property::TEXTURE2_URL:
381 value.Get(impl.mTexture2Url);
388 Property::Value Model3dView::GetProperty( BaseObject* object, Property::Index index )
390 Property::Value value;
392 Toolkit::Model3dView model3dView = Toolkit::Model3dView::DownCast( Dali::BaseHandle( object ) );
396 Model3dView& impl( GetImpl( model3dView ) );
399 case Toolkit::Model3dView::Property::GEOMETRY_URL:
401 value = impl.mObjUrl;
404 case Toolkit::Model3dView::Property::MATERIAL_URL:
406 value = impl.mTextureSetUrl;
409 case Toolkit::Model3dView::Property::IMAGES_URL:
411 value = impl.mImagesUrl;
414 case Toolkit::Model3dView::Property::ILLUMINATION_TYPE:
416 value = int(impl.mIlluminationType);
419 case Toolkit::Model3dView::Property::TEXTURE0_URL:
421 value = impl.mTexture0Url;
424 case Toolkit::Model3dView::Property::TEXTURE1_URL:
426 value = impl.mTexture1Url;
429 case Toolkit::Model3dView::Property::TEXTURE2_URL:
431 value = impl.mTexture2Url;
440 /////////////////////////////////////////////////////////////
443 void Model3dView::OnStageConnection( int depth )
445 Control::OnStageConnection( depth );
447 CustomActor self = Self();
448 self.AddRenderer( mRenderer );
450 if( mObjLoader.IsSceneLoaded() )
452 mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ) );
457 mRenderer.SetGeometry( mMesh );
459 //create constraint for lightPosition Property with uLightPosition in the shader
460 Vector3 lightPosition( 0, 0, 0 );
461 Dali::Property::Index lightProperty = mShader.RegisterProperty( "uLightPosition", lightPosition );
462 Constraint constraint = Constraint::New<Vector3>( mShader, lightProperty, EqualToConstraint() );
463 constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) );
468 ///////////////////////////////////////////////////////////
473 void Model3dView::OnInitialize()
475 //Create empty versions of the geometry and material so we always have a Renderer
476 Geometry mesh = Geometry::New();
477 Shader shader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
478 mRenderer = Renderer::New( mesh, shader );
482 void Model3dView::LoadGeometry()
484 //Load file in adaptor
485 std::streampos fileSize;
486 Dali::Vector<char> fileContent;
488 if (FileLoader::ReadFile(mObjUrl,fileSize,fileContent,FileLoader::TEXT))
490 mObjLoader.ClearArrays();
491 mObjLoader.LoadObject(fileContent.Begin(), fileSize);
493 //Get size information from the obj loaded
494 mSceneCenter = mObjLoader.GetCenter();
495 mSceneSize = mObjLoader.GetSize();
503 void Model3dView::LoadMaterial()
505 //Load file in adaptor
506 std::streampos fileSize;
507 Dali::Vector<char> fileContent;
509 if( FileLoader::ReadFile(mTextureSetUrl, fileSize, fileContent, FileLoader::TEXT) )
511 mObjLoader.LoadMaterial(fileContent.Begin(), fileSize, mTexture0Url, mTexture1Url, mTexture2Url);
519 void Model3dView::Load()
525 void Model3dView::OnRelayout( const Vector2& size, RelayoutContainer& container )
530 void Model3dView::UpdateView()
532 if( mObjLoader.IsSceneLoaded() )
534 //The object will always be centred
537 scaleMatrix.SetIdentityAndScale(Vector3(1.0, -1.0, 1.0));
539 mShader.RegisterProperty( "uObjectMatrix", scaleMatrix );
543 void Model3dView::CreateGeometry()
545 if( mObjLoader.IsSceneLoaded() )
547 mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ) );
551 mRenderer.SetGeometry( mMesh );
552 mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
557 void Model3dView::UpdateShaderUniforms()
561 //Update shader related info, uniforms, etc. for the new shader
564 Vector3 lightPosition( 0, 0, 0 );
565 Dali::Property::Index lightProperty = mShader.RegisterProperty( "uLightPosition", lightPosition );
567 CustomActor self = Self();
569 //create constraint for lightPosition Property with uLightPosition in the shader
572 Constraint constraint = Constraint::New<Vector3>( mShader, lightProperty, EqualToConstraint() );
573 constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) );
579 void Model3dView::CreateMaterial()
581 if( mObjLoader.IsMaterialLoaded() && (mTexture0Url != "") && mObjLoader.IsTexturePresent() )
583 if( (mTexture2Url != "") && (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) )
585 mShader = Shader::New( NRMMAP_VERTEX_SHADER, NRMMAP_FRAGMENT_SHADER );
587 else if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
588 mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
590 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
594 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
599 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
602 mTextureSet = TextureSet::New();
606 mRenderer.SetTextures( mTextureSet );
607 mRenderer.SetShader( mShader );
608 mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
611 UpdateShaderUniforms();
614 void Model3dView::LoadTextures()
621 Sampler sampler = Sampler::New();
622 sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
624 // Setup diffuse texture.
625 if( !mTexture0Url.empty() && ( mIlluminationType != Toolkit::Model3dView::DIFFUSE ) )
627 std::string imageUrl = mImagesUrl + mTexture0Url;
630 Texture diffuseTexture = LoadTexture( imageUrl.c_str() );
633 mTextureSet.SetTexture( DIFFUSE_TEXTURE_INDEX, diffuseTexture );
634 mTextureSet.SetSampler( DIFFUSE_TEXTURE_INDEX, sampler );
638 if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
640 // Setup normal map texture.
641 if( !mTexture1Url.empty() )
643 std::string imageUrl = mImagesUrl + mTexture1Url;
646 Texture normalTexture = LoadTexture( imageUrl.c_str() );
649 mTextureSet.SetTexture( NORMAL_TEXTURE_INDEX, normalTexture );
650 mTextureSet.SetSampler( NORMAL_TEXTURE_INDEX, sampler );
653 if( !mTexture2Url.empty() )
655 // Setup gloss map texture.
656 std::string imageUrl = mImagesUrl + mTexture2Url;
659 Texture glossTexture = LoadTexture( imageUrl.c_str() );
662 mTextureSet.SetTexture( GLOSS_TEXTURE_INDEX, glossTexture );
663 mTextureSet.SetSampler( GLOSS_TEXTURE_INDEX, sampler );
669 int Model3dView::GetShaderProperties( Toolkit::Model3dView::IlluminationType illuminationType )
671 int objectProperties = 0;
673 if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
674 illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
676 objectProperties |= ObjLoader::TEXTURE_COORDINATES;
679 if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
681 objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
684 return objectProperties;
687 } // namespace Internal
688 } // namespace Toolkit