2 * Copyright (c) 2017 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"
32 * Vertex shader for a skybox
34 const char* VERTEX_SHADER_SKYBOX = DALI_COMPOSE_SHADER(
35 attribute mediump vec3 aPosition;\n // DALi shader builtin
36 uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin
38 varying mediump vec3 vTexCoord;\n
41 vTexCoord = aPosition;\n
43 mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
44 vec4 clipSpacePosition = uMvpMatrix * vertexPosition;\n
45 gl_Position = clipSpacePosition.xyww;\n // Writes 1.0, the maximum depth value, into the depth buffer.
46 // This is an optimization to avoid running the fragment shader
47 // for the pixels hidden by the scene's objects.
52 * Fragment shader for a skybox
54 const char* FRAGMENT_SHADER_SKYBOX = DALI_COMPOSE_SHADER(
55 uniform samplerCube uSkyBoxTexture;\n
57 varying mediump vec3 vTexCoord;\n
60 mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord, 0.0);\n
61 gl_FragColor = texColor;\n
68 ModelSkybox::ModelSkybox()
72 ModelSkybox::~ModelSkybox()
76 void ModelSkybox::Init( const Vector3& size )
79 Geometry geometry = CreateGeometry();
80 Shader shader = Shader::New( VERTEX_SHADER_SKYBOX, FRAGMENT_SHADER_SKYBOX );
82 Renderer renderer = Renderer::New( geometry, shader );
86 renderer.SetTextures( mTextureSet );
89 // Face culling is enabled to hide the backwards facing sides of the model
90 // This is sufficient to render a single object; for more complex scenes depth-testing might be required
91 renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
92 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
93 renderer.SetProperty( Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL );
94 renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
96 mActor = Actor::New();
97 mActor.SetAnchorPoint( AnchorPoint::CENTER );
98 mActor.SetParentOrigin( ParentOrigin::CENTER );
99 mActor.SetSize( size );
100 mActor.AddRenderer( renderer );
104 * Set texture and sampler
106 void ModelSkybox::InitTexture(Texture texSkybox)
108 mTextureSet = TextureSet::New();
109 mTextureSet.SetTexture( 0u, texSkybox );
111 Sampler sampler = Sampler::New();
112 sampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE,WrapMode::CLAMP_TO_EDGE,WrapMode::CLAMP_TO_EDGE);
113 sampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR,FilterMode::LINEAR);
114 mTextureSet.SetSampler(0,sampler);
117 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 ) }
186 PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map()
187 .Add( "aPosition", Property::VECTOR3 ) );
188 vertexBuffer.SetData( skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex) );
190 geometry = Geometry::New();
191 geometry.AddVertexBuffer( vertexBuffer );
192 geometry.SetType( Geometry::TRIANGLES );