找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3032|回复: 2

重造轮子,把a5的NASM汇编光追第二弹改写成masm版本的

[复制链接]

30

主题

211

回帖

2792

积分

用户组: 版主

UID
1821
精华
7
威望
69 点
宅币
2172 个
贡献
206 次
宅之契约
0 份
在线时间
482 小时
注册时间
2016-7-12
发表于 2021-2-23 17:49:10 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 Ayala 于 2021-2-26 23:41 编辑

原帖 https://www.0xaa55.com/thread-26336-1-1.html


  1. ;******************************************************************************
  2. ; DOS Ray-tracing rendering Demo
  3. ; by 0xAA55
  4. ; 2021-2-17
  5. ;
  6. ; This program is a DOS `COM` program which is able to be run in native DOS
  7. ; system in Real mode x86 CPU
  8. ;******************************************************************************



  9. .model tiny
  10. .286
  11. ;option prologue:none
  12. ;option epilogue:none
  13. option casemap:none
  14. option prologue:prologuedef
  15. option epilogue:epiloguedef

  16. ;DEBUGMODE EQU 1

  17. Interlaced equ 1
  18. LightPow   equ 20
  19. NumSpheres equ 3

  20. WWWWW      equ 120
  21. MAXWW      equ 320

  22. HHHHH      equ 80
  23. MAXHH      equ 200

  24. OWAOH      = MAXWW * HHHHH + WWWWW

  25. include RAYTRC.inc


  26. ;std
  27. ;.data
  28. ;#

  29. ;shellcode
  30. .code
  31. _entry:
  32.         call Start
  33. ;#       
  34.        
  35.        
  36. _this   label tClass

  37. ;tConst struct
  38.         gF65536    dd 65536.0
  39.         gF30625    dd 3.0625
  40.         gFM175     dd -1.75
  41.         gFTimeFreq dd 1193186.0
  42.         gFBigValue dd 9999999.0
  43.         gFZero     dd 0.0
  44.         gF05       dd 0.5
  45.         gFM1       dd -1.0
  46.         gC2        dw 2
  47.         gC255      dw 255
  48.         gC256      dw 256
  49.         gC1000     dw 1000
  50. ;tConst ends
  51.        
  52.        
  53. ;tData struct
  54.         gDistEpsilon    dd 0.01
  55.         gDistEpsilon2   dd 0.02
  56.         gFogDistance    dd 100.0
  57.         gSavedVideoMode dw 0
  58.         gTempW          dw 0
  59.         gSizeOf_Sphere  dw sizeof tSphere
  60.         gRes            tPos <WWWWW,HHHHH>
  61. ;tData ends


  62. DitherMatrix label byte
  63.         dm_y = 0
  64.         REPEAT 16
  65.                 dm_x  = 0
  66.                 REPEAT 16
  67.                                 xor_val = dm_x XOR dm_y
  68.                                 dm_val =           (((xor_val AND 01h) SHR 0) SHL 7) OR (((dm_y AND 01) SHR 0) SHL 6)
  69.                                 dm_val = dm_val OR (((xor_val AND 02h) SHR 1) SHL 5) OR (((dm_y AND 02) SHR 1) SHL 4)
  70.                                 dm_val = dm_val OR (((xor_val AND 04h) SHR 2) SHL 3) OR (((dm_y AND 04) SHR 2) SHL 2)
  71.                                 dm_val = dm_val OR (((xor_val AND 08h) SHR 3) SHR 1) OR (((dm_y AND 08) SHR 3) SHR 0)
  72.                                 db dm_val
  73.                                 dm_x = dm_x + 1
  74.                 ENDM
  75.                 dm_y = dm_y + 1
  76.         ENDM


  77.         SampleDepth     EQU 12
  78.         MapCast_Iterate EQU 12

  79. ;tMapCastDist struct       
  80.         gLightRay        tLightRay  <<0,0,0>,<<0,0,0>,<1.0,-1.0, 1.0>>,0>
  81.         gMapCast    tMapCast  <<0,0,0>,<0,0,0>,0,0>
  82.         gMapDist    tMapDist  <<0,0,0>,0,0>
  83. ;tMapCastDist ends

  84.         gMask       tColor    <0, 0, 0>

  85.         gColorMix    tColorMix <<0.2, 0.5, 0.8>,\
  86.                       <1.0, 0.8, 0.6>,\
  87.                       <0.8, 0.9, 1.0>,\
  88.                       <0, 0, 0>\
  89.                       >
  90.         gCamPos     tPosition <0.0, 2.0, 7.0>

  91. ;tBB struct
  92.         gBB         tPosition < 2.5, 4.5, 3.5>
  93.         gBBneg      tPosition <-4.5,-0.5,-0.5>
  94. ;tBB ends

  95.         gPixel      tPixel    <<0, 0>,<0, 0, 0>>

  96.         gSpheres    tSphere << 0.0, 2.0, 0.0>, 2.0, <0.7, 0.9, 0.1>>, \
  97.                              << 1.0, 1.0, 2.0>, 1.0, <0.1, 0.7, 0.9>>,  \
  98.                              <<-3.0, 1.0, 0.0>, 1.0, <0.9, 0.1, 0.7>>
  99.                     
  100.         gReg       dw 8 dup(0)
  101.         gCheck     dw 0aa55h   ;data end for check
  102.        
  103. IF ($ - offset _this) NE (sizeof tClass)
  104. %   ECHO STRUCTCHECK
  105. %   ECHO  @CatStr(%(sizeof tClass) )
  106.         .ERR
  107. ENDIF
  108.                db '$',0
  109.        
  110. ;std
  111. ;.code
  112. ;org 100h
  113. ;#
  114. Start:
  115. ;shellcode
  116.         pop bx ;shellcode
  117.        
  118. ;std
  119.     ;lea bx,offset _this
  120.         assume bx:ptr tClass
  121.        
  122.         cmp [bx].Check, 0AA55h
  123.         mov al,'1'
  124.         jnz @F
  125.        
  126.         call SetupVideoMode  
  127.        
  128.         cmp [bx].Check, 0AA55h
  129.         mov al,'2'
  130.         jnz @F
  131.        
  132.         call RenderScreen
  133.        
  134.         cmp [bx].Check, 0AA55h
  135.         mov al,'3'
  136.         jnz @F
  137.        
  138.         call RestoreVideoMode
  139.         mov al,'4'

  140. @@: call @F
  141.         byte ' potato','$',13,10
  142. @@:        pop bx
  143.         mov [bx],al
  144.         mov dx,bx
  145.         mov ah,9
  146.         int 21h       
  147.        
  148.         ;Exit program
  149.         int 20h
  150.         ret
  151.        
  152. ;******************************************************************************
  153. ; Setup our specific palette to use our color-system.
  154. ; We use RGB:233 color format to encode the color index, and it's easier to
  155. ; implement the ordered-dithering algorithm to gain a better appearance.
  156. ;******************************************************************************
  157. SetupPalette proc
  158. assume si:error
  159. assume di:error       
  160.         ;Set up palette
  161.         mov bl, 0
  162.         mov cx, 256
  163.        
  164. LoopSetPalette:

  165.                 push cx
  166.                 mov dx, 03C8h
  167.                 mov al, bl ;The current color index
  168.                 out dx, al

  169.                 inc dl ;0x03C9 port

  170.                 ;Red: 2 bits
  171.                 and al, 03h
  172.                 mov bh, 55h
  173.                 mul bh
  174.                 out dx, al

  175.                 ;Green: 3 bits
  176.                 mov al, bl
  177.                 mov cl, 2
  178.                 shr al, cl
  179.                 and al, 07h
  180.                 mov bh, 49h
  181.                 mul bh
  182.                 out dx, al

  183.                 ;Blue: 3 bits
  184.                 mov al, bl
  185.                 mov cl, 5
  186.                 shr al, cl
  187.                 and al, 07h
  188.                 mul bh
  189.                 out dx, al

  190.                 ;Increase the color index
  191.                 inc bl
  192.                 pop cx
  193.         loop LoopSetPalette
  194. assume si:nothing
  195. assume di:nothing
  196.         ret
  197. SetupPalette endp


  198. ;******************************************************************************
  199. ; Setup video mode to 0x13: Graphic mode, 320x200 resolution, 256 colors.
  200. ; The VRAM buffer is 0xA000:0x0000
  201. ; Each byte of the buffer represents the color index of a palette.
  202. ; The previous video mode will be saved.
  203. ;******************************************************************************
  204. SetupVideoMode proc
  205. assume si:error
  206. assume di:error
  207.         ;Get current display mode
  208.        
  209.         ;push bx
  210.         mov [bx].R.bp_,bp
  211.         mov bp,bx
  212.        
  213.         mov ah, 0fh
  214.         int 10h  ;The bx data is corrupted
  215.        
  216.         mov bx,bp
  217.         mov byte ptr [bx].Data_.SavedVideoMode, al

  218.         ;Set display mode to 320x200 graphic 256-color mode
  219.         mov ax, 13h
  220.         int 10h   ;The bx data is corrupted
  221.        
  222.         mov bx,bp
  223.         call SetupPalette ;The bx data is corrupted
  224.        
  225.         ;pop bx
  226.         mov bx,bp
  227.         mov bp,[bx].R.bp_
  228.        
  229. assume si:nothing
  230. assume di:nothing
  231.         ret
  232. SetupVideoMode endp

  233. ;******************************************************************************
  234. ; Restore the video mode to the saved video mode.
  235. ; The saved video mode was saved by calling `SetupVideoMode`.
  236. ;******************************************************************************

  237. RestoreVideoMode proc
  238. assume si:error
  239. assume di:error

  240.         mov [bx].R.bp_,bp
  241.         mov bp,bx
  242.         ;Restore video mode
  243.         mov ax, [bx].Data_.SavedVideoMode
  244.         int 10h
  245.        
  246.         mov bx,bp
  247.         mov bp,[bx].R.bp_
  248.        
  249. assume si:nothing
  250. assume di:nothing
  251.         ret
  252. RestoreVideoMode endp
  253. ;******************************************************************************
  254. ; Pickup the best color for pixel position of `Pixel_X` and `Pixel_Y` with
  255. ; the color value of `Pixel_R` `Pixel_G` `Pixel_B`.
  256. ; Returns the best color index through register `AL`.
  257. ;******************************************************************************

  258. PickColor proc uses si

  259. assume di:error
  260.         mov al, byte ptr [bx].P.Pos.x ;[gPixel.Pos.x]
  261.         and al, 0fh
  262.         mov ah, byte ptr [bx].P.Pos.y
  263.         and ah, 0fh
  264.         mov cl, 4
  265.         shl ah, cl
  266.         or al, ah
  267.         mov ah, 0
  268.                
  269.         mov si,ax
  270.         mov al,[si][bx].DM
  271. ;        push bx
  272. ;        lea bx,[bx].DM
  273. ;        xlatb
  274. ;        pop bx
  275.        
  276.         mov dl, al
  277.         mov cl, 2
  278.         shr dl, cl
  279.         inc cl
  280.         shr al, cl
  281.        
  282.                
  283.         add byte ptr [bx].P.Col.R, dl
  284.         jnc @F
  285.         mov byte ptr [bx].P.Col.R, 0FFh
  286. @@:; .RedReady:
  287.         add byte ptr [bx].P.Col.G, al
  288.         jnc @F
  289.         mov byte ptr [bx].P.Col.G, 0FFh
  290. @@: ;.GreenReady:
  291.         add byte ptr [bx].P.Col.B, al
  292.         jnc @F
  293.         mov byte ptr [bx].P.Col.B, 0FFh
  294. @@: ;.BlueReady:

  295.         and byte ptr [bx].P.Col.R, 0C0h
  296.         and byte ptr [bx].P.Col.G, 0E0h
  297.         and byte ptr [bx].P.Col.B, 0E0h
  298.         mov al, byte ptr [bx].P.Col.R
  299.         shr al, cl ;cl = 3
  300.         or al, byte ptr [bx].P.Col.G
  301.         shr al, cl
  302.         or al, byte ptr [bx].P.Col.B
  303.        
  304. assume di:nothing
  305.         ret
  306. PickColor endp

  307. ;******************************************************************************
  308. ; Render the scene
  309. ;******************************************************************************
  310. RenderScreen proc uses di si

  311.         LOCAL Local_Cam_U:DWORD
  312.         LOCAL Local_Cam_V:DWORD
  313.         LOCAL Local_Cam_W:DWORD
  314.         LOCAL Local_Cam_D:DWORD
  315.        
  316.        
  317.         mov ax, 0a000h
  318.         mov es, ax
  319.        
  320.        
  321.         fld [bx].Val_.FM175;[FM175]
  322.         fstp  [Local_Cam_W]

  323.         ;Do some initialize
  324.         ;L = sqrt(x^2 + y^2 + z^2)
  325.         fld  [bx].M.Ray.dir.Light.x
  326.         fmul st(0),st(0)
  327.         fld  [bx].M.Ray.dir.Light.y
  328.         fmul st(0),st(0)
  329.         fadd
  330.         fld  [bx].M.Ray.dir.Light.z
  331.         fmul st(0),st(0)
  332.         fadd
  333.         fsqrt
  334.        
  335.         ;x = x / L
  336.         fld st(0)
  337.         fdivr [bx].M.Ray.dir.Light.x
  338.         fstp  [bx].M.Ray.dir.Light.x
  339.        
  340.         ;y = y / L
  341.         fld st(0)
  342.         fdivr [bx].M.Ray.dir.Light.y
  343.         fstp  [bx].M.Ray.dir.Light.y
  344.        
  345.         ;z = z / L
  346.         fdivr [bx].M.Ray.dir.Light.z
  347.         fstp  [bx].M.Ray.dir.Light.z

  348.         ;di: Used to write display buffer
  349.         mov di,OWAOH
  350.         ;Loop for scan lines
  351.        
  352.         mov  [bx].P.Pos.y, 0
  353. @@:
  354.         .repeat
  355.                 ;Calculate Ray-V
  356.                 mov ax, [bx].P.Pos.y
  357.                 add ax, ax
  358.                 sub ax, [bx].Data_.Res.y
  359.                 not ax
  360.                 inc ax
  361.                
  362.                 mov word ptr [Local_Cam_V], ax
  363.                 fild word ptr [Local_Cam_V]
  364.                 fidiv [bx].Data_.Res.y
  365.                 fstp [Local_Cam_V]

  366.                 ;Loop for pixels per scan line
  367.                 mov [bx].P.Pos.x,0
  368.                
  369.                 .repeat
  370.                         ;Calculate Ray-U
  371.                         mov ax, [bx].P.Pos.x
  372.                         add ax, ax
  373.                         sub ax, [bx].Data_.Res.x
  374.                        
  375.                         mov word ptr [Local_Cam_U], ax
  376.                         fild word ptr [Local_Cam_U]
  377.                         fidiv word ptr [bx].Data_.Res.y
  378.                         fst [Local_Cam_U]

  379.                         ;Normalize Ray
  380.                         fmul st(0),st(0)
  381.                         fld [Local_Cam_V]
  382.                         fmul st(0),st(0)
  383.                         fadd
  384.                         fadd [bx].Val_.F30625;[F30625]
  385.                         fsqrt
  386.                         ;D = sqrt(U^2 + V^2 + W^2)
  387.                        
  388.                         ;x = U / D
  389.                         fst [Local_Cam_D]
  390.                         fdivr [Local_Cam_U]
  391.                         fstp  [bx].M.Ray.dir.Ray.x
  392.                        
  393.                         ;y = V / D
  394.                         fld  [Local_Cam_V]
  395.                         fdiv  [Local_Cam_D]
  396.                         fstp  [bx].M.Ray.dir.Ray.y
  397.                        
  398.                         ;z = W / D
  399.                         fld  [Local_Cam_W]
  400.                         fdiv  [Local_Cam_D]
  401.                         fstp  [bx].M.Ray.dir.Ray.z


  402.                         ; Render the current pixel color
  403.                         call RenderScene
  404.                        
  405.                         ;push si
  406.                         ;mov si,[bx].P.Pos.x
  407.                         ;and si,0ffh
  408.                         ;mov al,[si][bx].DM
  409.                         ;pop si
  410.                        
  411.                         stosb
  412.                        
  413.        
  414.                         inc  [bx].P.Pos.x
  415.                 .until [bx].P.Pos.x >= WWWWW

  416.                 lea di,[di][MAXWW-WWWWW]
  417. IFDEF Interlaced

  418.                 lea di,[di+ MAXWW]
  419.                 inc [bx].P.Pos.y
  420. ENDIF
  421.                 inc [bx].P.Pos.y
  422.         .until [bx].P.Pos.y>= HHHHH
  423.        
  424. IFDEF Interlaced
  425.         and [bx].P.Pos.y,1
  426.         mov [bx].P.Pos.y,1
  427.         mov di,OWAOH + MAXWW
  428.         jz        @B
  429. ENDIF       
  430.        
  431.        
  432.         ; Wait for any keystroke to exit
  433.         mov ah, 07h
  434.         int 21h

  435.         ret
  436. RenderScreen endp


  437. ;******************************************************************************
  438. ; Pick up a color that represents the sky from the `RayDir` vector.
  439. ;******************************************************************************
  440. GetSkyColor proc
  441.         ; SunLum = Dot(RayDir, -LightDir)
  442. assume si:error
  443. assume di:error
  444.         fld  [bx].M.Ray.dir.Ray.x
  445.         fmul [bx].M.Ray.dir.Light.x
  446.         fld  [bx].M.Ray.dir.Ray.y
  447.         fmul [bx].M.Ray.dir.Light.y
  448.         fadd
  449.         fld  [bx].M.Ray.dir.Ray.z
  450.         fmul [bx].M.Ray.dir.Light.z
  451.         fadd
  452.         fchs

  453.         ; SunLum = Max(SunLum, 0)
  454.         fld st(0)
  455.         fabs
  456.         fadd
  457.         fidiv [bx].Val_.C2 ;[C2]
  458.        

  459.         ; SunLum = Pow(SunLum, LightPow)
  460.         IF LightPow GT 0
  461.                 fld st(0)
  462.                
  463.                 REPEAT LightPow - 1
  464.                
  465.                 fmul st(0),st(1)
  466.                 ENDM
  467.                 fmul
  468.         ENDIF

  469.         ; FogDensity = 1 - abs(RayDir_y)
  470.         fld1
  471.         fld  [bx].M.Ray.dir.Ray.y
  472.         fabs
  473.         fsub

  474.         ; Mix(SkyColor, FogColor, FogDensity)
  475.        
  476.         fld st(0)
  477.         fmul  [bx].Mix.FogColor.R
  478.         fstp  [bx].Mix.GetSkyColor.R
  479.         fld st(0)
  480.         fmul  [bx].Mix.FogColor.G
  481.         fstp  [bx].Mix.GetSkyColor.G
  482.         fld st(0)
  483.         fmul  [bx].Mix.FogColor.B
  484.         fstp  [bx].Mix.GetSkyColor.B

  485.         ; 1 - FogDensity
  486.         fld1
  487.         fsubr
  488.         fld st(0)
  489.         fmul  [bx].Mix.SkyColor.R
  490.         fadd  [bx].Mix.GetSkyColor.R
  491.         fstp  [bx].Mix.GetSkyColor.R
  492.         fld st(0)
  493.         fmul  [bx].Mix.SkyColor.G
  494.         fadd  [bx].Mix.GetSkyColor.G
  495.         fstp  [bx].Mix.GetSkyColor.G

  496.         fmul  [bx].Mix.SkyColor.B
  497.         fadd  [bx].Mix.GetSkyColor.B
  498.         fstp  [bx].Mix.GetSkyColor.B

  499.         ; LightColor
  500.         fld st(0)
  501.         fmul  [bx].Mix.LightColor.R
  502.         fadd  [bx].Mix.GetSkyColor.R
  503.         fstp  [bx].Mix.GetSkyColor.R
  504.         fld st(0)
  505.         fmul  [bx].Mix.LightColor.G
  506.         fadd  [bx].Mix.GetSkyColor.G
  507.         fstp  [bx].Mix.GetSkyColor.G

  508.         fmul  [bx].Mix.LightColor.B
  509.         fadd  [bx].Mix.GetSkyColor.B
  510.         fstp  [bx].Mix.GetSkyColor.B

  511. assume si:nothing
  512. assume di:nothing
  513.         ret
  514. GetSkyColor endp
  515. ;******************************************************************************
  516. ; Pick up a color that represents the sky from the `RayDir` vector.
  517. ;******************************************************************************
  518. MapDistProc proc uses si di


  519.         fld  [bx].M.MapDist.O.y
  520.         fstp [bx].M.MapDist.D
  521.         mov  [bx].M.MapDist.I,-1
  522.        
  523.         mov cx, NumSpheres
  524.         mov si, 0
  525.         mov di, 0

  526.                
  527. @@:

  528.         fld  [si][bx].S.Position.x
  529.         fsub [bx].M.MapDist.O.x
  530.         fmul st(0),st(0)
  531.        
  532.         fld  [si][bx].S.Position.y
  533.         fsub [bx].M.MapDist.O.y
  534.         fmul st(0),st(0)
  535.         fadd
  536.         fld  [si][bx].S.Position.z
  537.         fsub [bx].M.MapDist.O.z
  538.         fmul st(0),st(0)
  539.         fadd
  540.         fsqrt
  541.         fsub [si][bx].S.Radius
  542.         fcom [bx].M.MapDist.D
  543.         fstsw ax
  544.         sahf
  545. .if CARRY?
  546.         fst  [bx].M.MapDist.D
  547.         mov  [bx].M.MapDist.I, di
  548. .endif
  549.         fstp st(0)
  550.         inc di
  551.         add si,[bx].Data_.SizeOf_Sphere
  552.         loop @B

  553.         ret
  554. MapDistProc endp

  555. ;******************************************************************************
  556. ; Calculate a ray from origin `RayOrg` and towards the direction `RayDir` that
  557. ; casts to the scene. Returns the cast point coordinates `MapCast` and the
  558. ; distance to the origin of the ray and the surface normal from the scene.
  559. ;******************************************************************************
  560. MapCastProc proc uses si

  561. assume di:error
  562.         fldz
  563.         fst  [bx].M.MapCast.D
  564.         fst  [bx].M.MapCast.N.x
  565.         fst  [bx].M.MapCast.N.y
  566.         fstp [bx].M.MapCast.N.z


  567. ; Stepping the point to go forward
  568.         mov cx, MapCast_Iterate
  569. LoopIterate:

  570.         fld [bx].M.Ray.O.x
  571.         fld [bx].M.Ray.dir.Ray.x
  572.         fmul [bx].M.MapCast.D
  573.         fadd
  574.         fstp [bx].M.MapDist.O.x
  575.        

  576.         fld [bx].M.Ray.O.y
  577.         fld [bx].M.Ray.dir.Ray.y
  578.         fmul [bx].M.MapCast.D
  579.         fadd
  580.         fstp [bx].M.MapDist.O.y
  581.        
  582.        
  583.         fld [bx].M.Ray.O.z
  584.         fld [bx].M.Ray.dir.Ray.z
  585.         fmul [bx].M.MapCast.D
  586.         fadd
  587.         fstp [bx].M.MapDist.O.z

  588.         call IsAwayFromBB
  589.         jnc InsideBB

  590.         fld [bx].M.Ray.dir.Ray.y
  591.         fldz
  592.         fcompp
  593.         fstsw ax
  594.         sahf
  595.         jbe ToSky
  596.        
  597.         ; Hit the ground outside the bounding box
  598.         fld1
  599.         fstp [bx].M.MapCast.N.y

  600.         fld [bx].M.Ray.O.y
  601.         fldz
  602.         fsub [bx].M.Ray.dir.Ray.y
  603.         fdiv
  604.         fstp [bx].M.MapCast.D

  605.         call SetCastCrd

  606.         mov [bx].M.MapCast.I, -1
  607.         stc
  608.         jmp done
  609. ToSky:
  610. ; The origin of the ray is from outside of the bounding box and it's going to the sky

  611.         fld [bx].M.MapDist.O.x
  612.         fstp [bx].M.MapCast.O.x
  613.         fld [bx].M.MapDist.O.y
  614.         fstp [bx].M.MapCast.O.y
  615.         fld [bx].M.MapDist.O.z
  616.         fstp [bx].M.MapCast.O.z
  617.         mov [bx].M.MapCast.I, -2
  618.         clc
  619.         jmp done

  620. ; The origin of the ray is inside the bounding box
  621. InsideBB:
  622.         ;push cx
  623.         mov si,cx
  624.         call MapDistProc
  625.         ;pop cx
  626.         mov cx,si

  627.         fld [bx].M.MapDist.D
  628.         fcomp [bx].Data_.DistEpsilon;[DistEpsilon]
  629.         fstsw ax
  630.         sahf
  631.         ja NotNearEnough

  632.         mov ax, [bx].M.MapDist.I
  633.         mov [bx].M.MapCast.I, ax
  634.         cmp ax, 0
  635.         jge NotHitGround

  636.         ; Hit the ground inside the bounding box
  637.         fld1
  638.         fstp [bx].M.MapCast.N.y

  639.         fld [bx].M.MapDist.O.x
  640.         fstp [bx].M.MapCast.O.x
  641.         fld [bx].M.MapDist.O.y
  642.         fstp [bx].M.MapCast.O.y
  643.         fld [bx].M.MapDist.O.z
  644.         fstp [bx].M.MapCast.O.z
  645.         stc
  646.         jmp done

  647. ; Should iterate again
  648. NotNearEnough:
  649.         fld [bx].M.MapCast.D
  650.         fadd [bx].M.MapDist.D
  651.         fstp [bx].M.MapCast.D
  652.         dec cx
  653.         jcxz ExitIterate
  654.         jmp LoopIterate

  655. ; Hit the spheres
  656. NotHitGround:

  657.         mul [bx].Data_.SizeOf_Sphere;[SizeOf_Sphere]
  658.         mov si,ax
  659.        
  660.         ; Calculate the normal
  661.         fld [bx].M.MapDist.O.x
  662.         fsub [si][bx].S.Position.x
  663.         fst [bx].M.MapCast.N.x
  664.         fmul st(0),st(0)
  665.         fld [bx].M.MapDist.O.y
  666.         fsub [si][bx].S.Position.y
  667.         fst [bx].M.MapCast.N.y
  668.         fmul st(0),st(0)
  669.         fadd
  670.         fld [bx].M.MapDist.O.z
  671.         fsub [si][bx].S.Position.z
  672.         fst [bx].M.MapCast.N.z
  673.         fmul st(0),st(0)
  674.         fadd
  675.         fsqrt ; Normalize the normal
  676.         fld st(0)
  677.         fld [bx].M.MapCast.N.x
  678.         fdivr
  679.         fstp [bx].M.MapCast.N.x
  680.         fld st(0)
  681.         fld [bx].M.MapCast.N.y
  682.         fdivr
  683.         fstp [bx].M.MapCast.N.y
  684.         fdivr [bx].M.MapCast.N.z
  685.         fstp [bx].M.MapCast.N.z

  686.         ; Set cast coord
  687.         fld [bx].M.MapDist.O.x
  688.         fstp [bx].M.MapCast.O.x
  689.         fld [bx].M.MapDist.O.y
  690.         fstp [bx].M.MapCast.O.y
  691.         fld [bx].M.MapDist.O.z
  692.         fstp [bx].M.MapCast.O.z
  693.         stc
  694.         jmp done

  695. ; Finished iteration
  696. ExitIterate:
  697.         fld [bx].M.MapDist.O.x
  698.         fstp [bx].M.MapCast.O.x
  699.         fld [bx].M.MapDist.O.y
  700.         fstp [bx].M.MapCast.O.y
  701.         fld [bx].M.MapDist.O.z
  702.         fstp [bx].M.MapCast.O.z
  703.         mov [bx].M.MapCast.I, -2
  704.         clc
  705. done:
  706. assume di:nothing
  707.         ret
  708. MapCastProc endp

  709. ; Subroutine: MapCast = RayOrg + RayDir * Dist
  710. SetCastCrd proc
  711. assume si:error
  712. assume di:error

  713.         fld [bx].M.Ray.O.x
  714.         fld [bx].M.Ray.dir.Ray.x
  715.         fmul [bx].M.MapCast.D
  716.         fadd
  717.         fstp [bx].M.MapCast.O.x

  718.         fld [bx].M.Ray.O.y
  719.         fld [bx].M.Ray.dir.Ray.y
  720.         fmul [bx].M.MapCast.D
  721.         fadd
  722.         fstp [bx].M.MapCast.O.y

  723.         fld [bx].M.Ray.O.z
  724.         fld [bx].M.Ray.dir.Ray.z
  725.         fmul [bx].M.MapCast.D
  726.         fadd
  727.         fstp [bx].M.MapCast.O.z

  728.         ret
  729.        
  730. assume si:nothing
  731. assume di:nothing

  732. SetCastCrd endp       

  733. ;******************************************************************************
  734. ; Check if the ray from the origin `RayOrg` towards the direction `RayDir` is
  735. ; going away from the bounding box
  736. ;******************************************************************************
  737. IsAwayFromBB proc
  738. assume si:error
  739. assume di:error
  740.        
  741.         fld [bx].M.Ray.O.x
  742.         fcomp [bx].BB.N.x; [BBneg.x]
  743.         fstsw ax
  744.         sahf
  745.        
  746. jae XNIsOK
  747.         fld [bx].M.Ray.dir.Ray.x
  748.         fcomp [bx].Val_.FZero;[FZero]
  749.         fstsw ax
  750.         sahf
  751.        
  752. jae XNIsOK
  753.         stc
  754.         jmp done
  755.        
  756. XNIsOK:

  757.         fld [bx].M.Ray.O.x
  758.         fcomp [bx].BB.O.x;[BB.x]
  759.         fstsw ax
  760.         sahf
  761.        
  762. jbe XPIsOK
  763.         fld [bx].M.Ray.dir.Ray.x
  764.         fcomp [bx].Val_.FZero;[FZero]
  765.         fstsw ax
  766.         sahf
  767.        
  768. jbe XPIsOK
  769.         stc
  770.         jmp done
  771.        
  772. XPIsOK:
  773.        
  774.         fld [bx].M.Ray.O.y
  775.         fcomp  [bx].BB.N.y;[BBneg.y]
  776.         fstsw ax
  777.         sahf
  778.        
  779. jae YNIsOK
  780.         fld [bx].M.Ray.dir.Ray.y
  781.         fcomp [bx].Val_.FZero;[FZero]
  782.         fstsw ax
  783.         sahf
  784. jae YNIsOK
  785.         stc
  786.         jmp done
  787. YNIsOK:

  788.         fld [bx].M.Ray.O.y
  789.         fcomp [bx].BB.O.y;[BB.y]
  790.         fstsw ax
  791.         sahf
  792.        
  793. jbe YPIsOK

  794.         fld [bx].M.Ray.dir.Ray.y
  795.         fcomp [bx].Val_.FZero; [FZero]
  796.         fstsw ax
  797.         sahf
  798. jbe YPIsOK
  799.         stc
  800.         jmp done
  801. YPIsOK:

  802.         fld [bx].M.Ray.O.z
  803.         fcomp [bx].BB.N.z; [BBneg.z]
  804.         fstsw ax
  805.         sahf
  806.        
  807. jae ZNIsOK
  808.         fld [bx].M.Ray.dir.Ray.z
  809.         fcomp [bx].Val_.FZero;[FZero]
  810.         fstsw ax
  811.         sahf
  812.        
  813. jae ZNIsOK
  814.         stc
  815.         jmp done
  816.        
  817. ZNIsOK:

  818.         fld [bx].M.Ray.O.z
  819.         fcomp [bx].BB.O.z;[BB.z]
  820.         fstsw ax
  821.         sahf
  822. jbe ZPIsOK

  823.         fld [bx].M.Ray.dir.Ray.z
  824.         fcomp [bx].Val_.FZero;[FZero]
  825.         fstsw ax
  826.         sahf
  827. jbe ZPIsOK
  828.         stc
  829.         jmp done
  830.        
  831. ZPIsOK:

  832.         clc
  833. done:

  834.         ret
  835. assume si:nothing
  836. assume di:nothing
  837. IsAwayFromBB endp
  838.        
  839. ;******************************************************************************
  840. ; Render the pixel by a given ray from the origin `RayOrg` towards the
  841. ; direction `RayDir`
  842. ;******************************************************************************
  843. RenderScene proc uses si
  844. assume di:error
  845.         fld1
  846.         fst  [bx].Mask_.R
  847.         fst  [bx].Mask_.G
  848.         fstp [bx].Mask_.B


  849.         fld  [bx].Cam.x
  850.         fstp [bx].M.Ray.O.x
  851.         fld  [bx].Cam.y
  852.         fstp  [bx].M.Ray.O.y
  853.         fld  [bx].Cam.z
  854.         fstp  [bx].M.Ray.O.z
  855.        

  856.        
  857.         mov cx, SampleDepth
  858. LoopSampling:
  859.         ;push cx
  860.         mov si,cx
  861.         call MapCastProc
  862.         ;pop cx
  863.         mov cx,si
  864.        
  865. ;.if !CARRY?       
  866.         jc CastSphereOrGround

  867. ; If not cast then still step forward
  868. NotCast:
  869.         fld [bx].M.MapCast.O.x
  870.         fstp [bx].M.Ray.O.x
  871.         fld [bx].M.MapCast.O.y
  872.         fstp [bx].M.Ray.O.y
  873.         fld [bx].M.MapCast.O.z
  874.         fstp [bx].M.Ray.O.z

  875.         dec cx
  876.         jcxz Finished1
  877.         jmp LoopSampling

  878. Finished1:
  879.         jmp Finished

  880. ; If cast to the spheres or the ground, do coloring
  881. CastSphereOrGround:
  882.         mov ax, [bx].M.MapCast.I
  883.         cmp ax, 0
  884.         jl CastGround

  885.         ; Casting spheres
  886.         mul [bx].Data_.SizeOf_Sphere
  887.         mov si,ax
  888.        
  889.         fld [bx].Mask_.R
  890.         fmul [si][bx].S.Color.R
  891.         fstp [bx].Mask_.R
  892.         fld [bx].Mask_.G
  893.         fmul [si][bx].S.Color.G
  894.         fstp [bx].Mask_.G
  895.         fld [bx].Mask_.B
  896.         fmul [si][bx].S.Color.B
  897.         fstp [bx].Mask_.B

  898.         ; Reflection
  899.         fld [bx].M.MapCast.N.x
  900.         fmul [bx].M.Ray.dir.Ray.x
  901.         fld [bx].M.MapCast.N.y
  902.         fmul [bx].M.Ray.dir.Ray.y
  903.         fadd
  904.         fld [bx].M.MapCast.N.z
  905.         fmul [bx].M.Ray.dir.Ray.z
  906.         fadd
  907.         fadd st(0),st(0) ; (Normal dot Ray) * 2
  908.        
  909.         fld st(0)
  910.         fmul [bx].M.MapCast.N.x
  911.         fsubr [bx].M.Ray.dir.Ray.x
  912.         fstp [bx].M.Ray.dir.Ray.x
  913.        
  914.         fld st(0)
  915.         fmul [bx].M.MapCast.N.y
  916.         fsubr [bx].M.Ray.dir.Ray.y
  917.         fstp [bx].M.Ray.dir.Ray.y

  918.         fmul [bx].M.MapCast.N.z
  919.         fsubr [bx].M.Ray.dir.Ray.z
  920.         fstp [bx].M.Ray.dir.Ray.z

  921.         call SetRayOrg

  922.         dec cx
  923.         jcxz Finished
  924.         jmp LoopSampling
  925. ;.endif

  926. ; The ray is casting the ground
  927. CastGround:
  928.         fld [bx].M.MapCast.O.x
  929.         fadd st(0),st(0)
  930.         fistp [bx].Data_.TempW;[TempW]
  931.         fwait
  932.         mov ax, [bx].Data_.TempW;[TempW]
  933.         fld  [bx].M.MapCast.O.z
  934.         fadd st(0),st(0)
  935.         fistp [bx].Data_.TempW; [TempW]
  936.         fwait
  937.         xor ax,[bx].Data_.TempW; [TempW]
  938.         test ax, 1
  939.         jz GroundColorPattern

  940.         fld  [bx].Mask_.R
  941.         fmul [bx].Val_.F05;[F05]
  942.         fstp [bx].Mask_.R
  943.         fld  [bx].Mask_.G
  944.         fmul [bx].Val_.F05;[F05]
  945.         fstp [bx].Mask_.G
  946.         fld  [bx].Mask_.B
  947.         fmul [bx].Val_.F05;[F05]
  948.         fstp [bx].Mask_.B

  949. GroundColorPattern:
  950.         fld [bx].M.Ray.dir.Ray.y
  951.         fabs
  952.         fstp [bx].M.Ray.dir.Ray.y

  953.         call SetRayOrg

  954.         dec cx
  955.         jcxz Finished
  956.         jmp LoopSampling

  957. ; Finished iteration
  958. Finished:
  959.         call GetSkyColor
  960.         fld [bx].Mix.GetSkyColor.R
  961.         fmul [bx].Mask_.R
  962.         fimul [bx].Val_.C255;[C255]
  963.         fistp [bx].P.Col.R
  964.         fld [bx].Mix.GetSkyColor.G
  965.         fmul [bx].Mask_.G
  966.         fimul [bx].Val_.C255;[C255]
  967.         fistp [bx].P.Col.G
  968.         fld [bx].Mix.GetSkyColor.B
  969.         fmul[bx].Mask_.B
  970.         fimul [bx].Val_.C255;[C255]
  971.         fistp [bx].P.Col.B
  972.         fwait

  973. ; Clamp the color not to overflow
  974.         cmp [bx].P.Col.R, 255
  975.         jle R_OK
  976.         mov [bx].P.Col.R, 255
  977. R_OK:
  978.         cmp [bx].P.Col.G, 255
  979.         jle G_OK
  980.         mov [bx].P.Col.G, 255
  981. G_OK:
  982.         cmp [bx].P.Col.B, 255
  983.         jle B_OK
  984.         mov [bx].P.Col.B, 255
  985. B_OK:
  986.        

  987.         call PickColor
  988.        
  989.         ret
  990. assume si:nothing
  991. assume di:nothing
  992. RenderScene endp

  993. ; When cast, set the ray origin near the cast point
  994. SetRayOrg proc
  995. assume si:error
  996. assume di:error

  997.         fld  [bx].M.MapCast.O.x
  998.         fld  [bx].M.Ray.dir.Ray.x
  999.         fmul [bx].Data_.DistEpsilon2; [DistEpsilon2]
  1000.         fadd
  1001.         fstp  [bx].M.Ray.O.x
  1002.        
  1003.         fld  [bx].M.MapCast.O.y
  1004.         fld  [bx].M.Ray.dir.Ray.y
  1005.         fmul [bx].Data_.DistEpsilon2; [DistEpsilon2]
  1006.         fadd
  1007.         fstp [bx].M.Ray.O.y

  1008.         fld  [bx].M.MapCast.O.z
  1009.         fld  [bx].M.Ray.dir.Ray.z
  1010.         fmul [bx].Data_.DistEpsilon2; [DistEpsilon2]
  1011.         fadd
  1012.         fstp [bx].M.Ray.O.z

  1013.         ret
  1014. assume si:nothing
  1015. assume di:nothing
  1016. SetRayOrg endp

  1017. ;std
  1018. ;end start

  1019. ;shellcode
  1020. end _entry
