2 * Copyright (c) 2020 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;
51 void ModelPbr::Init(Shader shader, const std::string& modelUrl, const Vector3& position, const Vector3& size)
55 geometry = CreateGeometry(modelUrl);
57 Renderer renderer = Renderer::New(geometry, shader);
61 renderer.SetTextures(mTextureSet);
64 // Face culling is enabled to hide the backwards facing sides of the model
65 // This is sufficient to render a single object; for more complex scenes depth-testing might be required
66 renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON);
67 renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON);
68 renderer.SetProperty(Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL);
69 renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
71 mActor = Actor::New();
72 mActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
73 mActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
74 mActor.SetProperty(Actor::Property::POSITION, position);
75 mActor.SetProperty(Actor::Property::SIZE, size);
76 mActor.AddRenderer(renderer);
80 * Set texture and sampler
82 void ModelPbr::InitTexture(Texture albedoM, Texture normalR, Texture texDiffuse, Texture texSpecular)
84 mTextureSet = TextureSet::New();
85 mTextureSet.SetTexture(0u, albedoM);
86 mTextureSet.SetTexture(1u, normalR);
87 mTextureSet.SetTexture(2u, texDiffuse);
88 mTextureSet.SetTexture(3u, texSpecular);
90 Sampler sampler = Sampler::New();
91 sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
92 sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
93 mTextureSet.SetSampler(3, sampler);
96 Actor& ModelPbr::GetActor()
102 * Create geometry from OBJ path file
104 Geometry ModelPbr::CreateGeometry(const std::string& url)
106 std::streampos fileSize;
107 Dali::Vector<char> fileContent;
111 if(FileLoader::ReadFile(url, fileSize, fileContent, FileLoader::TEXT))
113 PbrDemo::ObjLoader objLoader;
115 objLoader.ClearArrays();
116 objLoader.LoadObject(fileContent.Begin(), fileSize);
118 geometry = objLoader.CreateGeometry(PbrDemo::ObjLoader::TEXTURE_COORDINATES | PbrDemo::ObjLoader::TANGENTS, true);