Skip to content

Commit ffe3da7

Browse files
committed
explain the gl rasterization pipeline better
1 parent 2a2903e commit ffe3da7

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/Drawable.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ const __isTouchingPosition = twgl.v3.create();
2323
* @return {twgl.v3} [x,y] texture space float vector - transformed by effects and matrix
2424
*/
2525
const getLocalPosition = (drawable, vec) => {
26-
// Transfrom from world coordinates to Drawable coordinates.
26+
// Transform from world coordinates to Drawable coordinates.
2727
const localPosition = __isTouchingPosition;
28-
// In "Scratch space" pixel coords, integers are pixel corners; in "texture space", integers are pixel centers,
29-
// so offset by 0.5 (-0.5 in the X direction because it's flipped).
28+
// World coordinates/screen-space coordinates refer to pixels by integer coordinates.
29+
// The GL rasterizer considers a pixel to be an area sample.
30+
// Without multisampling, it samples once from the pixel center,
31+
// which is offset by (0.5, 0.5) from the pixel's integer coordinate.
32+
// If you think of it as a pixel grid, the coordinates we're given are grid lines, but we want grid boxes.
33+
// That's why we offset by 0.5 (-0.5 in the X direction because it's flipped).
3034
const v0 = vec[0] - 0.5;
3135
const v1 = vec[1] + 0.5;
3236
const m = drawable._inverseMatrix;

src/Silhouette.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const __cornerWork = [
4545
/**
4646
* Get the color from a given silhouette at an x/y local texture position.
4747
* @param {Silhouette} The silhouette to sample.
48-
* @param {number} x X position of texture (0-1).
49-
* @param {number} y Y position of texture (0-1).
48+
* @param {number} x X position of texture [0, width).
49+
* @param {number} y Y position of texture [0, height).
5050
* @param {Uint8ClampedArray} dst A color 4b space.
5151
* @return {Uint8ClampedArray} The dst vector.
5252
*/
@@ -149,6 +149,7 @@ class Silhouette {
149149
// We cannot skip the "add 0.5 in Drawable.getLocalPosition -> subtract 0.5 here" roundtrip
150150
// because the two spaces are different--we add 0.5 in Drawable.getLocalPosition in "Scratch space"
151151
// (-240,240 & -180,180), but subtract 0.5 in silhouette space (0, width or height).
152+
// See https://web.archive.org/web/20190125211252/http://hacksoflife.blogspot.com/2009/12/texture-coordinate-system-for-opengl.html
152153
const x = (vec[0] * (this._width)) - 0.5;
153154
const y = (vec[1] * (this._height)) - 0.5;
154155

0 commit comments

Comments
 (0)