From e5da13f14635b6accc5eefd6986de03d2cff9662 Mon Sep 17 00:00:00 2001 From: James Canete Date: Fri, 21 Dec 2018 19:53:18 -0800 Subject: [PATCH] OpenGL2: r_cubemapping 2 for box cubemap parallax. --- code/renderergl2/glsl/lightall_fp.glsl | 34 ++++++++++++++++++++++++++ code/renderergl2/tr_glsl.c | 6 +++++ 2 files changed, 40 insertions(+) diff --git a/code/renderergl2/glsl/lightall_fp.glsl b/code/renderergl2/glsl/lightall_fp.glsl index 6465bd84..d77cd7c7 100644 --- a/code/renderergl2/glsl/lightall_fp.glsl +++ b/code/renderergl2/glsl/lightall_fp.glsl @@ -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() { 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/ 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; + #endif // 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 diff --git a/code/renderergl2/tr_glsl.c b/code/renderergl2/tr_glsl.c index 218017de..4ed49370 100644 --- a/code/renderergl2/tr_glsl.c +++ b/code/renderergl2/tr_glsl.c @@ -1129,9 +1129,15 @@ void GLSL_InitGPUShaders(void) Q_strcat(extradefines, 1024, "#define USE_SPECULARMAP\n"); if (r_cubeMapping->integer) + { 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) + { Q_strcat(extradefines, 1024, va("#define r_deluxeSpecular %f\n", r_deluxeSpecular->value)); + } switch (r_glossType->integer) {