Browse Source

cleaner design.

Ben Houston 9 years ago
parent
commit
8d0d4542a6

+ 8 - 8
src/renderers/shaders/ShaderChunk/common.glsl

@@ -92,9 +92,9 @@ struct ReflectedLight {
  	vec3 diffuse;
 };
 
-vec3 BRDF_Lambert( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 diffuseColor ) {
+void BRDF_Lambert( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 diffuseColor, inout ReflectedLight reflectedLight ) {
 
-	return incidentLight.color * diffuseColor * ( saturate( dot( normal, incidentLight.direction ) ) );
+	reflectedLight.diffuse += incidentLight.color * diffuseColor * ( saturate( dot( normal, incidentLight.direction ) ) );
 
 	// the above should be scaled by '' * RECIPROCAL_PI'
 }
@@ -127,7 +127,7 @@ float D_BlinnPhong( const in float shininess, const in float dotNH ) {
 
 }
 
-vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float shininess ) {
+void BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float shininess, inout ReflectedLight reflectedLight ) {
 
 	vec3 halfDir = normalize( incidentLight.direction + viewDir );
 	float dotNH = saturate( dot( normal, halfDir ) );
@@ -137,7 +137,7 @@ vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 	float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );
 	float D = D_BlinnPhong( shininess, dotNH );
 
-	return incidentLight.color * F * ( G * D );
+	reflectedLight.specular += incidentLight.color * F * ( G * D );
 
 }
 
@@ -150,7 +150,7 @@ vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 
 	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
 
