From ba7595d781db195b5683f7503065cdb68ce87fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Thu, 5 Oct 2017 15:05:05 +0200 Subject: [PATCH] Mulitple point lights --- raymarcher.cc | 8 +--- shaders/chunks/raymarcher.glsl | 5 +-- shaders/scene.f.glsl | 69 ++++++++++++++++++++++++++++------ 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/raymarcher.cc b/raymarcher.cc index a3ef35e..c7b2ed4 100644 --- a/raymarcher.cc +++ b/raymarcher.cc @@ -48,9 +48,8 @@ void T_Raymarcher::render( ) U_LOOK_AT = 3 , U_CAM_UP = 4 , U_NEAR_PLANE = 5 , - U_LIGHT_DIR = 6 , - U_RAYMARCHER = 7 , - U_FOG = 8 , + U_RAYMARCHER = 6 , + U_FOG = 7 , }; const auto id( program_.program( E_ShaderType::FRAGMENT ) ); @@ -62,11 +61,8 @@ void T_Raymarcher::render( ) glProgramUniform3fv( id , U_CAM_UP , 1 , &camera_.up.x ); glProgramUniform1f( id , U_NEAR_PLANE , camera_.np ); - glProgramUniform3f( id , U_LIGHT_DIR , 0 , 1 , -1 ); - glProgramUniform4f( id , U_RAYMARCHER , rmIterations , rmStep , rmEpsilon , rmMaxDist ); - glProgramUniform1f( id , U_FOG , fog ); glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 ); diff --git a/shaders/chunks/raymarcher.glsl b/shaders/chunks/raymarcher.glsl index 9239d2f..016bce2 100644 --- a/shaders/chunks/raymarcher.glsl +++ b/shaders/chunks/raymarcher.glsl @@ -6,9 +6,8 @@ layout( location = 2 ) uniform vec3 u_CamPos; layout( location = 3 ) uniform vec3 u_LookAt; layout( location = 4 ) uniform vec3 u_CamUp; layout( location = 5 ) uniform float u_NearPlane; -layout( location = 6 ) uniform vec3 u_LightDir; -layout( location = 7 ) uniform vec4 u_Render; -layout( location = 8 ) uniform float u_FogAttenuation; +layout( location = 6 ) uniform vec4 u_Render; +layout( location = 7 ) uniform float u_FogAttenuation; layout( location = 0 ) out vec3 o_Color; layout( location = 1 ) out float o_Z; diff --git a/shaders/scene.f.glsl b/shaders/scene.f.glsl index 285e1d4..d938dbc 100644 --- a/shaders/scene.f.glsl +++ b/shaders/scene.f.glsl @@ -8,15 +8,15 @@ T_BPMaterial BPMaterials[1] = { - { vec3( 1 , 1 , .4 ) * .1 , vec3( 1 , 1 , .4 ) , 40 , .1 } + { vec3( .95 , .8 , 1 ) , vec3( 1 ) , 1 , 5 } }; T_PBRMaterial PBRMaterials[1] = { { // Albedo color - vec3( 1 , 1 , 1 ) , + vec3( .8 , 1 , .8 ) , // Roughness , metallic , subsurface , anisotropy - .5 , .5 , 0 , 0 , + .2 , .7 , .7 , .7 , // Specular strength / tint% .5 , .5 } @@ -33,21 +33,53 @@ void mapMaterial( out int glowIndex ) { if ( matIndex == 0 ) { + type = 1; glowIndex = -1; } else { - //type = 1; + type = 0; glowIndex = 0; } - type = 1; tIndex = 0; } +float box( vec3 p , vec3 sz ) +{ + vec3 n = abs( p ) - sz; + return max( max( n.x , n.y ) , n.z ); +} + vec2 RM_Map( vec3 pos ) { pos = pos - vec3( 0 , 0 , 0 ); vec3 q = pos; q.xy = mod( q.xy + 4. , 8. ) - 4.; - return vec2( length( q ) - 1.8 , step( 0. , 1.9 - length( pos.xy ) ) ); + q.xz *= mat2( + cos( 1 ) , -sin(1) , + sin( 1 ) , cos( 1 ) ); + return vec2( mix( box( q , vec3( 1.5 ) ) , + length( q ) - 1.5 , + .5 ) , + step( 0. , 1.9 - length( pos.xy ) ) ); +} + + +vec3 pointlight( + in T_PBRMaterial material , + in vec3 hitPos , + in vec3 rayDir , + in vec3 normal , + in vec3 lightPos , + in vec3 lightColor , + in float lightIntensity ) +{ + vec3 lightRay = lightPos - hitPos; + float lPwr = lightIntensity / max( .01 , dot( lightRay , lightRay ) ); + if ( lPwr < .05 ) { + return vec3( 0 ); + } + + return lightColor * lPwr * PBR_Shade( + material , -rayDir , normal , lightRay ); } @@ -71,17 +103,32 @@ void main( ) if ( r.y >= 0. ) { const int midx = int( r.y ); const vec3 normal = RM_GetNormal( hitPos ); - const vec3 lightDir = normalize( -u_LightDir ); + + const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) ); + const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .1; + + const vec3 pl1Pos = vec3( 3 , 3 , 5 ); + const vec3 pl1Color = vec3( 1 , .7 , .8 ); + const float pl1Power = 20; + + const vec3 pl2Pos = vec3( -3 , -3 , 5 ); + const vec3 pl2Color = vec3( .6 , 1 , .8 ); + const float pl2Power = 20; // Remap materials through mapMaterials int mtype , mtidx , glowidx; mapMaterial( midx , mtype , mtidx , glowidx ); if ( mtype == 0 ) { - bc = BP_Shade( BPMaterials[ mtidx ] , - -rayDir , normal , lightDir ); + bc = sunlightColor * BP_Shade( BPMaterials[ mtidx ] , + -rayDir , normal , sunlightDir ); } else { - bc = PBR_Shade( PBRMaterials[ mtidx ] , - -rayDir , normal , lightDir ); + T_PBRMaterial mat = PBRMaterials[ mtidx ]; + bc = sunlightColor * PBR_Shade( mat , + -rayDir , normal , sunlightDir ); + bc += pointlight( mat , hitPos , rayDir , normal , + pl1Pos , pl1Color , pl1Power ); + bc += pointlight( mat , hitPos , rayDir , normal , + pl2Pos , pl2Color , pl2Power ); } if ( glowidx >= 0 ) {