Skip to content

Commit

Permalink
OpenGL2: r_cubemapping 2 for box cubemap parallax.
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileTheory committed Dec 22, 2018
1 parent b0d2b14 commit e5da13f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions code/renderergl2/glsl/lightall_fp.glsl
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions code/renderergl2/tr_glsl.c
Expand Up @@ -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)
{
Expand Down

1 comment on commit e5da13f

@brugal
Copy link

@brugal brugal commented on e5da13f Jan 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to wrap hitCube() with a USE_CUBEMAP define check. Without a check, it will lead to a compilation error in Mac OS X (textureCubeLod not defined) even if r_cubeMapping 0 is used.

Please sign in to comment.