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-skybox.h"
22 #include <dali/devel-api/adaptor-framework/file-loader.h>
27 #include "obj-loader.h"
34 * Vertex shader for a skybox
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
40 varying mediump vec3 vTexCoord;\n
43 vTexCoord = aPosition;\n
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.
54 * Fragment shader for a skybox
56 const char* FRAGMENT_SHADER_SKYBOX = DALI_COMPOSE_SHADER(
57 uniform samplerCube uSkyBoxTexture;\n
59 varying mediump vec3 vTexCoord;\n
62 mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord, 0.0);\n
63 gl_FragColor = texColor;\n
70 ModelSkybox::ModelSkybox()
74 ModelSkybox::~ModelSkybox()
78 void ModelSkybox::Init(const Vector3& size)
80 Geometry geometry = CreateGeometry();
81 Shader shader = Shader::New(VERTEX_SHADER_SKYBOX, FRAGMENT_SHADER_SKYBOX);
83 Renderer renderer = Renderer::New(geometry, shader);
87 renderer.SetTextures(mTextureSet);
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);
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);
105 * Set texture and sampler
107 void ModelSkybox::InitTexture(Texture texSkybox)
109 mTextureSet = TextureSet::New();
110 mTextureSet.SetTexture(0u, texSkybox);
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);
118 Actor& ModelSkybox::GetActor()
124 * @brief CreateGeometry
125 * This function creates a cube geometry including texture coordinates.
127 Geometry ModelSkybox::CreateGeometry()
136 Vertex skyboxVertices[] = {
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)},
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)},
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)},
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)},
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)},
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)}};
185 VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
186 .Add("aPosition", Property::VECTOR3));
187 vertexBuffer.SetData(skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex));
189 geometry = Geometry::New();
190 geometry.AddVertexBuffer(vertexBuffer);
191 geometry.SetType(Geometry::TRIANGLES);