-	void getDirLightDirect( const in DirectionalLight directionalLight, out IncidentLight incidentLight ) { 
+	void getDirIncidentLight( const in DirectionalLight directionalLight, out IncidentLight incidentLight ) { 
 	
 		incidentLight.color = directionalLight.color;
 		incidentLight.direction = directionalLight.direction; 
@@ -170,7 +170,7 @@ vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 
 	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
 
-	void getPointLightDirect( const in PointLight pointLight, const in vec3 position, out IncidentLight incidentLight ) { 
+	void getPointIncidentLight( const in PointLight pointLight, const in vec3 position, out IncidentLight incidentLight ) { 
 	
 		vec3 lightPosition = pointLight.position; 
 	
@@ -198,7 +198,7 @@ vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 
 	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
 
-	void getSpotLightDirect( const in SpotLight spotLight, const in vec3 position, out IncidentLight incidentLight ) {
+	void getSpotIncidentLight( const in SpotLight spotLight, const in vec3 position, out IncidentLight incidentLight ) {
 	
 		vec3 lightPosition = spotLight.position;
 	
@@ -226,7 +226,7 @@ vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 
 	layout(packed)uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
 
-	void getHemisphereLightIndirect( const in HemisphereLight hemiLight, const in vec3 normal, out IncidentLight incidentLight ) { 
+	void getHemisphereIncidentLight( const in HemisphereLight hemiLight, const in vec3 normal, out IncidentLight incidentLight ) { 
 	
 		float dotNL = dot( normal, hemiLight.direction );
 

+ 19 - 18
src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl

@@ -9,19 +9,23 @@ vLightFront = vec3( 0.0 );
 vec3 normal = normalize( transformedNormal );
 vec3 diffuse = vec3( 1.0 );
 
+IncidentLight incidentLight;
+ReflectedLight frontReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
+ReflectedLight backReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
+
+vec3 backNormal = -normal;
 
 #if MAX_POINT_LIGHTS > 0
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
-		IncidentLight incidentLight;
-		getPointLightDirect( pointLights[ i ], mvPosition.xyz, incidentLight );
+		getPointIncidentLight( pointLights[ i ], mvPosition.xyz, incidentLight );
 
-		vLightFront += BRDF_Lambert( incidentLight, normal, diffuse );
+		BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 
-			vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse );
+			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
 
 		#endif
 
@@ -33,14 +37,13 @@ vec3 diffuse = vec3( 1.0 );
 
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
-		IncidentLight incidentLight;
-		getSpotLightDirect( spotLights[ i ], mvPosition.xyz, incidentLight );
+		getSpotIncidentLight( spotLights[ i ], mvPosition.xyz, incidentLight );
 
-		vLightFront += BRDF_Lambert( incidentLight, normal, diffuse );
+		BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 
-			vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse );
+			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
 
 		#endif
 
@@ -52,14 +55,13 @@ vec3 diffuse = vec3( 1.0 );
 
 	for ( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
-		IncidentLight incidentLight;
-		getDirLightDirect( directionalLights[ i ], incidentLight );
+		getDirIncidentLight( directionalLights[ i ], incidentLight );
 
-		vLightFront += BRDF_Lambert( incidentLight, normal, diffuse );
+		vLightFront += BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 
-			vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse );
+			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
 
 		#endif
 
@@ -71,16 +73,15 @@ vec3 diffuse = vec3( 1.0 );
 
 	for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 
-		IncidentLight incidentLight;
 		getHemisphereLightIndirect( hemisphereLights[ i ], normal, incidentLight );
 
-		vLightFront += BRDF_Lambert( incidentLight, normal, diffuse );
+		BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 	
-			incidentLight = getHemisphereLightIndirect( hemisphereLights[ i ], -normal );
+			getHemisphereIncidentLight( hemisphereLights[ i ], backNormal, incidentLight );
 
-			vLightBack += BRDF_Lambert( incidentLight, -normal, diffuse );
+			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
 
 		#endif
 
@@ -88,10 +89,10 @@ vec3 diffuse = vec3( 1.0 );
 
 #endif
 
-vLightFront += ambientLightColor;
+vLightFront += ambientLightColor + frontReflectedLight.diffuse;
 
 #ifdef DOUBLE_SIDED
 
-	vLightBack += ambientLightColor;
+	vLightBack += ambientLightColor + backReflectedLight.diffuse;
 
 #endif

+ 11 - 17
src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl

@@ -17,13 +17,12 @@ IncidentLight incidentLight;
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
-		getPointLightDirect( pointLights[ i ], -vViewPosition, incidentLight );
+		getPointIncidentLight( pointLights[ i ], -vViewPosition, incidentLight );
 
-		directReflectedLight.diffuse +=
-			BRDF_Lambert( incidentLight, normal, diffuse );
+		BRDF_Lambert( incidentLight, normal, diffuse, directReflectedLight );
+
+		BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess, directReflectedLight );
 
-		directReflectedLight.specular +=
-			BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess );
 
 	}
 
@@ -35,11 +34,9 @@ IncidentLight incidentLight;
 
 		getSpotLightDirect( spotLights[ i ], -vViewPosition, incidentLight );
 
-		directReflectedLight.diffuse +=
-			BRDF_Lambert( incidentLight, normal, diffuse );
+		BRDF_Lambert( incidentLight, normal, diffuse, directReflectedLight );
 
-		directReflectedLight.specular +=
-			BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess );
+		BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess, directReflectedLight );
 
 	}
 
@@ -49,13 +46,11 @@ IncidentLight incidentLight;
 
 	for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
-		getDirLightDirect( directionalLights[ i ], incidentLight );
+		getDirIncidentLight( directionalLights[ i ], incidentLight );
 
-		directReflectedLight.diffuse +=
-			BRDF_Lambert( incidentLight, normal, diffuse );
+		BRDF_Lambert( incidentLight, normal, diffuse, directReflectedLight );
 
-		directReflectedLight.specular +=
-			BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess );
+		BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess, directReflectedLight );
 
 	}
 
@@ -65,10 +60,9 @@ IncidentLight incidentLight;
 
 	for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 
-		getHemisphereLightIndirect( hemisphereLights[ i ], normal, incidentLight );
+		getHemisphereIncidentLight( hemisphereLights[ i ], normal, incidentLight );
 
-		indirectReflectedLight.diffuse +=
-			BRDF_Lambert( incidentLight, normal, diffuse );
+		BRDF_Lambert( incidentLight, normal, diffuse, indirectReflectedLight );
 
 	}