home *** CD-ROM | disk | FTP | other *** search
/ Falcon 030 Power 2 / F030_POWER2.iso / ST_STE / MAGS / ICTARI07.ARJ / ictari.07 / C / GEM_TUT / GEM.009 next >
Text File  |  1993-05-12  |  21KB  |  449 lines

  1.                         *PROFESSIONAL GEM*
  2.                            By Tim Oren
  3.             Column #9 - VDI Graphics: Lines and Solids
  4.  
  5.      This  issue  of ST PRO GEM is the first in a  series  of  two
  6. which  will explore the fundamentals of VDI graphics  output.   In
  7. this installment, we will take a look at the commands necessary to
  8. output simple graphics such as lines,  squares and circles as well
  9. as  more complex figures such as polygons.   The following episode
  10. will  take a first look at graphics text output,  with an emphasis
  11. on  ways  to  optimize its drawing speed.   It will  also  include
  12. another  installment  of ONLINE Feedback.   As usual,  there is  a
  13. download  with  this column.   You should find it under  the  name
  14. GEMCL9.C in DL3 of ATARI16 (PCS-58).
  15.  
  16.                         A  BIT  OF HISTORY
  17.  
  18.     One  of  the reasons that the VDI can  be  confusing  is  that
  19. drawing anything at all,  even a simple line,  can involve setting
  20. around four different VDI parameters before making the draw  call!
  21. (Given  the state of the GEM documents,  just FINDING them can  be
  22. fun!)  Looking backwards a bit sheds some light on why the VDI  is
  23. structured this way,  and also gives us a framework for organizing
  24. a discussion of graphics output.
  25.  
  26.      The GEM VDI closely follows the so-called GKS standard, which
  27. defines  capabilities  and calling sequences  for  a  standardized
  28. graphic  input/output system.   GKS is itself an evolution from an
  29. early system called "Core".   Both of these standards were born in
  30. the  days  when  pen plotters,  vectored  graphics  displays,  and
  31. minicomputers  were  the  latest items.   So,  if you  wonder  why
  32. setting  the drawing pen color is a separate command,  just  think
  33. back  a  few  years when it actually meant  what  it  says!   (The
  34. cynical   may   choose   instead  to  ponder   the   benefits   of
  35. standardization.)
  36.  
  37.      When  doing  VDI  output,  it helps if you pretend  that  the
  38. display screen really is a plotter or some other separate  device,
  39. which  has  its own internal parameters which you can set  up  and
  40. read  back.   The class of VDI commands called Attribute Functions
  41. let  you set the parameters.   Output Functions cause the "device"
  42. to  actually  draw  someone once it is  configured.   The  Inquire
  43. Functions let you read back the parameters if necessary.
  44.  
  45.      There  are two parameters which are relevant no  matter  what
  46. type of object you are trying to draw.   They are the writing mode
  47. and  the clipping rectangle.   The writing mode is similar to that
  48. discussed in the column on raster operations.   It determines what
  49. effect the figure you are drawing will have on data already on the
  50. screen.  The writing mode is set with the call:
  51.  
  52.           vswr_mode(vdi_handle, mode);
  53.  
  54.    (Vdi_handle,  here  and  below,  is the  handle  obtained  from
  55. graf_handle at the beginning of the program.  Mode is a word which
  56. may be one of:
  57.  
  58.           1 - Replace Mode
  59.           2 - Transparent Mode
  60.           3 - XOR mode
  61.           4 - Reverse Transparent Mode
  62.  
  63.      In  replace mode,  whatever is on the screen is  overwritten.
  64. If  you are writing characters,  this means the background of each
  65. character cell will be erased.
  66.  
  67.      In  transparent  mode,  only  the pixels directly  under  the
  68. "positive"  part  of the image,  that is,  where 1-bits are  being
  69. written, will be changed.  When writing characters, the background
  70. of the cell will be left intact.
  71.  
  72.      In XOR mode,  an exclusive or is performed between the screen
  73. contents and what is being written.   The effect is to reverse the
  74. image under areas where a 1-bit occurs.
  75.  
  76.      Reverse transparent is like transparent,  but with a "reverse
  77. color  scheme".   That  is,  only  places where a 0-bit is  to  be
  78. put   are  changed  to  the  current  writing  color.    When  you
  79. write  characters in reverse transparent (over white),  the effect
  80. is reverse video.
  81.  
  82.      The  other  common parameter is the clipping  rectangle.   It
  83. defines the area on the screen where the VDI is permitted to draw.
  84. Any output which would fall outside of this area is ignored; it is
  85. effectively a null operation.   The clip rectangle is set with the
  86. call:
  87.  
  88.           vs_clip(vdi_handle, flag, pxy);
  89.  
  90.    Pxy is a four-word array.   Pxy[0] and pxy[1] are the X  and  Y
  91. screen coordinates,  respectively,  of one corner of your clipping
  92. rectangle.    Pxy[2]  and  pxy[3]  are  the  coordinates  of   the
  93. diagonally opposite corner of the rectangle.   (When working  with
  94. the  AES,  use  of  a  GRECT to define  the  clip  is  often  more
  95. convenient.  The routine set_clip() in the download does this.)
  96.  
  97.      Flag is set to TRUE if clipping is to be used.  If you set it
  98. to  FALSE,  the  entire  screen is assumed to be fair  game.
  99.  
  100.      Normally,  you should walk the rectangle list for the current
  101. window to obtain your clipping rectangles.  (See ST PRO GEM #2 for
  102. more details.)  However, turning off the clip speeds up all output
  103. operations,  particularly text.  You may do this ONLY when you are
  104. absolutely certain that the figure you are drawing will not extend
  105. out of the top-most window, or out of a dialog.
  106.  
  107.                     THE LINE FORMS ON THE LEFT
  108.  
  109.      The  VDI  line  drawing  operations  include  polyline,  arc,
  110. elliptical  arc,  and rounded rectangle.   I'll first look at  the
  111. Attribute Functions for line drawing,  then go through the drawing
  112. primitives themselves.
  113.  
  114.      The  most  common used line attributes are color  and  width.
  115. The color is set with:
  116.  
  117.           vsl_color(vdi_handle, color);
  118.  
  119.    where color is one of the standard VDI color  indices,  ranging
  120. from  zero to 15.   (As discussed in column #6,  the  color  which
  121. actually appears will depend on the pallette setting of your ST.)
  122.  
  123.      The  line width may only be set to ODD positive  values,  for
  124. reasons  of  symmetry.   If you try to set an even value,  the VDI
  125. will take the next lower odd value.  The call is:
  126.  
  127.           vsl_width(vdi_handle, width);
  128.  
  129.      The  two  less  used line parameters are the  end  style  and
  130. pattern.   With  the  end style you can cause the output  line  to
  131. have rounded ends or arrowhead ends.  The call is:
  132.  
  133.           vsl_ends(vdi_handle, begin_style, end_style);
  134.  
  135.    Begin_style  and end_style are each words which  may  have  the
  136. values zero for square ends (the default),  one for arrowed  ends,
  137. or  two  for  rounded ends.   They determine the  styles  for  the
  138. starting and finishing ends of the line, respectively.
  139.  
  140.      The line pattern attribute can select dotted or dashed  lines
  141. as  well  as more complicated patterns.   Before  continuing,  you
  142. should  note one warning:  VDI line output DOES NOT compensate for
  143. pixel aspect ratio.  That is, the dashes on a line will look twice
  144. as long drawn vertically on a medium-res ST screen as they do when
  145. drawn horizontally.  The command for setting the pattern is:
  146.  
  147.           vsl_type(vdi_handle, style);
  148.  
  149.    Style  is  a word with a value between 1  and  7.   The  styles
  150. selected are:
  151.  
  152.           1 - Solid (the default)
  153.           2 - Long Dash
  154.           3 - Dot
  155.           4 - Dash, Dot
  156.           5 - Dash
  157.           6 - Dash, Dot, Dot
  158.           7 - (User defined style)
  159.  
  160.    The  user  defined  style is determined  by  a  16-bit  pattern
  161. supplied  by the application.   A one bit in the pattern  turns  a
  162. pixel on, a zero bit leaves it off.  The pattern is cycled through
  163. repeatedly,  using the high bit first.  To use a custom style, you
  164. must make the call:
  165.  
  166.           vsl_udsty(vdi_handle, pattern);
  167.  
  168.   before doing vsl_type().
  169.  
  170.      As  I  mentioned  above,   the  line  type  Output  Functions
  171. available are polyline,  circular and ellliptical arc, and rounded
  172. rectangle.   Each  has  its own calling sequence.   The call for a
  173. polyline is:
  174.  
  175.           v_pline(vdi_handle, points, pxy);
  176.  
  177.   Points tells how many vertices will appear on the polyline.  For
  178. instance,  a  straight  line has two vertices:  the  end  and  the
  179. beginning.   A closed square would have five,  with the first  and
  180. last  identical.    (There  is  no  requirement  that  the  figure
  181. described be closed.)
  182.  
  183.      The pxy array contains the X and Y raster coordinates for the
  184. vertices,  with a total of 2 * points entries.   Pxy[0] and pxy[1]
  185. are the first X-Y pair, and so on.
  186.  
  187.      If you happen to be using the XOR drawing mode, remember that
  188. drawing  twice  at  a point is equivalent to no  drawing  at  all.
  189. Therefore,  for  a figure to appear closed in XOR mode,  the final
  190. stroke  should actually stop one pixel short of the origin of  the
  191. figure.
  192.  
  193.      You  may  notice  that  in the GEM  VDI  manual  the  rounded
  194. rectangle  and arc commands are referred to as  GDPs  (Generalized
  195. Drawing Primitives).  This denotation is historical in nature, and
  196. has  no effect unless you are writing your own VDI bindings.
  197.  
  198.      The  rounded rectangle is nice to use for customized  buttons
  199. in  windows and dialogs.   It gives a "softer" look to the  screen
  200. than the standard square objects.  The drawing command is:
  201.  
  202.           v_rbox(vdi_handle, pxy);
  203.  
  204.    Pxy  is  a  four word array  giving  opposite  corners  of  the
  205. rectangle,  just as for the vs_clip() call.   The corner  rounding
  206. occurs  within  the  confines of  this  rectangle.   Nothing  will
  207. protrude  unless  you specify a line thickness greater  than  one.
  208. The  corner rounding is approximately circular;  there is no  user
  209. control over the degree or shape of rounding.
  210.  
  211.      Both the arc and elliptical arc commands use a curious method
  212. of  specifying  angles.   The units are tenths of degrees,  so  an
  213. entire  circle is 3600 units.   The count starts at ninety degrees
  214. right of vertical, and proceeds counterclockwise.  This means that
  215. "3 o'clock" is 0 units,  "noon" is 900 units,  "9 o'clock" is 1800
  216. units, and 2700 units is at "half-past".  3600 units take you back
  217. to "3 o'clock".
  218.  
  219.      The command for drawing a circular arc is:
  220.  
  221.           v_arc(vdi_handle, x, y, radius, begin, end);
  222.  
  223.    X  and y specify the raster coordinates of the  center  of  the
  224. circle.   Radius specifies the distance from center to all  points
  225. on the arc.   Begin and end are angles given in units as described
  226. above,  both with values between 0 and 3600.   The drawing of  the
  227. arc  ALWAYS  proceeds  counterclockwise,   in  the  direction   of
  228. increasing arc number.   So values of 0 and 900 for begin and  end
  229. would  draw  a  quarter circle from  "three  o'clock"  to  "noon".
  230. Reversing  the values would draw the other three quarters  of  the
  231. circle.
  232.  
  233.      A  v_arc()  command  which specifies a  "full  turn"  is  the
  234. fastest  way to draw a complete circle on the screen.   Be warned,
  235. however,  that  the circle drawing algorithm used in the VDI seems
  236. to  have  some  serious  shortcomings at  small  radii!   You  can
  237. experiment  with  the  CIRCLE primitive in  ST  Logo,  which  uses
  238. v_arc(), to see what I mean.
  239.  
  240.      Notice  that if you want an arc to strike one or more  given
  241. points on the screen,  then you are in for some trigonometry.   If
  242. your  math  is  a  bit  rusty,  I highly  recommend  the  book  "A
  243. Programmer's  Geometry",  by  Bowyer  and Woodwark,  published  by
  244. Butterworths (London, Boston, Toronto).
  245.  
  246.      Finally, the elliptical arc is generated with:
  247.  
  248.           v_ellarc(vdi_handle, x, y, xrad, yrad, begin, end);
  249.  
  250.  X,  y,  begin, and end are just as before.  Xrad and yrad give the
  251. horizontal and vertical radii of the defining ellipse.  This means
  252. that  the distance of the arc from center will be yrad  pixels  at
  253. "noon"  and  "half-past",  and it will be xrad pixels at "3 and  9
  254. o'clock".  Again, the arc is always drawn counterclockwise.
  255.  
  256.      There  are  a  number  of approaches  to  keeping  the  VDI's
  257. attributes "in sync" with the actual output operations.   Probably
  258. the  LEAST efficient is to use the Inquire Functions to  determine
  259. the  current  attributes.   For  this  reason,  I have  omitted  a
  260. discussion of these calls from this column.
  261.  
  262.      Another  idea  is  to keep a local copy  of  all  significant
  263. attributes, use a test-before-set method to minimize the number of
  264. Attribute  Functions which need to be called.   This puts a burden
  265. on  the programmer to be sure that the local  attribute  variables
  266. are correctly maintained.   Failure to do so may result in obscure
  267. drawing  bugs.   If  your  application employs  user  defined  AES
  268. objects, you must be very careful because GEM might call your draw
  269. code  in the middle of a VDI operation (particularly if  the  user
  270. defined objects are in the menu).
  271.  
  272.      Always  setting  the attributes is a simplistic  method,  but
  273. often   proves  most  effective.    The  routines  pl_perim()  and
  274. rr_perim()  in the download exhibit this  approach.   Modification
  275. for  other  primitives  is straightforward.   This style  is  most
  276. useful  when  drawing  operations  are  scattered  throughout  the
  277. program,  so that keeping track of the current attribute status is
  278. difficult.  Although inherently inefficient, the difference is not
  279. very  noticable if the drawing operation requested is itself  time
  280. consuming.
  281.  
  282.      In  many  applications,  such  as data graphing  programs  or
  283. "Draw"  packages,  the output operations are centralized,  forming
  284. the primary functionality of the code.   In this case,  it is both
  285. easy  and  efficient  to keep track of  attribute  status  between
  286. successive drawing operations.
  287.  
  288.                               SOLIDS
  289.  
  290.     There  are  a wider variety of VDI  calls  for  drawing  solid
  291. figures.   They  include rectangle or  bar,  disk,  pie,  ellipse,
  292. elliptical  pie,  filled rounded rectangle,  and filled  polygonal
  293. area.   Of course,  filled figure calls also have their own set of
  294. attributes which you will need to set.
  295.  
  296.      The  fill color index determines what pen color will be  used
  297. to draw the solid.  It is set with:
  298.  
  299.           vsf_color(vdi_handle, color);
  300.  
  301.      Color is just the same as for line drawing.   A solid may  or
  302. may not have a visible border.  This is determined with the call:
  303.  
  304.           vsf_perimeter(vdi_handle, vis);
  305.  
  306.    Vis is a Boolean.   If it is true,  the figure will be given  a
  307. solid one pixel outline in the current fill color index.   This is
  308. often  useful  to improve the appearance of solids  drawn  with  a
  309. dithered fill pattern.  If vis is false, then no outline is drawn.
  310.  
  311.      There are two parameters which together determine the pattern
  312. used  to  fill your figure.   They are called interior  style  and
  313. interior  index.   The style determines the general type of  fill,
  314. and the index is used to select a particular pattern if necessary.
  315. The style is set with the command:
  316.  
  317.           vsf_interior(vdi_handle, style);
  318.  
  319.   where style is a value from zero through four.   Zero selects  a
  320. hollow  style:  the  fill is performed in  color  zero,  which  is
  321. usually  white.   Style one selects a solid fill with the  current
  322. fill  color.   A style of two is called "pattern" and a  three  is
  323. called "hatch", which are terms somewhat suggestive of the options
  324. which can then be selected using the interior index.   Style  four
  325. selects the user defined pattern, which is described below.
  326.  
  327.      The  interior  index is only significant for styles  two  and
  328. three. To set it, use:
  329.  
  330.           vsf_style(vdi_handle, index);
  331.  
  332.    (Be careful here: it is very easy to confuse this call with the
  333. one  above  due  to the unfortunate choice of  name.)   The  index
  334. selects the actual drawing pattern.  The GEM VDI manual shows fill
  335. patterns corresponding to index values from 1 to 24 under style 2,
  336. and  from  1 to 12 under style 3.   However,  some  of  these  are
  337. implemented  differently on the ST.   Rather than try to  describe
  338. them all here, I would suggest that you experiment.  You can do so
  339. easily  in  ST Logo by opening the Graphics  Settings  dialog  and
  340. playing with the style and index values there.
  341.  
  342.      The user defined style gives you some interesting options for
  343. multi-color fills.  It is set with:
  344.  
  345.           vsf_udpat(vdi_handle, pattern, planes);
  346.  
  347.     Planes  determines the number of color planes in  the  pattern
  348. which  you  supply.   It  is  set to one  if  you  are  setting  a
  349. monochrome  pattern.   (Remember,  monochrome is  not  necessarily
  350. black).   It may be set to higher values on color systems: two for
  351. ST medium-res mode, or four for low-res mode.  If you use a number
  352. lower than four under low-res, the other planes are zero filled.
  353.  
  354.      The  pattern  parameter  is  an array of  words  which  is  a
  355. multiple  of  16 words long.  The pattern determined is 16  by  16
  356. pixels,  with each word forming one row of the pattern.   The rows
  357. are  arranged top to bottom,  with the most significant bit to the
  358. left.   If  you  have selected a multi-plane pattern,  the  entire
  359. first plane is stored, then the second, and so on.
  360.  
  361.        Note that to use a multi-plane pattern, you set the writing
  362. mode  to replace using vswr_mode().   Since the each plane can  be
  363. different,  you can produce multi-colored patterns.   If you use a
  364. writing   color  other  than  black,   some  of  the  planes   may
  365. "disappear".
  366.  
  367.      Most  of  the  solids Output Functions  have  analogous  line
  368. drawing commands.   The polyline command corresponds to the filled
  369. area primitive.  The filled area routine is:
  370.  
  371.           v_fillarea(vdi_handle, count, pxy);
  372.  
  373.     Count  and pxy are just the same as  for  v_pline().   If  the
  374. polygon  defined  by pxy is not closed,  then the VDI  will  force
  375. closure  with  a straight line from the last to the  first  point.
  376. The  polygon may  be concave or self-intersecting.   If  perimeter
  377. show is on, the area will be outlined.
  378.  
  379.      One  note of caution is necessary for both  v_fillarea()  and
  380. v_pline().   There is a limit on the number of points which may be
  381. stored in pxy[].   This limit occurs because the contents of pxy[]
  382. are copied to the intin[] binding array before the VDI is  called.
  383. You  can  determine  the maximum number of  vertices  by  checking
  384. intout[14] after using the extended inquire function vq_extnd().
  385.  
  386.      For  reasons unknown to this writer,  there are TWO different
  387. filled rectangle commands in the VDI.  The first is
  388.  
  389.           vr_recfl(vdi_handle, pxy);
  390.  
  391.     Pxy is a four word array defining two opposite corners of  the
  392. rectangle,  just  as  in  vs_clip().   Vr_recfl()  uses  the  fill
  393. attribute settings, except that it NEVER draws a perimeter.
  394.  
  395.      The other rectangle routine is v_bar(), with exactly the same
  396. arguments  as  vr_recfl().    The  only  difference  is  that  the
  397. perimeter  setting  IS  respected.   These two  routines  are  the
  398. fastest way to produce a solid rectangle using the VDI.   They may
  399. be  used in XOR mode with a BLACK fill color to quickly invert  an
  400. area  of  the screen.   You can improve the speed even further  by
  401. turning off the clip (if possible), and byte aligning the left and
  402. right edges of the rectangle.
  403.  
  404.      Separate commands are provided for solid circle and  ellipse.
  405. The circle call is:
  406.  
  407.           v_circle(vdi_handle, x, y, radius);
  408.  
  409.    and the ellipse command is:
  410.  
  411.           v_ellipse(vdi_handle, x, y, xrad, yrad);
  412.  
  413.     All of the parameters are identical to those given  above  for
  414. v_arc()  and v_ellarc().   The solid analogue of an arc is a  "pie
  415. slice".  The VDI pie commands are:
  416.  
  417.           v_pieslice(vdi_handle, x, y, radius, begin, end);
  418.  
  419.   for a slice from a circular pie, and
  420.  
  421.           v_ellpie(vdi_handle, x, y, xrad, yrad, begin, end);
  422.  
  423.    for a slice from a "squashed" pie.   Again,  the parameters are
  424. identical  to  those in v_arc() and  v_ellarc().   The  units  and
  425. drawing  order  of angles are also the  same.   The  final  solids
  426. Output Function is:
  427.  
  428.           v_rfbox(vdi_handle, pxy);
  429.  
  430.   which draws a filled rounded rectangle.   The pxy array  defines
  431. two  two  opposite  corners of the  bounding  box,  as  shown  for
  432. vs_clip().
  433.  
  434.      The  issues involved in correctly setting the VDI  attributes
  435. for a fill operation are identical to those in drawing lines.  For
  436. those  who  want to employ the "always set" method,  I have  again
  437. included  two  skeleton  routines in the download,  which  can  be
  438. modified as desired.
  439.  
  440.                          TO BE CONTINUED
  441.  
  442.    This concludes the first part of our expedition  through  basic
  443. VDI  operations.   The  next  issue will tackle  the  problems  of
  444. drawing  bit mapped text at a reasonable speed.   This first  pass
  445. will  not attempt to tackle alternate or  proportional  fonts,  or
  446. alternate font sizes.   Instead,  I will concentrate on techniques
  447. for  squeezing greater performance out of the standard  monospaced
  448. system fonts.
  449.