File: CSGGraphicsImpl.mesa
Last edited by Bier on February 23, 1987 5:13:57 pm PST
Contents: Implementation of a simple graphics package for 3d renderings.
DIRECTORY
CoordSys, CSGGraphics, Imager, ImagerBackdoor, ImagerColor, ImagerColorPrivate, ImagerFont, ImagerPath, Matrix3d, Shading, SVPolygon3d, Real, Rope, SV2d, SV3d, SVDraw, SVLines2d, SVVector3d, SVModelTypes;
CSGGraphicsImpl: CEDAR PROGRAM
IMPORTS CoordSys, Imager, ImagerBackdoor, ImagerColor, ImagerColorPrivate, ImagerFont, ImagerPath, Matrix3d, Rope, Shading, SVDraw, SVLines2d, SVPolygon3d, SVVector3d
EXPORTS CSGGraphics =
BEGIN
Artwork: TYPE = SVModelTypes.Artwork;
Camera: TYPE = REF CameraObj;
CameraObj: TYPE = SVModelTypes.CameraObj;
Color: TYPE = Imager.Color;
CoordSystem: TYPE = SVModelTypes.CoordSystem;
DrawStyle: TYPE = SVModelTypes.DrawStyle;
FrameBox: TYPE = SVModelTypes.FrameBox;
LightSource: TYPE = SVModelTypes.LightSource;
LightSourceList: TYPE = Shading.LightSourceList;
Matrix4by4: TYPE = SV3d.Matrix4by4;
Plane: TYPE = SV3d.Plane;
Point2d: TYPE = SV2d.Point2d;
Point3d: TYPE = SV3d.Point3d;
Poly3d: TYPE = SV3d.Poly3d;
Projection: TYPE = SVModelTypes.Projection; -- {perspective, orthogonal}
QualityMode: TYPE = SVModelTypes.QualityMode;
Ray2d: TYPE = SV2d.Ray2d;
StrokeEnd: TYPE = Imager.StrokeEnd;
Vector3d: TYPE = SV3d.Vector3d;
CreateCamera: PUBLIC PROC [viewName: Rope.ROPE, coordSys: CoordSystem, screenCS: CoordSystem, resolution: REAL, focalLength: REAL, projection: Projection, frame: FrameBox, clippingPlanes: LIST OF Plane, visibleAssemblies: LIST OF Rope.ROPE, style: DrawStyle, colorFilm: BOOL, useBoundBoxes: BOOL, useBoundSpheresForShadows: BOOL] RETURNS [camera: Camera] = {
camera ← NEW[CameraObj ← [viewName, coordSys, screenCS, resolution, focalLength, projection, frame, clippingPlanes, visibleAssemblies, style, colorFilm, fast, FALSE, useBoundBoxes, useBoundSpheresForShadows, [0,0,0]]];
}; -- end of CreateCamera
PlaceCamera: PUBLIC PROC [camera: Camera, focus: Point3d, origin: Point3d, slant: REAL] = {
zAxis: Vector3d ← SVVector3d.Sub[origin, focus];
xAxis should be normal to zAxis, parallel to the xz plane, and counter-clockwise from the projection of z onto the xz plane. If z is vertical, I arbitrarily chose x axis aligned with world x axis.
camera.coordSys.mat ← Matrix3d.MakeHorizontalMatFromZAxis[zAxis, origin];
Now, rotate the coordSys counter-clockwise slant degrees around its z axis.
camera.coordSys.mat ← Matrix3d.LocalRotateAboutZAxis[camera.coordSys.mat, slant];
};
SetFocalLengthCamera: PUBLIC PROC [camera: Camera, focalLength: REAL] = {
camera.focalLength ← focalLength;
};
SetQualityCamera: PUBLIC PROC [camera: Camera, qual: QualityMode] = {
How you wish to make the speed/print quality trade off
camera.quality ← qual;
};
ColorFilmCamera: PUBLIC PROC [camera: Camera, colorFilm: BOOL] = {
camera.colorFilm ← colorFilm;
};
Clip: PUBLIC PROC [dc: Imager.Context, camera: Camera] = {
downLeft, upRight: Point2d;
IF camera.frame.fullScreen THEN RETURN;
puts into SCREEN coords
downLeft ← CoordSys.CameraToScreen[camera.frame.downLeft, camera.screenCS];
upRight ← CoordSys.CameraToScreen[camera.frame.upRight, camera.screenCS];
Imager.ClipRectangle[dc, [downLeft[1], downLeft[2], upRight[1] - downLeft[1], upRight[2] - downLeft[2]]];
};
DrawFrame: PUBLIC PROC [dc: Imager.Context, camera: Camera] = {
downLeft, upRight: Point2d;
IF camera.frame.fullScreen THEN RETURN;
downLeft ← CoordSys.CameraToScreen[camera.frame.downLeft, camera.screenCS];
upRight ← CoordSys.CameraToScreen[camera.frame.upRight, camera.screenCS];
SVDraw.LineSandwich[dc, downLeft[1], downLeft[2], downLeft[1], upRight[2]];
SVDraw.LineSandwich[dc, downLeft[1], upRight[2], upRight[1], upRight[2]];
SVDraw.LineSandwich[dc, upRight[1], upRight[2], upRight[1], downLeft[2]];
SVDraw.LineSandwich[dc, upRight[1], downLeft[2], downLeft[1], downLeft[2]];
}; -- fast, low quality
DoProjection: PUBLIC PROC [point3d: Point3d, camera: Camera] RETURNS [newPoint: Point2d] = {
Perform a perspective or orthogonal projection on point3d.
IF camera.projection = orthogonal THEN {
newPoint[1] ← point3d[1];
newPoint[2] ← point3d[2];
RETURN;
}
ELSE RETURN[Matrix3d.PerspectiveTrans[point3d, camera.focalLength]];
};
LocalToCamera: PUBLIC PROC [localPoint: Point3d, localCS: CoordSystem] RETURNS [cameraPoint: Point3d] = {
cameraPoint ← Matrix3d.Update[localPoint, localCS.wrtCamera]; -- puts in CAMERA
};
LocalToWorld: PUBLIC PROC [localPt: Point3d, localCS: CoordSystem] RETURNS [worldPt: Point3d] = {
worldPt ← Matrix3d.Update[localPt, localCS.wrtWorld];
};
VectorToWorld: PUBLIC PROC [vector: Vector3d, localCS: CoordSystem] RETURNS [worldVector: Vector3d] = {
worldVector ← Matrix3d.UpdateVectorWithInverse[localCS.worldWRTlocal,vector];
};
SetCP: PUBLIC PROC [dc: Imager.Context, point3d: Point3d, camera: Camera, localCS: CoordSystem] = {
camera.lastPoint ← LocalToCamera[point3d, localCS]; -- puts into CAMERA coords and stores in camera.
};
SetCPAbsolute: PUBLIC PROC [dc: Imager.Context, point3d: Point3d, camera: Camera] = {
camera.lastPoint ← point3d;
};
DrawTo: PUBLIC PROC [dc: Imager.Context, point3d: Point3d, camera: Camera, localCS: CoordSystem] = {
Find out how much of the line defined by point3d (in CAMERA) and lastCameraPoint is visible. If this section includes lastCameraPoint and camera.quality = qual and we are working on a visible section of polygon, then LineTo lastCameraPoint and LineTo point3d. If this is a new visible section, then MoveTo lastCameraPoint and LineTo point3d.
newP1, newP2, thisCameraPoint: Point3d;
lastScreenPoint, thisScreenPoint: Point2d;
newP1isP1, newP2isP2, nullSegment: BOOL;
thisCameraPoint ← LocalToCamera[point3d, localCS]; -- puts in CAMERA
[newP1, newP2, newP1isP1, newP2isP2, nullSegment] ← SVPolygon3d.ClipLineSegmentToPlanes[camera.lastPoint, thisCameraPoint, camera.clippingPlanes];
IF nullSegment THEN {camera.lastPoint ← thisCameraPoint; RETURN};
lastScreenPoint ← DoProjection[newP1, camera];
lastScreenPoint ← CoordSys.CameraToScreen[lastScreenPoint, camera.screenCS]; -- puts into SCREEN coords
thisScreenPoint ← DoProjection[newP2, camera];
thisScreenPoint ← CoordSys.CameraToScreen[thisScreenPoint, camera.screenCS]; -- puts into SCREEN coords
IF camera.quality = quality THEN { -- use round-ended strokes
line: ImagerPath.Trajectory;
line ← ImagerPath.MoveTo[[lastScreenPoint[1], lastScreenPoint[2]]];
line ← ImagerPath.LineTo[line, [thisScreenPoint[1], thisScreenPoint[2]]];
Imager.SetStrokeWidth[dc, 1];
Imager.SetStrokeEnd[dc, round];
Imager.MaskStrokeTrajectory[dc, line, FALSE];
}
ELSE { -- not a quality camera. Just draw line segment.
Imager.MaskVector[dc, [lastScreenPoint[1], lastScreenPoint[2]], [thisScreenPoint[1], thisScreenPoint[2]]];
};
camera.lastPoint ← thisCameraPoint;
}; -- end of DrawTo
DrawToAbsolute: PUBLIC PROC [dc: Imager.Context, point3d: Point3d, camera: Camera] = {
Like DrawTo, except that point3d is assumed to be already in CAMERA coordinates.
newP1, newP2, thisCameraPoint: Point3d;
lastScreenPoint, thisScreenPoint: Point2d;
newP1isP1, newP2isP2, nullSegment: BOOL;
thisCameraPoint ← point3d;
[newP1, newP2, newP1isP1, newP2isP2, nullSegment] ← SVPolygon3d.ClipLineSegmentToPlanes[camera.lastPoint, thisCameraPoint, camera.clippingPlanes];
IF nullSegment THEN {camera.lastPoint ← thisCameraPoint; RETURN};
lastScreenPoint ← DoProjection[newP1, camera];
lastScreenPoint ← CoordSys.CameraToScreen[lastScreenPoint, camera.screenCS]; -- puts into SCREEN coords
thisScreenPoint ← DoProjection[newP2, camera];
thisScreenPoint ← CoordSys.CameraToScreen[thisScreenPoint, camera.screenCS]; -- puts into SCREEN coords
IF camera.quality = quality THEN { -- use round-ended strokes
line: ImagerPath.Trajectory;
line ← ImagerPath.MoveTo[[lastScreenPoint[1], lastScreenPoint[2]]];
line ← ImagerPath.LineTo[line, [thisScreenPoint[1], thisScreenPoint[2]]];
Imager.SetStrokeWidth[dc, 2];
Imager.SetStrokeEnd[dc, round];
Imager.MaskStrokeTrajectory[dc, line, FALSE];
}
ELSE { -- not a quality camera. Just draw line segment.
Imager.SetStrokeWidth[dc, 2];
Imager.MaskVector[dc, [lastScreenPoint[1], lastScreenPoint[2]], [thisScreenPoint[1], thisScreenPoint[2]]];
};
camera.lastPoint ← thisCameraPoint;
}; -- end of DrawToAbsolute
MoveTo: PUBLIC PROC [path: ImagerPath.Trajectory, point3d: Point3d, camera: Camera, localCS: CoordSystem] = {
objPoint: Point2d;
point3d ← LocalToCamera[point3d, localCS]; -- puts in CAMERA
objPoint ← DoProjection[point3d, camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
path ← ImagerPath.MoveTo[[objPoint[1], objPoint[2]]];
};
MoveToAbsolute: PUBLIC PROC [path: ImagerPath.Trajectory, point3d: Point3d, camera: Camera] = {
objPoint: Point2d;
objPoint ← DoProjection[point3d, camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
path ← ImagerPath.MoveTo[[objPoint[1], objPoint[2]]];
};
Assumes point3d is a point in CAMERA coordinate system.
LineTo: PUBLIC PROC [path: ImagerPath.Trajectory, point3d: Point3d, camera: Camera, localCS: CoordSystem] = {
objPoint: Point2d;
point3d ← LocalToCamera[point3d, localCS]; -- puts in CAMERA
objPoint ← DoProjection[point3d, camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
path ← ImagerPath.LineTo[path, [objPoint[1], objPoint[2]]];
};
LineToAbsolute: PUBLIC PROC [path: ImagerPath.Trajectory, point3d: Point3d, camera: Camera] = {
objPoint: Point2d;
objPoint ← DoProjection[point3d, camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
path ← ImagerPath.LineTo[path, [objPoint[1], objPoint[2]]];
};
DrawStroke: PUBLIC PROC [dc: Imager.Context, path: ImagerPath.Trajectory, width: REAL ← 1, closed: BOOLEANFALSE, ends: StrokeEnd ← butt] = {
Imager.SetStrokeWidth[dc, width];
Imager.SetStrokeEnd[dc, ends];
Imager.MaskStrokeTrajectory[dc, path, closed];
};
DrawFilled: PUBLIC PROC[dc: Imager.Context, path: ImagerPath.Trajectory, parityFill: BOOLEANFALSE] = {
Imager.MaskFillTrajectory[dc, path, parityFill];
};
DrawPolygonAbsolute: PUBLIC PROC [dc: Imager.Context, poly: Poly3d, width: REAL ← 1, ends: StrokeEnd ← butt, camera: Camera] = {
Draws the edges of poly assuming that the vertex coordinates are CAMERA coordinates.
Assumes poly3d in camera coords.
outline: ImagerPath.Trajectory;
objPoint: Point2d;
Clip the polygon against the camera's clipping planes to produce a new clipped polygon.
poly ← SVPolygon3d.ClipPolyToPlanes[poly, camera.clippingPlanes];
objPoint ← DoProjection[poly[0], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.MoveTo[[objPoint[1], objPoint[2]]];
FOR i: NAT IN[1..poly.len) DO
objPoint ← DoProjection[poly[i], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.LineTo[outline, [objPoint[1], objPoint[2]]];
ENDLOOP;
Imager.SetStrokeWidth[dc, width];
Imager.SetStrokeEnd[dc, ends];
Imager.MaskStrokeTrajectory[dc, outline, TRUE];
};
DrawLine: PUBLIC PROC [dc: Imager.Context, start: Point3d, end: Point3d, camera: Camera, localCS: CoordSystem] = {
Like DrawTo except that starting and ending points are given at the same time.
lastScreenPoint, thisScreenPoint: Point2d;
newP1isP1, newP2isP2, nullSegment: BOOL;
newP1, newP2: Point3d;
start ← LocalToCamera[start, localCS]; -- puts in CAMERA
end ← LocalToCamera[end, localCS]; -- puts in CAMERA
[newP1, newP2, newP1isP1, newP2isP2, nullSegment] ← SVPolygon3d.ClipLineSegmentToPlanes[start, end, camera.clippingPlanes];
IF nullSegment THEN RETURN;
lastScreenPoint ← DoProjection[newP1, camera];
lastScreenPoint ← CoordSys.CameraToScreen[lastScreenPoint, camera.screenCS]; -- puts into SCREEN coords
thisScreenPoint ← DoProjection[newP2, camera];
thisScreenPoint ← CoordSys.CameraToScreen[thisScreenPoint, camera.screenCS]; -- puts into SCREEN coords
IF camera.quality = quality THEN { -- use round-ended strokes
line: ImagerPath.Trajectory;
line ← ImagerPath.MoveTo[[lastScreenPoint[1], lastScreenPoint[2]]];
line ← ImagerPath.LineTo[line, [thisScreenPoint[1], thisScreenPoint[2]]];
Imager.SetStrokeWidth[dc, 1];
Imager.SetStrokeEnd[dc, round];
Imager.MaskStrokeTrajectory[dc, line, FALSE];
}
ELSE { -- not a quality camera. Just draw line segment.
Imager.MaskVector[dc, [lastScreenPoint[1], lastScreenPoint[2]], [thisScreenPoint[1], thisScreenPoint[2]]];
};
}; -- end of DrawLine
DrawLineOnScreen: PUBLIC PROC [dc: Imager.Context, screenPoint1, screenPoint2: Point2d, camera: Camera] = {
IF camera.quality = quality THEN { -- use round-ended strokes
line: ImagerPath.Trajectory;
line ← ImagerPath.MoveTo[[screenPoint1[1], screenPoint1[2]]];
line ← ImagerPath.LineTo[line, [screenPoint2[1], screenPoint2[2]]];
Imager.SetStrokeWidth[dc, 1];
Imager.SetStrokeEnd[dc, round];
Imager.MaskStrokeTrajectory[dc, line, FALSE];
}
ELSE { -- not a quality camera. Just draw line segment.
Imager.MaskVector[dc, [screenPoint1[1], screenPoint1[2]], [screenPoint2[1], screenPoint2[2]]];
};
};
DrawChar: PUBLIC PROC [dc: Imager.Context, c: CHARACTER, camera: Camera] = {
screenPoint: Point2d;
screenPoint ← DoProjection[camera.lastPoint, camera];
screenPoint ← CoordSys.CameraToScreen[screenPoint, camera.screenCS]; -- puts into SCREEN coords
Imager.SetXY[dc, [screenPoint[1], screenPoint[2]]];
Imager.SetFont[dc, coordFont];
Imager.ShowChar[dc, c];
}; -- end of DrawChar
DrawArea: PUBLIC PROC [dc: Imager.Context, localNormal: Vector3d, poly3d: Poly3d, artwork: Artwork, lightSources: LightSourceList, camera: Camera, localCS: CoordSystem] = {
Given an ordered list (array) of Point3d's, defined in the current local coordinate system, find the corresponding WORLD points, and project the polygon they define onto the image plane with a perspective projection.
worldPoint3d: Point3d;
objPoint: Point2d;
colorshade: Imager.Color;
r, g, b, scaleFactor: REAL;
cameraNormal: Vector3d;
worldNormal: Vector3d;
eyepoint: Point3d;
cameraPolygon: Poly3d;
outline: ImagerPath.Trajectory;
Find the real normal.
cameraNormal ← Matrix3d.UpdateVectorWithInverse[localCS.cameraWRTlocal, localNormal];
worldNormal ← Matrix3d.UpdateVectorWithInverse[localCS.worldWRTlocal, localNormal];
A reverse-facing surface is one which is not visible to the camera. In our current world of opaque surfaces, we need not draw reverse-facing surfaces. To detect a reverse-facing surface, take its normal N. Find the dot product of N with the vector from the a point on the surface to the camera lens. If the result is negative, the surface is reverse-facing.
A simpler test which removes most reverse-facing surfaces is this one:
IF cameraNormal[3]<=0 THEN RETURN;
Don't draw reverse-facing surfaces.
Look at the first point.
worldPoint3d ← Matrix3d.Update[poly3d[0], localCS.wrtWorld];
Use this point to estimate surface color
eyepoint ← LocalToWorld[[0,0,camera.focalLength], camera.coordSys];
SELECT artwork.material FROM
plastic => [r,g,b] ← Shading.DiffuseAndSpecularReflectance[eyepoint, worldNormal, worldPoint3d, artwork.color, lightSources];
chalk => [r,g,b] ← Shading.DiffuseReflectance[worldNormal, worldPoint3d, artwork.color, lightSources];
ENDCASE => ERROR;
Find the polygon in camera coordinates.
cameraPolygon ← CameraPolygon[poly3d, localCS];
Clip this polygon against the camera's clipping planes.
cameraPolygon ← SVPolygon3d.ClipPolyToPlanes[cameraPolygon, camera.clippingPlanes];
Move to the first point.
objPoint ← DoProjection[cameraPolygon[0], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.MoveTo[[objPoint[1], objPoint[2]]];
Create a path which forms the edges of this area.
FOR i: NAT IN[1..cameraPolygon.len) DO
objPoint ← DoProjection[cameraPolygon[i], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.LineTo[outline, [objPoint[1], objPoint[2]]];
ENDLOOP;
scaleFactor ← MAX[r,g,b];
IF scaleFactor > 1.0 THEN {
r ← r/scaleFactor; g ← g/scaleFactor; b ← b/scaleFactor;
};
colorshade ← ImagerColor.ColorFromRGB[[r,g,b]];
SetColor[dc, camera, colorshade];
Imager.MaskFillTrajectory[dc, outline];
};
CameraPolygon: PRIVATE PROC [poly3d: Poly3d, localCS: CoordSystem] RETURNS [cameraPoly: Poly3d] = {
Converts a polygon from local to CAMERA coordinates
cameraPoly ← SVPolygon3d.CreatePoly[poly3d.len];
FOR i: NAT IN [0..poly3d.len) DO
cameraPoly ← SVPolygon3d.AddPolyPoint[cameraPoly, LocalToCamera[poly3d[i], localCS]];
ENDLOOP;
};
DrawAreaNormalAbsolute: PUBLIC PROC [dc: Imager.Context, cameraNormal: Vector3d, poly3d: Poly3d, artwork: Artwork, lightSources: LightSourceList, camera: Camera,
localCS: CoordSystem] = {
normal is assumed to be in Camera coordinates. poly3d in local coordinates.
given an ordered list (array) of Point3d's, defined in the current local coordinate system, find the corresponding WORLD points, and project the polygon they define onto the image plane with a perspective projection.
objPoint: Point2d;
colorshade: Imager.Color;
r, g, b, scaleFactor: REAL;
worldNormal: Vector3d;
eyepoint, worldPoint3d: Point3d;
cameraPolygon: Poly3d;
outline: ImagerPath.Trajectory;
Find the real normal.
worldNormal ← Matrix3d.UpdateVectorWithInverse[camera.coordSys.worldWRTlocal, cameraNormal];
A reverse-facing surface is one which is not visible to the camera. In our current world of opaque surfaces, we need not draw reverse-facing surfaces. To detect a reverse-facing surface, take its normal N. Find the dot product of N with the vector from the a point on the surface to the camera lens. If the result is negative, the surface is reverse-facing.
A simpler test which removes most reverse-facing surfaces is this one:
IF cameraNormal[3]<=0 THEN RETURN;
Look at the first point.
worldPoint3d ← Matrix3d.Update[poly3d[0], localCS.wrtWorld];
Use this point to estimate surface color.
eyepoint ← LocalToWorld[[0,0,camera.focalLength], camera.coordSys];
SELECT artwork.material FROM
plastic => [r,g,b] ← Shading.DiffuseAndSpecularReflectance[eyepoint, worldNormal, worldPoint3d, artwork.color, lightSources];
chalk => [r,g,b] ← Shading.DiffuseReflectance[worldNormal, worldPoint3d, artwork.color, lightSources];
ENDCASE => ERROR;
Find the polygon in camera coordinates.
cameraPolygon ← CameraPolygon[poly3d, localCS];
Clip this polygon against the camera's clipping planes.
cameraPolygon ← SVPolygon3d.ClipPolyToPlanes[cameraPolygon, camera.clippingPlanes];
Move to the first point.
objPoint ← DoProjection[cameraPolygon[0], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.MoveTo[[objPoint[1], objPoint[2]]];
Create a path which forms the edges of this area.
FOR i: NAT IN[1..cameraPolygon.len) DO
objPoint ← DoProjection[cameraPolygon[i], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.LineTo[outline, [objPoint[1], objPoint[2]]];
ENDLOOP;
scaleFactor ← MAX[r,g,b];
IF scaleFactor > 1.0 THEN {
r ← r/scaleFactor; g ← g/scaleFactor; b ← b/scaleFactor;
};
colorshade ← ImagerColor.ColorFromRGB[[r,g,b]];
SetColor[dc, camera, colorshade];
Imager.MaskFillTrajectory[dc, outline];
};
DrawAreaAbsolute: PUBLIC PROC [dc: Imager.Context, poly3d: Poly3d, camera: Camera] = {
Assumes poly3d in camera coords.
outline: ImagerPath.Trajectory;
objPoint: Point2d;
Clip the polygon against the camera's clipping planes to produce a new clipped polygon.
poly3d ← SVPolygon3d.ClipPolyToPlanes[poly3d, camera.clippingPlanes];
objPoint ← DoProjection[poly3d[0], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.MoveTo[[objPoint[1], objPoint[2]]];
FOR i: NAT IN[1..poly3d.len) DO
objPoint ← DoProjection[poly3d[i], camera];
objPoint ← CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords
outline ← ImagerPath.LineTo[outline, [objPoint[1], objPoint[2]]];
ENDLOOP;
Imager.MaskFillTrajectory[dc, outline];
}; -- end of DrawAreaAbsolute
DrawHorizonOfPlane: PUBLIC PROC [dc: Imager.Context, plane: Plane, camera: Camera, localCS: CoordSystem] = {
Given a plane, in local coordinates, convert the plane to CAMERA coordinates and then use DrawHorizonOfPlaneAbsolute.
plane ← Matrix3d.UpdatePlaneWithInverse[plane, localCS.cameraWRTlocal];
DrawHorizonOfPlaneAbsolute[dc, plane, camera];
}; -- end of DrawHorizonOfPlane
DrawHorizonOfPlaneAbsolute: PUBLIC PROC [dc: Imager.Context, plane: Plane, camera: Camera] = {
Given a plane, in CAMERA coordinates, find two points, in CAMERA coordinates, which are on the projection of the horizon onto the screen. Convert to SCREEN coordinates and draw as an infinite line.
If the plane is parallel to the screen, then there is no horizon.
almostZero: REAL ← 1.0e-12;
t: REAL;
vanishCamera1, vanishCamera2, vanishScreen1, vanishScreen2: Point2d;
IF Abs[plane.A] <= almostZero AND Abs[plane.B] <= almostZero THEN RETURN;
IF Abs[plane.B] > Abs[plane.A] THEN {
y1, y2: REAL;
The plane is more nearly horizontal than vertical.
Find two direction vectors which are parallel to the plane. Choose the first to be [-1,y,-1] and the second to be [1,y,-1]. If the plane is [A, B, C, D], then the first vector has the property that [A, B, C] dot [-1, y, -1] = 0. That is:
-A + By -C = 0. y1 = (A+C)/B.
Likewise, A + By -C = 0. y2 = (C-A)/B.
y1 ← (plane.A+plane.C)/plane.B;
y2 ← (plane.C-plane.A)/plane.B;
Find the first vanishing point.
t ← camera.focalLength; -- since we have chosen direction[3] to be -1
vanishCamera1[1] ← -t;
vanishCamera1[2] ← t*y1;
vanishScreen1 ← CoordSys.CameraToScreen[vanishCamera1, camera.screenCS];
Find the second vanishing point.
vanishCamera2[1] ← t;
vanishCamera2[2] ← t*y2;
vanishScreen2 ← CoordSys.CameraToScreen[vanishCamera2, camera.screenCS];
}
ELSE {
x1, x2: REAL;
The plane is more nearly vertical than horizontal. Proceed as above, but use the vectors [x, -1, -1] and [x, 1, -1]. The two equations are:
Ax -B -C = 0. x1 = (B+C)/A. and
Ax +B -C = 0. x2 = (C-B)/A.
x1 ← (plane.B+plane.C)/plane.A;
x2 ← (plane.C-plane.B)/plane.A;
Find the first vanishing point.
t ← camera.focalLength; -- since we have chosen direction[3] to be -1
vanishCamera1[1] ← t*x1;
vanishCamera1[2] ← -t;
vanishScreen1 ← CoordSys.CameraToScreen[vanishCamera1, camera.screenCS];
Find the second vanishing point.
vanishCamera2[1] ← t*x2;
vanishCamera2[2] ← t;
vanishScreen2 ← CoordSys.CameraToScreen[vanishCamera2, camera.screenCS];
};
DrawLineOnScreen[dc, vanishScreen1, vanishScreen2, camera];
}; -- end of DrawHorizonOfPlaneAbsolute
DrawInfiniteLine: PUBLIC PROC [dc: Imager.Context, p1, p2: Point3d, camera: Camera, localCS: CoordSystem] = {
p1 and p2 are assumed to be in local coordinates.
First find p1 and p2 in CAMERA coordinates:
cameraP1, cameraP2: Point3d;
screenP1, screenP2, vanishCamera, vanishScreen: Point2d;
almostZero: REAL ← 1.0e-12;
box: Imager.Rectangle ← ImagerBackdoor.GetBounds[dc];
count: NAT;
ray: Ray2d;
params: ARRAY[1..2] OF REAL;
cameraP1 ← LocalToCamera[p1, localCS];
cameraP2 ← LocalToCamera[p2, localCS];
screenP1 ← DoProjection[cameraP1, camera];
screenP1 ← CoordSys.CameraToScreen[screenP1, camera.screenCS];
screenP2 ← DoProjection[cameraP2, camera];
screenP2 ← CoordSys.CameraToScreen[screenP2, camera.screenCS];
Next find the vanishing point. "Shoot a ray" with direction p2-p1 from the eyepoint and find its intersection with the screen. In CAMERA coordinates, the screen has equation z = 0. The eyepoint is at [0,0,focalLength]. Hence, our ray has equation R[t] = [0,0,focalLength] + t*(p2-p1). z(t) = focalLength+t*(p2[3]-p1[3]). So the intersection occurs where z=0, i.e. t = -focalLength/(p2[3]-p1[3]). If p2[3]=p1[3], then there is no vanishing point.
IF Abs[cameraP2[3]-cameraP1[3]] > almostZero THEN { -- there is a vanishing point
t: REAL;
t ← -camera.focalLength/(cameraP2[3]-cameraP1[3]);
vanishCamera[1] ← t*(cameraP2[1]-cameraP1[1]);
vanishCamera[2] ← t*(cameraP2[2]-cameraP1[2]);
vanishScreen ← CoordSys.CameraToScreen[vanishCamera, camera.screenCS];
There other point can be found by projecting p1 and p2 onto the screen and finding the intersection of the ray (from the vanishing point to whichever has the farther (less negative) z component) with the bounding box or our display context.
IF cameraP2[3] > cameraP1[3] THEN ray ← SVLines2d.CreateRayFromPoints[vanishScreen, screenP2]
ELSE ray ← SVLines2d.CreateRayFromPoints[vanishScreen, screenP1];
[count, params] ← SVLines2d.RayMeetsBox[ray, box.x, box.y, box.x + box.w, box.y + box.h];
IF count = 0 THEN RETURN;
t ← params[count]; -- use the highest value of t.
screenP2 ← SVLines2d.EvalRay[ray, t];
IF camera.quality = quality THEN { -- use round-ended strokes
line: ImagerPath.Trajectory;
line ← ImagerPath.MoveTo[[vanishScreen[1], vanishScreen[2]]];
line ← ImagerPath.LineTo[line, [screenP2[1], screenP2[2]]];
Imager.SetStrokeWidth[dc, 1];
Imager.SetStrokeEnd[dc, round];
Imager.MaskStrokeTrajectory[dc, line, FALSE];
}
ELSE { -- not a quality camera. Just draw line segment.
Imager.MaskVector[dc, [vanishScreen[1], vanishScreen[2]], [screenP2[1], screenP2[2]]];
};
}
ELSE {
There is no vanishing point. Find the perspective projections of p1 and p2 onto the screen. Call the line determined by the resulting points L. Find the intersection of L with the bounding rectangle of our display context.
Consider the ray r(t) = screenP1 + t*(screenP2-screenP1).
ray ← SVLines2d.CreateRayFromPoints[screenP1, screenP2];
[count, params] ← SVLines2d.LineRayMeetsBox[ray, box.x, box.y, box.x + box.w, box.y + box.h];
IF count = 2 THEN {
screenP1 ← SVLines2d.EvalRay[ray, params[1]];
screenP2 ← SVLines2d.EvalRay[ray, params[2]];
IF camera.quality = quality THEN { -- use round-ended strokes
line: ImagerPath.Trajectory;
line ← ImagerPath.MoveTo[[screenP1[1], screenP1[2]]];
line ← ImagerPath.LineTo[line, [screenP2[1], screenP2[2]]];
Imager.SetStrokeWidth[dc, 1];
Imager.SetStrokeEnd[dc, round];
Imager.MaskStrokeTrajectory[dc, line, FALSE];
}
ELSE { -- not a quality camera. Just draw line segment.
Imager.MaskVector[dc, [screenP1[1], screenP1[2]], [screenP2[1], screenP2[2]]];
};
};
};
}; -- end of DrawInfiniteLine
Abs: PROC [r: REAL] RETURNS [REAL] = {
RETURN[IF r >= 0 THEN r ELSE -r];
};
DrawInfiniteLineAbsolute: PUBLIC PROC [dc: Imager.Context, p1, p2: Point3d, camera: Camera, localCS: CoordSystem] = {};
p1 and p2 are assumed to be in CAMERA coordinates.
DrawInfinitePlaneWireFrame: PUBLIC PROC [dc: Imager.Context, plane: Plane, camera: Camera, localCS: CoordSystem] = {
plane is assumed to be in local coordinates.
};
DrawInfinitePlaneWireFrameAbsolute: PUBLIC PROC [dc: Imager.Context, plane: Plane, camera: Camera, localCS: CoordSystem] = {};
plane is assumed to be in CAMERA coordinates.
DrawInfinitePlaneShaded: PUBLIC PROC [dc: Imager.Context, plane: Plane, artwork: Artwork, lightSources: LightSourceList, camera: Camera, localCS: CoordSystem] = {};
plane is assumed to be in local coordinates.
DrawInfinitePlaneShadedAbsolute: PUBLIC PROC [dc: Imager.Context, plane: Plane, artwork: Artwork, lightSources: LightSourceList, camera: Camera] = {};
plane is assumed to be in CAMERA coordinates.
SetColor: PRIVATE PROC [dc: Imager.Context, camera: Camera, color: Color] = {
IF camera.colorFilm THEN Imager.SetColor [dc, color]
ELSE {
intensity: REAL ← ImagerColorPrivate.IntensityFromColor[NARROW[color]];
Imager.SetColor[dc, ImagerColor.ColorFromGray[1.0-intensity]]
};
};
DrawStyleToRope: PUBLIC PROC [drawStyle: DrawStyle] RETURNS [rope: Rope.ROPE] = {
SELECT drawStyle FROM
wire => rope ← "wireframe";
shaded => rope ← "shaded";
rayCast => rope ← "rayCast";
normals => rope ← "normals";
ENDCASE => ERROR UpdateThisCode;
};
RopeToDrawStyle: PUBLIC PROC [rope: Rope.ROPE] RETURNS [drawStyle: DrawStyle, success: BOOL] = {
Does the inverse of DrawStyleToRope.
success ← TRUE;
SELECT TRUE FROM
Rope.Equal[rope, "wireframe", FALSE] => drawStyle ← wire;
Rope.Equal[rope, "shaded", FALSE] => drawStyle ← shaded;
Rope.Equal[rope, "rayCast", FALSE] => drawStyle ← rayCast;
Rope.Equal[rope, "normals", FALSE] => drawStyle ← normals;
ENDCASE => success ← FALSE;
};
ProjectionToRope: PUBLIC PROC [projection: Projection] RETURNS [rope: Rope.ROPE] = {
SELECT projection FROM
perspective => rope ← "perspect";
orthogonal => rope ← "ortho";
ENDCASE => ERROR UpdateThisCode;
};
RopeToProjection: PUBLIC PROC [rope: Rope.ROPE] RETURNS [projection: Projection, success: BOOL] = {
Does the inverse of ProjectionToRope.
success ← TRUE;
SELECT TRUE FROM
Rope.Equal[rope, "perspect", FALSE] => projection ← perspective;
Rope.Equal[rope, "ortho", FALSE] => projection ← orthogonal;
ENDCASE => success ← FALSE;
};
UpdateThisCode: PUBLIC ERROR = CODE;
coordFont: ImagerFont.Font;
Init: PROC [] = {
coordFont ← ImagerFont.Scale[ImagerFont.Find["xerox/TiogaFonts/Helvetica14B"], 1.0];
};
Init[];
END.