From 0296de3d1a022d13c964dbc3f7a632d53132af8a Mon Sep 17 00:00:00 2001 From: James Canete Date: Thu, 26 Apr 2012 13:36:38 +0000 Subject: [PATCH] Remove DiffuseRoughness shader param, add SpecularExponent shader param --- reaction/Reaction-ChangeLog | 1 + reaction/code/renderergl2/tr_glsl.c | 277 +++++++++++++------------- reaction/code/renderergl2/tr_local.h | 6 +- reaction/code/renderergl2/tr_shade.c | 10 +- reaction/code/renderergl2/tr_shader.c | 18 +- 5 files changed, 154 insertions(+), 158 deletions(-) diff --git a/reaction/Reaction-ChangeLog b/reaction/Reaction-ChangeLog index e1f5244b..c10c99ba 100644 --- a/reaction/Reaction-ChangeLog +++ b/reaction/Reaction-ChangeLog @@ -1,3 +1,4 @@ +- Remove DiffuseRoughness shader param, add SpecularExponent shader param - Change R_SubdividePatchToGrid() to subdivide patches more evenly - Calculate tangent space in fragment shader instead of storing per vertex - Fix sun flare with sky portals. Sun flare must be inside sky portal. diff --git a/reaction/code/renderergl2/tr_glsl.c b/reaction/code/renderergl2/tr_glsl.c index c101c9c1..251cac52 100644 --- a/reaction/code/renderergl2/tr_glsl.c +++ b/reaction/code/renderergl2/tr_glsl.c @@ -305,133 +305,131 @@ static const char *fallbackLightallShader_fp = "WMAP)\r\nuniform samplerCube u_ShadowMap;\r\n#endif\r\n\r\nuniform vec3 " " u_ViewOrigin;\r\n\r\n#if defined(USE_LIGHT_VECTOR)\r\nuniform vec3 u" "_DirectedLight;\r\nuniform vec3 u_AmbientLight;\r\nuniform float u" -"_LightRadius;\r\n#endif\r\n\r\n#if defined(USE_SPECULARMAP)\r\nuniform floa" -"t u_SpecularReflectance;\r\n#endif\r\n\r\n#if !defined(USE_FAST_LIGHT)" -"\r\nuniform float u_DiffuseRoughness;\r\n#endif\r\n\r\nvarying vec2 " -" var_DiffuseTex;\r\n#if defined(USE_LIGHTMAP)\r\nvarying vec2 var_Lig" -"htTex;\r\n#endif\r\nvarying vec4 var_Color;\r\nvarying vec3 var_P" -"osition;\r\n\r\nvarying vec3 var_SampleToView;\r\n\r\nvarying vec3 " -" var_Normal;\r\n#if defined(USE_VERT_TANGENT_SPACE)\r\nvarying vec3 v" -"ar_Tangent;\r\nvarying vec3 var_Bitangent;\r\n#endif\r\n\r\n#if define" -"d(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT)\r\nvarying vec3 var_Vec" -"tLight;\r\n#endif\r\n\r\n#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP)" -"\r\nvarying vec3 var_WorldLight;\r\n#endif\r\n\r\n#define EPSILON 0.00" -"000001\r\n\r\n#if defined(USE_PARALLAXMAP)\r\nfloat SampleHeight(sampler2D " -"normalMap, vec2 t)\r\n{\r\n #if defined(SWIZZLE_NORMALMAP)\r\n\treturn tex" -"ture2D(normalMap, t).r;\r\n #else\r\n\treturn texture2D(normalMap, t).a;\r" -"\n #endif\r\n}\r\n\r\nfloat RayIntersectDisplaceMap(vec2 dp, vec2 ds, samp" -"ler2D normalMap)\r\n{\r\n\tconst int linearSearchSteps = 16;\r\n\tconst int" -" binarySearchSteps = 6;\r\n\r\n\tfloat depthStep = 1.0 / float(linearSearch" -"Steps);\r\n\r\n\t// current size of search window\r\n\tfloat size = depthSt" -"ep;\r\n\r\n\t// current depth position\r\n\tfloat depth = 0.0;\r\n\r\n\t// " -"best match found (starts with last position 1.0)\r\n\tfloat bestDepth = 1.0" -";\r\n\r\n\t// search front to back for first point inside object\r\n\tfor(i" -"nt i = 0; i < linearSearchSteps - 1; ++i)\r\n\t{\r\n\t\tdepth += size;\r\n" -"\t\t\r\n\t\tfloat t = SampleHeight(normalMap, dp + ds * depth);\r\n\t\t\r\n" -"\t\tif(bestDepth > 0.996)\t\t// if no depth found yet\r\n\t\t\tif(depth >= " -"t)\r\n\t\t\t\tbestDepth = depth;\t// store best depth\r\n\t}\r\n\r\n\tdepth" -" = bestDepth;\r\n\t\r\n\t// recurse around first point (depth) for closest " -"match\r\n\tfor(int i = 0; i < binarySearchSteps; ++i)\r\n\t{\r\n\t\tsize *=" -" 0.5;\r\n\r\n\t\tfloat t = SampleHeight(normalMap, dp + ds * depth);\r\n\t" -"\t\r\n\t\tif(depth >= t)\r\n\t\t{\r\n\t\t\tbestDepth = depth;\r\n\t\t\tdept" -"h -= 2.0 * size;\r\n\t\t}\r\n\r\n\t\tdepth += size;\r\n\t}\r\n\r\n\treturn " -"bestDepth;\r\n}\r\n#endif\r\n\r\nfloat CalcDiffuse(vec3 N, vec3 L, vec3 E, " -"float NE, float NL, float fzero, float roughness)\r\n{\r\n #if defined(USE" -"_OREN_NAYAR)\r\n\tfloat gamma = dot(E - N * NE, L - N * NL);\r\n\tfloat r_s" -"q = roughness * roughness;\r\n\r\n\tfloat A = 1.0 - 0.5 * (r_sq / (r_sq + 0" -".57));\r\n\tfloat B = 0.45 * (r_sq / (r_sq + 0.09));\r\n\r\n\tfloat alpha =" -" max(acos(NE), acos(NL));\r\n\tfloat beta = min(acos(NE), acos(NL));\r\n\r" -"\n\tfloat C = sin(alpha) * tan(beta);\r\n\r\n\treturn A + B * clamp(gamma, " -"0.0, 1.0) * C;\r\n #else\r\n\treturn 1.0 - fzero;\r\n #endif\r\n}\r\n\r\n" -"#if defined(USE_SPECULARMAP)\r\nfloat CalcSpecular(float NH, float NL, floa" -"t NE, float EH, float fzero, float shininess)\r\n{\r\n #if defined(USE_BLI" -"NN) || defined(USE_TRIACE) || defined(USE_TORRANCE_SPARROW)\r\n\tfloat blin" -"n = pow(NH, shininess);\r\n #endif\r\n\r\n #if defined(USE_BLINN)\r\n\tre" -"turn blinn;\r\n #endif\r\n\r\n #if defined(USE_COOK_TORRANCE) || defined " -"(USE_TRIACE) || defined (USE_TORRANCE_SPARROW)\r\n\tfloat fresnel = fzero +" -" (1.0 - fzero) * pow(1.0 - EH, 5);\r\n #endif\r\n\r\n #if defined(USE_COO" -"K_TORRANCE) || defined(USE_TORRANCE_SPARROW)\r\n\tfloat geo = 2.0 * NH * mi" -"n(NE, NL);\r\n\tgeo /= max(EH, geo);\r\n #endif \r\n\r\n #if defined(USE" -"_COOK_TORRANCE)\r\n\tfloat m = sqrt(2.0 / shininess);\r\n\r\n\tfloat m_sq =" -" m * m;\r\n\tfloat NH_sq = NH * NH;\r\n\tfloat beckmann = exp((NH_sq - 1.0)" -" / max(m_sq * NH_sq, EPSILON)) / max(4.0 * m_sq * NH_sq * NH_sq, EPSILON);" -"\r\n\r\n\treturn fresnel * geo * beckmann / max(NE, EPSILON);\r\n #endif\r" -"\n\r\n #if defined(USE_TRIACE)\r\n\tfloat scale = 0.1248582 * shininess + " -"0.2691817;\r\n\r\n\treturn fresnel * scale * blinn / max(max(NL, NE), EPSIL" -"ON);\r\n #endif\r\n \r\n #if defined(USE_TORRANCE_SPARROW)\r\n\tfloat sc" -"ale = 0.125 * shininess + 1.0;\r\n\r\n\treturn fresnel * geo * scale * blin" -"n / max(NE, EPSILON);\r\n #endif\r\n}\r\n#endif\r\n\r\nvoid main()\r\n{\r" -"\n#if defined(USE_DELUXEMAP)\r\n\tvec3 worldLight = 2.0 * texture2D(u_Delux" -"eMap, var_LightTex).xyz - vec3(1.0);\r\n\t//worldLight += var_WorldLight * " -"0.0001;\r\n#elif defined(USE_LIGHT)\r\n\tvec3 worldLight = var_WorldLight;" -"\r\n#endif\r\n\r\n#if defined(USE_LIGHTMAP)\r\n\tvec4 lightSample = texture" -"2D(u_LightMap, var_LightTex).rgba;\r\n #if defined(RGBE_LIGHTMAP)\r\n\tlig" -"htSample.rgb *= exp2(lightSample.a * 255.0 - 128.0);\r\n #endif\r\n\tvec3 " -"directedLight = lightSample.rgb;\r\n#elif defined(USE_LIGHT_VECTOR)\r\n #i" -"f defined(USE_FAST_LIGHT)\r\n\tvec3 directedLight = var_VectLight;\r\n #el" -"se\r\n #if defined(USE_INVSQRLIGHT)\r\n\tfloat intensity = 1.0 / dot(wor" -"ldLight, worldLight);\r\n #else\r\n\tfloat intensity = clamp((1.0 - dot(" -"worldLight, worldLight) / (u_LightRadius * u_LightRadius)) * 1.07, 0.0, 1.0" -");\r\n #endif\r\n #if defined(USE_SHADOWMAP)\r\n \tvec3 dist3 = text" -"ureCube(u_ShadowMap, worldLight).rgb;\r\n\tfloat dist = dot(dist3, vec3(1.0" -" / (256.0 * 256.0), 1.0 / 256.0, 1.0)) * u_LightRadius;\r\n\r\n\tintensity " -"*= clamp(sign(dist - length(worldLight)), 0.0, 1.0);\r\n #endif\r\n\tvec" -"3 directedLight = u_DirectedLight * intensity;\r\n\tvec3 ambientLight = u_" -"AmbientLight;\r\n #endif\r\n#elif defined(USE_LIGHT_VERTEX)\r\n\tvec3 dire" -"ctedLight = var_Color.rgb;\r\n#endif\r\n\t\r\n#if defined(USE_NORMALMAP) ||" -" defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)\r\n\tvec3 SampleToView = no" -"rmalize(var_SampleToView);\r\n#endif\r\n\tvec2 tex = var_DiffuseTex;\r\n\r" -"\n\tfloat ambientDiff = 1.0;\r\n\r\n#if defined(USE_NORMALMAP)\r\n #if def" -"ined(USE_VERT_TANGENT_SPACE)\r\n vec3 tangent = normalize(var_Tangent)" -";\r\n\tvec3 bitangent = normalize(var_Bitangent);\r\n #else\r\n\tvec3 q0 " -"= dFdx(var_Position.xyz);\r\n\tvec3 q1 = dFdy(var_Position.xyz);\r\n\tvec2" -" st0 = dFdx(tex);\r\n\tvec2 st1 = dFdy(tex);\r\n\r\n\tvec3 tangent = -nor" -"malize( q0 * st1.t - q1 * st0.t);\r\n\tvec3 bitangent = -normalize(-q0 * st" -"1.s + q1 * st0.s);\r\n #endif\r\n\r\n\tmat3 tangentToWorld = mat3(tangent," -" bitangent, normalize(var_Normal.xyz));\r\n\r\n #if defined(USE_PARALLAXMA" -"P)\r\n\tvec3 offsetDir = normalize(SampleToView * tangentToWorld);\r\n #" -"if 0\r\n float height = SampleHeight(u_NormalMap, tex);\r\n\tfloat pdist" -" = 0.05 * height - (0.05 / 2.0);\r\n #else\r\n\toffsetDir.xy *= 0.02 / o" -"ffsetDir.z;\r\n\tfloat pdist = RayIntersectDisplaceMap(tex, offsetDir.xy, u" -"_NormalMap);\r\n #endif\t\r\n\ttex += offsetDir.xy * pdist;\r\n #endif" -"\r\n #if defined(SWIZZLE_NORMALMAP)\r\n\tvec3 normal = 2.0 * texture2D(u_N" -"ormalMap, tex).agb - 1.0;\r\n #else\r\n\tvec3 normal = 2.0 * texture2D(u_N" -"ormalMap, tex).rgb - 1.0;\r\n #endif\r\n\tnormal.z = sqrt(clamp(1.0 - dot(" -"normal.xy, normal.xy), 0.0, 1.0));\r\n\tvec3 worldNormal = tangentToWorld *" -" normal;\r\n #if defined(r_normalAmbient)\r\n\tambientDiff = 0.781341 * no" -"rmal.z + 0.218659;\r\n #endif\r\n#else\r\n\tvec3 worldNormal = var_Normal;" -"\r\n#endif\r\n\r\n\tvec4 diffuse = texture2D(u_DiffuseMap, tex);\r\n\r\n#if" -" defined(USE_LIGHT) && defined(USE_FAST_LIGHT)\r\n\tdiffuse.rgb *= directed" -"Light;\r\n#elif defined(USE_LIGHT)\r\n\tworldNormal = normalize(worldNormal" -");\r\n\tworldLight = normalize(worldLight);\r\n\r\n #if defined(USE_LIGHTM" -"AP) || defined(USE_LIGHT_VERTEX)\r\n\tdirectedLight /= max(dot(normalize(va" -"r_Normal), worldLight), 0.004);\r\n\r\n #if defined(r_normalAmbient)\r\n" -"\tvec3 ambientLight = directedLight * r_normalAmbient;\r\n\tdirectedLight -" -"= ambientLight;\r\n #else\r\n\tvec3 ambientLight = vec3(0);\r\n #endi" -"f\r\n #endif\r\n\r\n\tfloat NL = clamp(dot(worldNormal, worldLight), 0." -"0, 1.0);\r\n\tfloat NE = clamp(dot(worldNormal, SampleToView), 0.0, 1.0);" -"\r\n #if defined(USE_SPECULARMAP)\r\n\tfloat fzero = u_SpecularReflectance" -";\r\n #else\r\n\tfloat fzero = 0.0;\r\n #endif\r\n\tfloat directedDiff = " -"NL * CalcDiffuse(worldNormal, worldLight, SampleToView, NE, NL, fzero, u_Di" -"ffuseRoughness);\r\n\tdiffuse.rgb *= directedLight * directedDiff + ambient" -"Diff * ambientLight;\r\n \r\n #if defined(USE_SPECULARMAP)\r\n\tvec4 spec" -"ular = texture2D(u_SpecularMap, tex);\r\n\t//specular.rgb = clamp(specular." -"rgb - diffuse.rgb, 0.0, 1.0);\r\n\tfloat shininess = specular.a * 255 + 1.0" -";\r\n\r\n\tvec3 halfAngle = normalize(worldLight + SampleToView);\r\n\r\n\t" -"float EH = clamp(dot(SampleToView, halfAngle), 0.0, 1.0);\r\n\tfloat NH = c" -"lamp(dot(worldNormal, halfAngle), 0.0, 1.0);\r\n\r\n\tfloat directedSpec =" -" NL * CalcSpecular(NH, NL, NE, EH, fzero, shininess);\r\n \r\n #if defi" -"ned(r_normalAmbient)\r\n\tvec3 ambientHalf = normalize(var_Normal + SampleT" -"oView);\r\n\tfloat ambientSpec = max(dot(ambientHalf, worldNormal) + 0.5, 0" -".0);\r\n\tambientSpec *= ambientSpec * 0.44;\r\n\tambientSpec = pow(ambient" -"Spec, shininess) * fzero;\r\n #if defined(USE_TRIACE)\r\n\tambientSpec" -" *= 0.1248582 * shininess + 0.2691817;\r\n #endif \r\n\tspecular.rg" -"b *= directedSpec * directedLight + ambientSpec * ambientLight;\r\n #els" -"e\r\n\tspecular.rgb *= directedSpec * directedLight;\r\n #endif\r\n #en" -"dif\r\n#endif\r\n\r\n\tgl_FragColor = diffuse;\r\n\r\n#if defined(USE_SPECU" -"LARMAP) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)\r\n\tgl_FragColor" -".rgb += specular.rgb;\r\n#endif\r\n\r\n#if !defined(USE_LIGHT_VERTEX)\r\n\t" -"gl_FragColor *= var_Color;\r\n#endif\r\n}\r\n"; +"_LightRadius;\r\n#endif\r\n\r\n#if defined(USE_LIGHT)\r\nuniform vec2 " +"u_MaterialInfo;\r\n#endif\r\n\r\nvarying vec2 var_DiffuseTex;\r\n#if d" +"efined(USE_LIGHTMAP)\r\nvarying vec2 var_LightTex;\r\n#endif\r\nvaryin" +"g vec4 var_Color;\r\nvarying vec3 var_Position;\r\n\r\nvarying ve" +"c3 var_SampleToView;\r\n\r\nvarying vec3 var_Normal;\r\n#if defin" +"ed(USE_VERT_TANGENT_SPACE)\r\nvarying vec3 var_Tangent;\r\nvarying vec" +"3 var_Bitangent;\r\n#endif\r\n\r\n#if defined(USE_LIGHT_VECTOR) && def" +"ined(USE_FAST_LIGHT)\r\nvarying vec3 var_VectLight;\r\n#endif\r\n\r\n#" +"if defined(USE_LIGHT) && !defined(USE_DELUXEMAP)\r\nvarying vec3 var_W" +"orldLight;\r\n#endif\r\n\r\n#define EPSILON 0.00000001\r\n\r\n#if defined(U" +"SE_PARALLAXMAP)\r\nfloat SampleHeight(sampler2D normalMap, vec2 t)\r\n{\r\n" +" #if defined(SWIZZLE_NORMALMAP)\r\n\treturn texture2D(normalMap, t).r;\r\n" +" #else\r\n\treturn texture2D(normalMap, t).a;\r\n #endif\r\n}\r\n\r\nfloa" +"t RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap)\r\n{\r\n\t" +"const int linearSearchSteps = 16;\r\n\tconst int binarySearchSteps = 6;\r\n" +"\r\n\tfloat depthStep = 1.0 / float(linearSearchSteps);\r\n\r\n\t// current" +" size of search window\r\n\tfloat size = depthStep;\r\n\r\n\t// current dep" +"th position\r\n\tfloat depth = 0.0;\r\n\r\n\t// best match found (starts wi" +"th last position 1.0)\r\n\tfloat bestDepth = 1.0;\r\n\r\n\t// search front " +"to back for first point inside object\r\n\tfor(int i = 0; i < linearSearchS" +"teps - 1; ++i)\r\n\t{\r\n\t\tdepth += size;\r\n\t\t\r\n\t\tfloat t = Sample" +"Height(normalMap, dp + ds * depth);\r\n\t\t\r\n\t\tif(bestDepth > 0.996)\t" +"\t// if no depth found yet\r\n\t\t\tif(depth >= t)\r\n\t\t\t\tbestDepth = d" +"epth;\t// store best depth\r\n\t}\r\n\r\n\tdepth = bestDepth;\r\n\t\r\n\t//" +" recurse around first point (depth) for closest match\r\n\tfor(int i = 0; i" +" < binarySearchSteps; ++i)\r\n\t{\r\n\t\tsize *= 0.5;\r\n\r\n\t\tfloat t = " +"SampleHeight(normalMap, dp + ds * depth);\r\n\t\t\r\n\t\tif(depth >= t)\r\n" +"\t\t{\r\n\t\t\tbestDepth = depth;\r\n\t\t\tdepth -= 2.0 * size;\r\n\t\t}\r" +"\n\r\n\t\tdepth += size;\r\n\t}\r\n\r\n\treturn bestDepth;\r\n}\r\n#endif\r" +"\n\r\nfloat CalcDiffuse(vec3 N, vec3 L, vec3 E, float NE, float NL, float f" +"zero, float shininess)\r\n{\r\n #if defined(USE_OREN_NAYAR)\r\n\tfloat rou" +"ghness = sqrt(2.0 / max(shininess, EPSILON));\r\n\r\n\tfloat gamma = dot(E " +"- N * NE, L - N * NL);\r\n\tfloat r_sq = roughness * roughness;\r\n\r\n\tfl" +"oat A = 1.0 - 0.5 * (r_sq / (r_sq + 0.57));\r\n\tfloat B = 0.45 * (r_sq / (" +"r_sq + 0.09));\r\n\r\n\tfloat alpha = max(acos(NE), acos(NL));\r\n\tfloat b" +"eta = min(acos(NE), acos(NL));\r\n\r\n\tfloat C = sin(alpha) * tan(beta);" +"\r\n\r\n\treturn A + B * clamp(gamma, 0.0, 1.0) * C;\r\n #else\r\n\treturn" +" 1.0 - fzero;\r\n #endif\r\n}\r\n\r\n#if defined(USE_SPECULARMAP)\r\nfloat" +" CalcSpecular(float NH, float NL, float NE, float EH, float fzero, float sh" +"ininess)\r\n{\r\n #if defined(USE_BLINN) || defined(USE_TRIACE) || defined" +"(USE_TORRANCE_SPARROW)\r\n\tfloat blinn = pow(NH, shininess);\r\n #endif\r" +"\n\r\n #if defined(USE_BLINN)\r\n\treturn blinn;\r\n #endif\r\n\r\n #if " +"defined(USE_COOK_TORRANCE) || defined (USE_TRIACE) || defined (USE_TORRANCE" +"_SPARROW)\r\n\tfloat fresnel = fzero + (1.0 - fzero) * pow(1.0 - EH, 5);\r" +"\n #endif\r\n\r\n #if defined(USE_COOK_TORRANCE) || defined(USE_TORRANCE_" +"SPARROW)\r\n\tfloat geo = 2.0 * NH * min(NE, NL);\r\n\tgeo /= max(EH, geo);" +"\r\n #endif \r\n\r\n #if defined(USE_COOK_TORRANCE)\r\n\tfloat m = sqrt(" +"2.0 / max(shininess, EPSILON));\r\n\r\n\tfloat m_sq = m * m;\r\n\tfloat NH_" +"sq = NH * NH;\r\n\tfloat beckmann = exp((NH_sq - 1.0) / max(m_sq * NH_sq, E" +"PSILON)) / max(4.0 * m_sq * NH_sq * NH_sq, EPSILON);\r\n\r\n\treturn fresne" +"l * geo * beckmann / max(NE, EPSILON);\r\n #endif\r\n\r\n #if defined(USE" +"_TRIACE)\r\n\tfloat scale = 0.1248582 * shininess + 0.2691817;\r\n\r\n\tret" +"urn fresnel * scale * blinn / max(max(NL, NE), EPSILON);\r\n #endif\r\n " +"\r\n #if defined(USE_TORRANCE_SPARROW)\r\n\tfloat scale = 0.125 * shinines" +"s + 1.0;\r\n\r\n\treturn fresnel * geo * scale * blinn / max(NE, EPSILON);" +"\r\n #endif\r\n}\r\n#endif\r\n\r\nvoid main()\r\n{\r\n#if defined(USE_DELU" +"XEMAP)\r\n\tvec3 worldLight = 2.0 * texture2D(u_DeluxeMap, var_LightTex).xy" +"z - vec3(1.0);\r\n\t//worldLight += var_WorldLight * 0.0001;\r\n#elif defin" +"ed(USE_LIGHT)\r\n\tvec3 worldLight = var_WorldLight;\r\n#endif\r\n\r\n#if d" +"efined(USE_LIGHTMAP)\r\n\tvec4 lightSample = texture2D(u_LightMap, var_Ligh" +"tTex).rgba;\r\n #if defined(RGBE_LIGHTMAP)\r\n\tlightSample.rgb *= exp2(li" +"ghtSample.a * 255.0 - 128.0);\r\n #endif\r\n\tvec3 directedLight = lightSa" +"mple.rgb;\r\n#elif defined(USE_LIGHT_VECTOR)\r\n #if defined(USE_FAST_LIGH" +"T)\r\n\tvec3 directedLight = var_VectLight;\r\n #else\r\n #if defined(U" +"SE_INVSQRLIGHT)\r\n\tfloat intensity = 1.0 / dot(worldLight, worldLight);\r" +"\n #else\r\n\tfloat intensity = clamp((1.0 - dot(worldLight, worldLight)" +" / (u_LightRadius * u_LightRadius)) * 1.07, 0.0, 1.0);\r\n #endif\r\n " +" #if defined(USE_SHADOWMAP)\r\n \tvec3 dist3 = textureCube(u_ShadowMap, wo" +"rldLight).rgb;\r\n\tfloat dist = dot(dist3, vec3(1.0 / (256.0 * 256.0), 1.0" +" / 256.0, 1.0)) * u_LightRadius;\r\n\r\n\tintensity *= clamp(sign(dist - le" +"ngth(worldLight)), 0.0, 1.0);\r\n #endif\r\n\tvec3 directedLight = u_Dir" +"ectedLight * intensity;\r\n\tvec3 ambientLight = u_AmbientLight;\r\n #end" +"if\r\n#elif defined(USE_LIGHT_VERTEX)\r\n\tvec3 directedLight = var_Color.r" +"gb;\r\n#endif\r\n\t\r\n#if defined(USE_NORMALMAP) || defined(USE_LIGHT) && " +"!defined(USE_FAST_LIGHT)\r\n\tvec3 SampleToView = normalize(var_SampleToVie" +"w);\r\n#endif\r\n\tvec2 tex = var_DiffuseTex;\r\n\r\n\tfloat ambientDiff = " +"1.0;\r\n\r\n#if defined(USE_NORMALMAP)\r\n #if defined(USE_VERT_TANGENT_SP" +"ACE)\r\n vec3 tangent = normalize(var_Tangent);\r\n\tvec3 bitangent = " +"normalize(var_Bitangent);\r\n #else\r\n\tvec3 q0 = dFdx(var_Position.xyz)" +";\r\n\tvec3 q1 = dFdy(var_Position.xyz);\r\n\tvec2 st0 = dFdx(tex);\r\n\tv" +"ec2 st1 = dFdy(tex);\r\n\r\n\tvec3 tangent = -normalize( q0 * st1.t - q1 " +"* st0.t);\r\n\tvec3 bitangent = -normalize(-q0 * st1.s + q1 * st0.s);\r\n " +"#endif\r\n\r\n\tmat3 tangentToWorld = mat3(tangent, bitangent, normalize(va" +"r_Normal.xyz));\r\n\r\n #if defined(USE_PARALLAXMAP)\r\n\tvec3 offsetDir =" +" normalize(SampleToView * tangentToWorld);\r\n #if 0\r\n float height" +" = SampleHeight(u_NormalMap, tex);\r\n\tfloat pdist = 0.05 * height - (0.05" +" / 2.0);\r\n #else\r\n\toffsetDir.xy *= 0.02 / offsetDir.z;\r\n\tfloat p" +"dist = RayIntersectDisplaceMap(tex, offsetDir.xy, u_NormalMap);\r\n #end" +"if\t\r\n\ttex += offsetDir.xy * pdist;\r\n #endif\r\n #if defined(SWIZZLE" +"_NORMALMAP)\r\n\tvec3 normal = 2.0 * texture2D(u_NormalMap, tex).agb - 1.0;" +"\r\n #else\r\n\tvec3 normal = 2.0 * texture2D(u_NormalMap, tex).rgb - 1.0;" +"\r\n #endif\r\n\tnormal.z = sqrt(clamp(1.0 - dot(normal.xy, normal.xy), 0." +"0, 1.0));\r\n\tvec3 worldNormal = tangentToWorld * normal;\r\n #if defined" +"(r_normalAmbient)\r\n\tambientDiff = 0.781341 * normal.z + 0.218659;\r\n #" +"endif\r\n#else\r\n\tvec3 worldNormal = var_Normal;\r\n#endif\r\n\r\n\tvec4 " +"diffuse = texture2D(u_DiffuseMap, tex);\r\n\r\n#if defined(USE_LIGHT) && de" +"fined(USE_FAST_LIGHT)\r\n\tdiffuse.rgb *= directedLight;\r\n#elif defined(U" +"SE_LIGHT)\r\n\tworldNormal = normalize(worldNormal);\r\n\tworldLight = norm" +"alize(worldLight);\r\n\r\n #if defined(USE_LIGHTMAP) || defined(USE_LIGHT_" +"VERTEX)\r\n\tdirectedLight /= max(dot(normalize(var_Normal), worldLight), 0" +".004);\r\n\r\n #if defined(r_normalAmbient)\r\n\tvec3 ambientLight = dir" +"ectedLight * r_normalAmbient;\r\n\tdirectedLight -= ambientLight;\r\n #e" +"lse\r\n\tvec3 ambientLight = vec3(0);\r\n #endif\r\n #endif\r\n\r\n\tfl" +"oat NL = clamp(dot(worldNormal, worldLight), 0.0, 1.0);\r\n\tfloat NE = " +"clamp(dot(worldNormal, SampleToView), 0.0, 1.0);\r\n\t\r\n\tfloat fzero = " +"u_MaterialInfo.x;\r\n\tfloat shininess = u_MaterialInfo.y;\r\n #if defined" +"(USE_SPECULARMAP)\r\n\tvec4 specular = texture2D(u_SpecularMap, tex);\r\n\t" +"//specular.rgb = clamp(specular.rgb - diffuse.rgb, 0.0, 1.0);\r\n\tshinines" +"s *= specular.a;\r\n #endif\r\n\tfloat directedDiff = NL * CalcDiffuse(wor" +"ldNormal, worldLight, SampleToView, NE, NL, fzero, shininess);\r\n\tdiffuse" +".rgb *= directedLight * directedDiff + ambientDiff * ambientLight;\r\n \r" +"\n #if defined(USE_SPECULARMAP)\r\n\tvec3 halfAngle = normalize(worldLight" +" + SampleToView);\r\n\r\n\tfloat EH = clamp(dot(SampleToView, halfAngle), 0" +".0, 1.0);\r\n\tfloat NH = clamp(dot(worldNormal, halfAngle), 0.0, 1.0);\r" +"\n\r\n\tfloat directedSpec = NL * CalcSpecular(NH, NL, NE, EH, fzero, shini" +"ness);\r\n \r\n #if defined(r_normalAmbient)\r\n\tvec3 ambientHalf = no" +"rmalize(var_Normal + SampleToView);\r\n\tfloat ambientSpec = max(dot(ambien" +"tHalf, worldNormal) + 0.5, 0.0);\r\n\tambientSpec *= ambientSpec * 0.44;\r" +"\n\tambientSpec = pow(ambientSpec, shininess) * fzero;\r\n\tspecular.rgb *=" +" directedSpec * directedLight + ambientSpec * ambientLight;\r\n #else\r" +"\n\tspecular.rgb *= directedSpec * directedLight;\r\n #endif\r\n #endif" +"\r\n#endif\r\n\r\n\tgl_FragColor = diffuse;\r\n\r\n#if defined(USE_SPECULAR" +"MAP) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)\r\n\tgl_FragColor.rg" +"b += specular.rgb;\r\n#endif\r\n\r\n#if !defined(USE_LIGHT_VERTEX)\r\n\tgl_" +"FragColor *= var_Color;\r\n#endif\r\n}\r\n"; static const char *fallbackShadowfillShader_vp = "attribute vec4 attr_Position;\r\nattribute vec3 attr_Normal;\r\nattribute" @@ -1154,7 +1152,7 @@ static int GLSL_InitGPUShader2(shaderProgram_t * program, const char *name, int // if(attribs & ATTR_TEXCOORD3) // qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD3, "attr_TexCoord3"); -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if(attribs & ATTR_TANGENT) qglBindAttribLocationARB(program->program, ATTR_INDEX_TANGENT, "attr_Tangent"); @@ -1180,7 +1178,7 @@ static int GLSL_InitGPUShader2(shaderProgram_t * program, const char *name, int if(attribs & ATTR_NORMAL2) qglBindAttribLocationARB(program->program, ATTR_INDEX_NORMAL2, "attr_Normal2"); -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if(attribs & ATTR_TANGENT2) qglBindAttribLocationARB(program->program, ATTR_INDEX_TANGENT2, "attr_Tangent2"); @@ -1799,8 +1797,8 @@ void GLSL_InitGPUShaders(void) if (r_normalMapping->integer == 2) Q_strcat(extradefines, 1024, "#define USE_OREN_NAYAR\n"); -#ifdef USE_VERT_TANGENT_SPACE - Q_strcat(extradefines, 1024, "#define USE_VERT_TANGENT_SPACE"); +#ifdef USE_VERT_TANGENT_SPACE + Q_strcat(extradefines, 1024, "#define USE_VERT_TANGENT_SPACE"); attribs |= ATTR_TANGENT | ATTR_BITANGENT; #endif } @@ -1844,7 +1842,7 @@ void GLSL_InitGPUShaders(void) Q_strcat(extradefines, 1024, "#define USE_VERTEX_ANIMATION\n#define USE_MODELMATRIX\n"); attribs |= ATTR_POSITION2 | ATTR_NORMAL2; -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if (i & LIGHTDEF_USE_NORMALMAP && r_normalMapping->integer) { attribs |= ATTR_TANGENT2 | ATTR_BITANGENT2; @@ -1876,8 +1874,7 @@ void GLSL_InitGPUShaders(void) GLSL_AddUniform(&tr.lightallShader[i], GENERIC_UNIFORM_LIGHTORIGIN, "u_LightOrigin", GLSL_VEC4); GLSL_AddUniform(&tr.lightallShader[i], GENERIC_UNIFORM_LIGHTRADIUS, "u_LightRadius", GLSL_FLOAT); - GLSL_AddUniform(&tr.lightallShader[i], GENERIC_UNIFORM_SPECULARREFLECTANCE, "u_SpecularReflectance", GLSL_FLOAT); - GLSL_AddUniform(&tr.lightallShader[i], GENERIC_UNIFORM_DIFFUSEROUGHNESS, "u_DiffuseRoughness", GLSL_FLOAT); + GLSL_AddUniform(&tr.lightallShader[i], GENERIC_UNIFORM_MATERIALINFO, "u_MaterialInfo", GLSL_VEC2); GLSL_AddUniform(&tr.lightallShader[i], GENERIC_UNIFORM_BASECOLOR, "u_BaseColor", GLSL_VEC4); GLSL_AddUniform(&tr.lightallShader[i], GENERIC_UNIFORM_VERTCOLOR, "u_VertColor", GLSL_VEC4); @@ -2113,12 +2110,12 @@ void GLSL_ShutdownGPUShaders(void) qglDisableVertexAttribArrayARB(ATTR_INDEX_POSITION); qglDisableVertexAttribArrayARB(ATTR_INDEX_POSITION2); qglDisableVertexAttribArrayARB(ATTR_INDEX_NORMAL); -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE qglDisableVertexAttribArrayARB(ATTR_INDEX_TANGENT); qglDisableVertexAttribArrayARB(ATTR_INDEX_BITANGENT); #endif qglDisableVertexAttribArrayARB(ATTR_INDEX_NORMAL2); -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE qglDisableVertexAttribArrayARB(ATTR_INDEX_TANGENT2); qglDisableVertexAttribArrayARB(ATTR_INDEX_BITANGENT2); #endif @@ -2254,7 +2251,7 @@ void GLSL_VertexAttribsState(uint32_t stateBits) } } -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if(diff & ATTR_TANGENT) { if(stateBits & ATTR_TANGENT) @@ -2340,7 +2337,7 @@ void GLSL_VertexAttribsState(uint32_t stateBits) } } -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if(diff & ATTR_TANGENT2) { if(stateBits & ATTR_TANGENT2) @@ -2416,7 +2413,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) glState.vertexAttribPointersSet |= ATTR_NORMAL; } -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if((attribBits & ATTR_TANGENT) && !(glState.vertexAttribPointersSet & ATTR_TANGENT)) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_TANGENT )\n"); @@ -2466,7 +2463,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) glState.vertexAttribPointersSet |= ATTR_NORMAL2; } -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if((attribBits & ATTR_TANGENT2) && !(glState.vertexAttribPointersSet & ATTR_TANGENT2)) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_TANGENT2 )\n"); diff --git a/reaction/code/renderergl2/tr_local.h b/reaction/code/renderergl2/tr_local.h index e854dcc7..84df8139 100644 --- a/reaction/code/renderergl2/tr_local.h +++ b/reaction/code/renderergl2/tr_local.h @@ -449,8 +449,7 @@ typedef struct { stageType_t type; struct shaderProgram_s *glslShaderGroup; int glslShaderIndex; - float specularReflectance; - float diffuseRoughness; + vec2_t materialInfo; } shaderStage_t; struct shaderCommands_s; @@ -864,8 +863,7 @@ enum GENERIC_UNIFORM_MODELVIEWPROJECTIONMATRIX, GENERIC_UNIFORM_TIME, GENERIC_UNIFORM_VERTEXLERP, - GENERIC_UNIFORM_SPECULARREFLECTANCE, - GENERIC_UNIFORM_DIFFUSEROUGHNESS, + GENERIC_UNIFORM_MATERIALINFO, GENERIC_UNIFORM_COUNT }; diff --git a/reaction/code/renderergl2/tr_shade.c b/reaction/code/renderergl2/tr_shade.c index 477e0f55..ef989fb7 100644 --- a/reaction/code/renderergl2/tr_shade.c +++ b/reaction/code/renderergl2/tr_shade.c @@ -803,9 +803,8 @@ static void ForwardDlight( void ) { GLSL_SetUniformFloat(sp, GENERIC_UNIFORM_LIGHTRADIUS, radius); - GLSL_SetUniformFloat(sp, GENERIC_UNIFORM_SPECULARREFLECTANCE, pStage->specularReflectance); - GLSL_SetUniformFloat(sp, GENERIC_UNIFORM_DIFFUSEROUGHNESS, pStage->diffuseRoughness); - + GLSL_SetUniformVec2(sp, GENERIC_UNIFORM_MATERIALINFO, pStage->materialInfo); + // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light // where they aren't rendered GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL ); @@ -1002,7 +1001,7 @@ static unsigned int RB_CalcShaderVertexAttribs( shaderCommands_t *input ) if (vertexAttribs & ATTR_NORMAL) { vertexAttribs |= ATTR_NORMAL2; -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE vertexAttribs |= ATTR_TANGENT2; vertexAttribs |= ATTR_BITANGENT2; #endif @@ -1147,8 +1146,7 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input ) GLSL_SetUniformMatrix16(sp, GENERIC_UNIFORM_MODELMATRIX, backEnd.or.transformMatrix); - GLSL_SetUniformFloat(sp, GENERIC_UNIFORM_SPECULARREFLECTANCE, pStage->specularReflectance); - GLSL_SetUniformFloat(sp, GENERIC_UNIFORM_DIFFUSEROUGHNESS, pStage->diffuseRoughness); + GLSL_SetUniformVec2(sp, GENERIC_UNIFORM_MATERIALINFO, pStage->materialInfo); // // do multitexture diff --git a/reaction/code/renderergl2/tr_shader.c b/reaction/code/renderergl2/tr_shader.c index a0e13674..744a8615 100644 --- a/reaction/code/renderergl2/tr_shader.c +++ b/reaction/code/renderergl2/tr_shader.c @@ -907,7 +907,8 @@ static qboolean ParseStage( shaderStage_t *stage, char **text ) else if(!Q_stricmp(token, "specularMap")) { stage->type = ST_SPECULARMAP; - stage->specularReflectance = 0.04f; + stage->materialInfo[0] = 0.04f; + stage->materialInfo[1] = 256.0f; } else { @@ -926,20 +927,20 @@ static qboolean ParseStage( shaderStage_t *stage, char **text ) ri.Printf( PRINT_WARNING, "WARNING: missing parameter for specular reflectance in shader '%s'\n", shader.name ); continue; } - stage->specularReflectance = atof( token ); + stage->materialInfo[0] = atof( token ); } // - // diffuseRoughness + // specularExponent // - else if (!Q_stricmp(token, "diffuseroughness")) + else if (!Q_stricmp(token, "specularexponent")) { token = COM_ParseExt(text, qfalse); if ( token[0] == 0 ) { - ri.Printf( PRINT_WARNING, "WARNING: missing parameter for diffuse roughness in shader '%s'\n", shader.name ); + ri.Printf( PRINT_WARNING, "WARNING: missing parameter for specular exponent in shader '%s'\n", shader.name ); continue; } - stage->diffuseRoughness = atof( token ); + stage->materialInfo[1] = atof( token ); } // // rgbGen @@ -1881,7 +1882,7 @@ static void ComputeVertexAttribs(void) { shader.vertexAttribs |= ATTR_NORMAL; -#ifdef USE_VERT_TANGENT_SPACE +#ifdef USE_VERT_TANGENT_SPACE if (pStage->glslShaderIndex & LIGHTDEF_USE_NORMALMAP) { shader.vertexAttribs |= ATTR_BITANGENT | ATTR_TANGENT; @@ -2189,7 +2190,8 @@ static void CollapseStagesToLightall(shaderStage_t *diffuse, { //ri.Printf(PRINT_ALL, ", specularmap %s", specular->bundle[0].image[0]->imgName); diffuse->bundle[TB_SPECULARMAP] = specular->bundle[0]; - diffuse->specularReflectance = specular->specularReflectance; + diffuse->materialInfo[0] = specular->materialInfo[0]; + diffuse->materialInfo[1] = specular->materialInfo[1]; defs |= LIGHTDEF_USE_SPECULARMAP; } }