Ivan Safrin 14 лет назад
Родитель
Сommit
0bde8349c6

+ 150 - 0
Assets/Default asset pack/default/NorColSpecHDR.frag

@@ -0,0 +1,150 @@
+varying vec3 normal;
+varying vec3 tangent;
+varying vec3 binormal;
+varying vec4 pos;
+varying vec4 vertexColor;
+uniform sampler2D diffuse;
+uniform sampler2D normal_map;
+uniform sampler2D specular_map;
+
+float calculateAttenuation(in int i, in float dist)
+{
+    return(1.0 / (gl_LightSource[i].constantAttenuation +
+                  gl_LightSource[i].linearAttenuation * dist +
+                  gl_LightSource[i].quadraticAttenuation * dist * dist));
+}
+
+void pointLight(in int i, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+	vec4 color = gl_FrontMaterial.diffuse;
+	vec4 matspec = gl_FrontMaterial.specular;
+	float shininess = gl_FrontMaterial.shininess;
+	vec4 lightspec = gl_LightSource[i].specular;
+	vec4 lpos = gl_LightSource[i].position;
+	
+	vec3 tmpVec = lpos.xyz - pos.xyz;	
+	lpos.x = dot(tmpVec, tangent);
+	lpos.y = dot(tmpVec, binormal);
+	lpos.z = dot(tmpVec, normal);	
+	
+	float distSqr = dot(lpos.xyz, lpos.xyz);
+	vec3 lVec = lpos.xyz * inversesqrt(distSqr);
+	
+	tmpVec = -pos.xyz;
+	vec3 v;
+	v.x = dot(tmpVec, tangent);
+	v.y = dot(tmpVec, binormal);
+	v.z = dot(tmpVec, normal);	
+	
+	v = normalize(v);
+
+	float nDotL = dot(lVec, bump);
+	if(nDotL > 0.0) {
+		float dist = length(lpos.xyz);    
+		float attenuation = calculateAttenuation(i, dist);
+
+		diffuse  += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation;
+
+	  if (shininess != 0.0) {
+    	specular += lightspec * matspec * pow(clamp(dot(reflect(-lVec, bump), v),0.0,1.0), shininess) * attenuation;
+	  }
+	}
+}
+
+
+void spotLight(in int i, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+	vec4 color = gl_FrontMaterial.diffuse;
+	vec4 matspec = gl_FrontMaterial.specular;
+	float shininess = gl_FrontMaterial.shininess;
+	vec4 lightspec = gl_LightSource[i].specular;
+	vec4 lpos = gl_LightSource[i].position;
+	
+	vec3 tmpVec = lpos.xyz - pos.xyz;	
+	lpos.x = dot(tmpVec, tangent);
+	lpos.y = dot(tmpVec, binormal);
+	lpos.z = dot(tmpVec, normal);	
+	
+	float distSqr = dot(lpos.xyz, lpos.xyz);
+	vec3 lVec = lpos.xyz * inversesqrt(distSqr);
+	
+	tmpVec = -pos.xyz;
+	vec3 v;
+	v.x = dot(tmpVec, tangent);
+	v.y = dot(tmpVec, binormal);
+	v.z = dot(tmpVec, normal);	
+	
+	v = normalize(v);
+	
+	tmpVec = gl_LightSource[i].spotDirection.xyz;
+	vec3 lDir;
+	lDir.x = dot(tmpVec, tangent);
+	lDir.y = dot(tmpVec, binormal);
+	lDir.z = dot(tmpVec, normal);	
+	
+	lDir = normalize(lDir);
+
+	
+	float cos_outer_cone_angle = gl_LightSource[i].spotExponent;
+	float cos_cur_angle = dot(-lDir, lVec);
+	float cos_inner_cone_angle = gl_LightSource[i].spotCosCutoff;
+
+	float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle;
+	float spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0);
+	       	
+
+	float nDotL = dot(lVec, bump);
+	if(nDotL > 0.0) {
+		float dist = length(lpos.xyz);    
+		float attenuation = calculateAttenuation(i, dist);
+
+		diffuse  += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation * spot;
+
+	  if (shininess != 0.0) {
+    	specular += lightspec * matspec * pow(clamp(dot(reflect(-lVec, bump), v),0.0,1.0), shininess) * attenuation * spot;
+	  }
+	}}
+
+void doLights(in int numLights, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+	for (int i = 0; i < numLights; i++) {
+		if (gl_LightSource[i].spotCutoff == 180.0) {
+			pointLight(i, bump, normal, tangent, binormal, pos, diffuse, specular);
+		} else {
+			spotLight(i, bump, normal, tangent, binormal, pos, diffuse, specular);
+		}
+    }
+}
+
+
+void main()
+{
+	vec4 diffuse_val  = vec4(0.0);
+	vec4 specular_val = vec4(0.0);
+	
+	vec3 bump = normalize( texture2D(normal_map, gl_TexCoord[0].st).xyz * 2.0 - 1.0);
+		
+	doLights(6, bump, normal, tangent, binormal, pos, diffuse_val, specular_val);
+	
+	specular_val.xyz *= texture2D(specular_map, gl_TexCoord[0].st).xyz * gl_FrontMaterial.specular.a;
+	
+	diffuse_val.a = 1.0;
+	specular_val.a = 1.0;
+		
+	vec4 texColor = texture2D(diffuse, gl_TexCoord[0].st);		
+		
+    vec4 color = (diffuse_val  * texColor * vertexColor) +
+                 (specular_val * 1.0)+
+                 (gl_FrontMaterial.ambient * texColor * vertexColor);
+//    color = clamp(color, 0.0, 1.0);
+    
+    // fog
+	const float LOG2 = 1.442695;
+	float z = gl_FragCoord.z / gl_FragCoord.w;
+	float fogFactor = exp2( -gl_Fog.density * 
+				   gl_Fog.density * 
+				   z * 
+				   z * 
+				   LOG2 );
+
+	fogFactor = clamp(fogFactor, 0.0, 1.0);
+	gl_FragColor = mix(gl_Fog.color, color, fogFactor );    
+	gl_FragColor.a = 1.0;
+}

