DIRECTORY CoordSys, CSGGraphics, Graphics, GraphicsBasic, GraphicsColor, Matrix3d, Shading, SVPolygon3d, Real, Rope, SV2d, SV3d, SVDraw, SVLines2d, SVVector3d, SVModelTypes; CSGGraphicsImpl: PROGRAM IMPORTS CoordSys, Graphics, GraphicsColor, Matrix3d, Rope, Shading, SVDraw, SVLines2d, SVPolygon3d, SVVector3d EXPORTS CSGGraphics = BEGIN Artwork: TYPE = SVModelTypes.Artwork; Camera: TYPE = REF CameraObj; CameraObj: TYPE = SVModelTypes.CameraObj; Color: TYPE = GraphicsColor.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; StrokeEnds: TYPE = Graphics.StrokeEnds; Vector: TYPE = SV3d.Vector; 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: Vector _ SVVector3d.Sub[origin, focus]; camera.coordSys.mat _ Matrix3d.MakeHorizontalMatFromZAxis[zAxis, origin]; 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] = { camera.quality _ qual; }; ColorFilmCamera: PUBLIC PROC [camera: Camera, colorFilm: BOOL] = { camera.colorFilm _ colorFilm; }; Clip: PUBLIC PROC [dc: Graphics.Context, camera: Camera] = { clipPath: Graphics.Path; downLeft, upRight: Point2d; IF camera.frame.fullScreen THEN RETURN; clipPath _ Graphics.NewPath[4]; downLeft _ CoordSys.CameraToScreen[camera.frame.downLeft, camera.screenCS]; upRight _ CoordSys.CameraToScreen[camera.frame.upRight, camera.screenCS]; Graphics.MoveTo[clipPath, downLeft[1], downLeft[2]]; Graphics.LineTo[clipPath, downLeft[1], upRight[2]]; Graphics.LineTo[clipPath, upRight[1], upRight[2]]; Graphics.LineTo[clipPath, upRight[1], downLeft[2]]; Graphics.ClipArea[dc, clipPath, FALSE, FALSE]; }; DrawFrame: PUBLIC PROC [dc: Graphics.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] = { 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[localCS.wrtCamera,localPoint]; -- puts in CAMERA }; LocalToWorld: PUBLIC PROC [localPt: Point3d, localCS: CoordSystem] RETURNS [worldPt: Point3d] = { worldPt _ Matrix3d.Update[localCS.wrtWorld, localPt]; }; VectorToWorld: PUBLIC PROC [vector: Vector, localCS: CoordSystem] RETURNS [worldVector: Vector] = { worldVector _ Matrix3d.UpdateVectorWithInverse[localCS.worldWRTlocal,vector]; }; SetCP: PUBLIC PROC [dc: Graphics.Context, point3d: Point3d, camera: Camera, localCS: CoordSystem] = { camera.lastPoint _ LocalToCamera[point3d, localCS]; -- puts into CAMERA coords and stores in camera. }; SetCPAbsolute: PUBLIC PROC [dc: Graphics.Context, point3d: Point3d, camera: Camera] = { camera.lastPoint _ point3d; }; DrawTo: PUBLIC PROC [dc: Graphics.Context, point3d: Point3d, camera: Camera, localCS: CoordSystem] = { 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: Graphics.Path _ Graphics.NewPath[2]; Graphics.MoveTo[line, lastScreenPoint[1], lastScreenPoint[2]]; Graphics.LineTo[line, thisScreenPoint[1], thisScreenPoint[2]]; Graphics.DrawStroke[dc, line, 1, FALSE, round]; } ELSE { -- not a quality camera. Just draw line segment. Graphics.SetCP[dc, lastScreenPoint[1], lastScreenPoint[2]]; Graphics.DrawTo[dc, thisScreenPoint[1], thisScreenPoint[2]]; }; camera.lastPoint _ thisCameraPoint; }; -- end of DrawTo DrawToAbsolute: PUBLIC PROC [dc: Graphics.Context, point3d: Point3d, camera: Camera] = { 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: Graphics.Path _ Graphics.NewPath[2]; Graphics.MoveTo[line, lastScreenPoint[1], lastScreenPoint[2]]; Graphics.LineTo[line, thisScreenPoint[1], thisScreenPoint[2]]; Graphics.DrawStroke[dc, line, 1, FALSE, round]; } ELSE { -- not a quality camera. Just draw line segment. Graphics.SetCP[dc, lastScreenPoint[1], lastScreenPoint[2]]; Graphics.DrawTo[dc, thisScreenPoint[1], thisScreenPoint[2]]; }; camera.lastPoint _ thisCameraPoint; }; -- end of DrawToAbsolute MoveTo: PUBLIC PROC [path: Graphics.Path, 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 Graphics.MoveTo[path, objPoint[1], objPoint[2]]; }; MoveToAbsolute: PUBLIC PROC [path: Graphics.Path, point3d: Point3d, camera: Camera] = { objPoint: Point2d; objPoint _ DoProjection[point3d, camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.MoveTo[path, objPoint[1], objPoint[2]]; }; LineTo: PUBLIC PROC [path: Graphics.Path, 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 Graphics.LineTo[path, objPoint[1], objPoint[2]]; }; LineToAbsolute: PUBLIC PROC [path: Graphics.Path, point3d: Point3d, camera: Camera] = { objPoint: Point2d; objPoint _ DoProjection[point3d, camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.LineTo[path, objPoint[1], objPoint[2]]; }; DrawStroke: PUBLIC PROC [dc: Graphics.Context, path: Graphics.Path, width: REAL _ 1, closed: BOOLEAN _ FALSE, ends: StrokeEnds _ butt] = { Graphics.DrawStroke[dc, path, width, closed, ends]; }; DrawFilled: PUBLIC PROC[dc: Graphics.Context, path: Graphics.Path, parityFill: BOOLEAN _ FALSE] = { Graphics.DrawArea[dc, path, parityFill]; }; DrawPolygonAbsolute: PUBLIC PROC [dc: Graphics.Context, poly: Poly3d, width: REAL _ 1, ends: StrokeEnds _ butt, camera: Camera] = { outline: Graphics.Path _ Graphics.NewPath[poly.len]; objPoint: Point2d; poly _ SVPolygon3d.ClipPolyToPlanes[poly, camera.clippingPlanes]; objPoint _ DoProjection[poly[0], camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.MoveTo[outline, 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 Graphics.LineTo[outline, objPoint[1], objPoint[2]]; ENDLOOP; Graphics.DrawStroke[dc, outline, width, TRUE, ends]; }; DrawLine: PUBLIC PROC [dc: Graphics.Context, start: Point3d, end: Point3d, camera: Camera, localCS: CoordSystem] = { 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: Graphics.Path _ Graphics.NewPath[2]; Graphics.MoveTo[line, lastScreenPoint[1], lastScreenPoint[2]]; Graphics.LineTo[line, thisScreenPoint[1], thisScreenPoint[2]]; Graphics.DrawStroke[dc, line, 1, FALSE, round]; } ELSE { -- not a quality camera. Just draw line segment. Graphics.SetCP[dc, lastScreenPoint[1], lastScreenPoint[2]]; Graphics.DrawTo[dc, thisScreenPoint[1], thisScreenPoint[2]]; }; }; -- end of DrawLine DrawLineOnScreen: PUBLIC PROC [dc: Graphics.Context, screenPoint1, screenPoint2: Point2d, camera: Camera] = { IF camera.quality = quality THEN { -- use round-ended strokes line: Graphics.Path _ Graphics.NewPath[2]; Graphics.MoveTo[line, screenPoint1[1], screenPoint1[2]]; Graphics.LineTo[line, screenPoint2[1], screenPoint2[2]]; Graphics.DrawStroke[dc, line, 1, FALSE, round]; } ELSE { -- not a quality camera. Just draw line segment. Graphics.SetCP[dc, screenPoint1[1], screenPoint1[2]]; Graphics.DrawTo[dc, screenPoint2[1], screenPoint2[2]]; }; }; DrawChar: PUBLIC PROC [dc: Graphics.Context, c: CHARACTER, camera: Camera] = { screenPoint: Point2d; screenPoint _ DoProjection[camera.lastPoint, camera]; screenPoint _ CoordSys.CameraToScreen[screenPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.SetCP[dc, screenPoint[1], screenPoint[2], FALSE]; Graphics.DrawChar[dc, c]; }; -- end of DrawChar DrawArea: PUBLIC PROC [dc: Graphics.Context, localNormal: Vector, poly3d: Poly3d, artwork: Artwork, lightSources: LightSourceList, camera: Camera, localCS: CoordSystem] = { worldPoint3d: Point3d; objPoint: Point2d; colorshade: Graphics.Color; cameraNormal: Vector; worldNormal: Vector; eyepoint: Point3d; cameraPolygon: Poly3d; outline: Graphics.Path _ Graphics.NewPath[poly3d.len]; cameraNormal _ Matrix3d.UpdateVectorWithInverse[localCS.cameraWRTlocal, localNormal]; worldNormal _ Matrix3d.UpdateVectorWithInverse[localCS.worldWRTlocal, localNormal]; IF cameraNormal[3]<=0 THEN RETURN; worldPoint3d _ Matrix3d.Update[localCS.wrtWorld, poly3d[0]]; eyepoint _ LocalToWorld[[0,0,camera.focalLength], camera.coordSys]; SELECT artwork.material FROM plastic => colorshade _ Shading.DiffuseAndSpecularReflectance[eyepoint, worldNormal, worldPoint3d, artwork.color, lightSources]; chalk => colorshade _ Shading.DiffuseReflectance[worldNormal, worldPoint3d, artwork.color, lightSources]; ENDCASE => ERROR; cameraPolygon _ CameraPolygon[poly3d, localCS]; cameraPolygon _ SVPolygon3d.ClipPolyToPlanes[cameraPolygon, camera.clippingPlanes]; objPoint _ DoProjection[cameraPolygon[0], camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.MoveTo[outline, objPoint[1], objPoint[2]]; FOR i: NAT IN[1..cameraPolygon.len) DO objPoint _ DoProjection[cameraPolygon[i], camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.LineTo[outline, objPoint[1], objPoint[2]]; ENDLOOP; SetColor[dc, camera, colorshade]; Graphics.DrawArea[dc, outline]; }; CameraPolygon: PRIVATE PROC [poly3d: Poly3d, localCS: CoordSystem] RETURNS [cameraPoly: Poly3d] = { 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: Graphics.Context, cameraNormal: Vector, poly3d: Poly3d, artwork: Artwork, lightSources: LightSourceList, camera: Camera, localCS: CoordSystem] = { objPoint: Point2d; colorshade: Graphics.Color; worldNormal: Vector; eyepoint, worldPoint3d: Point3d; cameraPolygon: Poly3d; outline: Graphics.Path _ Graphics.NewPath[poly3d.len]; worldNormal _ Matrix3d.UpdateVectorWithInverse[camera.coordSys.worldWRTlocal, cameraNormal]; IF cameraNormal[3]<=0 THEN RETURN; worldPoint3d _ Matrix3d.Update[localCS.wrtWorld, poly3d[0]]; eyepoint _ LocalToWorld[[0,0,camera.focalLength], camera.coordSys]; SELECT artwork.material FROM plastic => colorshade _ Shading.DiffuseAndSpecularReflectance[eyepoint, worldNormal, worldPoint3d, artwork.color, lightSources]; chalk => colorshade _ Shading.DiffuseReflectance[worldNormal, worldPoint3d, artwork.color, lightSources]; ENDCASE => ERROR; cameraPolygon _ CameraPolygon[poly3d, localCS]; cameraPolygon _ SVPolygon3d.ClipPolyToPlanes[cameraPolygon, camera.clippingPlanes]; objPoint _ DoProjection[cameraPolygon[0], camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.MoveTo[outline, objPoint[1], objPoint[2]]; FOR i: NAT IN[1..cameraPolygon.len) DO objPoint _ DoProjection[cameraPolygon[i], camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.LineTo[outline, objPoint[1], objPoint[2]]; ENDLOOP; SetColor[dc, camera, colorshade]; Graphics.DrawArea[dc, outline]; }; DrawAreaAbsolute: PUBLIC PROC [dc: Graphics.Context, poly3d: Poly3d, camera: Camera] = { outline: Graphics.Path _ Graphics.NewPath[poly3d.len]; objPoint: Point2d; poly3d _ SVPolygon3d.ClipPolyToPlanes[poly3d, camera.clippingPlanes]; objPoint _ DoProjection[poly3d[0], camera]; objPoint _ CoordSys.CameraToScreen[objPoint, camera.screenCS]; -- puts into SCREEN coords Graphics.MoveTo[outline, 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 Graphics.LineTo[outline, objPoint[1], objPoint[2]]; ENDLOOP; Graphics.DrawArea[dc, outline]; }; -- end of DrawAreaAbsolute DrawHorizonOfPlane: PUBLIC PROC [dc: Graphics.Context, plane: Plane, camera: Camera, localCS: CoordSystem] = { plane _ Matrix3d.UpdatePlaneWithInverse[localCS.cameraWRTlocal, plane]; DrawHorizonOfPlaneAbsolute[dc, plane, camera]; }; -- end of DrawHorizonOfPlane DrawHorizonOfPlaneAbsolute: PUBLIC PROC [dc: Graphics.Context, plane: Plane, camera: Camera] = { 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; y1 _ (plane.A+plane.C)/plane.B; y2 _ (plane.C-plane.A)/plane.B; 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]; vanishCamera2[1] _ t; vanishCamera2[2] _ t*y2; vanishScreen2 _ CoordSys.CameraToScreen[vanishCamera2, camera.screenCS]; } ELSE { x1, x2: REAL; x1 _ (plane.B+plane.C)/plane.A; x2 _ (plane.C-plane.B)/plane.A; 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]; 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: Graphics.Context, p1, p2: Point3d, camera: Camera, localCS: CoordSystem] = { cameraP1, cameraP2: Point3d; screenP1, screenP2, vanishCamera, vanishScreen: Point2d; almostZero: REAL _ 1.0e-12; box: GraphicsBasic.Box _ Graphics.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]; 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]; IF cameraP2[3] > cameraP1[3] THEN ray _ SVLines2d.CreateRayFromPoints[vanishScreen, screenP2] ELSE ray _ SVLines2d.CreateRayFromPoints[vanishScreen, screenP1]; [count, params] _ SVLines2d.RayMeetsBox[ray, box.xmin, box.ymin, box.xmax, box.ymax]; 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: Graphics.Path _ Graphics.NewPath[2]; Graphics.MoveTo[line, vanishScreen[1], vanishScreen[2]]; Graphics.LineTo[line, screenP2[1], screenP2[2]]; Graphics.DrawStroke[dc, line, 1, FALSE, round]; } ELSE { -- not a quality camera. Just draw line segment. Graphics.SetCP[dc, vanishScreen[1], vanishScreen[2]]; Graphics.DrawTo[dc, screenP2[1], screenP2[2]]; }; } ELSE { ray _ SVLines2d.CreateRayFromPoints[screenP1, screenP2]; [count, params] _ SVLines2d.LineRayMeetsBox[ray, box.xmin, box.ymin, box.xmax, box.ymax]; 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: Graphics.Path _ Graphics.NewPath[2]; Graphics.MoveTo[line, screenP1[1], screenP1[2]]; Graphics.LineTo[line, screenP2[1], screenP2[2]]; Graphics.DrawStroke[dc, line, 1, FALSE, round]; } ELSE { -- not a quality camera. Just draw line segment. Graphics.SetCP[dc, screenP1[1], screenP1[2]]; Graphics.DrawTo[dc, 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: Graphics.Context, p1, p2: Point3d, camera: Camera, localCS: CoordSystem] = {}; DrawInfinitePlaneWireFrame: PUBLIC PROC [dc: Graphics.Context, plane: Plane, camera: Camera, localCS: CoordSystem] = { }; DrawInfinitePlaneWireFrameAbsolute: PUBLIC PROC [dc: Graphics.Context, plane: Plane, camera: Camera, localCS: CoordSystem] = {}; DrawInfinitePlaneShaded: PUBLIC PROC [dc: Graphics.Context, plane: Plane, artwork: Artwork, lightSources: LightSourceList, camera: Camera, localCS: CoordSystem] = {}; DrawInfinitePlaneShadedAbsolute: PUBLIC PROC [dc: Graphics.Context, plane: Plane, artwork: Artwork, lightSources: LightSourceList, camera: Camera] = {}; SetColor: PRIVATE PROC [dc: Graphics.Context, camera: Camera, color: Color] = { IF camera.colorFilm THEN Graphics.SetColor [dc, color] ELSE { intensity: REAL _ GraphicsColor.ColorToIntensity[color]; Graphics.SetColor[dc, GraphicsColor.IntensityToColor[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] = { 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] = { 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; END. ΤFile: CSGGraphicsImpl.mesa Last edited by Bier on May 23, 1985 11:10:30 pm PDT Contents: Implementation of a simple graphics package for 3d renderings. 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. Now, rotate the coordSys counter-clockwise slant degrees around its z axis. How you wish to make the speed/print quality trade off puts into SCREEN coords Graphics.SetCP[dc, downLeft[1], downLeft[2]]; Graphics.DrawTo[dc, downLeft[1], upRight[2]]; Graphics.DrawTo[dc, upRight[1], upRight[2]]; Graphics.DrawTo[dc, upRight[1], downLeft[2]]; Graphics.DrawTo[dc, downLeft[1], downLeft[2]]; Perform a perspective or orthogonal projection on point3d. 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. Like DrawTo, except that point3d is assumed to be already in CAMERA coordinates. Assumes point3d is a point in CAMERA coordinate system. Draws the edges of poly assuming that the vertex coordinates are CAMERA coordinates. Assumes poly3d in camera coords. Clip the polygon against the camera's clipping planes to produce a new clipped polygon. Like DrawTo except that starting and ending points are given at the same time. 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. Find the real normal. 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: Don't draw reverse-facing surfaces. Look at the first point. Use this point to estimate surface color Find the polygon in camera coordinates. Clip this polygon against the camera's clipping planes. Move to the first point. Create a path which forms the edges of this area. Converts a polygon from local to CAMERA coordinates 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. Find the real normal. 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: Look at the first point. Use this point to estimate surface color. Find the polygon in camera coordinates. Clip this polygon against the camera's clipping planes. Move to the first point. Create a path which forms the edges of this area. Assumes poly3d in camera coords. Clip the polygon against the camera's clipping planes to produce a new clipped polygon. Given a plane, in local coordinates, convert the plane to CAMERA coordinates and then use DrawHorizonOfPlaneAbsolute. 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. 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. Find the first vanishing point. Find the second vanishing point. 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. Find the first vanishing point. Find the second vanishing point. p1 and p2 are assumed to be in local coordinates. First find p1 and p2 in CAMERA coordinates: 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. 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. 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). p1 and p2 are assumed to be in CAMERA coordinates. plane is assumed to be in local coordinates. plane is assumed to be in CAMERA coordinates. plane is assumed to be in local coordinates. plane is assumed to be in CAMERA coordinates. Does the inverse of DrawStyleToRope. Does the inverse of ProjectionToRope. Init: PROC = { Create a Line Pool globalLinePool _ NEW[LinePoolObj]; FOR i: NAT IN[1..globalLinePoolCount] DO globalLinePool[i] _ Graphics.NewPath[2]; ENDLOOP; globalLinePoolPointer _ globalLinePoolCount; }; Init[]; Κγ– "cedar" style˜Iheadšœ™Iprocšœ3™3LšœH™HL˜šΟk ˜ Lšœ ˜ L˜ Lšœ ˜ Lšœ˜Lšœ˜Lšœ ˜ Lšœ˜Lšœ ˜ Lšœ˜L˜Lšœ˜L˜L˜Lšœ ˜ Lšœ ˜ Lšœ ˜ —L˜šœ˜Lšœh˜oLšœ˜—Lš˜˜Lšœ œ˜%Lšœœœ ˜Lšœ œ˜)Lšœœ˜"Lšœ œ˜-Lšœ œ˜)Lšœ œ˜'Lšœ œ˜-Lšœœ˜0Lšœ œ˜#Lšœœ˜Lšœ œ˜Lšœ œ˜Lšœœ˜Lšœ œ8˜HLšœ œ˜-L˜Lšœ œ˜'Lšœœ˜L˜L˜—šΟn œ œœ<œœ;œœœœœœœœœž˜ζLšœ œ“œ œ%˜ΪLšœΟc˜L˜—šž œœœ:œ˜[Lšœ.˜.LšœΕ™ΕLšœI˜IL™KLšœQ˜QLšœ˜—šžœœœœ˜ILšœ!˜!Lšœ˜—šžœœœ(˜ELšœ6™6Lšœ˜Lšœ˜—šžœœœœ˜BLšœ˜Lšœ˜—L˜šžœœœ+˜˜>Lšœ>˜>Lšœ!œ ˜/L˜—šœŸ1˜8Lšœ;˜;Lšœ<˜˜>Lšœ>˜>Lšœ!œ ˜/L˜—šœŸ1˜8Lšœ;˜;Lšœ<˜Lšœ)˜)Lšœ?Ÿ˜YLšœ0˜0Lšœ˜—šžœœœ<˜WLšœ˜Lšœ)˜)Lšœ?Ÿ˜YLšœ0˜0Lšœ˜—Lšœ7™7šžœœœR˜eLšœ˜Lšœ-Ÿ˜>Lšœ)˜)Lšœ?Ÿ˜YLšœ0˜0Lšœ˜—šžœœœ<˜WLšœ˜Lšœ)˜)Lšœ?Ÿ˜YLšœ0˜0Lšœ˜—š ž œœœ4œœœ˜ŠLšœ3˜3Lšœ˜—šž œ œ8œœ˜cLšœ(˜(Lšœ˜—L˜šžœ œ-œ2˜ƒL™TLšœ ™ Lšœ4˜4Lšœ˜L™WLšœA˜AL˜Lšœ)˜)Lšœ?Ÿ˜YLšœ4˜4šœœœ˜Lšœ)˜)Lšœ?Ÿ˜YLšœ3˜3—Lšœ˜Lšœ(œ˜4L˜L˜—šžœœœ_˜tL™OLšœ*˜*Lšœ#œ˜(Lšœ˜Lšœ)Ÿ˜:Lšœ$Ÿ˜5Lšœ{˜{Lšœ œœ˜Lšœ.˜.LšœMŸ˜gLšœ.˜.šœMŸ˜gL˜—šœœŸ˜=L˜*Lšœ>˜>Lšœ>˜>Lšœ!œ ˜/L˜—šœŸ1˜8Lšœ;˜;Lšœ<˜˜>Lšœ*˜*Lšœ?˜?L™Δšœ+œŸ˜QLšœœ˜Lšœ2˜2Lšœ.˜.Lšœ.˜.LšœF˜FL™πLšœœ<˜]Lšœ=˜ALšœU˜ULšœ œœ˜LšœŸ˜1Lšœ%˜%šœœŸ˜=L˜*Lšœ8˜8Lšœ0˜0Lšœ!œ ˜/L˜—šœŸ1˜8Lšœ5˜5Lšœ.˜.Lšœ˜—L˜—šœ˜L™αLšœ9™9Lšœ8˜8LšœY˜Yšœ œ˜Lšœ-˜-Lšœ-˜-šœœŸ˜=L˜*Lšœ0˜0Lšœ0˜0Lšœ!œ ˜/L˜—šœŸ1˜8Lšœ-˜-Lšœ.˜.Lšœ˜—L˜—L˜—L˜L˜—š žœœœœœ˜&Lšœœœœ˜!L˜—šžœ œT˜yL™3—šžœ œO˜vL™,L˜L˜—šž"œ œQ˜€L™.—šžœ œ‚˜¦L™,—šžœ œl˜˜Lšœ.™.—šžœœœ9˜OLšœœ˜6šœ˜Lšœ œ)˜8LšœB˜B—Lšœ˜—L˜L˜š žœœœœ œ˜Qšœ ˜Lšœ˜Lšœ˜Lšœ˜Lšœ˜Lšœœ˜ L˜——š žœœœ œœ!œ˜`Lšœ$™$Jšœ œ˜šœœ˜Jšœœ˜9Jšœœ˜8Jšœœ˜:Jšœœ˜:Jšœœ˜—Jšœ˜—š žœœœœ œ˜Tšœ ˜Lšœ!˜!Lšœ˜Lšœœ˜ L˜——š žœœœ œœ#œ˜cLšœ%™%Jšœ œ˜šœœ˜Jšœœ˜@Jšœœ˜