home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsf / graphtext / cga / text0001.txt < prev   
Encoding:
Text File  |  1995-03-27  |  13.6 KB  |  356 lines

  1. A simple data example would be an array of SEG pointers that is
  2. "grown" as needed, in blocks of...say...512 segments at a time. 
  3. When you run out of available segments, you simply allocate another
  4. block, and give one up.
  5.  
  6. Here's some psudo-code for the GetSegment() routine:
  7.  
  8. SEG *GetSegment( void )
  9. {
  10.    if (there are no available segments)
  11.    {
  12.       GrowReservoir()
  13.    }
  14.     
  15.    Find an available segment
  16.    Decrease the number of available segments in the reservoir
  17.    Return the segment pointer
  18. }
  19.  
  20. The segment manager might consist of these routines:
  21.  
  22.     InitSManager()
  23.  
  24.         Allocates the default base number of segments for the reservoir
  25.         probably by calling GrowReservoir()
  26.  
  27.     UninitSManager()
  28.  
  29.         Frees all memory in use
  30.  
  31.     ResetSManager()
  32.  
  33.         Marks all segments in reservoir as "available"
  34.  
  35.     GetSegment()
  36.  
  37.         Find an available segment (call GrowReservoir() if none are
  38.         available), mark it as used, and return it's pointer.
  39.  
  40.     CopySegment()
  41.  
  42.         A simple memcpy() will do
  43.  
  44.     GrowReservoir()
  45.  
  46.         realloc() the reservoir's array of segments in increments of
  47.         some pre-determined number.  Use a #define for this number
  48.         for fine-tuning memory usage.
  49.  
  50. -------------------------------------------------------------------------------
  51. Q:  "What are some ways to improve the Insertion routine?"
  52. -------------------------------------------------------------------------------
  53.  
  54. Now, it's your turn.  Let's have some optimizations.  I'd love to see
  55. any code/psudo-code submissions to the FAQ!  Here are some things to
  56. consider:
  57.  
  58.   1.  Using a Binary-searchable tree instead of a linked list
  59.  
  60.   2.  Checking entire New segments for fully behind or in-front
  61.       before adding or quitting.
  62.  
  63.   3.  [this section not complete]
  64.  
  65. -------------------------------------------------------------------------------    
  66. Q:  "What about accuracy?"
  67. -------------------------------------------------------------------------------    
  68.  
  69. Considering the accuracy of splitting two 2D line segments, the
  70. accuracy is perfectly pixel accurate where as a 16-bit z-Buffer is
  71. only partially accurate for a 32-bit world coordinate system.
  72.  
  73. -------------------------------------------------------------------------------    
  74. Q:  "What kind of performance can I expect?"
  75. -------------------------------------------------------------------------------    
  76.  
  77. I can't say, at this time, exactly what sort of performance
  78. improvements to expect over z-Buffer.  It all depends on the
  79. application, the code used for the segment manager, and especially the
  80. insertion routine.  When I have some numbers, I'll include them here.
  81.  
  82. However, let me explain why I believe you can expect better results
  83. out of s-Buffering than z-Buffering in hardware.  No this dude ain't
  84. nuts, that's right, software that's faster than hardware.  Keep on
  85. readin'. :)
  86.  
  87. When you're rendering to the s-Buffer, if you're clever about it in
  88. your insertion routine, you can eliminate many segments before they
  89. get into the s-Buffer, so you've just eliminated a segment that your
  90. code would have spent VERY valuable time drawing.  Consider this
  91. example:
  92.  
  93.  
  94.                     +----------+    +----------+
  95.                     |4444444444|    |2222222222|
  96.                     |4444444444|    |2222222222|
  97.                  +------------------|2222222222|--+
  98.                  |111111111111111111|2222222222|11|
  99.                  |111111111111111111|2222222222|11|
  100.                  |111111111111111111|2222222222|11|
  101.                  |111111111111111111|2222222222|11|
  102.                  |111111111111111111|2222222222|11|
  103.                  +------------------|2222222222|--+
  104.                     |4444444444|    |2222222222|
  105.                     |4444444444|    |2222222222|
  106.                     |4444444444|    |2222222222|
  107.                     |4444444444|    |2222222222|
  108.                  +--|4444444444|------------------+
  109.                  |33|4444444444|333333333333333333|
  110.                  |33|4444444444|333333333333333333|
  111.                  |33|4444444444|333333333333333333|
  112.                  |33|4444444444|333333333333333333|
  113.                  |33|4444444444|333333333333333333|
  114.                  +--|4444444444|------------------+
  115.                     |4444444444|    |2222222222|
  116.                     |4444444444|    |2222222222|
  117.                     +----------+    +----------+
  118.  
  119.      [You wouldn't believe how long it took to draw that!]
  120.  
  121.  
  122. In the above example, the polygons are numbered.  Notice, I even used
  123. the age-old problem of recursively overlapping polygons.  s-Buffers
  124. handle it intuitively.
  125.  
  126. There are (roughly) 300 pixels in the above example.  200 of which
  127. overlap.  Using z-Buffering in hardware, you will have to draw 500
  128. pixels, keep in mind that you'll also have to track the z-coords for
  129. each pixel for use with the z-buffer hardware.  But we'll ignore that
  130. fact for now.
  131.  
  132. So you draw blast out 500 pixels.  This is your lowest-level code. 
  133. This is your fastest stuff.
  134.  
  135. You've just rendered a full-screen of polygons, and 40% of it was for
  136. nothing.  Jeesh! 40%!  What a waste!
  137.  
  138. Consider s-Buffers.  They immediately find that where you have an
  139. overlap in the example, there is no extra drawing to be done. Granted
  140. this takes a little time (not much, mind you).  But the savings is
  141. 40% of screen-space coverage!  You're simply NOT drawing those
  142. pixels!
  143.  
  144. There are other advantages, too.
  145.  
  146. s-Buffers allow you to cut your drawing to the significant pixels
  147. ONLY. You draw 300 pixels (without tracking the z-coord along the
  148. way).
  149.  
  150. Unless the hardware is drawing the stuff for you (which s-Buffers
  151. should be nicely adaptable to -- if the hardware supports scanline
  152. drawing of textures, shades, etc. rather than entire polygons) then
  153. you're still doing the drawing.  The z-Buffer hardware is only helping
  154. you out with your low-level code.  It's only doing something for
  155. you.  It's not reducing the number of pixels you draw, it's just
  156. doing some work for you.
  157.  
  158. s-Buffers not only reduce your workload by reducing the amount of
  159. screen space coverage you're drawing to, but it does the z-Buffering
  160. for you.  Just like hardware does.  Granted there is overhead for
  161. inserting segments, but nothing compared to the rendering of them to
  162. the screen only to find that they're not viewed.
  163.  
  164. So, in this example, you see about a 5/3 speed improvement.  It gets
  165. much better as you have more hidden polygons.  But, with z-Buffering,
  166. your performance just plummets with more hidden polys. It's an
  167. inverse non-linear scale to your disadvantage.
  168.  
  169. -------------------------------------------------------------------------------    
  170. Q:  "Yeah, so like, what about memory?"
  171. -------------------------------------------------------------------------------    
  172.  
  173. Since you've written a segment manager (which is, by definition -- a
  174. memory manager) you've got control.  You decide how much memory to
  175. allocate at any one point in time.  The smarter your segment manager
  176. is the better off you'll be.
  177.  
  178. But there is still the issue of how much memory to store the required
  179. segments in the list.
  180.  
  181. For simple gouraud shading, here's what I would expect a segment
  182. structure to look like:
  183.  
  184. typedef struct segment
  185. {
  186.  
  187.    short  start_x, end_x;       // starting and ending Xs
  188.    short  start_z, end_z;       // starting and ending Zs
  189.    char   start_s, end_s;       // starting and ending shade values
  190.    struct segment *Next;        // linker
  191.  
  192. } SEG;
  193.  
  194. I count 14 bytes per segment structure.  This means that if your
  195. average segment is 7 pixels or more (consider this in YOUR
  196. application), then you've saved memory over a 16-bit z-Buffer.
  197.  
  198. This does NOT include any in-efficiencies in your segment manager or
  199. insertion routine (see the insertion routine questions for details).
  200.  
  201. You may not be doing simple grayscale gouraud shading.  You might
  202. have colors, too.  Add a byte (or whatever your app requires) for
  203. color.
  204.  
  205. Then there's the texture mapping information.  This adds to the size,
  206. too -- possibly almost doubling the amount of memory required.  But
  207. in some games (especially Wolfenstien-style games), you can still
  208. find a tremendous memory advantage over z-Buffers since the segments
  209. will most likely be so long.
  210.  
  211. These are things to consider when using s-Buffers.  If you find that
  212. the s-Buffers will use more memory than z-Buffers, you've got a
  213. decision to make:  Speed or Memory?
  214.  
  215. -------------------------------------------------------------------------------    
  216. Q:  "What are some more possible advantages of s-Buffers?"
  217. -------------------------------------------------------------------------------    
  218.  
  219. As you render the s-Buffer, you'll be rendering the screen top-down,
  220. and each scanline from left to right.  I can't think of any obvious
  221. advantages this offers other than the fact that it makes your
  222. low-level drawing routine a tightly optimized muscle machine.
  223.  
  224. If you have any cool ideas to make use of this unique feature, please
  225. lemme have 'em!
  226.  
  227. It would be an easy task to add information to your s-buffer lists
  228. that allows you to determine which scanlines have changed since the
  229. previous render so that your screen updates only contain updates to
  230. the scanlines that have changed.  This will reduce the amount of time
  231. you spend updating the slow RAM on your video card (for PCs).
  232.  
  233. For more, see the next two sections on Masking and Transparencies.
  234.  
  235. -------------------------------------------------------------------------------    
  236. Q:  "What about masking?"
  237. -------------------------------------------------------------------------------    
  238.  
  239. Many games have dash-board masks or the like.  s-Buffers offer a
  240. clean way to handle this.
  241.  
  242. You can add a flag to your segment structures that can make it a
  243. masking segment.
  244.  
  245. This way, you can build a masking s-Buffer, use it as your starting
  246. point when you render, and insert segments into it, not letting them
  247. interfere with the masking segments.  This will remove the need to
  248. perform pixel-by-pixel masking as the image is drawn to the screen.
  249.  
  250. This will also prevent your low-level drawing routines from having to
  251. render pixels behind the mask.
  252.  
  253. -------------------------------------------------------------------------------    
  254. Q:  "What about transparencies?"
  255. -------------------------------------------------------------------------------    
  256.  
  257. By extending the masking idea a bit further, you can flag certain
  258. segments as transparent.  As you insert segments, any non-transparent
  259. segments that fall behind the transparent ones won't remove the
  260. transparent segments.  Any non-transparent segments that end up
  261. in-front of the transparent ones will clip the transparent segments.
  262.  
  263. This brings up the problem that some segments now overlap with
  264. transparent segments, making the Insertion routine that much more
  265. complex.
  266.  
  267. Note that transparencies modify the image behind them.  If these
  268. transparent segments are kept in order of left-right (following their
  269. non-transparent neighbors in back-front order) within the linked-list
  270. for a scanline, as the low-level drawing routine draws the
  271. transparent segments, the non-transparent ones will already have been
  272. drawn, and the transparency modifications will fall into place
  273. nicely.
  274.  
  275. -------------------------------------------------------------------------------    
  276. Q:  "What are the disadvantages?"
  277. -------------------------------------------------------------------------------    
  278.  
  279. If s-Buffers are used to store many small segments, of if you use an
  280. in-efficient Insertion routine, you might find that the memory
  281. requirements will be quite high.  I would expect that this would only
  282. be a concern in a small percentage of the applications out there.
  283.  
  284. There can be much difficulty in writing insertion routine.
  285.  
  286. s-Buffers won't work with some implementations of curved surfaces as
  287. a z-Buffer will.
  288.  
  289.  
  290.  
  291.  
  292. Path: bcc.ac.uk!uknet!EU.net!howland.reston.ans.net!news.sprintlink.net!swcbbs.com!swcbbs!josh.whiting
  293. From: josh.whiting@swcbbs.com (JOSH WHITING)
  294. Newsgroups: comp.graphics.algorithms
  295. Subject: Converting 3D coords to 2
  296. Message-ID: <8A4D57D.005F0023A5.uuout@swcbbs.com>
  297. Date: Sat, 04 Mar 95 23:25:00 -500
  298. Distribution: world
  299. Organization: Software Creations BBS
  300. Reply-To: josh.whiting@swcbbs.com (JOSH WHITING)
  301. References: <8A4D2CE.005F00239C.uuout@swcbbs.com>
  302. X-Newsreader: PCBoard Version 15.21
  303. X-Mailer: PCBoard/UUOUT Version 1.10
  304. Lines: 49
  305.  
  306. BK>  Hi!  I've been reading this group for a while, but this question
  307. BK>hasn't been asked, how do you convert 3d coords to 2d so you can display
  308. BK>them on the screen.  I want to write some simple 3D polygon routines and
  309. BK>I'm really not in the mood to delve into the realms of Z-Buffering
  310. BK>and texture mapping.  Is there a fast technique for converting 3D coords
  311. BK>to 2D?  What is the equation for such a convertsion?  Thanx in advance
  312. BK>for any help anyone can give me.
  313.  
  314.     I think this has definately been asked before <g> but here it is:
  315.  
  316.  ScreenX = X * DVS / (Z+DVS)
  317.  ScreenY = Y * DVS / (Z+DVS)
  318.  
  319.         DVS is a constant which represents the distance to the view
  320. screen.  I usually choose a value from 100 to 200 for DVS...
  321.    Picture it like this:
  322.  
  323.    dvs
  324.   |---|
  325.                    _             _
  326.   *\  |            |             |
  327.      \|            | ScreenX     |
  328.       *\           -             |
  329.       |  \                       | X
  330.       |    \                     |
  331.       |      \                   |
  332.       |        \                 |
  333.       -----------*-----          -
  334.  
  335.   |--------------|
  336.          z
  337.  
  338.     And the same diagram can be drawn replacing all the Xs with Ys.  It
  339. uses similar triangles if you remeber your algebra classes
  340.  
  341.     This is the most generic one I know of...  there is the simpler one:
  342.  
  343.    ScreenX = X/Z
  344.    ScreenY = Y/Z
  345.  
  346.     but I have not had much success with that one...
  347.  
  348.   L8r
  349. -Josh Whiting
  350. josh.whiting@swcbbs.com
  351. ---
  352.  þ OLX 2.1 TD þ ..Most taglines are written by losers trying to be cool.
  353.  
  354.  
  355.  
  356.