home *** CD-ROM | disk | FTP | other *** search
/ 1,001 Nights of Doom / 1001NightsOfDoom1995wickedSensations.iso / text / doomeng.zip / DOOMENG.TXT
Text File  |  1994-03-29  |  24KB  |  525 lines

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