+ 6 - 0
Assets/Default asset pack/default/PassThrough.frag

@@ -0,0 +1,6 @@
+uniform sampler2D screenColorBuffer;
+ 
+void main(void)
+{
+   gl_FragColor = texture2D(screenColorBuffer,gl_TexCoord[0].st);
+}

BIN
Assets/Default asset pack/hdr.pak


+ 14 - 0
Assets/Default asset pack/hdr/ExtractBloom.frag

@@ -0,0 +1,14 @@
+uniform sampler2D screenColorBuffer;
+uniform float exposure;
+ 
+void main(void)
+{
+	float bright_threshold = 1.0;
+	
+	vec4 color =  texture2D(screenColorBuffer,gl_TexCoord[0].st);
+	float lum = dot(vec4(0.30, 0.59, 0.11, 0.0), color);
+    if (lum > bright_threshold)
+        gl_FragColor = color;
+    else
+        gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);		
+}

+ 19 - 0
Assets/Default asset pack/hdr/HDRBloomH.frag

@@ -0,0 +1,19 @@
+uniform sampler2D screenTexture;
+const float blurSize = 1.0/64.0;
+ 
+void main(void)
+{
+   vec2 vTexCoord = gl_TexCoord[0].st;
+   vec4 sum = vec4(0.0);
+   sum += texture2D(screenTexture, vec2(vTexCoord.x - 4.0*blurSize, vTexCoord.y)) * 0.05;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x - 3.0*blurSize, vTexCoord.y)) * 0.09;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x - 2.0*blurSize, vTexCoord.y)) * 0.12;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x - blurSize, vTexCoord.y)) * 0.15;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x + blurSize, vTexCoord.y)) * 0.15;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x + 2.0*blurSize, vTexCoord.y)) * 0.12;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x + 3.0*blurSize, vTexCoord.y)) * 0.09;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x + 4.0*blurSize, vTexCoord.y)) * 0.05;
+   
+   gl_FragColor = sum;
+}

+ 19 - 0
Assets/Default asset pack/hdr/HDRBloomV.frag

