OpenGL2: r_cubemapping 2 for box cubemap parallax.

This commit is contained in:
James Canete 2018-12-21 19:53:18 -08:00
parent b0d2b141e7
commit e5da13f146
2 changed files with 40 additions and 0 deletions

View file

@ -194,6 +194,36 @@ float CalcLightAttenuation(float point, float normDist)
} }
vec4 hitCube(vec3 ray, vec3 pos, vec3 invSize, float lod, samplerCube tex)
{
// find any hits on cubemap faces facing the camera
vec3 scale = (sign(ray) - pos) / ray;
// find the nearest hit
float minScale = min(min(scale.x, scale.y), scale.z);
// if the nearest hit is behind the camera, ignore
// should not be necessary as long as pos is inside the cube
//if (minScale < 0.0)
//return vec4(0.0);
// calculate the hit position, that's our texture coordinates
vec3 tc = pos + ray * minScale;
// if the texture coordinates are outside the cube, ignore
// necessary since we're not fading out outside the cube
if (any(greaterThan(abs(tc), vec3(1.00001))))
return vec4(0.0);
// fade out when approaching the cubemap edges
//vec3 fade3 = abs(pos);
//float fade = max(max(fade3.x, fade3.y), fade3.z);
//fade = clamp(1.0 - fade, 0.0, 1.0);
//return vec4(textureCubeLod(tex, tc, lod).rgb * fade, fade);
return vec4(textureCubeLod(tex, tc, lod).rgb, 1.0);
}
void main() void main()
{ {
vec3 viewDir, lightColor, ambientColor, reflectance; vec3 viewDir, lightColor, ambientColor, reflectance;
@ -374,7 +404,11 @@ void main()
// from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/ // from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir; vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir;
#if defined(USE_BOX_CUBEMAP_PARALLAX)
vec3 cubeLightColor = hitCube(R * u_CubeMapInfo.w, parallax, u_CubeMapInfo.www, ROUGHNESS_MIPS * roughness, u_CubeMap).rgb * u_EnableTextures.w;
#else
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, ROUGHNESS_MIPS * roughness).rgb * u_EnableTextures.w; vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, ROUGHNESS_MIPS * roughness).rgb * u_EnableTextures.w;
#endif
// normalize cubemap based on last roughness mip (~diffuse) // normalize cubemap based on last roughness mip (~diffuse)
// multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation // multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation

View file

@ -1129,9 +1129,15 @@ void GLSL_InitGPUShaders(void)
Q_strcat(extradefines, 1024, "#define USE_SPECULARMAP\n"); Q_strcat(extradefines, 1024, "#define USE_SPECULARMAP\n");
if (r_cubeMapping->integer) if (r_cubeMapping->integer)
{
Q_strcat(extradefines, 1024, "#define USE_CUBEMAP\n"); Q_strcat(extradefines, 1024, "#define USE_CUBEMAP\n");
if (r_cubeMapping->integer == 2)
Q_strcat(extradefines, 1024, "#define USE_BOX_CUBEMAP_PARALLAX\n");
}
else if (r_deluxeSpecular->value > 0.000001f) else if (r_deluxeSpecular->value > 0.000001f)
{
Q_strcat(extradefines, 1024, va("#define r_deluxeSpecular %f\n", r_deluxeSpecular->value)); Q_strcat(extradefines, 1024, va("#define r_deluxeSpecular %f\n", r_deluxeSpecular->value));
}
switch (r_glossType->integer) switch (r_glossType->integer)
{ {