[comp.sys.mac.programmer] Grad3D and What I Discovered Therein - part 2

mlab2@kuhub.cc.ukans.edu (11/28/90)

Graf3D and What I Discovered Therein - part 2

Yeah, part 1 was pretty much header info.  But, some people may not have it. 
Yes, I will present some code for setting up the 3D port and using it.  Wait
until about part 4 or 5 or so.  This is preliminary stuff to avoid a lot of
confusion later.

The only variable type I skipped over last time was the Port3D record.  Here it
is described:

Fields of the Port3D;
GPort      This is also documented as GrPort in the MPW docs.  In any event
           it is a pointer to the grafPort associated with the 3D port.  Thus
           standard Quickdraw calls to this port will show up with the 3D
           calls.  Want your 3D to appear white on black?  You can just
           InvertRect the portRect field of the G(r)Port field of your Port3D.

viewRect   Viewing rectangle within the grafPort.  In the 3D-way of describ-
           ing a 'point of view', this rectangle corresponds to the base of
           the pyramid formed by the user's "eye" and the four corners of the
           screen.

xLeft,     World coordinates corresponding to the viewRect.  You shouldn't
yTop,      have to manipulate these variables directly.  The procedures for
xRight,    setting up the camera take care of these.
yBottom

pen        Like the Quickdraw pen, but with 3 dimensional coordinates.

penPrime   The pen location as transformed by the xForm matrix.  Thus, calls
           to Yaw, Roll, etc will affect this pen's location.  Again, you
           shouldn't have to manipulate this variable.

eye        When you call ViewAngle, this variable is established.  It repres-
           ents the 3D point in space where the apex of the viewing pyramid
           is fixed.

hSize,     This will store the half-width and half-height of the viewRect in
vSize      screen coordinates.
 
hCenter,   Center of the viewRect in screen coordinates.
vCenter

xCotan,    Graf3D sets these up as viewing cotangents (when you call ViewAngle)
yCotan     for calls to Clip3D.

Ident      When TRUE, the transforming routines are skipped.  This is set to
           TRUE when you set the xForm matrix to the identity matrix (non-
           rotated).

xForm      This is a 4x4 matrix of variables for transforming points.  All
           calls to Yaw(), Roll(), etc are applied to this matrix and then
           Graf3D uses the matrix to transform points in space.  Unless
           reset by Identity, this matrix will continue to represent the net
           result of all transformation calls.

Setting up a port:
As with programs using Quickdraw, a call to IntGraf3D(@thePort3D) must be made
during the initialization portion of your program.  Here is how I've
initialized my 3D port:

var
  gPort3D                   :Port3D;
  gPort3DPtr                :Port3DPtr;
  origin                    :Point3D;
  viewScreen                :Rect;

begin
  InitGraf3D(@thePort3D);
  Open3DPort(@gPort3D);
  SetRect(viewScreen,100, 20, 348, 144);
  ViewPort(viewScreen);
  LookAt(-983040, 655360, 983040, -655360);
  ViewAngle(1604480);
  SetPt3D(origin, 0, 0, 0);
  gPort3D.eye:= origin;
  FillRect(gPort3D.viewRect, black);
  ...
end;

This will set up a small 248x124 3D port in the center of a 7-inch Mac screen. 
As well, it sets the eye at the 'origin' in space looking straight ahead
parallel to the "ground".  Finally it fills it black to look like outer space. 
Those lengthy numbers used in LookAt and ViewAngle are fixed type numbers.  The
ViewAngle (1604480) for example, represents an angle of 24 degrees
(1604480/65536).
Perhaps this is a good time to explain the view angles.  The view angle defines
the amount of perspective given to 3D points and lines when drawn to the
screen.  An angle of 0 represents no perspective (or orthographic) projection. 
10 degrees is similar to the perspective seen through a telephoto lens, 25
degrees is generally accepted as 'normal' perspective of the human eye, and 80
degree would give you a distorted 'fisheye'-like wide angle perspective.

Enough of Part 2.  Stay tuned for more.  Note: I can't get MAIL to work right. 
I can READ but can't reply.

john calhoun