Updated all files to new format
[platform/core/uifw/dali-demo.git] / examples / rendering-basic-pbr / model-skybox.cpp
1 /*
2  * Copyright (c) 2021 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-skybox.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 "generated/skybox-frag.h"
28 #include "generated/skybox-vert.h"
29 #include "obj-loader.h"
30
31 ModelSkybox::ModelSkybox()
32 {
33 }
34
35 ModelSkybox::~ModelSkybox()
36 {
37 }
38
39 void ModelSkybox::Init(const Vector3& size)
40 {
41   Geometry geometry = CreateGeometry();
42   Shader   shader   = Shader::New(SHADER_SKYBOX_VERT, SHADER_SKYBOX_FRAG);
43
44   Renderer renderer = Renderer::New(geometry, shader);
45
46   if(mTextureSet)
47   {
48     renderer.SetTextures(mTextureSet);
49   }
50
51   // Face culling is enabled to hide the backwards facing sides of the model
52   // This is sufficient to render a single object; for more complex scenes depth-testing might be required
53   renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON);
54   renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON);
55   renderer.SetProperty(Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL);
56   renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
57
58   mActor = Actor::New();
59   mActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
60   mActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
61   mActor.SetProperty(Actor::Property::SIZE, size);
62   mActor.AddRenderer(renderer);
63 }
64
65 /**
66  * Set texture and sampler
67  */
68 void ModelSkybox::InitTexture(Texture texSkybox)
69 {
70   mTextureSet = TextureSet::New();
71   mTextureSet.SetTexture(0u, texSkybox);
72
73   Sampler sampler = Sampler::New();
74   sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
75   sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
76   mTextureSet.SetSampler(0, sampler);
77 }
78
79 Actor& ModelSkybox::GetActor()
80 {
81   return mActor;
82 }
83
84 /**
85  * @brief CreateGeometry
86  * This function creates a cube geometry including texture coordinates.
87  */
88 Geometry ModelSkybox::CreateGeometry()
89 {
90   Geometry geometry;
91
92   struct Vertex
93   {
94     Vector3 aPosition;
95   };
96
97   Vertex skyboxVertices[] = {
98     // back
99     {Vector3(-1.0f, 1.0f, -1.0f)},
100     {Vector3(-1.0f, -1.0f, -1.0f)},
101     {Vector3(1.0f, -1.0f, -1.0f)},
102     {Vector3(1.0f, -1.0f, -1.0f)},
103     {Vector3(1.0f, 1.0f, -1.0f)},
104     {Vector3(-1.0f, 1.0f, -1.0f)},
105
106     // left
107     {Vector3(-1.0f, -1.0f, 1.0f)},
108     {Vector3(-1.0f, -1.0f, -1.0f)},
109     {Vector3(-1.0f, 1.0f, -1.0f)},
110     {Vector3(-1.0f, 1.0f, -1.0f)},
111     {Vector3(-1.0f, 1.0f, 1.0f)},
112     {Vector3(-1.0f, -1.0f, 1.0f)},
113
114     // right
115     {Vector3(1.0f, -1.0f, -1.0f)},
116     {Vector3(1.0f, -1.0f, 1.0f)},
117     {Vector3(1.0f, 1.0f, 1.0f)},
118     {Vector3(1.0f, 1.0f, 1.0f)},
119     {Vector3(1.0f, 1.0f, -1.0f)},
120     {Vector3(1.0f, -1.0f, -1.0f)},
121
122     // front
123     {Vector3(-1.0f, -1.0f, 1.0f)},
124     {Vector3(-1.0f, 1.0f, 1.0f)},
125     {Vector3(1.0f, 1.0f, 1.0f)},
126     {Vector3(1.0f, 1.0f, 1.0f)},
127     {Vector3(1.0f, -1.0f, 1.0f)},
128     {Vector3(-1.0f, -1.0f, 1.0f)},
129
130     // botton
131     {Vector3(-1.0f, 1.0f, -1.0f)},
132     {Vector3(1.0f, 1.0f, -1.0f)},
133     {Vector3(1.0f, 1.0f, 1.0f)},
134     {Vector3(1.0f, 1.0f, 1.0f)},
135     {Vector3(-1.0f, 1.0f, 1.0f)},
136     {Vector3(-1.0f, 1.0f, -1.0f)},
137
138     // top
139     {Vector3(-1.0f, -1.0f, -1.0f)},
140     {Vector3(-1.0f, -1.0f, 1.0f)},
141     {Vector3(1.0f, -1.0f, -1.0f)},
142     {Vector3(1.0f, -1.0f, -1.0f)},
143     {Vector3(-1.0f, -1.0f, 1.0f)},
144     {Vector3(1.0f, -1.0f, 1.0f)}};
145
146   VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
147                                                   .Add("aPosition", Property::VECTOR3));
148   vertexBuffer.SetData(skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex));
149
150   geometry = Geometry::New();
151   geometry.AddVertexBuffer(vertexBuffer);
152   geometry.SetType(Geometry::TRIANGLES);
153   return geometry;
154 }