Skip to content

Commit 4cfa9e9

Browse files
committed
add extra diagrams
1 parent f199d51 commit 4cfa9e9

File tree

3 files changed

+135
-13
lines changed

3 files changed

+135
-13
lines changed
Lines changed: 42 additions & 0 deletions
Loading
Lines changed: 61 additions & 0 deletions
Loading

webgl/lessons/webgl-3d-perspective.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,13 @@ back. Set `zNear` to 23 and you'll see the front of the spinning cubes
346346
get clipped. Set `zFar` to 24 and you'll see the back of the cubes get
347347
clipped.
348348

349-
There's just one problem left. This matrix assumes there's a viewer at
350-
0,0,0 and it assumes it's looking in the negative Z direction and that
351-
positive Y is up. Our matrices up to this point have done things in a
352-
different way. To make this work we need to put our objects in front of
353-
the view.
349+
There's just one problem left. This matrix assumes there's a viewer at 0,0,0
350+
and it assumes it's looking in the negative Z direction and that positive Y is
351+
up. Our matrices up to this point have done things in a different way.
354352

355-
We could do that by moving our F. We were drawing at (45, 150, 0). Let's
356-
move it to (-150, 0, -360)
353+
To make it appear we need to move it inside the frustum.
354+
We can do that by moving our F. We were drawing at (45, 150, 0). Let's move it to (-150, 0, -360)
355+
and let's set the rotation to something that makes it appear right side up.
357356

358357
Now, to use it we just need to replace our old call to `m4.projection`
359358
with a call to `m4.perspective`
@@ -392,18 +391,38 @@ away?
392391
The reason is up until this last sample our <code>m4.projection</code> function
393392
has made a projection from pixels to clip space. That means the area we
394393
were displaying represented 400x300 pixels. Using 'pixels' really doesn't
395-
make sense in 3D. The new projection makes a frustum that makes it so the
396-
area represented at <code>zNear</code> is 2 units tall and 2 * aspect units wide.
397-
Since our 'F' is 150 units big and the view can only see 2 units when it's
398-
at zNear we need to move it pretty far away from the origin to see it all.
394+
make sense in 3D.
399395

400396
</p>
401397
<p>
402398

403-
Similarly we moved 'X' from 45 to -150. Again, the view used to represent
404-
0 to 400 units across. Now it represents -1 to +1 units across.
399+
In other words if we tried to draw with the F at 0,0,0 and not rotated we'd get this
405400

406401
</p>
402+
403+
<div class="webgl_center"><img src="resources/f-big-and-wrong-side.svg" style="width: 500px;"></div>
404+
405+
<p>
406+
The F has its top left front corner at the origin. The projection
407+
looks toward negative Z but our F is built in positive Z. The projection has
408+
positive Y up but our F is built with positive Z down.
409+
</p>
410+
411+
<p>
412+
Our new projection only sees what's in the blue frustum. The new projection
413+
makes a frustum such that the area represented at <code>-zNear</code> is
414+
2 units tall, one unit above the center and one unit below. The frustum is 2 *
415+
aspect units wide. Since our F is 150 units big and the view can only see 2
416+
units when something is at <code>-zNear</code> we need to move it pretty far away from the origin to
417+
see all of it.
418+
</p>
419+
420+
<p>
421+
Moving it -360 units in Z moves in inside the frustum. We also rotated it to be right side up.
422+
</p>
423+
424+
<div class="webgl_center"><img src="resources/f-right-side.svg" style="width: 500px;"><div>not to scale</div></div>
425+
407426
</div>
408427

409428

0 commit comments

Comments
 (0)