From d9e2184c1a9a1f3115202fb66d3fb29f6753f7ca Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Mon, 10 Nov 2014 22:11:36 -0800 Subject: [PATCH] OpenGL2: Add support for parallax occlusion mapping. --- code/renderergl2/glsl/lightall_fp.glsl | 14 +++++++++++++- code/renderergl2/tr_glsl.c | 4 ++++ opengl2-readme.txt | 3 ++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/code/renderergl2/glsl/lightall_fp.glsl b/code/renderergl2/glsl/lightall_fp.glsl index f2da7821..d1155de4 100644 --- a/code/renderergl2/glsl/lightall_fp.glsl +++ b/code/renderergl2/glsl/lightall_fp.glsl @@ -100,6 +100,9 @@ float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap) // best match found (starts with last position 1.0) float bestDepth = 1.0; + // texture depth at best depth + float texDepth = 0.0; + // search front to back for first point inside object for(int i = 0; i < linearSearchSteps - 1; ++i) { @@ -109,11 +112,19 @@ float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap) if(bestDepth > 0.996) // if no depth found yet if(depth >= t) + { bestDepth = depth; // store best depth + texDepth = t; + } } depth = bestDepth; - + +#if !defined (USE_RELIEFMAP) + float prevDepth = depth - size; + float prevTexDepth = SampleDepth(normalMap, dp + ds * prevDepth); + bestDepth -= size * (prevDepth - prevTexDepth) / (size - texDepth + prevTexDepth); +#else // recurse around first point (depth) for closest match for(int i = 0; i < binarySearchSteps; ++i) { @@ -129,6 +140,7 @@ float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap) depth += size; } +#endif return bestDepth; } diff --git a/code/renderergl2/tr_glsl.c b/code/renderergl2/tr_glsl.c index 7dfea6a6..f526b8c6 100644 --- a/code/renderergl2/tr_glsl.c +++ b/code/renderergl2/tr_glsl.c @@ -1079,7 +1079,11 @@ void GLSL_InitGPUShaders(void) #endif if ((i & LIGHTDEF_USE_PARALLAXMAP) && !(i & LIGHTDEF_ENTITY) && r_parallaxMapping->integer) + { Q_strcat(extradefines, 1024, "#define USE_PARALLAXMAP\n"); + if (r_parallaxMapping->integer > 1) + Q_strcat(extradefines, 1024, "#define USE_RELIEFMAP\n"); + } } if (r_specularMapping->integer) diff --git a/opengl2-readme.txt b/opengl2-readme.txt index 167d26f1..5c4cb60b 100644 --- a/opengl2-readme.txt +++ b/opengl2-readme.txt @@ -187,7 +187,8 @@ Cvars for advanced material usage: r_parallaxMapping - Enable parallax mapping for materials that support it. 0 - No. (default) - 1 - Yes. + 1 - Use parallax occlusion mapping. + 2 - Use relief mapping. (slower) r_baseSpecular - Set the specular reflectance of materials which don't include a specular map or