Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / rendering-basic-pbr / model-skybox.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-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 "obj-loader.h"
28
29 namespace
30 {
31 // clang-format off
32
33 /*
34  * Vertex shader for a skybox
35  */
36 const char* VERTEX_SHADER_SKYBOX = DALI_COMPOSE_SHADER(
37 attribute mediump vec3 aPosition;\n // DALi shader builtin
38 uniform   mediump mat4 uMvpMatrix;\n // DALi shader builtin
39 \n
40 varying mediump vec3 vTexCoord;\n
41 void main()\n
42 {\n
43   vTexCoord =  aPosition;\n
44
45   mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
46   vec4 clipSpacePosition = uMvpMatrix * vertexPosition;\n
47   gl_Position = clipSpacePosition.xyww;\n // Writes 1.0, the maximum depth value, into the depth buffer.
48                                           // This is an optimization to avoid running the fragment shader
49                                           // for the pixels hidden by the scene's objects.
50 }\n
51 );
52
53 /*
54  * Fragment shader for a skybox
55  */
56 const char* FRAGMENT_SHADER_SKYBOX = DALI_COMPOSE_SHADER(
57 uniform samplerCube uSkyBoxTexture;\n
58 \n
59 varying mediump vec3 vTexCoord;\n
60 void main()\n
61 {\n
62   mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord, 0.0);\n
63   gl_FragColor = texColor;\n
64 }\n
65 );
66 // clang-format on
67
68 } // namespace
69
70 ModelSkybox::ModelSkybox()
71 {
72 }
73
74 ModelSkybox::~ModelSkybox()
75 {
76 }
77
78 void ModelSkybox::Init(const Vector3& size)
79 {
80   Geometry geometry = CreateGeometry();
81   Shader   shader   = Shader::New(VERTEX_SHADER_SKYBOX, FRAGMENT_SHADER_SKYBOX);
82
83   Renderer renderer = Renderer::New(geometry, shader);
84
85   if(mTextureSet)
86   {
87     renderer.SetTextures(mTextureSet);
88   }
89
90   // Face culling is enabled to hide the backwards facing sides of the model
91   // This is sufficient to render a single object; for more complex scenes depth-testing might be required
92   renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON);
93   renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON);
94   renderer.SetProperty(Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL);
95   renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
96
97   mActor = Actor::New();
98   mActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
99   mActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
100   mActor.SetProperty(Actor::Property::SIZE, size);
101   mActor.AddRenderer(renderer);
102 }
103
104 /**
105  * Set texture and sampler
106  */
107 void ModelSkybox::InitTexture(Texture texSkybox)
108 {
109   mTextureSet = TextureSet::New();
110   mTextureSet.SetTexture(0u, texSkybox);
111
112   Sampler sampler = Sampler::New();
113   sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
114   sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
115   mTextureSet.SetSampler(0, sampler);
116 }
117
118 Actor& ModelSkybox::GetActor()
119 {
120   return mActor;
121 }
122
123 /**
124  * @brief CreateGeometry
125  * This function creates a cube geometry including texture coordinates.
126  */
127 Geometry ModelSkybox::CreateGeometry()
128 {
129   Geometry geometry;
130
131   struct Vertex
132   {
133     Vector3 aPosition;
134   };
135
136   Vertex skyboxVertices[] = {
137     // back
138     {Vector3(-1.0f, 1.0f, -1.0f)},
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
145     // left
146     {Vector3(-1.0f, -1.0f, 1.0f)},
147     {Vector3(-1.0f, -1.0f, -1.0f)},
148     {Vector3(-1.0f, 1.0f, -1.0f)},
149     {Vector3(-1.0f, 1.0f, -1.0f)},
150     {Vector3(-1.0f, 1.0f, 1.0f)},
151     {Vector3(-1.0f, -1.0f, 1.0f)},
152
153     // right
154     {Vector3(1.0f, -1.0f, -1.0f)},
155     {Vector3(1.0f, -1.0f, 1.0f)},
156     {Vector3(1.0f, 1.0f, 1.0f)},
157     {Vector3(1.0f, 1.0f, 1.0f)},
158     {Vector3(1.0f, 1.0f, -1.0f)},
159     {Vector3(1.0f, -1.0f, -1.0f)},
160
161     // front
162     {Vector3(-1.0f, -1.0f, 1.0f)},
163     {Vector3(-1.0f, 1.0f, 1.0f)},
164     {Vector3(1.0f, 1.0f, 1.0f)},
165     {Vector3(1.0f, 1.0f, 1.0f)},
166     {Vector3(1.0f, -1.0f, 1.0f)},
167     {Vector3(-1.0f, -1.0f, 1.0f)},
168
169     // botton
170     {Vector3(-1.0f, 1.0f, -1.0f)},
171     {Vector3(1.0f, 1.0f, -1.0f)},
172     {Vector3(1.0f, 1.0f, 1.0f)},
173     {Vector3(1.0f, 1.0f, 1.0f)},
174     {Vector3(-1.0f, 1.0f, 1.0f)},
175     {Vector3(-1.0f, 1.0f, -1.0f)},
176
177     // top
178     {Vector3(-1.0f, -1.0f, -1.0f)},
179     {Vector3(-1.0f, -1.0f, 1.0f)},
180     {Vector3(1.0f, -1.0f, -1.0f)},
181     {Vector3(1.0f, -1.0f, -1.0f)},
182     {Vector3(-1.0f, -1.0f, 1.0f)},
183     {Vector3(1.0f, -1.0f, 1.0f)}};
184
185   VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
186                                                   .Add("aPosition", Property::VECTOR3));
187   vertexBuffer.SetData(skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex));
188
189   geometry = Geometry::New();
190   geometry.AddVertexBuffer(vertexBuffer);
191   geometry.SetType(Geometry::TRIANGLES);
192   return geometry;
193 }