home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / viewrend.asm < prev    next >
Assembly Source File  |  1996-03-19  |  10KB  |  496 lines

  1.     TITLE    VIEWREND - RENDERER SUPPORT MATH IN ASSEMBLER
  2.  
  3.     COMMENT $
  4.  
  5. // 26/12/93 by Dave Stampe
  6. // All algorithms and code (c) 1993 by Dave Stampe
  7.  
  8. /*
  9.  This code is part of the REND386 project, created by Dave Stampe and
  10.  Bernie Roehl.
  11.  
  12.  Copyright 1992, 1993, 1994 by Dave Stampe and Bernie Roehl.
  13.  
  14.  May be freely used to write software for release into the public domain;
  15.  all commercial endeavours MUST contact BOTH Bernie Roehl and Dave Stampe
  16.  for permission to incorporate any part of this software into their
  17.  products!  Usually there is no charge for under 50-100 items for
  18.  low-cost or shareware, and terms are reasonable.  Any royalties are used
  19.  for development, so equipment is often acceptable payment.
  20.  
  21.  ATTRIBUTION:  If you use any part of this source code or the libraries
  22.  in your projects, you must give attribution to REND386, Dave Stampe,
  23.  and Bernie Roehl in your documentation, source code, and at startup
  24.  of your program.  Let's keep the freeware ball rolling!  No more
  25.  code ripoffs please.
  26.  
  27.  CONTACTS: dstampe@psych.toronto.edu, broehl@sunee.uwaterloo.ca
  28.  See the COPYRITE.H file for more information.
  29. */
  30.  
  31. This file computes all the mathematically fiddly stuff for
  32. rendering, and caches it in the viewport structures.  It
  33. also unpacks the viewport to static variables to make
  34. rendering more efficient.  These two steps permit efficient viewpoint
  35. and look angle changes while keeping the renderer configuration flexible
  36.  
  37.  
  38. /* Contact: dstampe@sunee.waterloo.edu */
  39.  
  40.         $
  41.  
  42.     .MODEL large
  43.     .386
  44.  
  45.     .DATA
  46.  
  47. include 3dstruct.inc     ; 3D structures
  48.  
  49. create_data equ 1    ; force data to be created
  50. include viewdata.inc    ; static data for rendering
  51.  
  52.  
  53. ;                /* tables for sphere object clipping:   */
  54. ; extern long sclip_C[800]; /* 1/sqrt(zoom^2 + 1) table           */
  55. ; extern long sclip_M[800]; /* zoom * C table  (table: i = 32*zoom) */
  56. ;                /* range: FOV = 2*atan(1/zoom)          */
  57. ;                /* or about 150 to 7 degrees         */
  58. ; /* thus: for smaller window, divide zoom by fraction of h. screen */
  59.  
  60.  
  61. extrn    _sclip_C:DWORD        ; tables for volume clip computations
  62. extrn    _sclip_M:DWORD
  63.  
  64.     .CODE RENDERER
  65.  
  66.  
  67. VS_COPY    MACRO    n            ;; macro to copy dword to temp
  68.     mov    eax,es:[bx].VP_&n
  69.     mov    _VS_&n , eax
  70.     ENDM
  71.  
  72. ;void render_set_view(VIEW *v)    /* copy viewport data to fast access area */
  73.  
  74. v    equ    [bp+8]          ; arguments
  75.  
  76.     PUBLIC    _render_set_view
  77.  
  78. _render_set_view    proc    far
  79.  
  80.     .386
  81.     push    ebp
  82.     mov    ebp,esp
  83.  
  84.     push    edx
  85.     push    ecx
  86.  
  87.     les    bx,DWORD PTR v
  88.  
  89.     mov    dx,es:[bx].VP_orientation
  90.     mov    _VS_orientation,dx
  91.  
  92.     mov    eax,es:[bx].VP_sx     ; unpack, mirror scaling factors
  93.     test    dx,XFLIP
  94.     je    pos_sx
  95.     neg    eax
  96. pos_sx:
  97.     mov    _VS_sx,eax
  98.  
  99.     mov    eax,es:[bx].VP_sy
  100.     test    dx,YFLIP
  101.     je    pos_sy
  102.     neg    eax
  103. pos_sy:
  104.     mov    _VS_sy,eax
  105.  
  106.     mov    eax,es:[bx].VP_scx    ; copy and scale matrix entries
  107.     mov    _VS_scx,eax
  108.     mov    eax,es:[bx].VP_scy
  109.     mov    _VS_scy,eax
  110.  
  111.     mov    eax,es:[bx].VP_xform00
  112.     mov    _VS_fact1,eax
  113.     imul    DWORD PTR _VS_sx
  114.     shrd    eax,edx,16
  115.     adc    eax,0
  116.     mov    _VS_sfac1,eax
  117.  
  118.     mov    eax,es:[bx].VP_xform01
  119.     mov    _VS_fact2,eax
  120.     imul    DWORD PTR _VS_sx
  121.     shrd    eax,edx,16
  122.     adc    eax,0
  123.     mov    _VS_sfac2,eax
  124.  
  125.     mov    eax,es:[bx].VP_xform02
  126.     mov    _VS_fact3,eax
  127.     imul    DWORD PTR _VS_sx
  128.     shrd    eax,edx,16
  129.     adc    eax,0
  130.     mov    _VS_sfac3,eax
  131.  
  132.     mov    eax,es:[bx].VP_xform10
  133.     mov    _VS_fact4,eax
  134.     imul    DWORD PTR _VS_sy
  135.     shrd    eax,edx,16
  136.     adc    eax,0
  137.     mov    _VS_sfac4,eax
  138.  
  139.     mov    eax,es:[bx].VP_xform11
  140.     mov    _VS_fact5,eax
  141.     imul    DWORD PTR _VS_sy
  142.     shrd    eax,edx,16
  143.     adc    eax,0
  144.     mov    _VS_sfac5,eax
  145.  
  146.     mov    eax,es:[bx].VP_xform12
  147.     mov    _VS_fact6,eax
  148.     imul    DWORD PTR _VS_sy
  149.     shrd    eax,edx,16
  150.     adc    eax,0
  151.     mov    _VS_sfac6,eax
  152.  
  153.     mov    eax,es:[bx].VP_xform20     ; Z entries unscaled
  154.     mov    _VS_fact7,eax
  155.     mov    _VS_sfac7,eax
  156.  
  157.     mov    eax,es:[bx].VP_xform21
  158.     mov    _VS_fact8,eax
  159.     mov    _VS_sfac8,eax
  160.  
  161.     mov    eax,es:[bx].VP_xform22
  162.     mov    _VS_fact9,eax
  163.     mov    _VS_sfac9,eax
  164.  
  165.     mov    eax,es:[bx].VP_xform30       ; copy view position
  166.     mov    _VS_iview_x,eax
  167.     mov    eax,es:[bx].VP_xform31
  168.     mov    _VS_iview_y,eax
  169.     mov    eax,es:[bx].VP_xform32
  170.     mov    _VS_iview_z,eax
  171.  
  172.     mov    cx,2 ;PRESCALEZ         ; copy, prescale clipping values
  173.  
  174.     mov    eax,es:[bx].VP_yon
  175.     mov    _VS_yon,eax
  176.     shl    eax,cl
  177.     mov    _VS_yon4,eax
  178.  
  179.     mov    eax,es:[bx].VP_hither
  180.     mov    _VS_hither,eax
  181.     shl    eax,cl
  182.     mov    _VS_hither4,eax
  183.  
  184.     mov    cx,2 ;PRESCALE
  185.  
  186.     mov    eax,es:[bx].VP_left
  187.     mov    _VS_left,eax
  188.     shl    eax,cl
  189.     mov    _VS_left4,eax
  190.  
  191.     mov    eax,es:[bx].VP_right
  192.     mov    _VS_right,eax
  193.     shl    eax,cl
  194.     mov    _VS_right4,eax
  195.  
  196.     mov    eax,es:[bx].VP_top
  197.     mov    _VS_top,eax
  198.     shl    eax,cl
  199.     mov    _VS_top4,eax
  200.  
  201.     mov    eax,es:[bx].VP_bottom
  202.     mov    _VS_bottom,eax
  203.     shl    eax,cl
  204.     mov    _VS_bottom4,eax
  205.  
  206.     mov    eax,es:[bx].VP_hsc      ; scaled centering factors
  207.     shl    eax,cl
  208.     mov    _VS_hsc,eax
  209.  
  210.     mov    eax,es:[bx].VP_vsc
  211.     shl    eax,cl
  212.     mov    _VS_vsc,eax
  213.  
  214.     VS_COPY    hsw
  215.     VS_COPY hsh
  216.  
  217.     VS_COPY left_C
  218.     VS_COPY left_M
  219.     VS_COPY right_C
  220.     VS_COPY right_M
  221.     VS_COPY top_C
  222.     VS_COPY top_M
  223.     VS_COPY bot_C
  224.     VS_COPY bot_M
  225.  
  226.     mov    ax,es:[bx].VP_xshift
  227.     mov    _VS_xshift,ax
  228.  
  229.     mov    ax,es:[bx].VP_yshift
  230.     mov    _VS_yshift,ax
  231.  
  232.     pop    ecx
  233.     pop    edx
  234.  
  235.     mov    esp,ebp
  236.     pop    ebp
  237.     ret
  238.  
  239. _render_set_view    endp
  240.  
  241.  
  242.  
  243.  
  244. ;                    /* compute screen and viewport   */
  245. ;                    /* factors.  These stay constant */
  246. ;                    /* over eye point changes        */
  247. ;void initialize_screen_factors(VIEW *v)
  248.  
  249. v    equ    [bp+8]          ; arguments
  250.  
  251. ti        equ    WORD PTR [bp-4]        ; locals
  252. bi        equ    WORD PTR [bp-8]
  253. li        equ    WORD PTR [bp-12]
  254. ri        equ    WORD PTR [bp-16]
  255. zoom        equ    DWORD PTR [bp-20]
  256. x_offset    equ    DWORD PTR [bp-24]
  257. y_offset    equ    DWORD PTR [bp-28]
  258.  
  259.  
  260. orient    equ    WORD PTR  es:[bx].VP_orientation ; viewport flip flags
  261. aspect    equ    DWORD PTR es:[bx].VP_aspect     ; viewport aspect ratio
  262.  
  263. hsh    equ    DWORD PTR es:[bx].VP_hsh     ; viewport screen dimensions
  264. hsw    equ    DWORD PTR es:[bx].VP_hsw
  265. hsc    equ    DWORD PTR es:[bx].VP_hsc     ; viewport scaling factors
  266. vsc    equ    DWORD PTR es:[bx].VP_vsc
  267. scx    equ    DWORD PTR es:[bx].VP_scx     ; viewport scaling factors
  268. scy    equ    DWORD PTR es:[bx].VP_scy
  269. sx    equ    DWORD PTR es:[bx].VP_sx      ; viewport scaling mantissa
  270. sy    equ    DWORD PTR es:[bx].VP_sy
  271.  
  272. xshift    equ    WORD PTR es:[bx].VP_xshift      ; viewport scaling exponent
  273. yshift    equ    WORD PTR es:[bx].VP_yshift
  274.  
  275. top    equ    DWORD PTR es:[bx].VP_top     ; viewport window clip
  276. bottom    equ    DWORD PTR es:[bx].VP_bottom
  277. left    equ    DWORD PTR es:[bx].VP_left
  278. right    equ    DWORD PTR es:[bx].VP_right
  279.  
  280.  
  281.     PUBLIC    _initialize_screen_factors
  282.  
  283. _initialize_screen_factors    proc    far
  284.  
  285.     .386
  286.     push    ebp
  287.     mov    ebp,esp
  288.     sub    sp,32
  289.  
  290.     push    edi
  291.     push    esi
  292.     push    edx
  293.     push    ecx
  294.  
  295.     les    bx,DWORD PTR v            ; viewport pointer
  296.  
  297.     mov    eax,es:[bx].VP_xoffset    ; load locals
  298.     mov    x_offset,eax
  299.     mov    eax,es:[bx].VP_yoffset
  300.     mov    y_offset,eax
  301.  
  302.     mov    eax,left    ; compute screen "center"
  303.     add    eax,right
  304.     shr    eax,1
  305.     add    eax,x_offset
  306.     mov    hsc,eax
  307.  
  308.     mov    eax,top
  309.     add    eax,bottom
  310.     shr    eax,1
  311.     add    eax,y_offset
  312.     mov    vsc,eax
  313.  
  314.     mov    eax,right    ; half-screen dimensions
  315.     sub    eax,left
  316.     shr    eax,1
  317.     mov     hsw,eax
  318.  
  319.     mov    eax,bottom
  320.     sub    eax,top
  321.     shr    eax,1
  322.     mov     hsh,eax
  323.  
  324.     mov    eax,es:[bx].VP_zoom     ; clip zoom range
  325.     cmp    eax,08000h        ; less than 0.5?
  326.     jge    clip_low
  327.     mov    eax,08000h
  328. clip_low:
  329.     cmp    eax,0100000h        ; greater than 16?
  330.     jle    clip_high
  331.     mov    eax,0100000h
  332. clip_high:
  333.     mov    zoom,eax
  334.  
  335.     test    orient,XFLIP
  336.     je    noxflip
  337.     neg    x_offset
  338. noxflip:
  339.     test    orient,YFLIP
  340.     je    noyflip
  341.     neg    y_offset
  342. noyflip:                      ;  COMPUTE EDGES OF WINDOW
  343.  
  344.     mov    eax,zoom      ;/* left pc = z*w/(w+xo) */
  345.     mov    edx,hsw
  346.     imul    edx
  347.     push    eax
  348.     push    edx
  349.  
  350.     mov    esi,hsw
  351.     add    esi,x_offset
  352.     idiv    esi
  353.     shr    eax,12
  354.     cmp    ax,799
  355.     jbe    clip_li
  356.     mov    ax,799
  357. clip_li:
  358.     mov    li,ax
  359.  
  360.     pop    edx
  361.     pop    eax
  362.     mov    esi,hsw       ;/* right pc = z*w/(w-xo) */
  363.     sub    esi,x_offset
  364.     idiv    esi
  365.     shr    eax,12
  366.     cmp    ax,799
  367.     jbe    clip_ri
  368.     mov    ax,799
  369. clip_ri:
  370.     mov    ri,ax
  371.  
  372.             ;/* compute vert. scale zoom */
  373.             ;/* zoom * hsw / hsh * (aspect/65536.0); */
  374.  
  375.     mov    eax,zoom           ; /* <16.16> */
  376.     mul    DWORD PTR aspect       ; /* <16.16> -> <32.32> */
  377.     shrd    eax,edx,16           ; /* -> <16.16> */
  378.     mul    DWORD PTR hsw
  379.     div    DWORD PTR hsh           ; /* still <16.16> */
  380.  
  381.     mov    edx,hsh      ; /* top pc = z*h/(h+yo) */
  382.     imul    edx
  383.     push    edx
  384.     push    eax
  385.  
  386.     mov    esi,hsh
  387.     add    esi,y_offset
  388.     idiv    esi
  389.     shr    eax,12
  390.     cmp    ax,799
  391.     jbe    clip_ti
  392.     mov    ax,799
  393. clip_ti:
  394.     mov    ti,ax
  395.  
  396.     pop    eax          ; /* bot pc = z*h/(h-yo) */
  397.     pop    edx
  398.     mov    esi,hsh
  399.     sub    esi,y_offset
  400.     idiv    esi
  401.     shr    eax,12
  402.     cmp    ax,799
  403.     jbe    clip_bi
  404.     mov    ax,799
  405. clip_bi:
  406.     mov    bi,ax
  407.                 ; now look up clipping volume slopes!
  408.     les    si,_sclip_C
  409.     mov    bx,li
  410.     shl    bx,2
  411.     mov    eax,es:[bx+si]
  412.     mov    bx,ri
  413.     shl    bx,2
  414.     mov    edx,es:[bx+si]
  415.     mov    bx,ti
  416.     shl    bx,2
  417.     mov    ecx,es:[bx+si]
  418.     mov    bx,bi
  419.     shl    bx,2
  420.     mov    edi,es:[bx+si]
  421.     les    bx,v
  422.     mov    es:[bx].VP_left_C,eax
  423.     mov    es:[bx].VP_right_C,edx
  424.     mov    es:[bx].VP_top_C,ecx
  425.     mov    es:[bx].VP_bot_C,edi
  426.  
  427.     les    si,_sclip_M
  428.     mov    bx,li
  429.     shl    bx,2
  430.     mov    eax,es:[bx+si]
  431.     mov    bx,ri
  432.     shl    bx,2
  433.     mov    edx,es:[bx+si]
  434.     mov    bx,ti
  435.     shl    bx,2
  436.     mov    ecx,es:[bx+si]
  437.     mov    bx,bi
  438.     shl    bx,2
  439.     mov    edi,es:[bx+si]
  440.     les    bx,v
  441.     mov    es:[bx].VP_left_M,eax
  442.     mov    es:[bx].VP_right_M,edx
  443.     mov    es:[bx].VP_top_M,ecx
  444.     mov    es:[bx].VP_bot_M,edi
  445.  
  446. ;            /* compute screen scaling factors  */
  447. ;            /* which are pseudo-floating point */
  448. ;            /* to maximize precision and range */
  449. ;            /* width sets overall scaling      */
  450. ;/* sx = v->hsw * (zoom/65536.0);                       */
  451. ;/* sy = v->hsw * (zoom/65536.0) * (v->aspect/65536.0); */
  452.  
  453.     mov    eax,zoom        ;/* <16.16>            */
  454.     mul    DWORD PTR aspect        ;/* <16.16> -> <32.32> */
  455.     shrd    eax,edx,16        ;/* -> <16.16>         */
  456.     mul    DWORD PTR hsw           ;/* <16.0> -> <48.16>  */
  457.     mov    scy, eax
  458.     bsr    ecx,eax                 ; comvert to "float"
  459.     sub    cx,15
  460.     jg    nofixs
  461.     xor    cx,cx
  462. nofixs:
  463.     shrd    eax,edx,cl        ;/* normalize to <16.16> */
  464.     mov    DWORD PTR sy,eax
  465.     add     cx,2 ;PRESCALEZ        ;/* and record shift */
  466.     mov    yshift,cx
  467.  
  468.     mov    eax,zoom        ;/* <16.16>            */
  469.     mul    DWORD PTR hsw           ;/* <16.0> -> <48.16>  */
  470.     mov    scx,eax
  471.     bsr    ecx,eax
  472.     sub    cx,15                   ; comvert to "float"
  473.     jg    nofixs2
  474.     xor    cx,cx
  475. nofixs2:
  476.     shrd    eax,edx,cl        ;/* normalize to <16.16> */
  477.     mov    DWORD PTR sx,eax
  478.     add    cx,2 ;PRESCALEZ
  479.     mov     xshift,cx        ;/* and record shift */
  480.  
  481.     pop    ecx
  482.     pop    edx
  483.     pop    esi
  484.     pop    edi
  485.  
  486.     mov    esp,ebp
  487.     pop    ebp
  488.     ret
  489.  
  490. _initialize_screen_factors    endp
  491.  
  492.  
  493.  
  494.     end
  495.  
  496.