@@ -0,0 +1,19 @@
+uniform sampler2D screenTexture;
+const float blurSize = 1.0/64.0;
+ 
+void main(void)
+{
+   vec2 vTexCoord = gl_TexCoord[0].st;
+   vec4 sum = vec4(0.0);
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y - 4.0*blurSize)) * 0.05;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y - 3.0*blurSize)) * 0.09;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y - 2.0*blurSize)) * 0.12;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y - blurSize)) * 0.15;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y + blurSize)) * 0.15;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y + 2.0*blurSize)) * 0.12;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y + 3.0*blurSize)) * 0.09;
+   sum += texture2D(screenTexture, vec2(vTexCoord.x, vTexCoord.y + 4.0*blurSize)) * 0.05;
+   
+   gl_FragColor = sum;
+}

+ 10 - 0
Assets/Default asset pack/hdr/HDRProcess.frag

@@ -0,0 +1,10 @@
+uniform sampler2D screenColorBuffer;
+uniform float exposure;
+
+void main(void)
+{
+	float brightMax = 1.0;
+	float YD = exposure * (exposure/brightMax + 1.0) / (exposure + 1.0);
+	gl_FragColor = texture2D(screenColorBuffer,gl_TexCoord[0].st) * YD;
+	gl_FragColor.a = 1.0;
+}

+ 19 - 0
Assets/Default asset pack/hdr/HDRProcessBloom.frag

@@ -0,0 +1,19 @@
+uniform sampler2D baseTexture;
+uniform sampler2D bloomTexture;
+uniform float exposure;
+
+void main(void)
+{
+	float brightMax = 1.0;
+	float bloomFactor = 1.0;
+	
+	vec4 colorBloom = texture2D(bloomTexture, gl_TexCoord[0].st);
+	vec4 color = texture2D(baseTexture,gl_TexCoord[0].st);
+	
+	color += colorBloom * bloomFactor;
+//	color = colorBloom * bloomFactor;
+	
+	float YD = exposure * (exposure/brightMax + 1.0) / (exposure + 1.0);
+	gl_FragColor = color * YD;
+	gl_FragColor.a = 1.0;
+}

+ 150 - 0
Assets/Default asset pack/hdr/NorColSpecHDR.frag

