Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-renderer.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 #include "game-renderer.h"
19 #include "game-model.h"
20 #include "game-texture.h"
21
22 #include <dali/dali.h>
23
24 namespace
25 {
26 // clang-format off
27
28 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
29     attribute highp vec3 aPosition;\n
30     attribute highp vec3 aNormal;\n
31     attribute highp vec2 aTexCoord;\n
32     uniform highp mat4 uMvpMatrix;\n
33     varying highp vec2 vTexCoord;\n
34     void main()\n
35     {\n
36       gl_Position = uMvpMatrix * vec4(aPosition, 1.0 );\n
37       vTexCoord = aTexCoord;\n
38       vTexCoord.y = 1.0 - vTexCoord.y;\n
39     }\n
40 )
41     ;
42 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
43     uniform sampler2D sTexture;\n
44     varying highp vec2 vTexCoord;\n
45     void main()\n
46     {\n
47       gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4(1.2, 1.2, 1.2, 1.0);\n
48     }\n
49 );
50 // clang-format on
51
52 } // namespace
53
54 GameRenderer::GameRenderer()
55 : mModel(NULL),
56   mTexture(NULL)
57 {
58 }
59
60 GameRenderer::~GameRenderer()
61 {
62 }
63
64 void GameRenderer::SetModel(GameModel* model)
65 {
66   mModel = model;
67   Setup();
68 }
69
70 void GameRenderer::SetMainTexture(GameTexture* texture)
71 {
72   mTexture = texture;
73   Setup();
74 }
75
76 void GameRenderer::Setup()
77 {
78   if(!mRenderer && mModel)
79   {
80     Dali::Shader shader = Dali::Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
81     mRenderer           = Dali::Renderer::New(mModel->GetGeometry(), shader);
82     mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_WRITE_MODE, Dali::DepthWriteMode::ON);
83     mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_FUNCTION, Dali::DepthFunction::LESS_EQUAL);
84     mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_TEST_MODE, Dali::DepthTestMode::ON);
85   }
86
87   Dali::TextureSet textureSet;
88   Dali::Geometry   geometry;
89
90   if(mModel)
91   {
92     geometry = mModel->GetGeometry();
93   }
94
95   if(mTexture && mTexture->GetTextureSet())
96   {
97     textureSet = mTexture->GetTextureSet();
98   }
99
100   if(textureSet && geometry)
101   {
102     mRenderer.SetGeometry(geometry);
103     mRenderer.SetTextures(textureSet);
104   }
105 }
106
107 Dali::Renderer& GameRenderer::GetRenderer()
108 {
109   return mRenderer;
110 }