Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Shadowmapping depth bias  (Read 2852 times)
0 Members and 1 Guest are viewing this topic.
Offline thedanisaur

JGO Knight


Medals: 59



« Posted 2015-04-12 06:53:56 »

I'm having trouble finding a proper depth bias for my meshes (the comparison value in the following snippet)

1  
2  
3  
4  
5  
float sampleShadowMap(vec2 texcoords, float comparison)
{
   vec4 depth_val = texture2D(shadow_map, texcoords);
   return depth_val.z < comparison ? 0.0 : 1.0;
}


here's how I calculate it now

1  
2  
3  
4  
5  
6  
7  
   vec4 shadow_remove_persp;
   shadow_remove_persp.x = shadow_coord.x / shadow_coord.w;
   shadow_remove_persp.y = shadow_coord.y / shadow_coord.w;
   shadow_remove_persp.z = shadow_coord.z / shadow_coord.w;
   shadow_remove_persp.w = shadow_coord.w / shadow_coord.w;

 float depth_bias = shadow_remove_persp.z - (10.0 * model_scale / shadow_map_size);


and it works great, sometimes. Is there really not a way to calculate a good depth bias without just guessing?


Every village needs an idiot Cool
Offline theagentd
« Reply #1 - Posted 2015-04-12 23:24:47 »

1. Use hardware depth testing instead. It's faster and gives you free bilinear filtering.

2. Use glPolygonOffset() to apply the bias when rendering the shadow map instead of when sampling it. glPolygonOffset() can use the slope of the triangle to dynamically pick a bias, which is better than you can code yourself.

Myomyomyo.
Offline thedanisaur

JGO Knight


Medals: 59



« Reply #2 - Posted 2015-04-13 00:01:40 »

1. OK maybe I'm miss understanding this, but I am rendering and applying the shadow map in glsl(two passes, obviously?). Isn't that hardware?

2. I'm confused. By using glPolygonOffset() how does it apply the bias in such a way to know what the shadow value should be?


Edit: do you mean I should enable glPolygonOffset() when rendering from the light's position and then not even bother with doing a comparison?

Every village needs an idiot Cool
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2015-04-13 09:27:30 »

Your edit is correct. The problem with shadow mapping is that to be able to do a mathematical comparison between a projected fragment and the shadow map, you'd need both infinite floating point precision and infinite shadow map resolution. To avoid this, we use a bias. The thing is that GPUs have a way of pushing everything away from the light when rendering the shadow map. This is a better time to apply it, as the bias calculation can take the slope of the triangles being rendered into account. When rendering the shadow map in the first pass, call glEnable(GL_POLYGON_OFFSET_FILL), then glPolygonOffset() to tweak the setting. Remember to disable it afterwards.

GPUs also support hardware depth comparisons. In addition, it also supports doing 4 depth tests and bilinearly interpolating between the result of the depth test to achieve some basic filtering:

Without:


With:


These both run at the same speed (the FPS counter at the top is affected by the screenshot saving). See the shadow mapping part of this page http://www.java-gaming.org/index.php?topic=28018.0 to learn how to set it up. It's quite simple.

Myomyomyo.
Offline thedanisaur

JGO Knight


Medals: 59



« Reply #4 - Posted 2015-04-13 19:20:29 »

Thanks!

In regards to the optimizations though I've heard that certain drivers had trouble supporting sampler2DShadow and textureProj(), I have not tested this myself and am wondering if you have ran into any issues?

Every village needs an idiot Cool
Offline theagentd
« Reply #5 - Posted 2015-04-13 23:25:50 »

No drivers I've ever used have had problems with sampler2DShadow, and our game has been run on a lot of different computers. I personally test it on an AMD, an Nvidia and an Intel card at home, and none of them have ever had problems with hardware shadow testing.

About textureProj(), it is a bit redundant nowadays. Graphics cards don't actually have hardware for the projection anymore, so the w-divide is simply done in "software". textureProj() also isn't usable if you're doing PCF, as if you're taking multiple samples you need to do the z-divide first to not mess up the sampling offsets of the extra samples. Definitely use shadow samplers, but textureProj() you can avoid.

Myomyomyo.
Offline thedanisaur

JGO Knight


Medals: 59



« Reply #6 - Posted 2015-04-13 23:52:01 »

OK cool, good to know.

Thanks!

Every village needs an idiot Cool
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (578 views)
2019-09-04 15:33:17

hadezbladez (5495 views)
2018-11-16 13:46:03

hadezbladez (2398 views)
2018-11-16 13:41:33

hadezbladez (5756 views)
2018-11-16 13:35:35

hadezbladez (1219 views)
2018-11-16 13:32:03

EgonOlsen (4659 views)
2018-06-10 19:43:48

EgonOlsen (5667 views)
2018-06-10 19:43:44

EgonOlsen (3193 views)
2018-06-10 19:43:20

DesertCoockie (4093 views)
2018-05-13 18:23:11

nelsongames (5104 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!