Mulitple point lights

This commit is contained in:
Emmanuel BENOîT 2017-10-05 15:05:05 +02:00
parent 52bf5cd3fc
commit ba7595d781
3 changed files with 62 additions and 20 deletions

View file

@ -48,9 +48,8 @@ void T_Raymarcher::render( )
U_LOOK_AT = 3 , U_LOOK_AT = 3 ,
U_CAM_UP = 4 , U_CAM_UP = 4 ,
U_NEAR_PLANE = 5 , U_NEAR_PLANE = 5 ,
U_LIGHT_DIR = 6 , U_RAYMARCHER = 6 ,
U_RAYMARCHER = 7 , U_FOG = 7 ,
U_FOG = 8 ,
}; };
const auto id( program_.program( E_ShaderType::FRAGMENT ) ); 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 ); glProgramUniform3fv( id , U_CAM_UP , 1 , &camera_.up.x );
glProgramUniform1f( id , U_NEAR_PLANE , camera_.np ); glProgramUniform1f( id , U_NEAR_PLANE , camera_.np );
glProgramUniform3f( id , U_LIGHT_DIR , 0 , 1 , -1 );
glProgramUniform4f( id , U_RAYMARCHER , rmIterations , rmStep , glProgramUniform4f( id , U_RAYMARCHER , rmIterations , rmStep ,
rmEpsilon , rmMaxDist ); rmEpsilon , rmMaxDist );
glProgramUniform1f( id , U_FOG , fog ); glProgramUniform1f( id , U_FOG , fog );
glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 ); glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );

View file

@ -6,9 +6,8 @@ layout( location = 2 ) uniform vec3 u_CamPos;
layout( location = 3 ) uniform vec3 u_LookAt; layout( location = 3 ) uniform vec3 u_LookAt;
layout( location = 4 ) uniform vec3 u_CamUp; layout( location = 4 ) uniform vec3 u_CamUp;
layout( location = 5 ) uniform float u_NearPlane; layout( location = 5 ) uniform float u_NearPlane;
layout( location = 6 ) uniform vec3 u_LightDir; layout( location = 6 ) uniform vec4 u_Render;
layout( location = 7 ) uniform vec4 u_Render; layout( location = 7 ) uniform float u_FogAttenuation;
layout( location = 8 ) uniform float u_FogAttenuation;
layout( location = 0 ) out vec3 o_Color; layout( location = 0 ) out vec3 o_Color;
layout( location = 1 ) out float o_Z; layout( location = 1 ) out float o_Z;

View file

@ -8,15 +8,15 @@
T_BPMaterial BPMaterials[1] = { 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] = { T_PBRMaterial PBRMaterials[1] = {
{ {
// Albedo color // Albedo color
vec3( 1 , 1 , 1 ) , vec3( .8 , 1 , .8 ) ,
// Roughness , metallic , subsurface , anisotropy // Roughness , metallic , subsurface , anisotropy
.5 , .5 , 0 , 0 , .2 , .7 , .7 , .7 ,
// Specular strength / tint% // Specular strength / tint%
.5 , .5 .5 , .5
} }
@ -33,21 +33,53 @@ void mapMaterial(
out int glowIndex ) out int glowIndex )
{ {
if ( matIndex == 0 ) { if ( matIndex == 0 ) {
type = 1;
glowIndex = -1; glowIndex = -1;
} else { } else {
//type = 1; type = 0;
glowIndex = 0; glowIndex = 0;
} }
type = 1;
tIndex = 0; 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 ) vec2 RM_Map( vec3 pos )
{ {
pos = pos - vec3( 0 , 0 , 0 ); pos = pos - vec3( 0 , 0 , 0 );
vec3 q = pos; vec3 q = pos;
q.xy = mod( q.xy + 4. , 8. ) - 4.; 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. ) { if ( r.y >= 0. ) {
const int midx = int( r.y ); const int midx = int( r.y );
const vec3 normal = RM_GetNormal( hitPos ); 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 // Remap materials through mapMaterials
int mtype , mtidx , glowidx; int mtype , mtidx , glowidx;
mapMaterial( midx , mtype , mtidx , glowidx ); mapMaterial( midx , mtype , mtidx , glowidx );
if ( mtype == 0 ) { if ( mtype == 0 ) {
bc = BP_Shade( BPMaterials[ mtidx ] , bc = sunlightColor * BP_Shade( BPMaterials[ mtidx ] ,
-rayDir , normal , lightDir ); -rayDir , normal , sunlightDir );
} else { } else {
bc = PBR_Shade( PBRMaterials[ mtidx ] , T_PBRMaterial mat = PBRMaterials[ mtidx ];
-rayDir , normal , lightDir ); 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 ) { if ( glowidx >= 0 ) {