复制代码







  1. tPosition struc
  2.         x dd ?
  3.         y dd ?
  4.         z dd ?
  5. tPosition ends

  6. tPos struc
  7.         x dw ?
  8.         y dw ?
  9. tPos ends


  10. tColor struc
  11.         R dd ?
  12.         G dd ?
  13.         B dd ?
  14. tColor ends
  15. tCol struc
  16.         R dw ?
  17.         G dw ?
  18.         B dw ?
  19. tCol ends

  20. tPixel struct
  21.         Pos tPos <>
  22.         Col tCol <>
  23. tPixel ends

  24. tSphere struct
  25.         Position tPosition <>
  26.         Radius   dd        ?
  27.         Color    tColor    <>
  28. tSphere ends

  29. tColorMix struct
  30.         SkyColor    tColor <>
  31.         LightColor  tColor <>
  32.         FogColor    tColor <>
  33.         GetSkyColor tColor <>
  34. tColorMix ends


  35. tBB struct
  36.         O  tPosition <>
  37.         N  tPosition <>
  38. tBB ends

  39. tConst struct
  40.         F65536    dd ?;65536.0
  41.         F30625    dd ?;3.0625
  42.         FM175     dd ?;-1.75
  43.         FTimeFreq dd ?;1193186.0
  44.         FBigValue dd ?;9999999.0
  45.         FZero     dd ?;0
  46.         F05       dd ?;0.5
  47.         FM1       dd ?;-1.0
  48.         C2        dw ?;2
  49.         C255      dw ?;255
  50.         C256      dw ?;256
  51.         C1000     dw ?;1000
  52. tConst ends

  53. tData struct
  54.         DistEpsilon    dd ?;0.01
  55.         DistEpsilon2   dd ?;0.02
  56.         FogDistance    dd ?;100.0
  57.         SavedVideoMode dw ?;0
  58.         TempW          dw ?;0
  59.         SizeOf_Sphere  dw ?;sizeof tSphere
  60.         Res            tPos <>;<320,200>
  61. tData ends


  62. tLightRay struc
  63.         O    tPosition    <>
  64.         struc dir
  65.                 Ray   tPosition <>
  66.                 Light tPosition <>
  67.         ends
  68.         D    dd ?
  69. tLightRay ends

  70. tMapCast struc
  71.         O tPosition <>
  72.     N tPosition <>
  73.     D dd ?
  74.     I dw ?
  75. tMapCast ends

  76. tMapDist struc
  77.         O tPosition <>
  78.         D dd ?
  79.         I dw ?
  80. tMapDist ends



  81. tMapCastDist struct
  82.         Ray     tLightRay <>
  83.         MapCast tMapCast  <>
  84.         MapDist tMapDist  <>
  85. tMapCastDist ends



  86. tREG struct
  87.         ax_ dw ?
  88.         cx_ dw ?
  89.         dx_ dw ?
  90.         bx_ dw ?
  91.         sp_ dw ?
  92.         bp_ dw ?
  93.         si_ dw ?
  94.         di_ dw ?
  95. tREG ends


  96. tClass struct
  97.         Val_  tConst        <>
  98.         Data_ tData         <>
  99.         DM    db  16*16  dup(?)
  100.         M          tMapCastDist  <>
  101.         Mask_ tColor        <>
  102.         Mix   tColorMix     <>
  103.     Cam   tPosition     <>
  104.     BB    tBB           <>
  105.         P     tPixel        <>
  106.         S     tSphere NumSpheres dup(<>)
  107.        
  108.         R     tREG          <>
  109.         Check dw            <> ;0AA55h
  110. tClass ends
  111. STRUCTCHECK TEXTEQU <@CatStr(%($ - offset _this))>



复制代码










https://github.com/AyalaRs/RAYTRC_
回复

使用道具 举报

1112

主题

1653

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
245
威望
744 点
宅币
24254 个
贡献
46222 次
宅之契约
0 份
在线时间
2298 小时
注册时间
2014-1-26
发表于 2021-2-23 21:27:35 | 显示全部楼层
噗 masm的“宏汇编”看起来比nasm的好看很多。

以及,既然都.686p了,左移右移寄存器不需要再用cl存储位数了吧

回复 赞! 靠!

使用道具 举报

30

主题

211

回帖

2792

积分

用户组: 版主

UID
1821
精华
7
威望
69 点
宅币
2172 个
贡献
206 次
宅之契约
0 份
在线时间
482 小时
注册时间
2016-7-12
 楼主| 发表于 2021-2-23 21:41:38 | 显示全部楼层
0xAA55 发表于 2021-2-23 21:27
噗 masm的“宏汇编”看起来比nasm的好看很多。

以及,既然都.686p了,左移右移寄存器不需要再用cl存储位数 ...

方便调试使用了686p, .286就能编译过,最大程度还原源代码
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-26 22:16 , Processed in 0.041911 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表