@@ -0,0 +1,150 @@
+varying vec3 normal;
+varying vec3 tangent;
+varying vec3 binormal;
+varying vec4 pos;
+varying vec4 vertexColor;
+uniform sampler2D diffuse;
+uniform sampler2D normal_map;
+uniform sampler2D specular_map;
+
+float calculateAttenuation(in int i, in float dist)
+{
+    return(1.0 / (gl_LightSource[i].constantAttenuation +
+                  gl_LightSource[i].linearAttenuation * dist +
+                  gl_LightSource[i].quadraticAttenuation * dist * dist));
+}
+
+void pointLight(in int i, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+	vec4 color = gl_FrontMaterial.diffuse;
+	vec4 matspec = gl_FrontMaterial.specular;
+	float shininess = gl_FrontMaterial.shininess;
+	vec4 lightspec = gl_LightSource[i].specular;
+	vec4 lpos = gl_LightSource[i].position;
+	
+	vec3 tmpVec = lpos.xyz - pos.xyz;	
+	lpos.x = dot(tmpVec, tangent);
+	lpos.y = dot(tmpVec, binormal);
+	lpos.z = dot(tmpVec, normal);	
+	
+	float distSqr = dot(lpos.xyz, lpos.xyz);
+	vec3 lVec = lpos.xyz * inversesqrt(distSqr);
+	
+	tmpVec = -pos.xyz;
+	vec3 v;
+	v.x = dot(tmpVec, tangent);
+	v.y = dot(tmpVec, binormal);
+	v.z = dot(tmpVec, normal);	
+	
+	v = normalize(v);
+
+	float nDotL = dot(lVec, bump);
+	if(nDotL > 0.0) {
+		float dist = length(lpos.xyz);    
+		float attenuation = calculateAttenuation(i, dist);
+
+		diffuse  += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation;
+
+	  if (shininess != 0.0) {
+    	specular += lightspec * matspec * pow(clamp(dot(reflect(-lVec, bump), v),0.0,1.0), shininess) * attenuation;
+	  }
+	}
+}
+
+
+void spotLight(in int i, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+	vec4 color = gl_FrontMaterial.diffuse;
+	vec4 matspec = gl_FrontMaterial.specular;
+	float shininess = gl_FrontMaterial.shininess;
+	vec4 lightspec = gl_LightSource[i].specular;
+	vec4 lpos = gl_LightSource[i].position;
+	
+	vec3 tmpVec = lpos.xyz - pos.xyz;	
+	lpos.x = dot(tmpVec, tangent);
+	lpos.y = dot(tmpVec, binormal);
+	lpos.z = dot(tmpVec, normal);	
+	
+	float distSqr = dot(lpos.xyz, lpos.xyz);
+	vec3 lVec = lpos.xyz * inversesqrt(distSqr);
+	
+	tmpVec = -pos.xyz;
+	vec3 v;
+	v.x = dot(tmpVec, tangent);
+	v.y = dot(tmpVec, binormal);
+	v.z = dot(tmpVec, normal);	
+	
+	v = normalize(v);
+	
+	tmpVec = gl_LightSource[i].spotDirection.xyz;
+	vec3 lDir;
+	lDir.x = dot(tmpVec, tangent);
+	lDir.y = dot(tmpVec, binormal);
+	lDir.z = dot(tmpVec, normal);	
+	
+	lDir = normalize(lDir);
+
+	
+	float cos_outer_cone_angle = gl_LightSource[i].spotExponent;
+	float cos_cur_angle = dot(-lDir, lVec);
+	float cos_inner_cone_angle = gl_LightSource[i].spotCosCutoff;
+
+	float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle;
+	float spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0);
+	       	
+
+	float nDotL = dot(lVec, bump);
+	if(nDotL > 0.0) {
+		float dist = length(lpos.xyz);    
+		float attenuation = calculateAttenuation(i, dist);
+
+		diffuse  += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation * spot;
+
+	  if (shininess != 0.0) {
+    	specular += lightspec * matspec * pow(clamp(dot(reflect(-lVec, bump), v),0.0,1.0), shininess) * attenuation * spot;
+	  }
+	}}
+
+void doLights(in int numLights, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+	for (int i = 0; i < numLights; i++) {
+		if (gl_LightSource[i].spotCutoff == 180.0) {
+			pointLight(i, bump, normal, tangent, binormal, pos, diffuse, specular);
+		} else {
+			spotLight(i, bump, normal, tangent, binormal, pos, diffuse, specular);
+		}
+    }
+}
+
+
+void main()
+{
+	vec4 diffuse_val  = vec4(0.0);
+	vec4 specular_val = vec4(0.0);
+	
+	vec3 bump = normalize( texture2D(normal_map, gl_TexCoord[0].st).xyz * 2.0 - 1.0);
+		
+	doLights(6, bump, normal, tangent, binormal, pos, diffuse_val, specular_val);
+	
+	specular_val.xyz *= texture2D(specular_map, gl_TexCoord[0].st).xyz * gl_FrontMaterial.specular.a;
+	
+	diffuse_val.a = 1.0;
+	specular_val.a = 1.0;
+		
+	vec4 texColor = texture2D(diffuse, gl_TexCoord[0].st);		
+		
+    vec4 color = (diffuse_val  * texColor * vertexColor) +
+                 (specular_val * 1.0)+
+                 (gl_FrontMaterial.ambient * texColor * vertexColor);
+//    color = clamp(color, 0.0, 1.0);
+    
+    // fog
+	const float LOG2 = 1.442695;
+	float z = gl_FragCoord.z / gl_FragCoord.w;
+	float fogFactor = exp2( -gl_Fog.density * 
+				   gl_Fog.density * 
+				   z * 
+				   z * 
+				   LOG2 );
+
+	fogFactor = clamp(fogFactor, 0.0, 1.0);
+	gl_FragColor = mix(gl_Fog.color, color, fogFactor );    
+	gl_FragColor.a = 1.0;
+}

+ 94 - 0
Assets/Default asset pack/hdr/hdr.mat

