2011-10-25 05:03:10 +00:00
//
// KRShader.cpp
// KREngine
//
2012-03-15 20:09:01 +00:00
// Copyright 2012 Kearwood Gilbert. All rights reserved.
2011-10-25 06:16:47 +00:00
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
2011-10-25 05:03:10 +00:00
//
# include "KRShader.h"
2013-01-11 03:21:19 +00:00
# include "assert.h"
2012-11-16 02:58:23 +00:00
# include "KRLight.h"
# include "KRDirectionalLight.h"
# include "KRSpotLight.h"
# include "KRPointLight.h"
2011-10-25 05:03:10 +00:00
2012-12-14 00:51:45 +00:00
const char * KRShader : : KRENGINE_UNIFORM_NAMES [ ] = {
" material_ambient " , // KRENGINE_UNIFORM_MATERIAL_AMBIENT
" material_diffuse " , // KRENGINE_UNIFORM_MATERIAL_DIFFUSE
" material_specular " , // KRENGINE_UNIFORM_MATERIAL_SPECULAR
" material_reflection " , // KRENGINE_UNIFORM_MATERIAL_REFLECTION
" material_alpha " , // KRENGINE_UNIFORM_MATERIAL_ALPHA
" material_shininess " , // KRENGINE_UNIFORM_MATERIAL_SHININESS
" light_position " , // KRENGINE_UNIFORM_LIGHT_POSITION
" light_direction_model_space " , // KRENGINE_UNIFORM_LIGHT_DIRECTION_MODEL_SPACE
" light_direction_view_space " , // KRENGINE_UNIFORM_LIGHT_DIRECTION_VIEW_SPACE
" light_color " , // KRENGINE_UNIFORM_LIGHT_COLOR
" light_decay_start " , // KRENGINE_UNIFORM_LIGHT_DECAY_START
" light_cutoff " , // KRENGINE_UNIFORM_LIGHT_CUTOFF
" light_intensity " , // KRENGINE_UNIFORM_LIGHT_INTENSITY
" flare_size " , // KRENGINE_UNIFORM_FLARE_SIZE
" view_space_model_origin " , // KRENGINE_UNIFORM_VIEW_SPACE_MODEL_ORIGIN
" mvp_matrix " , // KRENGINE_UNIFORM_MVP
" inv_projection_matrix " , // KRENGINE_UNIFORM_INVP
" inv_mvp_matrix " , // KRENGINE_UNIFORM_INVMVP
" inv_mvp_matrix_no_translate " , // KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE
" model_view_inverse_transpose_matrix " , // KRENGINE_UNIFORM_MODEL_VIEW_INVERSE_TRANSPOSE
" model_inverse_transpose_matrix " , // KRENGINE_UNIFORM_MODEL_INVERSE_TRANSPOSE
" model_view_matrix " , // KRENGINE_UNIFORM_MODEL_VIEW
" model_matrix " , // KRENGINE_UNIFORM_MODEL_MATRIX
" projection_matrix " , // KRENGINE_UNIFORM_PROJECTION_MATRIX
" camera_position_model_space " , // KRENGINE_UNIFORM_CAMERAPOS_MODEL_SPACE
" viewport " , // KRENGINE_UNIFORM_VIEWPORT
2014-04-29 00:30:14 -07:00
" viewport_downsample " , // KRENGINE_UNIFORM_VIEWPORT_DOWNSAMPLE
2012-12-14 00:51:45 +00:00
" diffuseTexture " , // KRENGINE_UNIFORM_DIFFUSETEXTURE
" specularTexture " , // KRENGINE_UNIFORM_SPECULARTEXTURE
" reflectionCubeTexture " , // KRENGINE_UNIFORM_REFLECTIONCUBETEXTURE
" reflectionTexture " , // KRENGINE_UNIFORM_REFLECTIONTEXTURE
" normalTexture " , // KRENGINE_UNIFORM_NORMALTEXTURE
" diffuseTexture_Scale " , // KRENGINE_UNIFORM_DIFFUSETEXTURE_SCALE
" specularTexture_Scale " , // KRENGINE_UNIFORM_SPECULARTEXTURE_SCALE
" reflectionTexture_Scale " , // KRENGINE_UNIFORM_REFLECTIONTEXTURE_SCALE
" normalTexture_Scale " , // KRENGINE_UNIFORM_NORMALTEXTURE_SCALE
" normalTexture_Scale " , // KRENGINE_UNIFORM_AMBIENTTEXTURE_SCALE
" diffuseTexture_Offset " , // KRENGINE_UNIFORM_DIFFUSETEXTURE_OFFSET
" specularTexture_Offset " , // KRENGINE_UNIFORM_SPECULARTEXTURE_OFFSET
" reflectionTexture_Offset " , // KRENGINE_UNIFORM_REFLECTIONTEXTURE_OFFSET
" normalTexture_Offset " , // KRENGINE_UNIFORM_NORMALTEXTURE_OFFSET
" ambientTexture_Offset " , // KRENGINE_UNIFORM_AMBIENTTEXTURE_OFFSET
" shadow_mvp1 " , // KRENGINE_UNIFORM_SHADOWMVP1
" shadow_mvp2 " , // KRENGINE_UNIFORM_SHADOWMVP2
" shadow_mvp3 " , // KRENGINE_UNIFORM_SHADOWMVP3
" shadowTexture1 " , // KRENGINE_UNIFORM_SHADOWTEXTURE1
" shadowTexture2 " , // KRENGINE_UNIFORM_SHADOWTEXTURE2
" shadowTexture3 " , // KRENGINE_UNIFORM_SHADOWTEXTURE3
" lightmapTexture " , // KRENGINE_UNIFORM_LIGHTMAPTEXTURE
" gbuffer_frame " , // KRENGINE_UNIFORM_GBUFFER_FRAME
" gbuffer_depth " , // KRENGINE_UNIFORM_GBUFFER_DEPTH
" depthFrame " , // KRENGINE_UNIFORM_DEPTH_FRAME
" volumetricEnvironmentFrame " , // KRENGINE_UNIFORM_VOLUMETRIC_ENVIRONMENT_FRAME
" renderFrame " , // KRENGINE_UNIFORM_RENDER_FRAME
" time_absolute " , // KRENGINE_UNIFORM_ABSOLUTE_TIME
" fog_near " , // KRENGINE_UNIFORM_FOG_NEAR
" fog_far " , // KRENGINE_UNIFORM_FOG_FAR
" fog_density " , // KRENGINE_UNIFORM_FOG_DENSITY
" fog_color " , // KRENGINE_UNIFORM_FOG_COLOR
" fog_scale " , // KRENGINE_UNIFORM_FOG_SCALE
" fog_density_premultiplied_exponential " , // KRENGINE_UNIFORM_DENSITY_PREMULTIPLIED_EXPONENTIAL
" fog_density_premultiplied_squared " , // KRENGINE_UNIFORM_DENSITY_PREMULTIPLIED_SQUARED
" slice_depth_scale " , // KRENGINE_UNIFORM_SLICE_DEPTH_SCALE
" particle_origin " , // KRENGINE_UNIFORM_PARTICLE_ORIGIN
2013-12-05 01:09:31 -08:00
" bone_transforms " , // KRENGINE_UNIFORM_BONE_TRANSFORMS
" rim_color " , // KRENGINE_UNIFORM_RIM_COLOR
2014-06-25 00:45:00 -07:00
" rim_power " , // KRENGINE_UNIFORM_RIM_POWER
" fade_color " , // KRENGINE_UNIFORM_FADE_COLOR
2012-12-14 00:51:45 +00:00
} ;
2012-11-15 22:05:25 +00:00
KRShader : : KRShader ( KRContext & context , char * szKey , std : : string options , std : : string vertShaderSource , const std : : string fragShaderSource ) : KRContextObject ( context )
{
2012-03-15 20:09:01 +00:00
strcpy ( m_szKey , szKey ) ;
2011-10-25 05:03:10 +00:00
m_iProgram = 0 ;
2013-05-08 17:47:27 -07:00
2011-10-25 05:03:10 +00:00
GLuint vertexShader = 0 , fragShader = 0 ;
try {
2012-04-13 23:24:07 +00:00
const GLchar * vertSource [ 2 ] = { options . c_str ( ) , vertShaderSource . c_str ( ) } ;
const GLchar * fragSource [ 2 ] = { options . c_str ( ) , fragShaderSource . c_str ( ) } ;
2011-10-25 05:03:10 +00:00
// Create shader program.
2012-09-13 20:09:19 +00:00
GLDEBUG ( m_iProgram = glCreateProgram ( ) ) ;
2011-10-25 05:03:10 +00:00
// Create and compile vertex shader.
2012-09-13 20:09:19 +00:00
GLDEBUG ( vertexShader = glCreateShader ( GL_VERTEX_SHADER ) ) ;
GLDEBUG ( glShaderSource ( vertexShader , 2 , vertSource , NULL ) ) ;
GLDEBUG ( glCompileShader ( vertexShader ) ) ;
2011-10-25 05:03:10 +00:00
2012-04-13 22:48:13 +00:00
// Report any compile issues to stderr
2017-04-27 17:48:12 -07:00
GLint logLength = 0 ;
2012-09-13 20:09:19 +00:00
GLDEBUG ( glGetShaderiv ( vertexShader , GL_INFO_LOG_LENGTH , & logLength ) ) ;
2012-04-13 22:48:13 +00:00
if ( logLength > 0 ) {
2013-12-14 16:06:44 -08:00
GLchar * log = ( GLchar * ) malloc ( logLength + 1 ) ;
2012-09-20 09:32:20 +00:00
assert ( log ! = NULL ) ;
2017-04-27 17:48:12 -07:00
log [ 0 ] = ' \0 ' ; // In case glGetShaderInfoLog fails
2012-09-13 20:09:19 +00:00
GLDEBUG ( glGetShaderInfoLog ( vertexShader , logLength , & logLength , log ) ) ;
2013-12-14 16:06:44 -08:00
log [ logLength ] = ' \0 ' ;
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " KREngine - Failed to compile vertex shader: %s \n Shader compile log: \n %s " , szKey , log ) ;
2012-04-13 22:48:13 +00:00
free ( log ) ;
}
2011-10-25 05:03:10 +00:00
// Create and compile vertex shader.
2012-09-13 20:09:19 +00:00
GLDEBUG ( fragShader = glCreateShader ( GL_FRAGMENT_SHADER ) ) ;
GLDEBUG ( glShaderSource ( fragShader , 2 , fragSource , NULL ) ) ;
GLDEBUG ( glCompileShader ( fragShader ) ) ;
2011-10-25 05:03:10 +00:00
2012-04-13 22:48:13 +00:00
// Report any compile issues to stderr
2017-04-27 17:48:12 -07:00
logLength = 0 ; // In case glGetShaderiv fails
2012-09-13 20:09:19 +00:00
GLDEBUG ( glGetShaderiv ( fragShader , GL_INFO_LOG_LENGTH , & logLength ) ) ;
2012-04-13 22:48:13 +00:00
if ( logLength > 0 ) {
2013-12-14 16:06:44 -08:00
GLchar * log = ( GLchar * ) malloc ( logLength + 1 ) ;
2012-09-20 09:32:20 +00:00
assert ( log ! = NULL ) ;
2017-04-27 17:48:12 -07:00
log [ 0 ] = ' \0 ' ; // In case glGetShaderInfoLog fails
2012-09-13 20:09:19 +00:00
GLDEBUG ( glGetShaderInfoLog ( fragShader , logLength , & logLength , log ) ) ;
2013-12-14 16:06:44 -08:00
log [ logLength ] = ' \0 ' ;
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " KREngine - Failed to compile fragment shader: %s \n Shader compile log: \n %s " , szKey , log ) ;
2012-04-13 22:48:13 +00:00
free ( log ) ;
}
2011-10-25 05:03:10 +00:00
// Attach vertex shader to program.
2012-09-13 20:09:19 +00:00
GLDEBUG ( glAttachShader ( m_iProgram , vertexShader ) ) ;
2011-10-25 05:03:10 +00:00
// Attach fragment shader to program.
2012-09-13 20:09:19 +00:00
GLDEBUG ( glAttachShader ( m_iProgram , fragShader ) ) ;
2011-10-25 05:03:10 +00:00
// Bind attribute locations.
// This needs to be done prior to linking.
2013-01-09 22:37:23 +00:00
GLDEBUG ( glBindAttribLocation ( m_iProgram , KRMesh : : KRENGINE_ATTRIB_VERTEX , " vertex_position " ) ) ;
GLDEBUG ( glBindAttribLocation ( m_iProgram , KRMesh : : KRENGINE_ATTRIB_NORMAL , " vertex_normal " ) ) ;
GLDEBUG ( glBindAttribLocation ( m_iProgram , KRMesh : : KRENGINE_ATTRIB_TANGENT , " vertex_tangent " ) ) ;
GLDEBUG ( glBindAttribLocation ( m_iProgram , KRMesh : : KRENGINE_ATTRIB_TEXUVA , " vertex_uv " ) ) ;
GLDEBUG ( glBindAttribLocation ( m_iProgram , KRMesh : : KRENGINE_ATTRIB_TEXUVB , " vertex_lightmap_uv " ) ) ;
GLDEBUG ( glBindAttribLocation ( m_iProgram , KRMesh : : KRENGINE_ATTRIB_BONEINDEXES , " bone_indexes " ) ) ;
GLDEBUG ( glBindAttribLocation ( m_iProgram , KRMesh : : KRENGINE_ATTRIB_BONEWEIGHTS , " bone_weights " ) ) ;
2011-10-25 05:03:10 +00:00
// Link program.
2012-09-13 20:09:19 +00:00
GLDEBUG ( glLinkProgram ( m_iProgram ) ) ;
2011-10-25 05:03:10 +00:00
2012-09-20 09:32:20 +00:00
GLint link_success = GL_FALSE ;
GLDEBUG ( glGetProgramiv ( m_iProgram , GL_LINK_STATUS , & link_success ) ) ;
if ( link_success ! = GL_TRUE ) {
// Report any linking issues to stderr
2013-12-14 16:06:44 -08:00
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " KREngine - Failed to link shader program: %s " , szKey ) ;
2017-04-27 17:48:12 -07:00
logLength = 0 ; // In case glGetProgramiv fails
2012-09-20 09:32:20 +00:00
GLDEBUG ( glGetProgramiv ( m_iProgram , GL_INFO_LOG_LENGTH , & logLength ) ) ;
if ( logLength > 0 )
{
2013-12-14 16:06:44 -08:00
GLchar * log = ( GLchar * ) malloc ( logLength + 1 ) ;
2012-09-20 09:32:20 +00:00
assert ( log ! = NULL ) ;
2017-04-27 17:48:12 -07:00
log [ 0 ] = ' \0 ' ; // In case glGetProgramInfoLog fails
2012-09-20 09:32:20 +00:00
GLDEBUG ( glGetProgramInfoLog ( m_iProgram , logLength , & logLength , log ) ) ;
2013-12-14 16:06:44 -08:00
log [ logLength ] = ' \0 ' ;
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " Program link log: \n %s " , log ) ;
2012-09-20 09:32:20 +00:00
free ( log ) ;
}
GLDEBUG ( glDeleteProgram ( m_iProgram ) ) ;
m_iProgram = 0 ;
} else {
2012-04-13 22:48:13 +00:00
2012-09-20 09:32:20 +00:00
// Get uniform locations
2012-12-14 00:51:45 +00:00
for ( int i = 0 ; i < KRENGINE_NUM_UNIFORMS ; i + + ) {
GLDEBUG ( m_uniforms [ i ] = glGetUniformLocation ( m_iProgram , KRENGINE_UNIFORM_NAMES [ i ] ) ) ;
2013-05-08 17:47:27 -07:00
m_uniform_value_index [ i ] = - 1 ;
2012-12-14 00:51:45 +00:00
}
2012-09-20 09:32:20 +00:00
}
2012-09-19 20:26:30 +00:00
2011-10-25 05:03:10 +00:00
} catch ( . . . ) {
if ( vertexShader ) {
2012-09-13 20:09:19 +00:00
GLDEBUG ( glDeleteShader ( vertexShader ) ) ;
2011-10-25 05:03:10 +00:00
vertexShader = 0 ;
}
if ( fragShader ) {
2012-09-13 20:09:19 +00:00
GLDEBUG ( glDeleteShader ( fragShader ) ) ;
2011-10-25 05:03:10 +00:00
fragShader = 0 ;
}
if ( m_iProgram ) {
2012-09-13 20:09:19 +00:00
GLDEBUG ( glDeleteProgram ( m_iProgram ) ) ;
2011-10-25 05:03:10 +00:00
m_iProgram = 0 ;
}
}
// Release vertex and fragment shaders.
if ( vertexShader ) {
2012-09-13 20:09:19 +00:00
GLDEBUG ( glDeleteShader ( vertexShader ) ) ;
2011-10-25 05:03:10 +00:00
}
if ( fragShader ) {
2012-09-13 20:09:19 +00:00
GLDEBUG ( glDeleteShader ( fragShader ) ) ;
2011-10-25 05:03:10 +00:00
}
}
KRShader : : ~ KRShader ( ) {
if ( m_iProgram ) {
2012-09-13 20:09:19 +00:00
GLDEBUG ( glDeleteProgram ( m_iProgram ) ) ;
2013-05-08 17:47:27 -07:00
if ( getContext ( ) . getShaderManager ( ) - > m_active_shader = = this ) {
getContext ( ) . getShaderManager ( ) - > m_active_shader = NULL ;
}
}
}
void KRShader : : setUniform ( int location , float value )
{
if ( m_uniforms [ location ] ! = - 1 ) {
int value_index = m_uniform_value_index [ location ] ;
bool needs_update = true ;
if ( value_index = = - 1 ) {
m_uniform_value_index [ location ] = m_uniform_value_float . size ( ) ;
m_uniform_value_float . push_back ( value ) ;
} else if ( m_uniform_value_float [ value_index ] = = value ) {
needs_update = false ;
} else {
m_uniform_value_float [ value_index ] = value ;
}
if ( needs_update ) {
GLDEBUG ( glUniform1f ( m_uniforms [ location ] , value ) ) ;
}
}
}
void KRShader : : setUniform ( int location , int value )
{
if ( m_uniforms [ location ] ! = - 1 ) {
int value_index = m_uniform_value_index [ location ] ;
bool needs_update = true ;
if ( value_index = = - 1 ) {
m_uniform_value_index [ location ] = m_uniform_value_int . size ( ) ;
m_uniform_value_int . push_back ( value ) ;
} else if ( m_uniform_value_int [ value_index ] = = value ) {
needs_update = false ;
} else {
m_uniform_value_int [ value_index ] = value ;
}
if ( needs_update ) {
GLDEBUG ( glUniform1i ( m_uniforms [ location ] , value ) ) ;
}
}
}
void KRShader : : setUniform ( int location , const KRVector2 & value )
{
if ( m_uniforms [ location ] ! = - 1 ) {
int value_index = m_uniform_value_index [ location ] ;
bool needs_update = true ;
if ( value_index = = - 1 ) {
m_uniform_value_index [ location ] = m_uniform_value_vector2 . size ( ) ;
m_uniform_value_vector2 . push_back ( value ) ;
} else if ( m_uniform_value_vector2 [ value_index ] = = value ) {
needs_update = false ;
} else {
m_uniform_value_vector2 [ value_index ] = value ;
}
if ( needs_update ) {
GLDEBUG ( glUniform2f ( m_uniforms [ location ] , value . x , value . y ) ) ;
}
}
}
void KRShader : : setUniform ( int location , const KRVector3 & value )
{
if ( m_uniforms [ location ] ! = - 1 ) {
int value_index = m_uniform_value_index [ location ] ;
bool needs_update = true ;
if ( value_index = = - 1 ) {
m_uniform_value_index [ location ] = m_uniform_value_vector3 . size ( ) ;
m_uniform_value_vector3 . push_back ( value ) ;
} else if ( m_uniform_value_vector3 [ value_index ] = = value ) {
needs_update = false ;
} else {
m_uniform_value_vector3 [ value_index ] = value ;
}
if ( needs_update ) {
GLDEBUG ( glUniform3f ( m_uniforms [ location ] , value . x , value . y , value . z ) ) ;
}
}
}
void KRShader : : setUniform ( int location , const KRVector4 & value )
{
if ( m_uniforms [ location ] ! = - 1 ) {
int value_index = m_uniform_value_index [ location ] ;
bool needs_update = true ;
if ( value_index = = - 1 ) {
m_uniform_value_index [ location ] = m_uniform_value_vector4 . size ( ) ;
m_uniform_value_vector4 . push_back ( value ) ;
} else if ( m_uniform_value_vector4 [ value_index ] = = value ) {
needs_update = false ;
} else {
m_uniform_value_vector4 [ value_index ] = value ;
}
if ( needs_update ) {
GLDEBUG ( glUniform4f ( m_uniforms [ location ] , value . x , value . y , value . z , value . w ) ) ;
}
}
}
void KRShader : : setUniform ( int location , const KRMat4 & value )
{
if ( m_uniforms [ location ] ! = - 1 ) {
int value_index = m_uniform_value_index [ location ] ;
bool needs_update = true ;
if ( value_index = = - 1 ) {
m_uniform_value_index [ location ] = m_uniform_value_mat4 . size ( ) ;
m_uniform_value_mat4 . push_back ( value ) ;
} else if ( m_uniform_value_mat4 [ value_index ] = = value ) {
needs_update = false ;
} else {
m_uniform_value_mat4 [ value_index ] = value ;
}
if ( needs_update ) {
GLDEBUG ( glUniformMatrix4fv ( m_uniforms [ location ] , 1 , GL_FALSE , value . c ) ) ;
}
2011-10-25 05:03:10 +00:00
}
}
2014-06-25 00:45:00 -07:00
bool KRShader : : bind ( KRCamera & camera , const KRViewport & viewport , const KRMat4 & matModel , const std : : vector < KRPointLight * > & point_lights , const std : : vector < KRDirectionalLight * > & directional_lights , const std : : vector < KRSpotLight * > & spot_lights , const KRNode : : RenderPass & renderPass , const KRVector3 & rim_color , float rim_power , const KRVector4 & fade_color ) {
2012-09-20 09:32:20 +00:00
if ( m_iProgram = = 0 ) {
return false ;
}
2012-11-15 22:05:25 +00:00
2013-05-24 12:20:47 -07:00
bool shander_changed = false ;
2013-05-08 17:47:27 -07:00
if ( getContext ( ) . getShaderManager ( ) - > m_active_shader ! = this ) {
getContext ( ) . getShaderManager ( ) - > m_active_shader = this ;
GLDEBUG ( glUseProgram ( m_iProgram ) ) ;
2013-05-24 12:20:47 -07:00
shander_changed = true ;
2013-05-08 17:47:27 -07:00
}
2012-11-23 01:02:22 +00:00
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_ABSOLUTE_TIME , getContext ( ) . getAbsoluteTime ( ) ) ;
2012-11-23 01:02:22 +00:00
2012-11-16 02:58:23 +00:00
int light_directional_count = 0 ;
2017-04-27 17:48:12 -07:00
//int light_point_count = 0;
//int light_spot_count = 0;
2012-11-21 22:47:33 +00:00
// TODO - Need to support multiple lights and more light types in forward rendering
2013-03-21 19:58:35 -07:00
if ( renderPass ! = KRNode : : RENDER_PASS_DEFERRED_LIGHTS & & renderPass ! = KRNode : : RENDER_PASS_DEFERRED_GBUFFER & & renderPass ! = KRNode : : RENDER_PASS_DEFERRED_OPAQUE & & renderPass ! = KRNode : : RENDER_PASS_GENERATE_SHADOWMAPS ) {
2013-04-25 16:21:28 -07:00
for ( std : : vector < KRDirectionalLight * > : : const_iterator light_itr = directional_lights . begin ( ) ; light_itr ! = directional_lights . end ( ) ; light_itr + + ) {
KRDirectionalLight * directional_light = ( * light_itr ) ;
if ( light_directional_count = = 0 ) {
int cShadowBuffers = directional_light - > getShadowBufferCount ( ) ;
if ( m_uniforms [ KRENGINE_UNIFORM_SHADOWTEXTURE1 ] ! = - 1 & & cShadowBuffers > 0 ) {
2014-06-01 00:58:28 -07:00
if ( m_pContext - > getTextureManager ( ) - > selectTexture ( GL_TEXTURE_2D , 3 , directional_light - > getShadowTextures ( ) [ 0 ] ) ) {
GLDEBUG ( glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ) ;
GLDEBUG ( glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ) ;
}
2013-04-25 16:21:28 -07:00
m_pContext - > getTextureManager ( ) - > _setWrapModeS ( 3 , GL_CLAMP_TO_EDGE ) ;
m_pContext - > getTextureManager ( ) - > _setWrapModeT ( 3 , GL_CLAMP_TO_EDGE ) ;
}
2012-11-16 02:58:23 +00:00
2013-04-25 16:21:28 -07:00
if ( m_uniforms [ KRENGINE_UNIFORM_SHADOWTEXTURE2 ] ! = - 1 & & cShadowBuffers > 1 & & camera . settings . m_cShadowBuffers > 1 ) {
2014-06-01 00:58:28 -07:00
if ( m_pContext - > getTextureManager ( ) - > selectTexture ( GL_TEXTURE_2D , 4 , directional_light - > getShadowTextures ( ) [ 1 ] ) ) {
GLDEBUG ( glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ) ;
GLDEBUG ( glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ) ;
}
2013-04-25 16:21:28 -07:00
m_pContext - > getTextureManager ( ) - > _setWrapModeS ( 4 , GL_CLAMP_TO_EDGE ) ;
m_pContext - > getTextureManager ( ) - > _setWrapModeT ( 4 , GL_CLAMP_TO_EDGE ) ;
}
2012-11-16 02:58:23 +00:00
2013-04-25 16:21:28 -07:00
if ( m_uniforms [ KRENGINE_UNIFORM_SHADOWTEXTURE3 ] ! = - 1 & & cShadowBuffers > 2 & & camera . settings . m_cShadowBuffers > 2 ) {
2014-06-01 00:58:28 -07:00
if ( m_pContext - > getTextureManager ( ) - > selectTexture ( GL_TEXTURE_2D , 5 , directional_light - > getShadowTextures ( ) [ 2 ] ) ) {
GLDEBUG ( glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ) ;
GLDEBUG ( glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ) ;
}
2013-04-25 16:21:28 -07:00
m_pContext - > getTextureManager ( ) - > _setWrapModeS ( 5 , GL_CLAMP_TO_EDGE ) ;
m_pContext - > getTextureManager ( ) - > _setWrapModeT ( 5 , GL_CLAMP_TO_EDGE ) ;
2012-11-16 02:58:23 +00:00
}
2013-04-25 16:21:28 -07:00
KRMat4 matBias ;
matBias . translate ( 1.0 , 1.0 , 1.0 ) ;
matBias . scale ( 0.5 ) ;
for ( int iShadow = 0 ; iShadow < cShadowBuffers ; iShadow + + ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_SHADOWMVP1 + iShadow , matModel * directional_light - > getShadowViewports ( ) [ iShadow ] . getViewProjectionMatrix ( ) * matBias ) ;
2013-04-25 16:21:28 -07:00
}
if ( m_uniforms [ KRENGINE_UNIFORM_LIGHT_DIRECTION_MODEL_SPACE ] ! = - 1 ) {
KRMat4 inverseModelMatrix = matModel ;
inverseModelMatrix . invert ( ) ;
// Bind the light direction vector
KRVector3 lightDirObject = KRMat4 : : Dot ( inverseModelMatrix , directional_light - > getWorldLightDirection ( ) ) ;
lightDirObject . normalize ( ) ;
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_LIGHT_DIRECTION_MODEL_SPACE , lightDirObject ) ;
2013-04-25 16:21:28 -07:00
}
2012-11-16 02:58:23 +00:00
}
2013-04-25 16:21:28 -07:00
light_directional_count + + ;
2012-11-16 02:58:23 +00:00
}
2013-04-25 16:21:28 -07:00
2017-04-27 17:48:12 -07:00
//light_point_count = point_lights.size();
//light_spot_count = spot_lights.size();
2012-11-15 22:05:25 +00:00
}
2012-11-16 02:58:23 +00:00
2012-09-20 09:32:20 +00:00
2012-11-16 02:58:23 +00:00
if ( m_uniforms [ KRENGINE_UNIFORM_CAMERAPOS_MODEL_SPACE ] ! = - 1 ) {
2012-10-20 02:15:48 +00:00
KRMat4 inverseModelMatrix = matModel ;
inverseModelMatrix . invert ( ) ;
if ( m_uniforms [ KRENGINE_UNIFORM_CAMERAPOS_MODEL_SPACE ] ! = - 1 ) {
// Transform location of camera to object space for calculation of specular halfVec
2012-10-26 19:31:27 +00:00
KRVector3 cameraPosObject = KRMat4 : : Dot ( inverseModelMatrix , viewport . getCameraPosition ( ) ) ;
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_CAMERAPOS_MODEL_SPACE , cameraPosObject ) ;
2012-10-20 02:15:48 +00:00
}
}
2012-09-20 09:32:20 +00:00
2012-10-26 01:17:35 +00:00
if ( m_uniforms [ KRENGINE_UNIFORM_MVP ] ! = - 1 | | m_uniforms [ KRShader : : KRENGINE_UNIFORM_INVMVP ] ! = - 1 ) {
// Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram
2012-10-26 19:31:27 +00:00
KRMat4 mvpMatrix = matModel * viewport . getViewProjectionMatrix ( ) ;
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_MVP , mvpMatrix ) ;
2012-10-26 01:17:35 +00:00
if ( m_uniforms [ KRShader : : KRENGINE_UNIFORM_INVMVP ] ! = - 1 ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRShader : : KRENGINE_UNIFORM_INVMVP , KRMat4 : : Invert ( mvpMatrix ) ) ;
2012-10-26 01:17:35 +00:00
}
}
2012-10-12 00:02:24 +00:00
2012-11-16 02:58:23 +00:00
if ( m_uniforms [ KRShader : : KRENGINE_UNIFORM_VIEW_SPACE_MODEL_ORIGIN ] ! = - 1 | | m_uniforms [ KRENGINE_UNIFORM_MODEL_VIEW_INVERSE_TRANSPOSE ] ! = - 1 | | m_uniforms [ KRShader : : KRENGINE_UNIFORM_MODEL_VIEW ] ! = - 1 ) {
2012-10-26 01:17:35 +00:00
KRMat4 matModelView = matModel * viewport . getViewMatrix ( ) ;
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_MODEL_VIEW , matModelView ) ;
2012-10-20 02:15:48 +00:00
2012-11-16 02:58:23 +00:00
if ( m_uniforms [ KRShader : : KRENGINE_UNIFORM_VIEW_SPACE_MODEL_ORIGIN ] ! = - 1 ) {
2012-10-20 02:15:48 +00:00
KRVector3 view_space_model_origin = KRMat4 : : Dot ( matModelView , KRVector3 : : Zero ( ) ) ; // Origin point of model space is the light source position. No perspective, so no w divide required
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_VIEW_SPACE_MODEL_ORIGIN , view_space_model_origin ) ;
2012-10-20 02:15:48 +00:00
}
if ( m_uniforms [ KRENGINE_UNIFORM_MODEL_VIEW_INVERSE_TRANSPOSE ] ! = - 1 ) {
KRMat4 matModelViewInverseTranspose = matModelView ;
matModelViewInverseTranspose . transpose ( ) ;
matModelViewInverseTranspose . invert ( ) ;
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_MODEL_VIEW_INVERSE_TRANSPOSE , matModelViewInverseTranspose ) ;
2012-10-20 02:15:48 +00:00
}
}
2012-10-12 00:02:24 +00:00
2012-10-20 02:15:48 +00:00
if ( m_uniforms [ KRENGINE_UNIFORM_MODEL_INVERSE_TRANSPOSE ] ! = - 1 ) {
KRMat4 matModelInverseTranspose = matModel ;
matModelInverseTranspose . transpose ( ) ;
matModelInverseTranspose . invert ( ) ;
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_MODEL_INVERSE_TRANSPOSE , matModelInverseTranspose ) ;
2012-10-20 02:15:48 +00:00
}
2012-10-10 22:09:28 +00:00
2012-10-20 02:15:48 +00:00
if ( m_uniforms [ KRShader : : KRENGINE_UNIFORM_INVP ] ! = - 1 ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_INVP , viewport . getInverseProjectionMatrix ( ) ) ;
2012-10-20 02:15:48 +00:00
}
2012-10-10 22:09:28 +00:00
2012-10-20 02:15:48 +00:00
if ( m_uniforms [ KRShader : : KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE ] ! = - 1 ) {
2012-10-26 01:17:35 +00:00
KRMat4 matInvMVPNoTranslate = matModel * viewport . getViewMatrix ( ) ; ;
2012-10-20 02:15:48 +00:00
// Remove the translation
matInvMVPNoTranslate . getPointer ( ) [ 3 ] = 0 ;
matInvMVPNoTranslate . getPointer ( ) [ 7 ] = 0 ;
matInvMVPNoTranslate . getPointer ( ) [ 11 ] = 0 ;
matInvMVPNoTranslate . getPointer ( ) [ 12 ] = 0 ;
matInvMVPNoTranslate . getPointer ( ) [ 13 ] = 0 ;
matInvMVPNoTranslate . getPointer ( ) [ 14 ] = 0 ;
matInvMVPNoTranslate . getPointer ( ) [ 15 ] = 1.0 ;
2012-10-26 01:17:35 +00:00
matInvMVPNoTranslate = matInvMVPNoTranslate * viewport . getProjectionMatrix ( ) ;
2012-10-20 02:15:48 +00:00
matInvMVPNoTranslate . invert ( ) ;
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE , matInvMVPNoTranslate ) ;
2012-10-20 02:15:48 +00:00
}
2012-10-10 22:09:28 +00:00
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_MODEL_MATRIX , matModel ) ;
2012-11-09 09:18:38 +00:00
if ( m_uniforms [ KRENGINE_UNIFORM_PROJECTION_MATRIX ] ! = - 1 ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_PROJECTION_MATRIX , viewport . getProjectionMatrix ( ) ) ;
2012-11-09 09:18:38 +00:00
}
2012-10-12 00:02:24 +00:00
2012-10-20 02:15:48 +00:00
if ( m_uniforms [ KRENGINE_UNIFORM_VIEWPORT ] ! = - 1 ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_VIEWPORT , KRVector4 (
2014-04-29 00:30:14 -07:00
( GLfloat ) 0.0 ,
( GLfloat ) 0.0 ,
( GLfloat ) viewport . getSize ( ) . x ,
( GLfloat ) viewport . getSize ( ) . y
2013-05-08 17:47:27 -07:00
)
) ;
2012-10-12 00:02:24 +00:00
}
2012-04-19 23:39:32 +00:00
2014-04-29 00:30:14 -07:00
if ( m_uniforms [ KRENGINE_UNIFORM_VIEWPORT_DOWNSAMPLE ] ! = - 1 ) {
setUniform ( KRENGINE_UNIFORM_VIEWPORT_DOWNSAMPLE , camera . getDownsample ( ) ) ;
}
2013-12-05 01:09:31 -08:00
// Rim highlighting parameters
setUniform ( KRENGINE_UNIFORM_RIM_COLOR , rim_color ) ;
setUniform ( KRENGINE_UNIFORM_RIM_POWER , rim_power ) ;
2014-06-25 00:45:00 -07:00
// Fade parameters
setUniform ( KRENGINE_UNIFORM_FADE_COLOR , fade_color ) ;
2012-11-21 22:47:33 +00:00
// Fog parameters
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_FOG_NEAR , camera . settings . fog_near ) ;
setUniform ( KRENGINE_UNIFORM_FOG_FAR , camera . settings . fog_far ) ;
setUniform ( KRENGINE_UNIFORM_FOG_DENSITY , camera . settings . fog_density ) ;
2013-12-05 01:09:31 -08:00
setUniform ( KRENGINE_UNIFORM_FOG_COLOR , camera . settings . fog_color ) ;
2012-11-21 22:47:33 +00:00
if ( m_uniforms [ KRENGINE_UNIFORM_FOG_SCALE ] ! = - 1 ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_FOG_SCALE , 1.0f / ( camera . settings . fog_far - camera . settings . fog_near ) ) ;
2012-11-21 22:47:33 +00:00
}
if ( m_uniforms [ KRENGINE_UNIFORM_DENSITY_PREMULTIPLIED_EXPONENTIAL ] ! = - 1 ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_DENSITY_PREMULTIPLIED_EXPONENTIAL , - camera . settings . fog_density * 1.442695f ) ; // -fog_density / log(2)
2012-11-21 22:47:33 +00:00
}
if ( m_uniforms [ KRENGINE_UNIFORM_DENSITY_PREMULTIPLIED_SQUARED ] ! = - 1 ) {
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_DENSITY_PREMULTIPLIED_SQUARED , ( float ) ( - camera . settings . fog_density * camera . settings . fog_density * 1.442695 ) ) ; // -fog_density * fog_density / log(2)
2012-11-21 22:47:33 +00:00
}
2011-10-25 05:03:10 +00:00
// Sets the diffuseTexture variable to the first texture unit
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_DIFFUSETEXTURE , 0 ) ;
2011-10-25 05:03:10 +00:00
// Sets the specularTexture variable to the second texture unit
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_SPECULARTEXTURE , 1 ) ;
2011-10-25 05:03:10 +00:00
// Sets the normalTexture variable to the third texture unit
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_NORMALTEXTURE , 2 ) ;
2011-10-25 05:03:10 +00:00
// Sets the shadowTexture variable to the fourth texture unit
2013-05-08 17:47:27 -07:00
setUniform ( KRENGINE_UNIFORM_SHADOWTEXTURE1 , 3 ) ;
setUniform ( KRENGINE_UNIFORM_SHADOWTEXTURE2 , 4 ) ;
setUniform ( KRENGINE_UNIFORM_SHADOWTEXTURE3 , 5 ) ;
setUniform ( KRENGINE_UNIFORM_REFLECTIONCUBETEXTURE , 4 ) ;
setUniform ( KRENGINE_UNIFORM_LIGHTMAPTEXTURE , 5 ) ;
setUniform ( KRENGINE_UNIFORM_GBUFFER_FRAME , 6 ) ;
setUniform ( KRENGINE_UNIFORM_GBUFFER_DEPTH , 7 ) ; // Texture unit 7 is used for reading the depth buffer in gBuffer pass #2 and in post-processing pass
setUniform ( KRENGINE_UNIFORM_REFLECTIONTEXTURE , 7 ) ; // Texture unit 7 is used for the reflection map textures in gBuffer pass #3 and when using forward rendering
setUniform ( KRENGINE_UNIFORM_DEPTH_FRAME , 0 ) ;
setUniform ( KRENGINE_UNIFORM_RENDER_FRAME , 1 ) ;
setUniform ( KRENGINE_UNIFORM_VOLUMETRIC_ENVIRONMENT_FRAME , 2 ) ;
2012-09-19 20:26:30 +00:00
2016-07-10 03:33:58 -07:00
# if defined(DEBUG)
2016-01-30 20:13:23 -08:00
if ( shander_changed ) { // FINDME!! KIP!! HACK!!
2013-05-08 17:47:27 -07:00
GLint logLength ;
GLint validate_status = GL_FALSE ;
GLDEBUG ( glValidateProgram ( m_iProgram ) ) ;
GLDEBUG ( glGetProgramiv ( m_iProgram , GL_VALIDATE_STATUS , & validate_status ) ) ;
if ( validate_status ! = GL_TRUE ) {
2013-12-14 16:06:44 -08:00
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " KREngine - Failed to validate shader program: %s " , m_szKey ) ;
2017-04-27 17:48:12 -07:00
logLength = 0 ; // In case glGetProgramiv fails
2013-05-08 17:47:27 -07:00
GLDEBUG ( glGetProgramiv ( m_iProgram , GL_INFO_LOG_LENGTH , & logLength ) ) ;
if ( logLength > 0 )
{
2013-12-14 16:06:44 -08:00
GLchar * log = ( GLchar * ) malloc ( logLength + 1 ) ;
2013-05-08 17:47:27 -07:00
assert ( log ! = NULL ) ;
2017-04-27 17:48:12 -07:00
log [ 0 ] = ' \0 ' ; // In case glGetProgramInfoLog fails
2013-05-08 17:47:27 -07:00
GLDEBUG ( glGetProgramInfoLog ( m_iProgram , logLength , & logLength , log ) ) ;
2013-12-14 16:06:44 -08:00
log [ logLength ] = ' \0 ' ;
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " Program validate log: \n %s " , log ) ;
2013-05-08 17:47:27 -07:00
free ( log ) ;
}
return false ;
2012-09-20 10:51:28 +00:00
}
2011-10-25 05:03:10 +00:00
}
2012-02-10 05:48:59 +00:00
# endif
2012-09-20 09:32:20 +00:00
return true ;
2011-10-25 05:03:10 +00:00
}
2012-11-15 23:20:59 +00:00
const char * KRShader : : getKey ( ) const {
2012-03-15 20:09:01 +00:00
return m_szKey ;
}