Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / rendering-basic-pbr / model-skybox.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-skybox.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 /*
32  * Vertex shader for a skybox
33  */
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
37 \n
38 varying mediump vec3 vTexCoord;\n
39 void main()\n
40 {\n
41   vTexCoord =  aPosition;\n
42
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.
48 }\n
49 );
50
51 /*
52  * Fragment shader for a skybox
53  */
54 const char* FRAGMENT_SHADER_SKYBOX = DALI_COMPOSE_SHADER(
55 uniform samplerCube uSkyBoxTexture;\n
56 \n
57 varying mediump vec3 vTexCoord;\n
58 void main()\n
59 {\n
60   mediump vec4 texColor = textureCube( uSkyBoxTexture, vTexCoord, 0.0);\n
61   gl_FragColor = texColor;\n
62 }\n
63 );
64
65 } // namespace
66
67
68 ModelSkybox::ModelSkybox()
69 {
70 }
71
72 ModelSkybox::~ModelSkybox()
73 {
74 }
75
76 void ModelSkybox::Init( const Vector3& size )
77 {
78
79   Geometry geometry = CreateGeometry();
80   Shader shader = Shader::New( VERTEX_SHADER_SKYBOX, FRAGMENT_SHADER_SKYBOX );
81
82   Renderer renderer = Renderer::New( geometry, shader );
83
84   if( mTextureSet )
85   {
86     renderer.SetTextures( mTextureSet );
87   }
88
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 );
95
96   mActor = Actor::New();
97   mActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
98   mActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
99   mActor.SetProperty( Actor::Property::SIZE, size );
100   mActor.AddRenderer( renderer );
101 }
102
103 /**
104  * Set texture and sampler
105  */
106 void ModelSkybox::InitTexture(Texture texSkybox)
107 {
108   mTextureSet = TextureSet::New();
109   mTextureSet.SetTexture( 0u, texSkybox );
110
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);
115 }
116
117 Actor& ModelSkybox::GetActor()
118 {
119   return mActor;
120 }
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
186   PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map()
187                                                      .Add( "aPosition", Property::VECTOR3 ) );
188   vertexBuffer.SetData( skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex) );
189
190   geometry = Geometry::New();
191   geometry.AddVertexBuffer( vertexBuffer );
192   geometry.SetType( Geometry::TRIANGLES );
193   return geometry;
194 }