@@ -0,0 +1,94 @@
+<?xml version="1.0" ?>
+<polycode>	
+	<shaders>
+		<shader type="glsl" name="NorColSpecHDR" numAreaLights="4" numSpotLights="2">		
+			<vp source="NormalShader.vert">
+				<params>			
+				</params>				
+			</vp>
+			<fp source="NorColSpecHDR.frag">
+				<params>			
+				</params>				
+			</fp>
+		</shader>	
+		<shader type="glsl" name="HDRProcessShader">
+			<vp source="ScreenShader.vert"/>
+			<fp source="HDRProcess.frag">
+				<params>
+					<param name="exposure" type="Number" default="1.0"/>
+				</params>								
+			</fp>
+		</shader>		
+		<shader type="glsl" name="HDRProcessShaderBloom">
+			<vp source="ScreenShader.vert"/>
+			<fp source="HDRProcessBloom.frag">
+				<params>
+					<param name="exposure" type="Number" default="1.0"/>
+				</params>								
+			</fp>			
+		</shader>		
+		<shader type="glsl" name="HDRBloomH">
+			<vp source="ScreenShader.vert"/>
+			<fp source="HDRBloomH.frag"/>
+		</shader>
+		<shader type="glsl" name="HDRBloomV">
+			<vp source="ScreenShader.vert"/>
+			<fp source="HDRBloomV.frag"/>
+		</shader>	
+		<shader type="glsl" name="PassThrough">
+			<vp source="ScreenShader.vert"/>
+			<fp source="PassThrough.frag"/>
+		</shader>	
+		<shader type="glsl" name="ExtractBloom">
+			<vp source="ScreenShader.vert"/>
+			<fp source="ExtractBloom.frag">
+				<params>
+					<param name="exposure" type="Number" default="1.0"/>
+				</params>								
+			</fp>			
+		</shader>							
+	</shaders>
+	<materials>
+		<material name="HDRProcess">
+			<shader name="HDRProcessShader">
+			</shader>
+		</material>	
+		<material name="HDRProcessBloom">
+			<rendertargets>
+				<rendertarget id="base_target"  sizeMode="normalized" width="1.0" height="1.0"/>			
+				<rendertarget id="bloomtarget"  sizeMode="pixels" width="64" height="64"/>
+				<rendertarget id="bloomtarget2"  sizeMode="pixels" width="64" height="64"/>				
+				<rendertarget id="bloomtarget_final"  sizeMode="pixels" width="64" height="64"/>
+			</rendertargets>
+			<shader name="PassThrough">
+				<targettextures>
+					<targettexture mode="out" id="base_target"/>
+				</targettextures>
+			</shader>	
+			<shader name="ExtractBloom">
+				<targettextures>			
+					<targettexture mode="out" id="bloomtarget"/>
+				</targettextures>
+			</shader>									
+			<shader name="HDRBloomH">
+				<targettextures>
+					<targettexture mode="in" name="screenTexture" id="bloomtarget"/>				
+					<targettexture mode="out" id="bloomtarget2"/>
+				</targettextures>
+			</shader>		
+			<shader name="HDRBloomV">
+				<targettextures>
+					<targettexture mode="in" name="screenTexture" id="bloomtarget2"/>
+					<targettexture mode="out" id="bloomtarget_final"/>					
+				</targettextures>													
+			</shader>
+			
+			<shader name="HDRProcessShaderBloom">
+				<targettextures>
+					<targettexture mode="in" name="baseTexture" id="base_target"/>								
+					<targettexture mode="in" name="bloomTexture" id="bloomtarget_final"/>				
+				</targettextures>													
+			</shader>			
+		</material>
+	</materials>
+</polycode>

+ 2 - 1
Core/Contents/Source/PolyCamera.cpp

@@ -243,7 +243,7 @@ void Camera::setParentScene(Scene *parentScene) {
 void Camera::setPostFilter(const String& shaderName) {
 	Material *shaderMaterial = (Material*) CoreServices::getInstance()->getResourceManager()->getResource(Resource::RESOURCE_MATERIAL, shaderName);
 	if(shaderMaterial)
-		createPostFilter(shaderMaterial);
+		createPostFilter(shaderMaterial);		
 }
 
 void Camera::removePostFilter() {
@@ -276,6 +276,7 @@ void Camera::createPostFilter(Material *shaderMaterial) {
 //			binding->addTexture("screenDepthBuffer", zBufferSceneTexture);
 		}
 		localShaderOptions.push_back(binding);
+		binding->addLocalParam("exposure", (void*)&exposureLevel);				
 	}
 	
 

+ 1 - 1
Core/Contents/Source/PolyGLRenderer.cpp

@@ -595,7 +595,7 @@ void OpenGLRenderer::createRenderTextures(Texture **colorBuffer, Texture **depth
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);	
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
 
 	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTexture, 0);