TilersImpl.mesa
Last Edited by: Crow, May 29, 1986 5:55:28 pm PDT
DIRECTORY
Atom USING [PropList, GetPropFromList, PutPropOnList],
Real USING [FixI, Float, RoundI, RoundC, FixC, LargestNumber],
RealFns USING [SqRt, Power],
Imager USING [Context, PathProc, MaskFill, SetColor],
ImagerColor USING [RGB, ColorFromRGB],
ImagerPixelMap USING [PixelMap],
Vector3d USING [Normalize, Pair, Triple, Mul, Add],
Pixels USING [PixelBuffer, SampleSet, GetSampleSet],
ScanConvert USING [Spot, IntVec2Sequence, GetColorProc, ConstantPoly,
SmoothPoly, ShinyPoly, RealSequence, SpotSequence,
IntRGB, justNoticeable, PutSpot, Extend],
ThreeDScenes USING [Context, ShadingValue, VtxToRealSeqProc, VertexInfo,
Vertex, GetShading, ShadePt, ShadingProcs],
ThreeDSurfaces USING [Patch, VertexInfo],
ThreeDMisc USING [GetMappedColor],
TextureMaps USING [GetTxtrAt, TextureMap],
Tilers USING [];
Types
TilersError: PUBLIC SIGNAL [reason: ATOM] ~ CODE;
RGB: TYPE ~ ImagerColor.RGB;
Pair: TYPE ~ Vector3d.Pair; -- RECORD [ x, y: REAL];
Triple: TYPE ~ Vector3d.Triple; -- RECORD [ x, y, z: REAL];
SampleSet: TYPE ~ Pixels.SampleSet;
Patch: TYPE ~ ThreeDSurfaces.Patch;
ColorPrimary: TYPE ~ { red, green, blue, grey };
PixelMap: TYPE ~ ImagerPixelMap.PixelMap;
BooleanSequence: TYPE ~ RECORD [ SEQUENCE length: NAT OF BOOLEAN ];
RealSequence: TYPE ~ ScanConvert.RealSequence;
Vertex: TYPE ~ ThreeDScenes.Vertex;
VertexInfo: TYPE ~ ThreeDScenes.VertexInfo;
LerpVtx:
TYPE =
RECORD [
x, y: REAL,
val: REF RealSequence
];
Data Structure for trapezoid edges
EdgeBlock:
TYPE =
RECORD [
moreVertical: BOOLEAN, start, end: REAL,
x, y, xIncr, yIncr: REAL,
val, incr: REF RealSequence
];
ScanSegment:
TYPE =
RECORD [
start, end: REAL,
coverage, cvrgIncr: REAL,
lMask, rMask: CARDINAL,
val, yIncr, xIncrVal, xIncrForY: REF RealSequence
];
FancyPatch:
TYPE =
RECORD [
spot: ScanConvert.Spot, -- [x, y, coverage, mask, val, yIncr, xIncr, props, proc],
vtx: SEQUENCE length: NAT OF LerpVtx
];
Utility procedures
Sqr:
PROCEDURE [number:
REAL]
RETURNS [
REAL] ~
INLINE {
RETURN[number * number]; };
Sgn:
PROCEDURE [number:
REAL]
RETURNS [
REAL] ~
INLINE {
IF number < 0. THEN RETURN[-1.] ELSE RETURN[1.];
};
Ceiling:
PROC[ in:
REAL ]
RETURNS[ out:
INTEGER ] ~ {
out ← Real.RoundI[in];
IF Real.Float[out] < in THEN out ← out + 1;
};
Floor:
PROC[ in:
REAL ]
RETURNS[ out:
INTEGER ] ~ {
out ← Real.RoundI[in];
IF Real.Float[out] > in THEN out ← out - 1;
CalculateWeights:
PROC[] ~ {
Calculates the integral over the left half of a pyramid function, equal to the left half of a parabolic window or B-spline basis function
FOR i:
NAT
IN [0..tblLngth/2]
DO
t: REAL ← i * 1.0 / (tblLngth/2);
weight[i] ← Sqr[t] / 2.;
weight[i + tblLngth/2] ← 1. - Sqr[1. - t] / 2.;
ENDLOOP;
weightsCalculated ← TRUE;
};
DupLerpVtx:
PROC[vtx: LerpVtx]
RETURNS[newVtx: LerpVtx] ~ {
newVtx.x ← vtx.x;
newVtx.y ← vtx.y;
newVtx.val ← NEW[RealSequence[vtx.val.length]];
FOR i: NAT IN [0..vtx.val.length) DO newVtx.val[i] ← vtx.val[i] ENDLOOP;
newVtx.val.length ← vtx.val.length;
};
DupEdgeBlock:
PROC[srce: EdgeBlock]
RETURNS[dest : EdgeBlock] ~ {
dest.moreVertical ← srce.moreVertical;
dest.start ← srce.start; dest.end ← srce.end;
dest.x ← srce.x; dest.xIncr ← srce.xIncr;
dest.y ← srce.y; dest.yIncr ← srce.yIncr;
dest.val ← NEW[RealSequence[srce.val.length]]; dest.val.length ← srce.val.length;
FOR i: NAT IN [0..srce.val.length) DO dest.val[i] ← srce.val[i] ENDLOOP;
dest.incr ← NEW[RealSequence[srce.incr.length]]; dest.incr.length ← srce.incr.length;
FOR i: NAT IN [0..srce.incr.length) DO dest.incr[i] ← srce.incr[i] ENDLOOP;
};
GetLerpedVals: ThreeDScenes.VtxToRealSeqProc ~ {
PROC[dest: REF RealSequence, source: VertexInfo] RETURNS[REF RealSequence];
IF dest = NIL OR dest.maxLength < 9 THEN dest ← NEW[ RealSequence[9] ]; -- leave room
dest[0] ← source.shade.ir / 255.0;
dest[1] ← source.shade.ig / 255.0;
dest[2] ← source.shade.ib / 255.0;
dest[3] ← source.shade.it / 255.0;
dest.length ← 4;
RETURN [dest];
};
GetLerpedValsForHighLights: ThreeDScenes.VtxToRealSeqProc ~ {
PROC[dest: REF RealSequence, source: VertexInfo] RETURNS[REF RealSequence];
IF dest = NIL OR dest.maxLength < 15 THEN dest ← NEW[ RealSequence[15] ]; -- leave room
dest[0] ← source.shade.r;
dest[1] ← source.shade.g;
dest[2] ← source.shade.b;
dest[3] ← source.shade.t;
dest[4] ← source.shade.exn;
dest[5] ← source.shade.eyn;
dest[6] ← source.shade.ezn;
dest[7] ← source.coord.ex;
dest[8] ← source.coord.ey;
dest[9] ← source.coord.ez;
dest.length ← 10;
RETURN [dest];
};
RecoverColor: ScanConvert.GetColorProc ~ {
PROC[spot: Spot] RETURNS[IntRGTB]
IF Atom.GetPropFromList[spot.props, $TextureMap] #
NIL
THEN spot ← TextureMaps.GetTxtrAt[spot];
IF Atom.GetPropFromList[spot.props, $HltPwr] #
NIL
THEN spot ← AddHighlight[spot];
RETURN[ [
r: Real.RoundC[spot.val[0]*255.0],
g: Real.RoundC[spot.val[1]*255.0],
b: Real.RoundC[spot.val[2]*255.0],
t: Real.RoundC[spot.val[3]*255.0]
] ];
};
AddHighlight:
PUBLIC
PROC[spot: ScanConvert.Spot]
RETURNS[ScanConvert.Spot] ~ {
context: REF ThreeDScenes.Context ← NARROW[ Atom.GetPropFromList[spot.props, $Context] ];
ref: REF ← Atom.GetPropFromList[spot.props, $HltPwr];
shininess: REAL ← IF ref # NIL THEN NARROW[ref, REF REAL]^ ELSE 0.0;
pt: VertexInfo;
pt.coord.ex ← spot.val[7]; pt.coord.ey ← spot.val[8]; pt.coord.ez ← spot.val[9];
pt.shade.exn ← spot.val[4]; pt.shade.eyn ← spot.val[5]; pt.shade.ezn ← spot.val[6];
pt.shade.r ← spot.val[0]; pt.shade.g ← spot.val[1]; pt.shade.b ← spot.val[2];
pt.shade.t ← spot.val[3];
[ [spot.val[0], spot.val[1], spot.val[2]], spot.val[3] ] ← ThreeDScenes.ShadePt[
context, pt, shininess];
RETURN[spot];
};
Anti-Aliasing Tiler for Fancier shading, etc.
recurseLevel: NAT ← 0;
FancyTiler:
PUBLIC
PROC[context:
REF ThreeDScenes.Context, poly:
REF Patch] ~ {
getVtxProc: ThreeDScenes.VtxToRealSeqProc ← NIL;
getColorProc: ScanConvert.GetColorProc ← NIL;
LerpVtxFromVtx:
PROC[vtx: VertexInfo, fancy:
BOOL]
RETURNS[LerpVtx] ~ {
lerpVtx: LerpVtx ← [vtx.coord.sx, vtx.coord.sy, NIL];
lerpVtx.val ←
IF fancy
THEN GetLerpedValsForHighLights[dest: lerpVtx.val, source: vtx]
ELSE GetLerpedVals[dest: lerpVtx.val, source: vtx];
RETURN[lerpVtx];
};
AddTextureCoords:
PROC[vtx: LerpVtx, addVtx: VertexInfo]
RETURNS[LerpVtx] ~ {
IF vtx.val.maxLength < vtx.val.length+2
THEN vtx.val ← ScanConvert.Extend[vtx.val, vtx.val.length+2];
vtx.val[vtx.val.length ] ← addVtx.shade.txtrX;
vtx.val[vtx.val.length+1] ← addVtx.shade.txtrY;
vtx.val.length ← vtx.val.length+2;
RETURN [vtx];
};
AdjustTexture:
PROC[poly:
REF Patch] ~ {
maxXtxtr, maxYtxtr, minXtxtr, minYtxtr: REAL ← 0.0;
FOR i:
CARDINAL
IN [0..poly.nVtces)
DO
-- find maximum
IF maxXtxtr < poly[i].shade.txtrX THEN maxXtxtr ← poly[i].shade.txtrX;
IF maxYtxtr < poly[i].shade.txtrY THEN maxYtxtr ← poly[i].shade.txtrY;
ENDLOOP;
FOR i:
CARDINAL
IN [0..poly.nVtces)
DO
-- push small ones beyond maximum
WHILE maxXtxtr - poly[i].shade.txtrX > .5
DO
poly[i].shade.txtrX ← poly[i].shade.txtrX + 1.0; ENDLOOP;
WHILE maxYtxtr - poly[i].shade.txtrY > .5
DO
poly[i].shade.txtrY ← poly[i].shade.txtrY + 1.0; ENDLOOP;
ENDLOOP;
minXtxtr ← maxXtxtr; minYtxtr ← maxYtxtr;
FOR i:
CARDINAL
IN [0..poly.nVtces)
DO
-- find minimum
IF minXtxtr > poly[i].shade.txtrX THEN minXtxtr ← poly[i].shade.txtrX;
IF minYtxtr > poly[i].shade.txtrY THEN minYtxtr ← poly[i].shade.txtrY;
ENDLOOP;
minXtxtr ← Real.Float[Real.FixI[minXtxtr]]; minYtxtr ← Real.Float[Real.FixI[minYtxtr]];
FOR i:
CARDINAL
IN [0..poly.nVtces)
DO
-- adjust everything to minima under 1.0
poly[i].shade.txtrX ← poly[i].shade.txtrX - minXtxtr;
poly[i].shade.txtrY ← poly[i].shade.txtrY - minYtxtr;
ENDLOOP;
};
keepYIncrements, shadingPerPixel, textureMapping: BOOLEAN ← FALSE;
spot: ScanConvert.Spot;
fancyPoly: REF FancyPatch ← NEW[ FancyPatch[poly.nVtces] ];
shininess:
REF
REAL;
Get custom procedures for storing and extracting spot values
ref: REF ← Atom.GetPropFromList[poly.props, $ShadingProcs];
IF ref #
NIL
THEN [getVtxProc, getColorProc] ← NARROW[ ref, REF ThreeDScenes.ShadingProcs]^;
IF getColorProc #
NIL
THEN shadingPerPixel ← TRUE -- custom shading, must use raw surface color
ELSE getColorProc ← RecoverColor; -- default shading
IF
NOT context.alphaBuffer
THEN
SIGNAL TilersError[$NoAlphaBuffer];
Do we have a highlight?
shininess ← NARROW[Atom.GetPropFromList[poly.props, $Shininess], REF REAL];
IF shininess #
NIL
AND shininess^ > 0.0
THEN
IF GotAHilite[context, poly, shininess^]
THEN {
spot.props ← Atom.PutPropOnList[spot.props, $HltPwr, shininess];
keepYIncrements ← TRUE; shadingPerPixel ← TRUE;
};
Do we have mapped texture?
ref ← Atom.GetPropFromList[poly.props, $TextureMap];
IF ref #
NIL
THEN {
spot.props ← Atom.PutPropOnList[ spot.props, $TextureMap, ref ];
IF
NARROW[ref,
REF TextureMaps.TextureMap].type = $Bump
THEN
IF
NOT shadingPerPixel
THEN {
IF shininess = NIL THEN shininess ← NEW[REAL ← 0.0];
spot.props ← Atom.PutPropOnList[spot.props, $HltPwr, shininess];
shadingPerPixel ← TRUE; -- change shading mode for bump mapping
};
keepYIncrements ← TRUE;
textureMapping ← TRUE;
AdjustTexture[poly]; -- fix texture seams
};
spot.proc ← getColorProc; -- set up remaining spot fields
IF keepYIncrements
THEN spot.props ← Atom.PutPropOnList[ spot.props, $KeepYIncrements, $DoIt ];
spot.props ← Atom.PutPropOnList[spot.props, $Context, context]; -- pass context.
spot.props ← Atom.PutPropOnList[spot.props, $ShapeShadingProps, poly.props ]; -- pass props
IF NOT weightsCalculated THEN CalculateWeights[]; -- get filter kernel
FOR i:
NAT
IN [0..poly.nVtces)
DO
-- convert vertices to lerp
fancyPoly[i] ← LerpVtxFromVtx[poly[i], shadingPerPixel];
ENDLOOP;
IF textureMapping
THEN {
-- add texture coordinates
FOR i:
NAT
IN [0..poly.nVtces)
DO
fancyPoly[i] ← AddTextureCoords[ fancyPoly[i], poly[i] ];
ENDLOOP;
spot.props ← Atom.PutPropOnList[
-- store texture coord position marker
spot.props,
$MapVals,
NEW[ NAT ← fancyPoly[0].val.length - 2 ]
];
};
IF getVtxProc #
NIL
THEN
FOR i:
CARDINAL
IN [0..poly.nVtces)
DO
-- for custom shading
fancyPoly[i].val ← getVtxProc[ dest: fancyPoly[i].val, source: poly[i] ];
ENDLOOP;
fancyPoly.spot ← spot;
recurseLevel ← 0;
RealFancyTiler[context, fancyPoly]; -- now go tile it
};
RealFancyTiler:
PROC[context:
REF ThreeDScenes.Context, poly:
REF FancyPatch] ~ {
least, top, bottom: REAL;
lEdge, rEdge: EdgeBlock;
vtxCount, lVtx, rVtx, nxtlVtx, nxtrVtx, nVtcesMinusOne: NAT;
leftVtxNeeded, rightVtxNeeded: BOOLEAN;
least ← poly.vtx[0].y; nxtlVtx ← 0;
FOR i:
CARDINAL
IN [1..poly.length)
DO
-- find bottom vertex
IF poly.vtx[i].y < least
THEN { least ← poly[i].y; nxtlVtx ← i; };
ENDLOOP;
nxtrVtx ← nxtlVtx; -- set pointers to bottom vertex
leftVtxNeeded ← rightVtxNeeded ← TRUE;
nVtcesMinusOne ← poly.length - 1;
vtxCount ← 1;
WHILE vtxCount < poly.length
DO
-- Do until all vertices reached
IF leftVtxNeeded
THEN {
-- work around left side
lVtx ← nxtlVtx; nxtlVtx ← (nxtlVtx + 1) MOD poly.length;
lEdge ← MakeEdge[poly[lVtx], poly[nxtlVtx]];
leftVtxNeeded ← FALSE;
};
IF rightVtxNeeded
THEN {
-- work around right side
rVtx ← nxtrVtx; nxtrVtx ← (nxtrVtx + nVtcesMinusOne) MOD poly.length;
rEdge ← MakeEdge[poly[rVtx], poly[nxtrVtx]];
rightVtxNeeded ← FALSE;
};
get trapezoid given by next higher vertex
IF poly[nxtlVtx].y < poly[nxtrVtx].y
THEN {
top ← poly[nxtlVtx].y; -- next left vertex reached
leftVtxNeeded ← TRUE; vtxCount ← vtxCount + 1;
}
ELSE {
top ← poly[nxtrVtx].y; -- next right vertex reached
rightVtxNeeded ← TRUE; vtxCount ← vtxCount + 1;
};
bottom ← MAX[poly[lVtx].y, poly[rVtx].y];
ShowFancyTrap[ context, poly.spot, bottom, top, lEdge, rEdge];
ENDLOOP;
MakeEdge:
PROC[vtx1, vtx2: LerpVtx]
RETURNS[edge: EdgeBlock] ~ {
length: REAL;
edge.val ← NEW[ RealSequence[vtx1.val.length] ];
edge.incr ← NEW[ RealSequence[vtx1.val.length] ];
IF ABS[vtx2.y - vtx1.y] >= ABS[vtx2.x - vtx1.x]
THEN {
length ← vtx2.y - vtx1.y;
edge.start ← MIN[vtx1.y, vtx2.y];
edge.end ← MAX[vtx1.y, vtx2.y];
edge.moreVertical ← TRUE;
}
ELSE {
length ← vtx2.x - vtx1.x;
edge.start ← MIN[vtx1.x, vtx2.x];
edge.end ← MAX[vtx1.x, vtx2.x];
edge.moreVertical ← FALSE;
};
IF ABS[length] < justNoticeable THEN length ← 1.; -- prevent divide errors
edge.x ← vtx1.x; edge.xIncr ← (vtx2.x - vtx1.x) / length;
edge.y ← vtx1.y; edge.yIncr ← (vtx2.y - vtx1.y) / length;
FOR i:
NAT
IN [0..vtx1.val.length)
DO
edge.val[i] ← vtx1.val[i]; edge.incr[i] ← (vtx2.val[i] - vtx1.val[i]) / length;
ENDLOOP;
edge.val.length ← edge.incr.length ← vtx1.val.length;
RETURN[edge];
};
EvalEdgeAt:
PROC[vtx: LerpVtx, edge: EdgeBlock, position:
REAL]
RETURNS[LerpVtx] ~ {
pos, dist: REAL;
IF vtx.val =
NIL
OR vtx.val.maxLength < edge.val.length
THEN vtx.val ← NEW[ RealSequence[edge.val.length] ];
IF position > edge.end THEN pos ← edge.end -- keep values between vertex values
ELSE IF position < edge.start THEN pos ← edge.start
ELSE pos ← position;
dist ← IF edge.moreVertical THEN pos - edge.y ELSE pos - edge.x;
vtx.x ← edge.x + edge.xIncr * dist;
vtx.y ← edge.y + edge.yIncr * dist;
FOR i:
NAT
IN [0..edge.val.length)
DO
vtx.val[i] ← edge.val[i] + edge.incr[i] * dist;
ENDLOOP;
vtx.val.length ← edge.val.length;
RETURN[vtx];
};
EvalCvrgeAt:
PROC[ start, end, position:
REAL]
RETURNS[ cvrge:
REAL, mask:
CARDINAL] ~ {
Get Pixel area coverage weighted by function stored in "weight"
rCoverage, rUnCoverage: REAL;
lCoverage: REAL ← position - start;
IF lCoverage >= 1. THEN lCoverage ← 2.0 -- fully covered
ELSE IF lCoverage > -1.0 THEN lCoverage ← 1.0 + lCoverage -- partially covered
ELSE lCoverage ← 0.;
lCoverage ← weight[Real.FixI[ tblLngth/2 * lCoverage ]];
rCoverage ← end - position;
IF rCoverage >= 1. THEN rCoverage ← 2.0 -- fully covered
ELSE IF rCoverage > -1.0 THEN rCoverage ← 1.0 + rCoverage -- partially covered
ELSE rCoverage ← 0.;
rUnCoverage ← weight[Real.FixI[ tblLngth/2 * (2.0 - rCoverage) ]]; -- weight uncovered part
cvrge ← lCoverage - rUnCoverage; -- l - r is total coverage
mask ← 0; -- not yet implemented
};
MakeScanSeg:
PROC[seg:
REF ScanSegment, lEdge, rEdge: EdgeBlock, position:
REAL,
increments:
BOOLEAN]
RETURNS[
REF ScanSegment] ~ {
length, lCvrge, rCvrge: REAL;
size: NAT ← lEdge.val.length;
IF seg =
NIL
OR seg.val.length < size
THEN {
seg ← NEW[ScanSegment];
seg.val ← NEW[ RealSequence[size] ];
seg.yIncr ← NEW[ RealSequence[size] ];
seg.xIncrVal ← NEW[ RealSequence[size] ];
seg.xIncrForY ← NEW[ RealSequence[size] ];
};
lVtx ← EvalEdgeAt[lVtx, lEdge, position]; -- lVtx & rVtx are global to limit allocations
rVtx ← EvalEdgeAt[rVtx, rEdge, position];
IF lEdge.moreVertical -- horizontal scan segment
THEN {
length ← rVtx.x - lVtx.x;
seg.start ← lVtx.x; seg.end ← rVtx.x;
}
ELSE {
-- vertical scan segment
length ← rVtx.y - lVtx.y;
seg.start ← lVtx.y; seg.end ← rVtx.y;
};
IF ABS[length] < justNoticeable THEN RETURN[seg]; -- too short to show up
[lCvrge, seg.lMask] ← EvalCvrgeAt[lEdge.start, lEdge.end, position];
[rCvrge, seg.rMask] ← EvalCvrgeAt[rEdge.start, rEdge.end, position];
seg.coverage ← lCvrge;
seg.cvrgIncr ← (rCvrge - lCvrge) / length;
FOR i:
NAT
IN [0..lEdge.val.length)
DO
seg.val[i] ← lVtx.val[i];
seg.xIncrVal[i] ← (rVtx.val[i] - lVtx.val[i]) / length; -- x-increments
ENDLOOP;
seg.val.length ← seg.xIncrVal.length ← lEdge.val.length;
IF increments
THEN {
FOR i:
NAT
IN [0..lEdge.val.length)
DO
seg.yIncr[i] ← lEdge.incr[i]; -- y—increments
seg.xIncrForY[i] ← (rEdge.incr[i] - lEdge.incr[i]) / length; -- x-incmnts for y—incmnts
ENDLOOP;
seg.yIncr.length ← seg.xIncrForY.length ← lEdge.val.length;
};
IF seg.val[0] < 0.0 THEN SIGNAL TilersError[$NegativeColor];
RETURN[seg];
};
EvalScanSegAt:
PROC[spot: ScanConvert.Spot, seg:
REF ScanSegment, position:
REAL]
RETURNS[
REAL, ScanConvert.Spot] ~ {
pos, dist, coverage: REAL;
mask: CARDINAL;
IF spot.val =
NIL
OR spot.val.maxLength < seg.val.length
THEN {
spot.val ← NEW[ RealSequence[seg.val.length] ];
spot.yIncr ← NEW[ RealSequence[seg.val.length] ];
spot.xIncr ← NEW[ RealSequence[seg.val.length] ];
};
IF position > seg.end THEN pos ← seg.end -- keep values between vertex values
ELSE IF position < seg.start THEN pos ← seg.start
ELSE pos ← position;
dist ← pos - seg.start;
FOR i:
NAT
IN [0..seg.val.length)
DO
-- load up spot values
spot.val[i] ← seg.val[i] + seg.xIncrVal[i] * dist;
spot.yIncr[i] ← seg.yIncr[i] + seg.xIncrForY[i] * dist;
ENDLOOP;
spot.xIncr ← seg.xIncrVal; -- use x increments as is
spot.val.length ← spot.yIncr.length ← spot.xIncr.length ← seg.val.length;
[coverage, mask] ← EvalCvrgeAt[seg.start, seg.end, position];
coverage ← coverage * (seg.coverage + seg.cvrgIncr * dist);
RETURN[coverage, spot];
};
ShowFancyTrap:
PROC[ context:
REF ThreeDScenes.Context, spot: ScanConvert.Spot,
bottom, top:
REAL, lEdge, rEdge: EdgeBlock] ~ {
GetXcoordAt:
PROC[edge: EdgeBlock, yPos:
REAL]
RETURNS [
REAL] ~ {
dist: REAL ← yPos - edge.y;
RETURN [ edge.x + dist / edge.yIncr ];
};
tEdge, bEdge, midlEdge, midrEdge: EdgeBlock;
leftTopVtx, leftBotVtx, rightTopVtx, rightBotVtx, vtx0, vtx1, vtx2, vtx3: LerpVtx;
sideways, midSection: BOOLEAN ← TRUE;
toughCase: BOOLEAN ← FALSE;
a, b: REAL; -- parameters for defining 45 degree lines
IF bottom + justNoticeable >= top THEN RETURN[]; -- too thin to affect image
midlEdge ← DupEdgeBlock[lEdge]; -- copy sides for possible later use
midrEdge ← DupEdgeBlock[rEdge];
FOR i:
NAT
IN [0..midlEdge.val.length)
DO
midlEdge.val[i] ← lEdge.val[i]; midlEdge.incr[i] ← lEdge.incr[i];
midrEdge.val[i] ← rEdge.val[i]; midrEdge.incr[i] ← rEdge.incr[i];
ENDLOOP;
IF
NOT (lEdge.moreVertical
AND rEdge.moreVertical)
THEN {
-- get corners
IF lEdge.moreVertical
THEN {
leftTopVtx ← EvalEdgeAt[leftTopVtx, lEdge, top];
leftBotVtx ← EvalEdgeAt[leftBotVtx, lEdge, bottom];
}
ELSE {
topX: REAL ← GetXcoordAt[lEdge, top];
botX: REAL ← GetXcoordAt[lEdge, bottom];
leftTopVtx ← EvalEdgeAt[leftTopVtx, lEdge, topX];
leftBotVtx ← EvalEdgeAt[leftBotVtx, lEdge, botX];
};
IF rEdge.moreVertical
THEN {
rightTopVtx ← EvalEdgeAt[rightTopVtx, rEdge, top];
rightBotVtx ← EvalEdgeAt[rightBotVtx, rEdge, bottom];
}
ELSE {
topX: REAL ← GetXcoordAt[rEdge, top];
botX: REAL ← GetXcoordAt[rEdge, bottom];
rightTopVtx ← EvalEdgeAt[rightTopVtx, rEdge, topX];
rightBotVtx ← EvalEdgeAt[rightBotVtx, rEdge, botX];
};
IF rightTopVtx.x + justNoticeable < leftTopVtx.x
OR rightBotVtx.x + justNoticeable < leftBotVtx.x
THEN RETURN[]; -- twisted or backfacing
};
if left side more horizontal, check for slope, make top or bottom edge,
do right triangle, do new left edge
IF
NOT lEdge.moreVertical
THEN IF lEdge.yIncr < 0.
THEN {
-- left edge is more horizontal, top vertex is leftmost
tEdge ← MakeEdge[leftTopVtx, rightTopVtx]; bEdge ← DupEdgeBlock[lEdge];
IF leftBotVtx.x <= rightTopVtx.x
THEN {
-- easy case: right triangle containing whole left edge
ShowSteepTrap[context, spot,
leftTopVtx.x, leftBotVtx.x, bEdge, tEdge, sideways];
midlEdge ← MakeEdge[leftBotVtx, EvalEdgeAt[lVtx, tEdge, leftBotVtx.x]];
}
ELSE {
-- right top is left of left bottom
IF rEdge.moreVertical
THEN {
-- difficult case bot. more horz. top more vert.
toughCase ← TRUE;
ShowSteepTrap[context, spot,
leftTopVtx.x, rightTopVtx.x, bEdge, tEdge, sideways];
vtx0 ← EvalEdgeAt[vtx0, bEdge, rightTopVtx.x]; --build new polygon
vtx1 ← EvalEdgeAt[vtx1, rEdge, rightTopVtx.y];
vtx2 ← EvalEdgeAt[vtx2, rEdge, rightBotVtx.y];
vtx3 ← EvalEdgeAt[vtx3, bEdge, leftBotVtx.x];
a ← .707; b ← .707; -- test against negative 45 degree slope
}
ELSE {
-- both more horz. do triangle then trapezoid
ShowSteepTrap[context, spot,
leftTopVtx.x, rightTopVtx.x, bEdge, tEdge, sideways];
ShowSteepTrap[context, spot,
rightTopVtx.x, leftBotVtx.x, bEdge, rEdge, sideways];
midSection ← FALSE;
};
};
}
ELSE {
-- left edge is more horizontal, bottom vertex is leftmost
bEdge ← MakeEdge[leftBotVtx, rightBotVtx]; tEdge ← DupEdgeBlock[lEdge];
IF leftTopVtx.x <= rightBotVtx.x
THEN {
-- easy case: right triangle containing whole left edge
ShowSteepTrap[context, spot,
leftBotVtx.x, leftTopVtx.x, bEdge, tEdge, sideways];
midlEdge ← MakeEdge[leftTopVtx, EvalEdgeAt[lVtx, bEdge, leftTopVtx.x]];
}
ELSE {
-- right bottom is left of left top
IF rEdge.moreVertical
THEN {
-- difficult case bot. more vert. top more horz.
toughCase ← TRUE;
ShowSteepTrap[context, spot,
leftBotVtx.x, rightBotVtx.x, bEdge, tEdge, sideways];
vtx0 ← EvalEdgeAt[vtx0, rEdge, rightBotVtx.y]; --build new polygon
vtx1 ← EvalEdgeAt[vtx1, tEdge, rightBotVtx.x];
vtx2 ← EvalEdgeAt[vtx2, tEdge, leftTopVtx.x];
vtx3 ← EvalEdgeAt[vtx3, rEdge, rightTopVtx.y];
a ← -.707; b ← .707; -- test against positive 45 degree slope
}
ELSE {
-- both more horz. do triangle then trapezoid
ShowSteepTrap[context, spot,
leftBotVtx.x, rightBotVtx.x, bEdge, tEdge, sideways];
ShowSteepTrap[context, spot,
rightBotVtx.x, leftTopVtx.x, rEdge, tEdge, sideways];
midSection ← FALSE;
};
};
};
if right side more horizontal do likewise
IF
NOT rEdge.moreVertical
THEN IF rEdge.yIncr < 0.
THEN {
-- right edge is more horizontal, top vertex is leftmost
bEdge ← MakeEdge[leftBotVtx, rightBotVtx]; tEdge ← DupEdgeBlock[rEdge];
IF leftBotVtx.x <= rightTopVtx.x
THEN {
-- easy case: right triangle containing whole right edge
ShowSteepTrap[context, spot,
rightTopVtx.x, rightBotVtx.x, bEdge, tEdge, sideways];
midrEdge ← MakeEdge[EvalEdgeAt[rVtx, bEdge, rightTopVtx.x], rightTopVtx];
}
ELSE {
-- left bottom is right of right top
IF lEdge.moreVertical
THEN {
-- difficult case bot. more vert. top more horz.
toughCase ← TRUE;
vtx0 ← EvalEdgeAt[vtx0, lEdge, rightTopVtx.y]; --build new polygon
vtx1 ← EvalEdgeAt[vtx1, tEdge, rightTopVtx.x];
vtx2 ← EvalEdgeAt[vtx2, tEdge, leftBotVtx.x];
vtx3 ← EvalEdgeAt[vtx3, lEdge, leftBotVtx.y];
a ← .707; b ← .707; -- test against negative 45 degree slope
};
get triangle at right, if both horz. then left edge got middle trapezoid
ShowSteepTrap[context, spot,
leftBotVtx.x, rightBotVtx.x, bEdge, tEdge, sideways];
};
}
ELSE {
-- right edge is more horizontal, bottom vertex is leftmost
tEdge ← MakeEdge[leftTopVtx, rightTopVtx]; bEdge ← DupEdgeBlock[rEdge];
IF leftTopVtx.x <= rightBotVtx.x
THEN {
-- easy case: right triangle containing whole right edge
ShowSteepTrap[context, spot,
rightBotVtx.x, rightTopVtx.x, bEdge, tEdge, sideways];
midrEdge ← MakeEdge[rightBotVtx, EvalEdgeAt[rVtx, tEdge, rightBotVtx.x]];
}
ELSE {
-- left top is right of right bottom
IF lEdge.moreVertical
THEN {
-- difficult case bot. more vert. top more horz.
toughCase ← TRUE;
vtx0 ← EvalEdgeAt[vtx0, bEdge, rightBotVtx.x]; --build new polygon
vtx1 ← EvalEdgeAt[vtx1, lEdge, rightBotVtx.y];
vtx2 ← EvalEdgeAt[vtx2, lEdge, leftTopVtx.y];
vtx3 ← EvalEdgeAt[vtx3, bEdge, leftTopVtx.x];
a ← -.707; b ← .707; -- test against positive 45 degree slope
};
get triangle at right, if both horz. then left edge got middle trapezoid
ShowSteepTrap[context, spot,
leftTopVtx.x, rightTopVtx.x, bEdge, tEdge, sideways];
};
};
Do middle section
IF toughCase
THEN {
-- quadrilateral with top and bottom slopes on both sides of 45 deg.
c, d1, d2, d3: REAL; -- evaluate area based on distance of vertices from 45 degree line
c ← -(a * vtx0.x + b * vtx0.y); -- equation for line through vtx0
d1 ← a * vtx1.x + b * vtx1.y + c; -- distances of other vertices from line
d2 ← a * vtx2.x + b * vtx2.y + c;
d3 ← a * vtx3.x + b * vtx3.y + c;
IF (top - bottom) * (
MAX[d1, d2, d3, 0.0] -
MIN[d1, d2, d3, 0.0] ) / 2.0 < justNoticeable
THEN RETURN[] -- estimated area too small to matter
ELSE {
poly: REF FancyPatch ← NEW[FancyPatch[4]];
poly.vtx[0] ← DupLerpVtx[vtx0]; poly.vtx[1] ← DupLerpVtx[vtx1];
poly.vtx[2] ← DupLerpVtx[vtx2]; poly.vtx[3] ← DupLerpVtx[vtx3];
poly.spot ← spot;
recurseLevel ← recurseLevel + 1;
IF recurseLevel > 64 THEN SIGNAL TilersError[$DeepRecursionInTiler];
RealFancyTiler[context, poly]; -- go draw it (recursively)
};
}
ELSE IF midSection THEN ShowSteepTrap[context, spot, bottom, top, midlEdge, midrEdge];
};
lVtx, rVtx: LerpVtx; -- made global to limit allocations
ShowSteepTrap: PROC[ context: REF ThreeDScenes.Context, spot: ScanConvert.Spot,
bottom, top: REAL, lEdge, rEdge: EdgeBlock,
sideways:
BOOLEAN ←
FALSE ] ~ {
Swap:
PROC[ref1, ref2:
REF RealSequence]
RETURNS [outRef1, outRef2:
REF RealSequence] ~{
RETURN[ outRef1: ref2, outRef2: ref1 ];
};
shadingType:
ATOM ←
NARROW[ Atom.GetPropFromList[
NARROW[ Atom.GetPropFromList[spot.props, $ShapeShadingProps], Atom.PropList ],
$Type
] ];
writeOp: ATOM ← IF shadingType = $Lines THEN $WriteLineUnder ELSE $WriteUnder;
lStartSave: REAL ← lEdge.start; lEndSave: REAL ← lEdge.end; -- save limits to restore later
rStartSave: REAL ← rEdge.start; rEndSave: REAL ← rEdge.end;
yLimit: INTEGER ← IF sideways THEN Real.FixI[context.viewPort.w]
ELSE Real.FixI[context.viewPort.h];
xLimit: INTEGER ← IF sideways THEN Real.FixI[context.viewPort.h]
ELSE Real.FixI[context.viewPort.w];
yIncrements: BOOLEAN ← FALSE;
IF bottom + justNoticeable >= top THEN RETURN[]; -- too vertically thin to affect image
IF Atom.GetPropFromList[spot.props, $KeepYIncrements] = $DoIt THEN yIncrements ←TRUE;
lEdge.start ← rEdge.start ← bottom; -- set edge limits for coverage calcs.
lEdge.end ← rEdge.end ← top;
FOR y:
INTEGER
IN [Floor[bottom]..Ceiling[top]]
DO
IF y < 0 OR y >= yLimit THEN LOOP; -- scissor off if out-of-bounds
scanSeg ← MakeScanSeg[scanSeg, lEdge, rEdge, Real.Float[y], yIncrements];
IF scanSeg.end - scanSeg.start > justNoticeable
THEN {
-- do if wide enough to affect image
FOR x:
INTEGER
IN [Floor[scanSeg.start]..Ceiling[scanSeg.end]]
DO
IF x < 0 OR x >= xLimit THEN LOOP; -- scissor off if out-of-bounds
[spot.coverage, spot] ← EvalScanSegAt[ spot, scanSeg, Real.Float[x] ];
IF sideways
THEN { [spot.yIncr, spot.xIncr] ← Swap[spot.xIncr, spot.yIncr]; -- switch x and y
spot.x ← y; spot.y ← x; }
ELSE { spot.x ← x; spot.y ← y; };
ScanConvert.PutSpot[ context.display, spot, writeOp, context.renderMode ];
IF sideways THEN [spot.yIncr, spot.xIncr] ← Swap[spot.xIncr, spot.yIncr]; -- put back
ENDLOOP;
};
ENDLOOP;
lEdge.start ← lStartSave; lEdge.end ← lEndSave; -- restore limits
rEdge.start ← rStartSave; rEdge.end ← rEndSave;
};