Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / rendering-basic-pbr / model-pbr.cpp
1 /*
2  * Copyright (c) 2017 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 <cstdio>
24 #include <string.h>
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
44 ModelPbr::ModelPbr()
45 {
46 }
47
48 ModelPbr::~ModelPbr()
49 {
50 }
51
52 void ModelPbr::Init( Shader shader, const std::string& modelUrl, const Vector3& position, const Vector3& size )
53 {
54   Geometry geometry;
55
56   geometry = CreateGeometry( modelUrl );
57
58   Renderer renderer = Renderer::New( geometry, shader );
59
60   if( mTextureSet )
61   {
62     renderer.SetTextures( mTextureSet );
63   }
64
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 );
71
72   mActor = Actor::New();
73   mActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
74   mActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
75   mActor.SetProperty( Actor::Property::POSITION, position );
76   mActor.SetProperty( Actor::Property::SIZE, size );
77   mActor.AddRenderer( renderer );
78 }
79
80 /**
81  * Set texture and sampler
82  */
83 void ModelPbr::InitTexture(Texture albedoM, Texture normalR, Texture texDiffuse, Texture texSpecular)
84 {
85   mTextureSet = TextureSet::New();
86   mTextureSet.SetTexture( 0u, albedoM );
87   mTextureSet.SetTexture( 1u, normalR );
88   mTextureSet.SetTexture( 2u, texDiffuse );
89   mTextureSet.SetTexture( 3u, texSpecular );
90
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);
95 }
96
97 Actor& ModelPbr::GetActor()
98 {
99   return mActor;
100 }
101
102 /**
103  * Create geometry from OBJ path file
104  */
105 Geometry ModelPbr::CreateGeometry( const std::string& url )
106 {
107   std::streampos fileSize;
108   Dali::Vector<char> fileContent;
109
110   Geometry geometry;
111
112   if( FileLoader::ReadFile( url, fileSize, fileContent, FileLoader::TEXT ) )
113   {
114     PbrDemo::ObjLoader objLoader;
115
116     objLoader.ClearArrays();
117     objLoader.LoadObject( fileContent.Begin(), fileSize );
118
119     geometry = objLoader.CreateGeometry( PbrDemo::ObjLoader::TEXTURE_COORDINATES | PbrDemo::ObjLoader::TANGENTS, true );
120   }
121
122   return geometry;
123 }