[comp.sys.mac] 3-D Graphics

dr%mac-teacher@freedom.msfc.nasa.gov (Dan C. Richard) (06/07/91)

In article <steveh.675615526@tasman>, steveh@tasman.cc.utas.edu.au (Steven Howell) writes:
> 
> 
> G'day folks,
> 	I have a bit of a query concerning 3D GrafPorts. In the manual to
> THINK pascal, it mentions that to use "advanced quickdraw routines" you
> should include the appropriate libraries. One of these libraries is
> "Graf3D.lib". My question is, how do you use it??? How does one create
> a new window that has a Graf3DPort, not a GrafPort? I can't find any
> mention of this unit in Inside Mac I-V... what's the story? Does any
> documentation of these 3D routines exist? Help!!!
> 
> Thanks in advance,
> steve h.
> 
> 

Here is the beginnings of an article I started to write about Graf-3D:
FPGraf3D:

A Floating Point version of Graf3D

by Dan C. Richard
Copyright 1990-1991 by Dan C. Richard


Dan C. Richard is a course developer, instructor and software developer for the Macintosh. 
Trained as a compiler writer from Georgia Tech, he worked two years at Perkin-Elmer Data 
Systems (now Concurrent Computers) and a year at NCR in their compiler development groups. 
He spent the next five years at TRW in the development of CASE tools. The next three years were 
in the research of CASE tools and their user interfaces at General Research Corporation (GRC). 
The last two years he has been GRC's corporate Macintosh Wizard. He can be reached at GRC in 
Huntsville, AL.


Introduction


FPGraf3D is a Mac II oriented reimplementation of Apple's Graf3D. The original Graf3D is a 
library of graphics routines that support the primitives for 3 dimensional graphics. To give good 
performance, Graf3D used 32 bit fixed point arithmetic and was coded in hand optimized assembly 
code.

FPGraf3D provides several features different from Graf3D. First, FPGraf3D uses 32 bit 
floating point numbers. Second, FPGraf3D is written in Think Pascal source. Third, FPGraf3D is 
written for code clarity over performance. The intent of these changes is to make the understanding, 
use and debugging of 3D graphics code much easier. An example is to first develop a application 
using FPGraf3D and then later convert to Graf3D. This allows code to be developed and running 
before using the fixed point arithmetic.


The use of single precision floating point provides a wider dynamic range than 32 bit fixed point 
numbers. The THINK Pascal with the 68881/2 coprocessor informs the programmer of numerical 
problems with the data overflow or underflow. The 32 bit fixed point toolbox routines do not return 
errors of overflow or underflow. As a source code unit, the user can turn on or off debugging and 
range checking and tracing of the graphics routines. The code used in FPGraf3D is derived from 
algorithms described in Principles of Interactive Computer Graphics [Newman 79] with 
supplementary material from Fundamentals of Interactive Computer Graphics [Foley 82]. 
Optimization of the original code is kept to a minimum. This helps bridge between the text book 
descriptions and the running code.


Originals of FPGraf3D's interface, algorithms, and code


The definition of FPGraf3D interface come from Graf3D.Txt that describe the data structures and 
procedure interfaces. Scott Berfield's article Understanding Graf3D [Berfield 1987] provided much 
of the understanding on how to use Graf3D. Algorithms came from Principles of Interactive 
Computer Graphics by Newman and Sproull [Newman 79] and Fundamentals of Interactive 
Computer Graphics by Foley and Van Dam [Foley 82]. These two text where the "state of the art" 
computer graphics text in the early 80's when Graf3D was originally written. To follow the code, 
these text must available. The final source of information was a disassembly of Graf3D. This 
disassembly was examined for confirmation of the algorithms and data structures that were not 
addressed in the written descriptions.


Key Routines: Clip3D


The two key routines of FPGraf3D (and Graf3D) are Clip3D and Transform. The Clip3D 
routine inputs the two 3 D endpoints of a line and then projects and clips the line into the window 
coordinates. Transform applies the 3 D matrix that contains translation, rotation and skew 
information to a 3 D data point. The remaining routines provide support and house keeping to make 
these two routines work.


 Clip3D routine is found in Principles of Interactive Computer Graphics, chapter 22, 
page 345. Several modifications were added to Clip3D code. The first was to add the code to 
covert the coordinates into an eye centered, left handed coordinate system. Next was to multiply the 
the x and y coordinates by the distance from the eye point to display over one half the display width. 
This places the endpoints in a clipping coordinate system. This is accomplished in the routine 
DoverSandnegZ routine. The second change was to apply the perspective transform after the line 
had been clipped. This was accomplished in the routine Perspective. The clipping coordinate 
transform is remove and the data points are scaled to screen coordinates.


The third and final modification to Clip3D was to remove a infinite loop. Clip3D clips to true 
pyramid. The "industrial strength" clipping volume is a pyramid frustum. There are numerical 
problems with data points outsize the clipping volume. When a data point is near the apex of the of 
the volume there seems to be the most problems. I have had both Graf3D and FPGraf3D go into an 
infinite loop on certain input values. The whole reason I spent the time to study Graf3D and write 
FPGraf3D was to find out what were the data values that cause these problems. There Clip3D 
algorithm has a "bug". A counter is implemented to prevent the clipping loop from cycling more than 
4 times. In theory a 3D line can be clipped by the viewing pyramid no more than 2 times. As an 
engineering "fudge" I put the loop limit to twice that.



Key Routine: Transform


Transform applies the 3D matrix to a data point. The 3D matrix, called xForm, contains the 
current rotation, translation, scaling and skew values to be applied to a data point. The routines 
Yaw, Pitch and Roll modify xForm for their rotations. These rotations are positive in the 
clockwise direction in FPGraf3D and Graf3D. The description of each type of rotation matrix is 
described on page 335 of Newman 79]. The footnote on page 255 of [Foley 82] states they use a 
counter clockwise for positive angles. This is the opposite of what [Newman 79] and of Graf3D 
uses. This sense of positive rotation can cause very subtle problems as you reference different 
computer graphics books.

The routines Translate


How to Use the Program


Pretty Forest is used to draw tree 


Conclusion


Pretty Forest is essentially an editor. P


Acknowledgments


The foundations 


Glossary


be found and explained in Principles of Interactive Computer Graphics by Newman and 
Sproull [Newman 79] and Fundamentals of Interactive Computer Graphics by Foley 
and Van Dam [Foley 82]. This gives the user a bridge between the theoretical to the principal 
application of 3D graphics principles. With this foundation the user can extended FPGraf3D to 
his needs. As an example, the arbitrary placement of the eye point is presented based on the 
explanation from Three Dimensional Computer Graphics by Alan Watt [Watt 89].


References


[Berfield 87] Berfield, Scott; Understanding Graf3D, MacTutor, Volume 3 No 3, pages 43 to 53, 
March 1987.

[Foley 81] Foley, James D. and Van Dam, Andries; Fundamentals of Interactive Computer 
Graphics, first edition, Addison-Wesley, 1981.

[Newman 79] Newman, William M. and Sproull, Robert F; Principles of Interactive 
Computer Graphics, second edition, McGraw-Hill, 1979.

[Watt 89] Watt, Alan; Fundamentals of Three-Dimensional Computer Graphics, Addison-
Wesley, 1989.


[[Knaster 88] Knaster, Scott; How to Write Macintosh Software, 2nd edition, Hayden, 1988.