2 * Copyright (c) 2017 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 "model-pbr.h"
22 #include <dali/devel-api/adaptor-framework/file-loader.h>
27 #include "obj-loader.h"
31 const int SEGMENTS = 16;
35 Vector<Vector3> positions;
36 Vector<Vector3> normals;
37 Vector<Vector3> tangents;
38 Vector<Vector2> texCoords;
52 void ModelPbr::Init( Shader shader, const std::string& modelUrl, const Vector3& position, const Vector3& size )
56 geometry = CreateGeometry( modelUrl );
58 Renderer renderer = Renderer::New( geometry, shader );
62 renderer.SetTextures( mTextureSet );
65 // Face culling is enabled to hide the backwards facing sides of the model
66 // This is sufficient to render a single object; for more complex scenes depth-testing might be required
67 renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
68 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
69 renderer.SetProperty( Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL );
70 renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
72 mActor = Actor::New();
73 mActor.SetAnchorPoint( AnchorPoint::CENTER );
74 mActor.SetParentOrigin( ParentOrigin::CENTER );
75 mActor.SetPosition( position );
76 mActor.SetSize( size );
77 mActor.AddRenderer( renderer );
81 * Set texture and sampler
83 void ModelPbr::InitTexture(Texture albedoM, Texture normalR, Texture texDiffuse, Texture texSpecular)
85 mTextureSet = TextureSet::New();
86 mTextureSet.SetTexture( 0u, albedoM );
87 mTextureSet.SetTexture( 1u, normalR );
88 mTextureSet.SetTexture( 2u, texDiffuse );
89 mTextureSet.SetTexture( 3u, texSpecular );
91 Sampler sampler = Sampler::New();
92 sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE,WrapMode::CLAMP_TO_EDGE,WrapMode::CLAMP_TO_EDGE);
93 sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR,FilterMode::LINEAR);
94 mTextureSet.SetSampler(3,sampler);
97 Actor& ModelPbr::GetActor()
103 * Create geometry from OBJ path file
105 Geometry ModelPbr::CreateGeometry( const std::string& url )
107 std::streampos fileSize;
108 Dali::Vector<char> fileContent;
112 if( FileLoader::ReadFile( url, fileSize, fileContent, FileLoader::TEXT ) )
114 PbrDemo::ObjLoader objLoader;
116 objLoader.ClearArrays();
117 objLoader.LoadObject( fileContent.Begin(), fileSize );
119 geometry = objLoader.CreateGeometry( PbrDemo::ObjLoader::TEXTURE_COORDINATES | PbrDemo::ObjLoader::TANGENTS, true );