Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / rendering-basic-pbr / model-pbr.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "model-pbr.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/file-loader.h>
23 #include <string.h>
24 #include <cstdio>
25
26 // INTERNAL INCLUDES
27 #include "obj-loader.h"
28
29 namespace
30 {
31 const int SEGMENTS = 16;
32
33 struct Vertices
34 {
35   Vector<Vector3> positions;
36   Vector<Vector3> normals;
37   Vector<Vector3> tangents;
38   Vector<Vector2> texCoords;
39 };
40
41 } // namespace
42
43 ModelPbr::ModelPbr()
44 {
45 }
46
47 ModelPbr::~ModelPbr()
48 {
49 }
50
51 void ModelPbr::Init(Shader shader, const std::string& modelUrl, const Vector3& position, const Vector3& size)
52 {
53   Geometry geometry;
54
55   geometry = CreateGeometry(modelUrl);
56
57   Renderer renderer = Renderer::New(geometry, shader);
58
59   if(mTextureSet)
60   {
61     renderer.SetTextures(mTextureSet);
62   }
63
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);
70
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);
77 }
78
79 /**
80  * Set texture and sampler
81  */
82 void ModelPbr::InitTexture(Texture albedoM, Texture normalR, Texture texDiffuse, Texture texSpecular)
83 {
84   mTextureSet = TextureSet::New();
85   mTextureSet.SetTexture(0u, albedoM);
86   mTextureSet.SetTexture(1u, normalR);
87   mTextureSet.SetTexture(2u, texDiffuse);
88   mTextureSet.SetTexture(3u, texSpecular);
89
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);
94 }
95
96 Actor& ModelPbr::GetActor()
97 {
98   return mActor;
99 }
100
101 /**
102  * Create geometry from OBJ path file
103  */
104 Geometry ModelPbr::CreateGeometry(const std::string& url)
105 {
106   std::streampos     fileSize;
107   Dali::Vector<char> fileContent;
108
109   Geometry geometry;
110
111   if(FileLoader::ReadFile(url, fileSize, fileContent, FileLoader::TEXT))
112   {
113     PbrDemo::ObjLoader objLoader;
114
115     objLoader.ClearArrays();
116     objLoader.LoadObject(fileContent.Begin(), fileSize);
117
118     geometry = objLoader.CreateGeometry(PbrDemo::ObjLoader::TEXTURE_COORDINATES | PbrDemo::ObjLoader::TANGENTS, true);
119   }
120
121   return geometry;
122 }