/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <dali/public-api/animation/constraint.h>
#include <dali/public-api/animation/constraint-source.h>
#include <dali/public-api/animation/constraints.h>
-#include <dali/devel-api/object/type-registry-helper.h>
+#include <dali/public-api/object/type-registry-helper.h>
#include <dali-toolkit/public-api/controls/model3d-view/model3d-view.h>
#include <dali/public-api/images/resource-image.h>
#include <dali/devel-api/adaptor-framework/file-loader.h>
+#include <dali/devel-api/adaptor-framework/image-loading.h>
// INTERNAL INCLUDES
#include <dali-toolkit/internal/controls/model3d-view/obj-loader.h>
namespace
{
+// Texture indices are constants.
+enum TextureIndex
+{
+ DIFFUSE_TEXTURE_INDEX,
+ NORMAL_TEXTURE_INDEX,
+ GLOSS_TEXTURE_INDEX
+};
+
+/**
+ * @brief Loads a texture from a file.
+ * @param[in] imageUrl The URL of the file
+ * @return A texture if loading succeeds, an empty handle otherwise
+ */
+Texture LoadTexture( const char* imageUrl )
+{
+ Texture texture;
+ Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl );
+ if( pixelBuffer )
+ {
+ texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
+ PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
+ texture.Upload( pixelData );
+ texture.GenerateMipmaps();
+ }
+
+ return texture;
+}
+
// Type registration
BaseHandle Create()
{
using namespace Dali;
-void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
-{
- Vector3 vZ = target - eye;
- vZ.Normalize();
-
- Vector3 vX = up.Cross(vZ);
- vX.Normalize();
-
- Vector3 vY = vZ.Cross(vX);
- vY.Normalize();
-
- result.SetInverseTransformComponents(vX, vY, vZ, eye);
-}
-
-
Model3dView::Model3dView()
- : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) )
+: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
{
- mTexture0Url = "";
- mTexture1Url = "";
- mTexture2Url = "";
-
mIlluminationType = Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP;
mCameraFOV = Math::PI_OVER_180 * 45.f;
}
case Toolkit::Model3dView::Property::MATERIAL_URL:
{
- if( value.Get(impl.mMaterialUrl) )
+ if( value.Get(impl.mTextureSetUrl) )
{
impl.LoadMaterial();
impl.CreateMaterial();
+ impl.LoadTextures();
}
break;
}
}
case Toolkit::Model3dView::Property::MATERIAL_URL:
{
- value = impl.mMaterialUrl;
+ value = impl.mTextureSetUrl;
break;
}
case Toolkit::Model3dView::Property::IMAGES_URL:
void Model3dView::OnStageConnection( int depth )
{
- Control::OnStageConnection( depth );
-
CustomActor self = Self();
self.AddRenderer( mRenderer );
if( mObjLoader.IsSceneLoaded() )
{
- mMesh = mObjLoader.CreateGeometry(mIlluminationType);
+ mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true );
CreateMaterial();
LoadTextures();
constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) );
constraint.Apply();
}
+
+ Control::OnStageConnection( depth );
}
///////////////////////////////////////////////////////////
{
//Create empty versions of the geometry and material so we always have a Renderer
Geometry mesh = Geometry::New();
- Shader shader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER, (Shader::ShaderHints)(Shader::HINT_REQUIRES_SELF_DEPTH_TEST | Shader::HINT_MODIFIES_GEOMETRY) );
- Material material = Material::New( shader );
- mRenderer = Renderer::New( mesh, material );
+ Shader shader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
+ mRenderer = Renderer::New( mesh, shader );
+
}
void Model3dView::LoadGeometry()
if (FileLoader::ReadFile(mObjUrl,fileSize,fileContent,FileLoader::TEXT))
{
mObjLoader.ClearArrays();
-
- std::string materialUrl;
- mObjLoader.Load(fileContent.Begin(), fileSize, materialUrl);
+ mObjLoader.LoadObject(fileContent.Begin(), fileSize);
//Get size information from the obj loaded
mSceneCenter = mObjLoader.GetCenter();
std::streampos fileSize;
Dali::Vector<char> fileContent;
- if( FileLoader::ReadFile(mMaterialUrl, fileSize, fileContent, FileLoader::TEXT) )
+ if( FileLoader::ReadFile(mTextureSetUrl, fileSize, fileContent, FileLoader::TEXT) )
{
mObjLoader.LoadMaterial(fileContent.Begin(), fileSize, mTexture0Url, mTexture1Url, mTexture2Url);
}
{
if( mObjLoader.IsSceneLoaded() )
{
- mMesh = mObjLoader.CreateGeometry(mIlluminationType);
+ mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true );
if( mRenderer )
{
mRenderer.SetGeometry( mMesh );
+ mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
+ mRenderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
}
}
}
void Model3dView::CreateMaterial()
{
- if( mObjLoader.IsMaterialLoaded() && (mTexture0Url != ""))
+ if( mObjLoader.IsMaterialLoaded() && (mTexture0Url != "") && mObjLoader.IsTexturePresent() )
{
- if( (mTexture2Url != "") && (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP))
+ if( (mTexture2Url != "") && (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) )
{
- mShader = Shader::New( NRMMAP_VERTEX_SHADER, NRMMAP_FRAGMENT_SHADER, (Shader::ShaderHints)(Shader::HINT_REQUIRES_SELF_DEPTH_TEST | Shader::HINT_MODIFIES_GEOMETRY) );
+ mShader = Shader::New( NRMMAP_VERTEX_SHADER, NRMMAP_FRAGMENT_SHADER );
}
- else if(mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE)
+ else if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
+ mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
{
- mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, (Shader::ShaderHints)(Shader::HINT_REQUIRES_SELF_DEPTH_TEST | Shader::HINT_MODIFIES_GEOMETRY) );
+ mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
}
else
{
- mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER, (Shader::ShaderHints)(Shader::HINT_REQUIRES_SELF_DEPTH_TEST | Shader::HINT_MODIFIES_GEOMETRY) );
+ mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
}
}
else
{
- mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER, (Shader::ShaderHints)(Shader::HINT_REQUIRES_SELF_DEPTH_TEST | Shader::HINT_MODIFIES_GEOMETRY) );
+ mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
}
- mMaterial = Material::New( mShader );
+ mTextureSet = TextureSet::New();
if( mRenderer )
{
- mRenderer.SetMaterial( mMaterial );
- mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, Renderer::NONE);
+ mRenderer.SetTextures( mTextureSet );
+ mRenderer.SetShader( mShader );
+ mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
}
UpdateShaderUniforms();
void Model3dView::LoadTextures()
{
- if( !mMaterial )
- return ;
+ if( !mTextureSet )
+ {
+ return;
+ }
- if( mTexture0Url != "" )
+ Sampler sampler = Sampler::New();
+ sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
+
+ // Setup diffuse texture.
+ if( !mTexture0Url.empty() && ( mIlluminationType != Toolkit::Model3dView::DIFFUSE ) )
{
- std::string imgUrl = mImagesUrl + mTexture0Url;
+ std::string imageUrl = mImagesUrl + mTexture0Url;
//Load textures
- Image tex0 = ResourceImage::New( imgUrl );
- if( tex0 )
+ Texture diffuseTexture = LoadTexture( imageUrl.c_str() );
+ if( diffuseTexture )
{
- mMaterial.AddTexture( tex0, "sDiffuse" );
+ mTextureSet.SetTexture( DIFFUSE_TEXTURE_INDEX, diffuseTexture );
+ mTextureSet.SetSampler( DIFFUSE_TEXTURE_INDEX, sampler );
}
}
- if( (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) )
+ if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
{
- std::string imgUrl = mImagesUrl + mTexture1Url;
+ // Setup normal map texture.
+ if( !mTexture1Url.empty() )
+ {
+ std::string imageUrl = mImagesUrl + mTexture1Url;
- //Load textures
- Image tex1 = ResourceImage::New( imgUrl );
- if (tex1)
+ //Load textures
+ Texture normalTexture = LoadTexture( imageUrl.c_str() );
+ if( normalTexture )
+ {
+ mTextureSet.SetTexture( NORMAL_TEXTURE_INDEX, normalTexture );
+ mTextureSet.SetSampler( NORMAL_TEXTURE_INDEX, sampler );
+ }
+ }
+ if( !mTexture2Url.empty() )
{
- mMaterial.AddTexture( tex1, "sNormal" );
+ // Setup gloss map texture.
+ std::string imageUrl = mImagesUrl + mTexture2Url;
+
+ //Load textures
+ Texture glossTexture = LoadTexture( imageUrl.c_str() );
+ if( glossTexture )
+ {
+ mTextureSet.SetTexture( GLOSS_TEXTURE_INDEX, glossTexture );
+ mTextureSet.SetSampler( GLOSS_TEXTURE_INDEX, sampler );
+ }
}
}
+}
- if( (mTexture2Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) )
+int Model3dView::GetShaderProperties( Toolkit::Model3dView::IlluminationType illuminationType )
+{
+ int objectProperties = 0;
+
+ if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
+ illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
{
- std::string imgUrl = mImagesUrl + mTexture2Url;
+ objectProperties |= ObjLoader::TEXTURE_COORDINATES;
+ }
- //Load textures
- Image tex2 = ResourceImage::New( imgUrl );
- if( tex2 )
- {
- mMaterial.AddTexture( tex2, "sGloss" );
- }
+ if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
+ {
+ objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
}
+
+ return objectProperties;
}
} // namespace Internal