Debian Codegraph @

examples/research_special_rendering_methods/dynamic_ambient_occlusion/README.md

raw symbols SCIP
1 # Dynamic Ambient Occlusion
2
3 Sample implementation of "Dynamic ambient occlusion". See below for algorithm descriptions and links.
4
5 By default we open the "peach" model from `data/` subdirectory. You can pass on command-line URL of a different model to open, like `castle-data:/chinchilla_awakens.x3dv`.
6
7 Navigate by mouse/keys like in [Castle Model Viewer](https://castle-engine.io/castle-model-viewer).
8
9 Use keys s / S scale shadow more / less (for more/less dramatic effect; the default scale is anyway 2 times larger than what math suggests, to be more dramatic).
10
11 Using [Castle Game Engine](https://castle-engine.io/).
12
13 ## Building
14
15 Compile by:
16
17 - [CGE editor](https://castle-engine.io/editor). Just use menu items _"Compile"_ or _"Compile And Run"_.
18
19 - Or use [CGE command-line build tool](https://castle-engine.io/build_tool). Run `castle-engine compile` in this directory.
20
21 - Or use [Lazarus](https://www.lazarus-ide.org/). Open in Lazarus `dynamic_ambient_occlusion_standalone.lpi` file and compile / run from Lazarus. Make sure to first register [CGE Lazarus packages](https://castle-engine.io/lazarus).
22
23 ## Algorithm
24
25 Dynamic ambient occlusion, based on GPU Gems 2 chapter 14, online on
26 http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter14.html
27 (PDF version on
28 http://http.download.nvidia.com/developer/GPU_Gems_2/GPU_Gems2_ch14.pdf).
29
30 Idea:
31
32 - Crude assumption: model is actually a number of elements, each element
33 corresponding to a vertex of orig model, with normal and area correctly
34 calculated.
35 See menu _"Program -> Show Elements"_.
36
37 - Encode elements info into the texture.
38
39 - On each vertex do (in vertex shader): calculate AO using elements info.
40
41 Practice:
42
43 - First of all, this AO will have too much shadow (multiple shadows
44 on the same item). So calculate 2nd time, multiplying by 1st result.
45 The key is that "if element is already in shadow, then it also doesn't
46 contribute anything to the other shadows".
47
48 2nd pass will make the result too much light (because we multiplied
49 by results of 1st pass, which were too dark...). Theoretically,
50 more passes are needed. In practice: the result is between 1st
51 and 2nd passes, so just at the end of 2nd pass take the results
52 and average.
53
54 - Note that you cannot actually do this in vertex shader: with arch 3,
55 texture2D calls in vertex shader are not allowed or
56 very very horribly-terribly slow. And you want to read texture a lot
57 to get all element infos.
58
59 (I tried to do the 1st testing implementation
60 of one pass on vertex shader, it simply doesn't work when tested on older
61 NVidia and works with 1 frame / couple seconds (even with 1 element count!!!)
62 on fglrx.)
63
64 - So do this on fragment shader. Also, render such that 1 screen pixel =
65 one element, and simply write result back to buffer. Using the texture,
66 1st shader passes results to the 2nd, and 2nd passes it to the CPU.
67
68 For communication between shaders, texture data doesn't have
69 to go through CPU --- glCopyTexImage2D is perfectly designed for such tricks.
70
71 For actual rendering, I grab the colors to the CPU --- while it would be possible
72 to avoid this, I would then run into trouble trying to access textures
73 from vertex shader, which is practically prohibited for shaders gen <= 3,
74 see above notes.
75
76 - Shader optimization notes:
77
78 - Remember to expand constant variables for the shader, instead
79 of making them uniforms. This is important, not only for speed,
80 also for correctness: older GPUs need to unroll "for" loop
81 (to elements_count) at compilation.
82
83 (I didn't expand all possible vars, because fglrx doesn't tolerate
84 floats with fractional part in GLSL code; so to be safe for fglrx too,
85 only ints can be expanded such.)
86
87 - Above unrolling also means that this may just get too difficult
88 for older GPUs for larger models: too many instructions after unrolling.
89 E.g. my NVidia GPU (kocury) "GeForce FX 5200/AGP/SSE2/3DNOW!" handles
90 smaller models (simplico), but fails with
91 "error: too many instructions" on larger ones (peach, chinchilla).
92 More tests show that the border is somewhere between
93 24 (Ok) and 42 (not Ok) verts (for this GPU, of course;
94 much newer Radeon on MacBookPro can handle at least 20k (although
95 naive implementation gets really slow then).
96
97 - Note that GPU Gems text gives two (different) equations for calculating
98 element-to-element shadowing, both of them wrong IMO...
99 Yes, I did implemented and tried them. It's a matter of changing
100 "color -= ..." line.
101 Math eq on page 3 is equivalent to
102
103 ```
104 color -= sqrt(sqr_distance) * cos_emitter_angle *
105 max(1.0, 4.0 * cos_current_angle) /
106 sqrt(element_area / pi + sqr_distance);
107 ```
108
109 Code on page 4 is (possibly --- not sure, as they put "1 - " part
110 already there, so I don't see a reasonable way to use it?) equivalent to
111
112 ```
113 color -= inversesqrt((element_area/pi)/sqr_distance + 1) *
114 cos_emitter_angle *
115 min(1.0, 4.0 * cos_current_angle);
116 ```
117
118 If you grok reasoning behind their equations, and/or if there's an
119 error in my implementations above, mail me. My implementation follows
120 normal approximation for solid angle:
121
122 ```
123 element_area * cos_emitter_angle / sqr_distance
124 ```
125
126 We also multiply by cos_current_angle
127 (as light coming from an angle is scaled down, so blocker for
128 which cos_current_angle is small blocks less light).
129
130 Advantages:
131
132 - Since all work is done on the fly, the scene may be absolutely dynamic.
133 (Although on change, list of elements must be updated, this causes CPU work.)
134 For example, see:
135
136 - data/chinchilla_awakens (timesensor animation) and
137 - data/dynamic_world_ifs (interactive world changing, press left mouse down, use keys werxdf, uiojkl)
138
139 - Can make bent normals, indirect lighting almost for free.
140
141 - Although the work is done on shaders, resulting colors can be grabbed to CPU.
142
143 While this is not efficient, it allowed us to make simple implementation, and to easily debug it too.
144
145 - Can work with practically any 3d model.
146
147 For example, check out castle/data/levels/fountain/fountain_final.wrl
148
149 Disadvantages:
150
151 - Like PRT and shadow fields, this requires a large number of vertexes (elements) to look good, and still number vertexes is also the key thing that determines speed. (On the up side, it's not difficult to make lower LOD versions of elements, since they are so simple? Hierarchy idea will also help a lot.)
152
153 On dynamic models, when too few vertexes are available, it can be seen as moving shadows are "sticky" to vertexes.
154
155 - Requires a good GPU. Fragment shader must do a *lot* of work, a lot of texture lookups. Simply not possible on older GPUs.
156
157 TODO: our current method of generating elements works only for nodes
158 with explicit vertexes, so will not work for X3D primitives (sphere,
159 cone and and such).
160
161 - Or use [Delphi](https://www.embarcadero.com/products/Delphi). Open in Delphi `dynamic_ambient_occlusion_standalone.dproj` file and compile / run from Delphi. See [CGE and Delphi](https://castle-engine.io/delphi) documentation for details.