-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtechnical.html
More file actions
148 lines (147 loc) · 13 KB
/
Copy pathtechnical.html
File metadata and controls
148 lines (147 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>technical</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<link href="VisualRef.css" rel="stylesheet" type="text/css" />
<!-- InstanceEndEditable -->
<link href="VisualRef.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="230" height="30" border="0">
<tr>
<td width="66"><a name="top" id="top"></a><a href="index.html"><strong>Home</strong></a></td>
<td width="154"><span class="Normal"><a href="primitives.html"><strong>Pictures</strong></a> of 3D objects</span></td>
</tr>
</table>
<table width="438" height="30" border="0">
<tr>
<td width="151"><select id="menu1" onchange="jumpMenu(this)">
</select></td>
<td width="163"><select id="menu2" onchange="jumpMenu(this)">
</select></td>
<td width="110"><select id="menu3" onchange="jumpMenu(this)">
</select></td>
</tr>
</table>
<table width="454" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutDefaultTable-->
<tr>
<td width="454" rowspan="2" valign="top"><!-- InstanceBeginEditable name="content" -->
<div>
<h1 class="Heading-1"><font color="#0000A0">Technical issues</font></h1>
<p class="Normal">Here are collected some discussions of technical aspects of GlowScript, which may be of interest to other software developers.</p>
<p class="Normal"><strong><font color="#0000a0">Pixel-level transparency based on depth peeling</font></strong></p>
<p class="Normal">Handling transparency correctly in WebGL is challenging. A standard technique is to order the centers of objects according to their z depth from the camera and render back to front. This approach can make serious errors in the case of intersecting or enclosing transparent objects, where some parts of one object are in front of a second object, and other parts are behind the second object.</p>
<p class="Normal">A better approach is called "depth peeling", in which transparency is dealt with at the pixel level rather than at the object level. Consider for example a transparent sphere enclosing an opaque box, and carry out the following operations:</p>
<p class="Normal">1) Render the pixels of the opaque box to a texture, which we'll call a color texture. This of course takes into consideration the lights in the scene. Call this color texture C0. Note that all opacity values (the "a" in rgba) are 1.0 for this opaque object.</p>
<p class="Normal">2) Render the z depths (distance from the camera) to a texture, which we'll call a depth texture. That is, given the depth of a pixel, the fragment shader stores a false color into the texture, a false color whose 4 bytes represents in some form the depth. Call this depth texture D0.</p>
<p class="Normal">3) Render the transparent object (the sphere) to a texture C1, but using information in the depth texture D0. In the fragment shader, read the (false color) depth from D0, and if the z depth of the transparent sphere pixel is not in front of the opaque box pixel, discard the pixel (using the "discard" statement in the fragment shader). Upon completion of this render step, C1 contains color and opacity information (rgba) for the front-most transparent surface, a hemisphere. This is called a "depth peel".</p>
<p class="Normal">4) We now have two color textures, C0 and C1, which we can merge to form a scene with an opaque box and a transparent hemisphere in front of the box. Render a simple quad object (two triangles) that fill the canvas. In the fragment shader, read the color information from C0 and C1. Store a pixel color that is determined like this, where (1.0-C1.a) is the transparency of the transparent layer:</p>
<p class="program">vec3 color = C1.rgb*C1.a +<br />
(1.0-C1.a)*C0.rgb;<br />
gl_FragColor = vec4 (color, 1.0);</p>
<p class="Normal">These four steps illustrate the basic idea. In GlowScript, four transparent depth peels are performed rather than one, and 10 separate renders are carried out:</p>
<p class="Normal"><strong>C0</strong> -- opaque color texture<br />
<strong>D0</strong> -- opaque depth texture<br />
<strong>C1</strong> -- frontmost transparent color texture<br />
<strong>D1</strong> -- frontmost transparent depth texture, corresponding to C1<br />
<strong>C2</strong> -- transparent color texture for the next deeper "peel" after C1; if the pixel does not have a depth between that of D0 and D1, the pixel is discarded<br />
<strong>D2</strong> -- transparent depth texture corresponding to C2<br />
<strong>C3</strong> -- next deeper transparent color texture; discard a pixel if z not between D0 and D2<br />
<strong>D3</strong> -- transparent depth texture corresponding to C3<br />
<strong>C4 </strong>-- deepest transparent color texture; discard a pixel if z not between D0 and D3 </p>
<p class="Normal"><strong>MERGE</strong> -- The final render is a merge of C0, C1, C2, C3, and C4:</p>
<p class="program">vec3 color = C1.rgb*C1.a + <br />
(1.0-C1.a)*(C2.rgb*C2.a +<br />
(1.0-C2.a)*(C3.rgb*C3.a +<br />
(1.0-C3.a)*(C4.rgb*C4.a + <br />
(1.0-C4.a)*C0.rgb)));<br />
gl_FragColor = vec4 (color, 1.0);</p>
<p class="Normal">It may be that C2, C3, and C4 are all empty, but there is no obvious inexpensive way to get the information needed to tell the CPU to avoid scheduling those extra renders, because readPixels is very expensive. If there are more than four transparent layers, this algorithm will not treat them properly. However, note that the fifth and later peels will contribute little to the final pixel color, being partially occluded by four transparent layers in front.</p>
<p class="Normal">Before starting the many renders the objects are sorted into opaque and transparent lists, and if there are no transparent objects a simple C0 render is all that is needed. Moreover, this simple render can exploit antialiasing, whereas the storage into textures for depth peeling unfortunately turns off antialiasing.</p>
<p class="Normal">It is remarkable that doing 10 separate renders runs adequately fast for real-time rendering of moderately complicated scenes. For example, displaying a 10x10x10 grid of rotating transparent boxes can run at around 20 frames per second on ordinary computers, depending on the graphics card.</p>
<p class="Normal">In OpenGL it is possible to create several textures in one render, but WegGL permits attaching just one texture to a framebuffer object, hence a large number of separate renders are needed.</p>
<p class="Normal">To see this algorithm in action, run this <a href="http://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/program/Transparency" target="_blank">Transparency</a> example program. </p>
<p class="Normal">Note that the code for this transparency demo is remarkably short. GlowScript is aimed at making it feasible for nonexpert programmers to exploit WebGL to generate navigable real-time 3D animations.</p>
<p class="Normal">At <a href="https://github.com/BruceSherwood/glowscript" target="_blank">https://github.com/BruceSherwood/glowscript</a>, the key files dealing with depth peeling are lib/glow/WebGLRender.js and the shader programs in the shaders folder.</p>
<p class="Normal">It is possible to implement "Fast Approximate Anti-Aliasing" (FXAA) to get around the problem that the use of framebuffer objects turns off anti-aliasing:<br />
<a href="http://www.codinghorror.com/blog/2011/12/fast-approximate-anti-aliasing-fxaa.html" target="_blank">http://www.codinghorror.com/blog/2011/12/fast-approximate-anti-aliasing-fxaa.html</a></p>
<p class="Normal">This has not been tried in GlowScript.</p>
<p class="Normal"><strong><font color="#0000a0">Mouse picking</font></strong><br />
</p>
<p class="Normal">Here is how mouse picking has been implemented in GlowScript. For an object with id number N, create a false color like this:</p>
<p class="program">function id_to_falsecolor(N) { <br />
// convert integer object id<br />
// to floating RGBA<br />
var R=0, G=0, B=0<br />
if (N >= 16777216) {<br />
R = Math.floor(N/16777216)<br />
N -= R*16777216<br />
}<br />
if (N >= 65536) {<br />
G = Math.floor(N/65536)<br />
N -= G*65536<br />
}<br />
if (N >= 256) {<br />
B = Math.floor(N/256)<br />
N -= B*256<br />
}<br />
return [R/255, G/255, B/255, N/255]<br />
}</p>
<p class="Normal">We want to render the false colors of the objects to a texture the size of the canvas (need not be powers of 2):</p>
<p class="program">var pick_texture = gl.createTexture()<br />
gl.bindTexture(gl.TEXTURE_2D, pick_texture )</p>
<p class="program">gl.texParameteri(gl.TEXTURE_2D, <br />
gl.TEXTURE_MIN_FILTER, gl.LINEAR)<br />
gl.texParameteri(gl.TEXTURE_2D,<br />
gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)<br />
gl.texParameteri(gl.TEXTURE_2D,<br />
gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)</p>
<p class="program">gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA,<br />
width, height, 0, gl.RGBA, <br />
gl.UNSIGNED_BYTE, null)<br />
gl.bindTexture(gl.TEXTURE_2D, null)</p>
<p class="Normal">Set up a framebuffer to render to:</p>
<p class="program">var pickFramebuffer = gl.createFramebuffer()<br />
gl.bindFramebuffer(gl.FRAMEBUFFER,<br />
pickFramebuffer)</p>
<p class="program">// create depth buffer:<br />
var pickRenderbuffer = <br />
gl.createRenderbuffer()<br />
gl.bindRenderbuffer(gl.RENDERBUFFER, <br />
pickRenderbuffer)<br />
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height)<br />
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, pickRenderbuffer)</p>
<p class="program">gl.bindRenderbuffer(gl.RENDERBUFFER, null)<br />
gl.bindFramebuffer(gl.FRAMEBUFFER, null)</p>
<p class="Normal">At the start of a render cycle execute this:</p>
<p class="program">gl.bindFramebuffer(gl.FRAMEBUFFER,<br />
pickFramebuffer)<br />
gl.framebufferTexture2D(gl.FRAMEBUFFER, <br />
gl.COLOR_ATTACHMENT0,<br />
gl.TEXTURE_2D,
pick_texture, 0)</p>
<p class="Normal">After rendering all the objects, with no lighting calculations (so that all pixels of an object have the same false color), get the id number of the object under the mouse like this:</p>
<p class="program">var pixels = new Uint8Array(4)<br />
gl.readPixels(canvas.mouse.__pickx, canvas.mouse.__picky, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels)<br />
var id = 16777216*pixels[0] + <br />
65536*pixels[1] + <br />
256*pixels[2] + pixels[3]</p>
<p class="Normal">A non-power-of-two texture has restrictions; see <br />
<a href="http://www.khronos.org/webgl/wiki/WebGL_and_OpenGL_Differences" target="_blank">http://www.khronos.org/webgl/wiki/WebGL_and_OpenGL_Differences</a>. <br />
Important note: This destroys antialiasing, so render to texture is fully useful only for doing some computations, unless we do our own antialiasing.</p>
<p class="Normal">Note that antialiasing being turned off is actually what one wants for mouse picking because you don't want an object with id of 10 that is next to an object of id 30 to get picked with an id of 20, which could happen with antialiasing turned on, due to the averaging of neighboring colors. </p>
</div><div>
</div>
<div> </div>
<!-- InstanceEndEditable --></td>
</tr>
</table>
<p><a href="#top"><strong>Top of page</strong></a></p>
</body>
<script type="text/javascript" language="javascript" src="navigation.js"></script>
<!-- InstanceEnd --></html>