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