threedkit(7) Svgalib User Manual threedkit(7)NAMEthreedkit - a set of functions for 3D support.DESCRIPTIONThe 3dkit consists mainly of the following triangle func- tionsgl_striangle(3),gl_swtriangle(3),gl_triangle(3),gl_trigetcolorlookup(3),gl_trisetcolorlookup(3),gl_trisetdrawpoint(3),gl_wtriangle(3). Beware, these functions are not a direct part of the svgalib library. Instead their source is part of svgalib and can be found in thethreeDkit/subdirectory of the original svgalib distribution. However, it is not installed in the system by default, s.t. it is unclear where you can find it if your svgalib was installed by some linux distribution. In case of any such problem, simply get an svgalib distri- bution from the net. You even don't need to install it. Justmakein thethreeDkit/subdirectory. As of this writ- ing,svgalib-1.2.12.tar.gzis the latest version and can be retrieved by ftp fromsunsite.unc.eduat/pub/Linux/libs/graphicsandtsx-11.mit.eduat/pub/linux/sources/libswhich will most probably be mir- rored by a site close to you. The functions are defined in thetri.oandtriangl.ofiles (or their resp. sources) which you must link to your pro- gram.EXPLANATIONON3DKIT.CThis is main engine for 3D rendering. Program flow:1.The function called from outside of3dkit.cisTD_drawsolid. This first calculates the rotation matrix from the camera rotation angles (see below for more details). It then allocates memory for the temporary array for holding temporary coords in subsequently called functions. It also sorts the surfaces from furthest to closest; according to the distance of the centre grid-point of each surface from the camera. It also establishes whetherROTATE_OBJECToption is on and zero's the camera position if so --- this is for displaying the object at the screen centre like in a 3D CAD package, as apposed to virtual reality where the object can be anywhere and the actual camera position can move. In the case ofROTATE_OBJECTbeing on, although the camera position is zero, some distance has to be placed between the camera and the object (or else it would appear to be infinitely large on the screen). This is done using the variables_camwhich is initialized todistancewhich is set by the calling application. It then loops through each surface (ordering them in the way they were just sorted --- i.e. according tosortarrayindex- ing) and calls one of five graphic routines to write the 3D surface to the hardware.2.Assume thatTD_drawsolidthen callsTD_drawmesh. Here, each surface grid point is firstTD_trans-late'd into a 2D screen point and stored in thetemparray. There are obviously w(idth)*h(eight) points in the grid. Following, each line from the 2D temp array is drawn on the screen. To draw the surface, the cor- ner wishbone (two lines) from each grid square is drawn while advancing across and the down. After completing the scan, the furthest two edges of the surface must then be filled in, vis.: _ _ _ _ _ _ |_|_|_|_|_|_ |_|_|_|_|_|_ |_|_|_|_|_|_ |_|_|_|_|_|_ |_|_|_|_|_|_ | | | | | | To understand the object rotation, a knowledge of matrix multiplication is required. I once derived a camera rotation before I learned matrix computa- tion. It amounted to the same thing, but was unnec- essarily complicated to optimise.3.TD_translatecalled fromTD_drawmesh(and others) converts from the 3D grid point coordinate to the 2D screen coordinate using:(a)the three camera position coordinates, (or the single camera dis- tance value,s_cam, ifROTATE_OBJECTis set), and(b)the three camera rotation angles. However, the three camera rotation angles have already been con- verted into a rotation matrix whenTD_calc_rota-tion_matrixwas called byTD_draw_solid. To convert from a 3D coordinate to a 2D screen coordinate, the camera position (or more correctly, the position of the object from the camera) must first be added to each of the 3D grid coordinates. If the user has chosen to use 32 bit values for the discription of the surface, then these must be right shifted to the same size as the 16 bit case.x,yandznow hold the 3D position of the object relative to the camera centre (or in these terms, the centre of the video screenRIGHTONthe screen). The vector[xyz]must now be multiplied by the rotation matrix. Thextvalue must also have the camera distance,s_cam, added to it in case theROTATE_CAMERAis set (in which casex_cam,y_camandz_cam(the camera position) will be zero and insteads_camwill have a value to provide the nec- essary object-camera distance). A test is also made as to whether this value is zero or negative. In the case, the point is too close to the camera, or behind the camera, and must not be drawn. After the multiplication, the resulting vector[xtytzt]has been rotated to be aligned with screen. The vector is now adjusted for perspective by dividing theytandztvalues (horizontal and ver- tical respectively) by thextvalue (into the screen). Division is done bymuldiv64because the intermediate product is larger than 32 bits.xscaleandyscaleare factors that scale the image to size.posxandposyis just the centre of the screen, or more precisely: The exact position of the pinhole camera viewing the object.4.TD_calc_rotation_matrixcalculates the nine entries of the 3 by 3 matrix used inTD_translate. In order that only integer arithmetic is performed, these values are stored and used as integers. Since this matrix's entries are always between -1 and +1, they have to be integer left shifted to give them accuracy.TD_MULCONSTANTscales them to sufficient bits of accuracy before they are converted to inte- gers. This also means that results (of multiplications with them) have to be scaled down by the same amount. This scaling is inherent in the final mul- tiplication and division (muldiv64) done in theTD_translatefunction, so an extra division is not consumed. The rotation matrix effectively rotates the vector by the Eulerian anglesalpha,betaandgamma. These angles represent successive rotations about each of the 3D axes. You can test which angles do what by looking at the calling application. Their precise definitions are not all that important since you can get the keyboard to do the right thing with a little trial and error. Intrisics of drawing non-transparent surfaces... to be continued ?!SEEALSOvgagl(7),svgalib(7),gl_striangle(3),gl_swtriangle(3),gl_triangle(3),gl_trigetcolorlookup(3),gl_trisetcolor-lookup(3),gl_trisetdrawpoint(3),gl_wtriangle(3),plane(6),wrapdemo(6).AUTHORThis manual page was edited by Michael Weller <eowmob@exp- math.uni-essen.de>. The demos, the initial documentation and the whole threedkit stuff was done by Paul Sheer <psheer@icon.co.za>. Paper mail: Paul Sheer P O BOX 890507 Lyndhurst Johannesburg 2106 South Africa Donations (by check or postal order) will be appreciated and will encourage further development of this software. However this is strictly on a voluntary basis where this software falls under the GNU LIBRARY GENERAL PUBLIC LICENSE. Svgalib (>= 1.2.11) 2 Aug 1997 1