home *** CD-ROM | disk | FTP | other *** search
/ WADS of WADS / WadsOfWads.1994.zip / TEXT / DOOMTE.TXT < prev    next >
Text File  |  1994-04-09  |  23KB  |  516 lines

  1. >Newsgroups: comp.graphics.algorithms
  2. >From: brianm@vissci.demon.co.uk (Brian Marshall)
  3. >Path: news.delphi.com!noc.near.net!MathWorks.Com!europa.eng.gtefsd.com!howland.
  4. reston.ans.net!pipex!demon!dis.demon.co.uk!vissci.demon.co.uk!brianm
  5. >Subject: Doom Techniques
  6. >Organization: Visual Sciences Ltd.
  7. >Reply-To: brianm@vissci.demon.co.uk
  8. >X-Newsreader: Simple NEWS 2.0 (ka9q DIS 1.24)
  9. >Lines: 504
  10. >Date: Sun, 13 Feb 1994 12:56:02 +0000
  11. >Message-ID: <761144162snz@vissci.demon.co.uk>
  12. >Sender: usenet@demon.co.uk
  13.  
  14. ******************************************************************************
  15. *                       'Doom' 3D Engine techniques                          *
  16. ******************************************************************************
  17. By Brian 'Neuromancer' Marshall
  18. (Email: brianm@vissci.demon.co.uk)
  19.  
  20.         This document is submitted subject to certain conditions:
  21.  
  22. 1. This Document is not in any way related to Id Software, and is 
  23.    not meant to be representive of their techniques : it is based
  24.    upon my own investigations of a realtime 3d engine that produces
  25.    a screen display similar to 'Doom' by Id software.
  26.  
  27. 2. I take no responsibility for any damange to data or computer equipment
  28.    caused by attempts to implement these algorithms.
  29.  
  30. 3. Although I have made every attempt to ensure that this document is error
  31.    free i take no responsability for any errors it may contain.
  32.  
  33. 4. Anyone is free to use this information as they wish, however I would
  34.    appreciate being credited if the information has been useful.
  35.  
  36. 5. I take no responsability for the spelling or grammar.
  37.    (My written english is none too good...so I won't take offence
  38.     at any corrections: I am a programmer not a writer...)
  39.  
  40.         Right now that that little lot is out of the way I will start this
  41. document proper....
  42.  
  43. 1:  Definition of Terms
  44. ======================
  45.  
  46.         Throughout this document I will be making use of many graphical terms
  47. using my understanding of them as they apply to this algorithm. I will
  48. explain all the terms below. Feel free to skip this part....
  49.  
  50. Texture:
  51.         A texture for the purpose of this is a square image.
  52.  
  53. U and V:
  54.         U and V are the equivelants of x and y but are in texture space.
  55. ie They are the the two axies of the two dimensional texture.
  56.  
  57. Screen:
  58.         For my purposes 'screen' is the window we wish to fill: it doesn't
  59. have to be the whole screen.
  60.  
  61. Affine Mapping:
  62.         A affine mapping is a texture map where the texture is sampled
  63. in a linear fashion in both U and V.
  64.  
  65. Biquadratic Mapping:
  66.         A biquadratic mapping is a mapping where the texture is sampled
  67. along a curve in both U and V that approximates the perspective transform.
  68. This gives almost proper forshortening.
  69.  
  70.  
  71. Projective Mapping:
  72.         A projective mapping is a mapping where a changing homogenous
  73. coordinated is added to the texture coordinateds to give (U,V,W) and
  74. a division is performed at every pixel. This is the mathematically and
  75. visual correct for of texture mapping for the square to quadrilateral
  76. mappings we are using.
  77.         (As an aside it is possible to do a projective mapping without
  78. the divide (or 3 multiplies) but that is totally unrelated to the matter
  79. in hand...)
  80.  
  81. Ray Casting:
  82.         Ray Casting in this context is back-firing 'rays' along a two
  83. dinesional map. The rays do however follow heights... more on that later
  84.  
  85. Sprite:
  86.         A Sprite is a bitmap that is either a monster or an object. To
  87. put it another way it is anything that is not made out of wall or
  88. floor sectins.
  89.  
  90. Sprite Scaling:
  91.         By this I mean scaling a bitmap in either x or y or both.
  92.  
  93. Right... Now thats over with onto the foundation:
  94.  
  95. 2:   Two Dimensional Ray Casting Techniques
  96. ===========================================
  97.  
  98.         In order to make this accessible to anyone I will start by
  99. explaining 2d raycasting as used in Wolfenstein 3d style games.
  100.  
  101.   2.1: Wolfenstien 3D Style Techniques...
  102.   =======================================
  103.  
  104.           Wolfenstein 3d was a game that rocked the world (well me anyway!).
  105.   It used a technique where you fire a ray accross a 2d grid based map to
  106.   find all its walls and objects. The walls were then drawn vertically
  107.   using sprite scaling techniques to simulate texture mapping.
  108.  
  109.           The tracing accross the map looked something like this;
  110.  
  111.  
  112.         =============================================
  113.         =   =   =   =   =   =  /=   =   =   =   =   =
  114.         =   =   =   =   =   = / =   =   =   =   =   =
  115.         =   =   =   =   =   =/  =   =   =   =   =   =
  116.         ====================/========================
  117.         =   =   =   =   =  /=   =   =   =   =   =   =
  118.         =   =   =   =   = / =   =   =   =   =   =   =
  119.         =   =   =   =   =/  =   =   =   =   =   =   =
  120.         ================/============================
  121.         =   =   =   =  /#   =   =   =   =   =   =   =
  122.         =   =   =   = / #   =   =   =   =   =   =   =
  123.         =   =   =   =/  #   =   =   =   =   =   =   =
  124.         ============/===#########====================
  125.         =   =   =  /=   =   =   #   =   =   =   =   =
  126.         =   =   = / =   =   =   #   =   =   =   =   =
  127.         =   =   =/  =   =   =   #   =   =   =   =   =
  128.         ========/===============#====================
  129.         =   =  /=   =   =   =   #   =   =   =   =   =
  130.         =   = P =   =   =   =   #   =   =   =   =   =
  131.         =   =  \=   =   =   =   #   =   =   =   =   =
  132.         ========\===============#====================
  133.         =   =   =\  =   =   =   #   =   =   =   =   =
  134.         =   =   = \ =   =   =   #   =   =   =   =   =
  135.         =   =   =  \=   =   =   #   =   =   =   =   =
  136.         ============\=======#####====================
  137.         =   =   =   =\  =   #   =   =   =   =   =   =
  138.         =   =   =   = \ =   #   =   =   =   =   =   =
  139.         =   =   =   =  \=   #   =   =   =   =   =   =
  140.         ================\===#========================
  141.         =   =   =   =   =\  #   =   =   =   =   =   =
  142.         =   =   =   =   = \ #   =   =   =   =   =   =
  143.         =   =   =   =   =  \#   =   =   =   =   =   =
  144.         =============================================
  145.  
  146.         (#'s are walls, = is the grid....)
  147.  
  148.         This is just a case of firing a ray for each vertical
  149.   line on the screen. This ray is traced accross the map to
  150.   see where it crosses a grid boundry. Where it crosses a
  151.   boundry you cjeck to see if there is a wall there we see how
  152.   far away it it and draw a scaled vertical line from the texture
  153.   on screen. The line we draw is selected from the texture by
  154.   seeing where the line has intersected on the side of the square it
  155.   hit.
  156.         This is repeated with a ray for each vertical line on the
  157.   screen that we wish to display.
  158.         This is a very quick explaination of how it works missing
  159.   out how the sprites are handled. If you want a more detailed 
  160.   explaination then I suggest getting acksrc.zip from
  161.   ftp.funet.fi in /pub/msdos/games/programming
  162.  
  163.         This is someone's source for a Wolfenstien engine written
  164.   in Borland C and Assembly language on the Pc.
  165.         Its is not the fastest or best but has good documentation
  166.   and solves similiar sprite probelms, distance probelms and has
  167.   some much better explaination of the tracing technique tahn I have
  168.   put here. I recommend to everyone interested taht you get a copy
  169.   and have a thorough play around with it.
  170.   (Even if you don't have a Pc: Everything but the drawing and video
  171.    mode setting is done in 'C' so it should not be too hard to port
  172.    ....)
  173.  
  174.  
  175.   2.2 Ray Casting in the Doom Environment
  176.   =======================================
  177.  
  178.         When you look at a screen from Doom you see floors, steps
  179.   walls and lots of other trappings.
  180.         You look out of windows and accross courtyards and you
  181.   say WOW! what a great 3d game!!
  182.         Then you fire your gun a baddie who's in line with you but
  183.   above you and bang! he's a corpse.
  184.         Then you climb up to the level where the corpse is and look
  185.   out the window to where you were and you say Gosh! a 3d game!!
  186.  
  187.         Hmmm....
  188.  
  189.         Stop gawping at the graphics for a minute and look at the map
  190.   screen. Nice line vectors. But isn't the map a bit simple???
  191.         Notice how depite colours showing you that there are different
  192.   heights. Then notice that despite the fact that there is NEVER a
  193.   place where you can exist on two different levels. Smelling a little
  194.   2d yet???
  195.         Look where there are bridges (or sort of bridges) : managed to
  196.   see under them yet??
  197.  
  198.         The whole point to this is that Doom is a 2D games just like
  199.   its ancestor Wolfenstein but it has rather more advanced raycasting
  200.   which does a very nice job of fooling the player into thinking its a
  201.   3d game that shifting loads of polygons and back-culling, depth
  202.   sorting etc... 
  203.  
  204.         Right the explaination of how you turn a 2d map into the 3d
  205.   doom screen is complex so if you are having difficulty try reading
  206.   it a few times and if all else fails mail me....
  207.  
  208.  
  209.   2.3 What is actually done!
  210.   ==========================
  211.  
  212.         Right to start with the raycasting is started in the same
  213.   way as Wolfenstien. That is find out where the player is in the 2d
  214.   map and get a ray setup for the first vertical line on the screen.
  215.  
  216.         Now we have an extra stage from the Wolfenstein I described
  217.   whcih involves a data srtucture that we will use later to actually
  218.   draw the screen.
  219.  
  220.         In this data structure we start the ray off as at the bottom
  221.   of the screen. This is shown in the diagram below;
  222.  
  223.         =================================
  224.         =                               =
  225.         =                               =
  226.         =                               =
  227.         =                               =
  228.         =                               =
  229.         =                               =
  230.         =                               =
  231.         =                               =
  232.         =                               =
  233.         =                               =
  234.         =                               =
  235.         =                               =
  236.         =                               =
  237.         =                               =
  238.         =                               =
  239.         =                               =
  240.         =*                              =
  241.         =================================
  242.  
  243.  
  244.         Where the '=' show the boundry of the screen and '*' is the virtual
  245.   position of the ray.
  246.  
  247.         Note: the Data structure is really two structures:
  248.         One which is a set of list for each vertical 'scanline' and
  249.         One which is a corresponding list for horizontal scanlines.
  250.  
  251.         Now we start tracing the ray. We skip accross the 2d map until
  252.   we hit something interesting. By something interesting I mean something
  253.   that is an actual wall or florr section edge.
  254.         Right we have hit the edge of either a floor or wall section.
  255.   We have several things to do know. These are;
  256.  
  257.         If it was a wall we hit:
  258.  
  259.   1: Find out how 'high' of screen this section of wall should be
  260.      due to the distance it is accross the 2d map.
  261.   2: Find out at what 'virtual height' it is: This is so that we can see
  262.      where in the vertical scanline in comes for testing where to insert
  263.      it and for clipping it.
  264.   3: Test in our structure to see if you draw it or not.
  265.      (This is done so that you can look through windows : how this works
  266.       will become apparent later.)
  267.   4: If any of the wall segment is visible then we find out where along
  268.      the texture we have hit it and write into the structure the area of
  269.      the screen it takes up as well as the texture, the point where we
  270.      have hit the texture and the size it should be on screen. (This is
  271.      so that we can draw it correctly even if the whole span is not on
  272.      screen.
  273.  
  274.  
  275.         If it was a floor section that we hit:
  276.  
  277.   1: Find out where on the vertical line we are working the floor section
  278.      that the ray has hit is. (We know the height of the the floor in the
  279.      virtual map (2d) and we know the height of the player and the distance
  280.      of the floor square from the player so it is easy).
  281.      As a side effect of this we now know the U,V value where the ray has
  282.      hit the floor square.
  283.  
  284.   2: Trace Accross the floor square till we hit the far edge of the floor
  285.      square : we then workout where this is on the vertical scanline using
  286.      the same technique as above. We now know the vertical span of the
  287.      floor section, and where on the span it is.
  288.  
  289.   3: We check to see if the span is visible on the vertical span.
  290.      If it is or part of it is used then we mark that part of the vertical
  291.      scanline as used.
  292.      We also have to make use of the horizontal buffer I mentioned. We
  293.      insert into this in 2 places. The first is the x coordinate of where
  294.      we hit the floor square into the y line where we where on the screen.
  295.      Phew got that bit?? We also insert here the U,V value which we knew 
  296.      from the tracing. (I told you we'd need it later....)                                                                
  297.  
  298.  
  299.         As you can see there's a little more to hiting a floor segment than
  300. a wall segment. Also note that a you exit a floor segment you may also hit
  301. a wall segment.
  302.  
  303.         Tracing the individual ray is continued until we hit a special kind
  304. of wall. This wall is marked as a wall that connects to the ceiling.
  305. This is one place to stop tracing this ray. However we can stop tracing early
  306. if we have found enough to fill the whole vertical scanline then we can stop
  307. whenevr we have done this.
  308.  
  309.         Next come a trick. I said we were tracing along a 2d map. Well I
  310. lied a bit. There are (In my implementation at least..) TWO 2d maps. One is
  311. basically from the floor along including all the 'floor' walls and everything
  312. up to and including the walls that join onto the ceiling. The other map
  313. is basically the ceiling (with anything coming down from the ceiling on it
  314. if you are doing this: this makes life a little more complex as I'll explain
  315. below..)
  316.         Now when we have traced along the bottom map and hit a wall that 
  317. connects to the ceiling then we go back and trace along the ceiling from
  318. the start to fill in the gaps. There is a problem with this however.
  319. The problem is when you have things like a monolith or something else built
  320. out of walls jutting down from the ceiling. you have to decide whether to
  321. draw it or draw whatever was already in the scanline structure. This means
  322. either storing extra information in the buffer ie z coordinates or tracing
  323. along both the ceiling and floor at the same time.... for most people I would
  324. suggest just not having anything jutting down from the ceiling.
  325.         Also you could trace backwards instead of starting a new ray. This 
  326. would be fasterfor many cases as you wouldn't be tracing through lots
  327. of floor squares that aren't on screen. By tracing backwards you can keep
  328. going up the vertical scanline and you know that you are on the screen. As
  329. soon as something goes off the top of the screen you can handle that and then
  330. stop tracing.
  331.  
  332.         Phew. has everyone got that???
  333.  
  334.         Now we just go back and fire rays up the rest of the vertical
  335. scanlines. Easy!!???
  336.  
  337.         At the end of this lot we have the necessary data in the two buffers
  338. to go back and draw the screen background.
  339. (There is one more thing done while tracing but I'll explain that later...)
  340.  
  341.  
  342.         Oh... one other thing... you have may want to change the raycasting
  343. a bit to subdivide the map... it helps with speed.
  344.         And don't forget the added complexity that walls aren't all at
  345. 90 degrees to each other...
  346.  
  347. 3: Drawing the walls and Why it works!!
  348. =======================================
  349.  
  350.         If you are familiar with Wolfenstein then please still read this
  351. as it is esential background to understanding the floor routine.
  352.  
  353.  
  354.         As all of you probably know the walls are drawn by scaling the line
  355. of the texture to the correct size for the screen. The information in the
  356. vertical buffer makes this easy. What you probably don't know is why this
  357. creates texture mapping that is good enough to fool us.
  358.  
  359.         The wall function is a Affine texture mapping. (well almost)
  360. Now affine texture mappings look abysmal unless you do quite a lot of
  361. subdivision (The amount needed varies according to the angle the projected
  362. square is at.). So why does the Doom technique work??
  363.  
  364.         Well when we traced the rays we found out exactly where along the
  365. side of the square we hit we were in relation to the width of the texture.
  366. This means that the top and bottom pixels of the scaled wall piece are
  367. calculated correctly. This means that we have effecively subdivided the
  368. texture along vertical scanlines and as the effective subdidvisons are
  369. calculated exactly with proper forshortening as a result of the tracing.
  370. So the ray casting has made the texture mapping easy for us.
  371.         (We have enough subdivision by this scanline effect as the wall
  372. only rotates about one axis and we have proper foreshortening.)
  373.  
  374.         This knowlege helps us understand how to do the floors and why
  375. that works.
  376.  
  377.         We can now draw all the wall segments by just looking at the buffer
  378. and drawing the parts marked as walls.(Skiping where we put in the bits used
  379. by the floor/ceiling bits: we draw them later.)
  380.  
  381. 4:  Drawing the Floor/Ceiling and why it works!
  382. ===============================================
  383.  
  384.         If you have grasped why the walls work then you have just about
  385. won for the floors.
  386.         We have the information needed to draw the floors from the horizontal
  387. buffer.
  388.         All we have to do is look at the horizontal spans in the buffer
  389. and draw them in all.
  390.         Each of these spans has 2 end coordinates for which we have
  391. exact texture coorinates. This tells us which line across the texture
  392. we have to step along to do an Affine or linear mapping.
  393.         This is shown below;
  394.  
  395.  
  396.         =================================
  397.         =                               =
  398.         =                               =
  399.         =                               =
  400.         =                               = U1,V1 (exit)
  401.         =                              **
  402.         =                           *** =
  403.         =                        ***    =
  404.         =                     ***       =
  405.         =                  ***          =
  406.         =               ***             =
  407.         =            ***                =
  408.         =         ***                   =
  409.         =       **                      =
  410.         =     **                        =
  411.         =   **                          =
  412.         = **                            =
  413.   U0,V0 **                              =
  414. (entry) =                               =
  415.         =                               =
  416.         =                               =
  417.         =                               =
  418.         =                               =
  419.         =                               =
  420.         =                               =
  421.         =================================
  422.  
  423. (apologies for the wonky line: it should be straight!!)
  424.  
  425.         Now...as the end coordinates are correct and the axis along
  426. which forshortening takes place is not involved (this is a fudge)
  427. we can step linearly along this line across the texture to approximate
  428. the mapping. (This is far easier than a proper texture map).
  429.         This is effectivly a wall lying on its side which works as the
  430. texture coordinates at the ends of the span have been calculated correctly.
  431. This is a benefit of the raycasting we used to find everything.
  432.         Easy huh??
  433.  
  434.  
  435. 5: Sprites
  436. ==========
  437.  
  438.         The Sprites are really quite easy to do. The basic technique is the
  439. same as used in Wolfenstein 3d.
  440.         This is done as follows:
  441.  
  442. When you enter a 'square' on the floor map you test to see if there are
  443. any sprites in the square. If there are you flag that sprite as visible
  444. and add it to a list of visible sprites.
  445.  
  446. When you have finished tracing and drawing the walls and floor you
  447. depth sort the sprites and draw them from the back to the front. (painters
  448. algorithm). The only complication in drawing them is that you have to check 
  449. buffer that has the walls in, in order to clip the sprites correctly.
  450.  
  451.         (If you're interested in Doom you can occasionally see large 
  452. explosions (ie BFG) slip partially behind a wall segment.)
  453.  
  454.         On possibly faster way of handling the sprites would be to mark
  455. them like wall segments as you find them in the buffer. The only (ONLY!)
  456. complication to this approach is that sprites can have holes in them. By
  457. this I mean things like the gap between an arm and a leg which should be 
  458. the background colour.
  459.  
  460.  
  461. 6: Lighting and Depth Cueing
  462. ============================
  463.  
  464.         Lighting and Depth Cueing fits nicely in with the way that we have
  465. prepared the screen ready for drawing.
  466.         All we have to do is see how far away we are when we found either
  467. the floor or wall section and set the light level according to the distance.
  468.         The other thing that is applied is a light level. This is taken from
  469. the map at the edges where you have hit something. As the map is 2D it is
  470. easy to manage lighting, flickering etc.
  471.         For things like pools of light on the floor all you have to do
  472. is subdivide that patch of floor so that you can set the bit under the 
  473. skylight to a lighter colour. Its also very easy to frig this for the
  474. lighting goggles.
  475.  
  476.  
  477. 7: Controlling the Baddies
  478. ==========================
  479.         
  480.  
  481.         This is pretty easy: all you have to think about is moving and
  482. reacting on a 2d map. the only complications are things like the monsters
  483. looking through windows and seeing a player but this all degenerates into
  484. a simple 2d problem. Things like deciding whether the player has been hit or
  485. has he/she hit a monster is just another case of firing a ray. (Or do it
  486. another way...)
  487.  
  488.  
  489. 8: Where next???
  490. ================
  491.  
  492.         Thats all folks... hopefully a useful and intersting insight into
  493. my Doom engine works.
  494.         As to the question where next... well I already have some enhancements
  495. to my Doom enigine and others are in the works...
  496.  
  497. Some of what you may eventually see are:
  498.  
  499.         Proper lighting (I have done this already...its easier than you
  500.                         think)
  501.         Non-Vertical walls (i.e. Aliens style corridors...)
  502.         Orgranic Walls (i.e. Curved like the Aliens nest...)
  503.         Fractal Landscapes (This one is still very much a theory but how
  504.                         about being able to go outside and walk up and down
  505.                         hills etc??)
  506.  
  507.         If there are bits people are really shaky about I may post a new
  508. version of this... but I cannot get into implimentation issues as all
  509. implementation work is under copyright...
  510.  
  511.         By the way if anyone out there implements this I'd love to here
  512. how you get on...
  513.  
  514.         Anyone got any comments or any other interesting algorithms???
  515.  
  516.