2 * Copyright (c) 2014 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 <dali/internal/render/renderers/render-material.h>
22 #include <dali/internal/common/internal-constants.h>
23 #include <dali/internal/render/gl-resources/texture.h>
24 #include <dali/internal/render/gl-resources/texture-cache.h>
25 #include <dali/internal/render/shaders/program.h>
34 void RenderMaterialUniforms::ResetCustomUniforms()
36 for( unsigned int subType = SHADER_DEFAULT; subType < SHADER_SUBTYPE_LAST; ++subType )
38 for( unsigned int i = 0; i < mNumberOfCustomUniforms; ++i )
40 mCustomUniform[ subType ][ i ].Reset();
45 void RenderMaterialUniforms::SetUniforms( const RenderMaterial& material, Program& program, ShaderSubTypes shaderType )
47 GLint location = Program::UNIFORM_UNKNOWN;
49 location = mCustomUniform[ shaderType ][ 0 ].GetUniformLocation( program, "uMaterial.mOpacity" );
50 if( Program::UNIFORM_UNKNOWN != location )
52 program.SetUniform1f( location, material.mOpacity );
55 location = mCustomUniform[ shaderType ][ 1 ].GetUniformLocation( program, "uMaterial.mShininess" );
56 if( Program::UNIFORM_UNKNOWN != location )
58 program.SetUniform1f( location, material.mShininess );
61 location = mCustomUniform[ shaderType ][ 2 ].GetUniformLocation( program, "uMaterial.mAmbient" );
62 if( Program::UNIFORM_UNKNOWN != location )
64 const Vector4& color = material.mAmbientColor;
65 program.SetUniform4f( location, color.r, color.g, color.b, color.a );
68 location = mCustomUniform[ shaderType ][ 3 ].GetUniformLocation( program, "uMaterial.mDiffuse" );
69 if( Program::UNIFORM_UNKNOWN != location )
71 const Vector4& color = material.mDiffuseColor;
72 program.SetUniform4f( location, color.r, color.g, color.b, color.a );
75 location = mCustomUniform[ shaderType ][ 4 ].GetUniformLocation( program, "uMaterial.mSpecular" );
76 if( Program::UNIFORM_UNKNOWN != location )
78 const Vector4& color = material.mSpecularColor;
79 program.SetUniform4f( location, color.r, color.g, color.b, color.a );
82 location = mCustomUniform[ shaderType ][ 5 ].GetUniformLocation( program, "uMaterial.mEmissive" );
83 if( Program::UNIFORM_UNKNOWN != location )
85 const Vector4& color = material.mEmissiveColor;
86 program.SetUniform4f( location, color.r, color.g, color.b, color.a );
92 RenderMaterial::RenderMaterial()
93 : mTextureCache(NULL),
97 mNormalMapTextureId(0),
99 mDiffuseTexture(NULL),
100 mOpacityTexture(NULL),
101 mNormalMapTexture(NULL),
106 mAmbientColor(Vector4(0.2f, 0.2f, 0.2f, 1.0f)),
107 mDiffuseColor(Vector4(0.8f, 0.8f, 0.8f, 1.0f)),
108 mSpecularColor(Vector4(0.0f, 0.0f, 0.0f, 1.0f)),
109 mEmissiveColor(Vector4(0.0f, 0.0f, 0.0f, 1.0f))
113 RenderMaterial::~RenderMaterial()
115 if(mDiffuseTextureId > 0)
117 mTextureCache->RemoveObserver( mDiffuseTextureId, this );
120 if(mOpacityTextureId > 0)
122 mTextureCache->RemoveObserver( mOpacityTextureId, this );
125 if(mNormalMapTextureId > 0)
127 mTextureCache->RemoveObserver( mNormalMapTextureId, this );
131 void RenderMaterial::Initialize(TextureCache& textureCache)
133 mTextureCache = &textureCache;
136 void RenderMaterial::SetDiffuseTextureId( unsigned int textureId )
138 DALI_ASSERT_DEBUG( NULL != mTextureCache );
140 if(mDiffuseTextureId > 0)
142 mTextureCache->RemoveObserver( mDiffuseTextureId, this );
145 mDiffuseTextureId = textureId;
146 mDiffuseTexture = NULL;
148 if(mDiffuseTextureId > 0)
150 mTextureCache->AddObserver( textureId, this );
154 void RenderMaterial::SetOpacityTextureId( unsigned int textureId )
156 DALI_ASSERT_DEBUG( NULL != mTextureCache );
158 if(mOpacityTextureId > 0)
160 mTextureCache->RemoveObserver( mOpacityTextureId, this );
163 mOpacityTextureId = textureId;
164 mOpacityTexture = NULL;
166 if(mOpacityTextureId > 0)
168 mTextureCache->AddObserver( textureId, this );
172 void RenderMaterial::SetNormalMapTextureId( unsigned int textureId )
174 DALI_ASSERT_DEBUG( NULL != mTextureCache );
176 if(mNormalMapTextureId > 0)
178 mTextureCache->RemoveObserver( mNormalMapTextureId, this );
181 mNormalMapTextureId = textureId;
182 mNormalMapTexture = NULL;
184 if(mNormalMapTextureId > 0)
186 mTextureCache->AddObserver( textureId, this );
190 void RenderMaterial::SetOpacity(float opacity)
195 void RenderMaterial::SetShininess(float shininess)
197 mShininess = shininess;
200 void RenderMaterial::SetAmbientColor( const Vector4& color )
202 mAmbientColor = color;
205 void RenderMaterial::SetDiffuseColor( const Vector4& color )
207 mDiffuseColor = color;
210 void RenderMaterial::SetSpecularColor( const Vector4& color )
212 mSpecularColor = color;
215 void RenderMaterial::SetEmissiveColor( const Vector4& color )
217 mEmissiveColor = color;
220 bool RenderMaterial::HasTexture() const
222 return( mDiffuseTextureId != 0 );
225 void RenderMaterial::SetUniforms( RenderMaterialUniforms& uniforms, Program& program, ShaderSubTypes shaderType ) const
227 uniforms.SetUniforms( *this, program, shaderType );
230 void RenderMaterial::BindTexture( Program& program, ResourceId id, Texture* texture, unsigned int textureUnit, Program::UniformType samplerIndex ) const
232 DALI_ASSERT_DEBUG( NULL != mTextureCache );
234 if( texture != NULL )
236 mTextureCache->BindTexture( texture, id, GL_TEXTURE_2D, GL_TEXTURE0 + textureUnit );
237 // Set sampler uniforms for textures
238 GLint samplerLoc = program.GetUniformLocation( samplerIndex );
239 if( -1 != samplerLoc )
241 program.SetUniform1i( samplerLoc, textureUnit );
244 GLint location = program.GetUniformLocation(Program::UNIFORM_CUSTOM_TEXTURE_COORDS);
245 if( Program::UNIFORM_UNKNOWN != location )
248 texture->GetTextureCoordinates(uvs);
250 // Account for UV mapping on non power of 2 textures
251 program.SetUniform4f(location, uvs.u0, uvs.v0, uvs.u2-uvs.u0, uvs.v2-uvs.v0);
256 void RenderMaterial::BindTextures( Program& program, unsigned int textureSampler )
258 DALI_ASSERT_DEBUG( NULL != mTextureCache );
260 if( mDiffuseTexture == NULL )
262 if( mDiffuseTextureId > 0 )
264 mDiffuseTexture = mTextureCache->GetTexture( mDiffuseTextureId );
268 if( mOpacityTexture == NULL )
270 if( mOpacityTextureId > 0 )
272 mOpacityTexture = mTextureCache->GetTexture( mOpacityTextureId );
276 if( mNormalMapTexture == NULL )
278 if( mNormalMapTextureId > 0 )
280 mNormalMapTexture = mTextureCache->GetTexture( mNormalMapTextureId );
284 BindTexture( program, mDiffuseTextureId, mDiffuseTexture, 0, Program::UNIFORM_SAMPLER );
286 if( mDiffuseTexture )
288 mDiffuseTexture->ApplySampler( textureSampler );
291 // GL_TEXTURE1 is used by shader effect texture
292 BindTexture( program, mOpacityTextureId, mOpacityTexture, 2, Program::UNIFORM_SAMPLER_OPACITY );
294 if( mOpacityTexture )
296 mOpacityTexture->ApplySampler( textureSampler );
299 BindTexture( program, mNormalMapTextureId, mNormalMapTexture, 3, Program::UNIFORM_SAMPLER_NORMAL_MAP );
301 if( mNormalMapTexture )
303 mNormalMapTexture->ApplySampler( textureSampler );
307 void RenderMaterial::TextureDiscarded( unsigned int textureId )
309 if( mDiffuseTextureId == textureId )
311 mDiffuseTextureId = 0;
312 mDiffuseTexture = NULL;
315 if( mOpacityTextureId == textureId )
317 mOpacityTextureId = 0;
318 mOpacityTexture = NULL;
321 if( mNormalMapTextureId == textureId )
323 mNormalMapTextureId = 0;
324 mNormalMapTexture = NULL;