Subversion Repositories Kolibri OS

Rev

Rev 2736 | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. ; application : View3ds ver. 0.062 - tiny .3ds files viewer.
  3. ; compiler    : FASM
  4. ; system      : KolibriOS
  5. ; author      : Macgub aka Maciej Guba
  6. ; email       : macgub3@wp.pl
  7. ; web         : www.macgub.hekko.pl
  8. ; Fell free to use this intro in your own distribution of KolibriOS/MenuetOS.
  9. ; Special greetings to all MenuetOS maniax in the world.
  10. ; I hope because my intros Christian Belive will be near to each of You.
  11.  
  12.  
  13. ; Some adjustments made by Madis Kalme
  14. ; madis.kalme@mail.ee
  15. ; I tried optimizing it a bit, but don't know if it was successful. The objects
  16. ; can be:
  17. ; 1) Read from a file (*.3DS standard)
  18. ; 2) Written in manually (at the end of the code)
  19. ;include 'proc32.inc'
  20.  
  21. SIZE_X equ 512
  22. SIZE_Y equ 512                               ;      /////     I want definitely
  23. TIMEOUT equ 10                               ;     ------     say:
  24. ROUND equ 10                                 ;     \ @ @/     keep smiling every
  25. TEX_X equ 512    ; texture width             ;      \ ./    / day.
  26. TEX_Y equ 512    ;         height            ;       \/    /
  27. TEX_SHIFT equ 9  ; texture width shifting    ;     __||__ /
  28. TEXTURE_SIZE EQU (TEX_X * TEX_Y)-1           ;   /|      |
  29. TEX equ SHIFTING ; TEX={SHIFTING | FLUENTLY} ;  / \      /
  30. FLUENTLY = 0                                 ; /   |    |
  31. SHIFTING = 1                                 ;     ------
  32. CATMULL_SHIFT equ 8                          ;      |  |
  33. LIGHT_SIZE equ 22                            ;      |  |
  34. NON   =   0                                  ;     -/  \-
  35. MMX   =   1
  36. SSE   =   2
  37. SSE2  =   3
  38. Ext   =   SSE2           ;Ext={ NON | MMX | SSE | SSE2 }
  39.  
  40. ; 0 for short names (Menuet-compatible), 1 for long names (Kolibri features)
  41. USE_LFN = 1
  42.  
  43. use32
  44.         org    0x0
  45.         db     'MENUET01'       ; 8 byte id
  46.         dd     0x01             ; header version
  47.         dd     START            ; start of code
  48.         dd     I_END            ; size of image
  49.         dd     MEM_END          ; memory for app
  50.         dd     MEM_END          ; esp
  51.         dd     I_Param          ; I_Param
  52.         dd     0x0              ; I_Icon
  53.  
  54. START:    ; start of execution
  55.         cld
  56.         call   alloc_buffer_mem
  57.         call   read_param
  58.         call   read_from_disk    ; read, if all is ok eax = 0
  59.         cmp    eax,0
  60.         jne    .gen
  61.         call   read_tp_variables ; init points and triangles count variables
  62.         cmp    eax,0
  63.         je     .gen
  64.         jmp    .malloc
  65.     .gen:
  66.      if USE_LFN
  67.         mov    [triangles_count_var],1000
  68.         mov    [points_count_var],1000
  69.         call   alloc_mem_for_tp
  70.      end if
  71.         call   generate_object
  72.         jmp    .opt
  73.     .malloc:
  74.      if USE_LFN
  75.         call   alloc_mem_for_tp
  76.      end if
  77.         call   read_from_file
  78.     .opt:
  79.         call   optimize_object1     ;  proc in file b_procs.asm
  80.                                     ;  set point(0,0,0) in center and  calc all coords
  81.                                     ;  to be in <-1.0,1.0>
  82.         call   normalize_all_light_vectors
  83.         call   init_triangles_normals2
  84.         call   init_point_normals
  85.         call   init_envmap2
  86.         call   init_envmap_cub
  87.         call   generate_texture2
  88.         call   init_sincos_tab
  89.  
  90.         call   do_color_buffer   ; intit color_map
  91.         mov    edi,bumpmap
  92.         call   calc_bumpmap
  93.         call   calc_bumpmap_coords   ; bump and texture mapping
  94.         call   draw_window
  95.  
  96. still:
  97.  
  98.         mov     eax,23          ; wait here for event with timeout
  99.         mov     ebx,TIMEOUT
  100.         cmp     [speed_flag],1
  101.         jne     .skip
  102.         mov     eax,11
  103.     .skip:
  104.         int     0x40
  105.  
  106.         cmp     eax,1           ; redraw request ?
  107.         je      red
  108.         cmp     eax,2           ; key in buffer ?
  109.         je      key
  110.         cmp     eax,3           ; button in buffer ?
  111.         je      button
  112.  
  113.         jmp     noclose
  114.  
  115.     red:                        ; redraw
  116.         call    draw_window
  117.  
  118.         jmp     noclose
  119.  
  120.     key:                        ; key
  121.         mov     eax,2           ; just read it and ignore
  122.         int     0x40
  123.         jmp     noclose
  124.  
  125.     button:                     ; button
  126.         mov     eax,17          ; get id
  127.         int     0x40
  128.  
  129.         cmp     ah,1            ; button id=1 ?
  130.         jne     @f
  131.  
  132.         mov     eax,-1          ; close this program
  133.         int     0x40
  134.     @@:
  135.         cmp     ah,30
  136.         jge     add_vec_buttons
  137.         call    update_flags          ; update flags and write labels of flags
  138.  
  139.                                       ; do other operations according to flag
  140.         cmp     ah,3                  ; ah = 3 -> shading model
  141.         jne     .next_m6
  142.         cmp     [dr_flag],2
  143.         jne     @f
  144.    ;     call    init_envmap2    ;   <----! this don't works in env mode
  145.                                  ;          and more than ~18 kb objects
  146.  ;       call    init_envmap_cub2
  147.      @@:
  148.         cmp     [dr_flag],4
  149.         jne     @f
  150.         call    generate_texture2
  151.  
  152.      @@:
  153.      .next_m6:
  154.                                       ; ah = 5 -> scale-
  155.         cmp     ah,5
  156.         jne     @f
  157.         mov     [scale],0.7
  158.         fninit
  159.         fld     [rsscale]
  160.         fmul    [scale]
  161.         fstp    [rsscale]
  162.  
  163.       @@:
  164.         cmp     ah,6                 ; ah = 6 ->  scale+
  165.         jne     @f
  166.         mov     [scale],1.3
  167.         fninit
  168.         fld     [rsscale]
  169.         fmul    [scale]
  170.         fstp    [rsscale]
  171.  
  172.       @@:
  173.         cmp     ah,9    ; lights random                 ;    'flat'  0
  174.         jne     .next_m5                                ;    'grd '  1
  175.         call    make_random_lights                      ;    'env '  2
  176.         call    normalize_all_light_vectors             ;    'bump'  3
  177.         call    do_color_buffer   ; intit color_map     ;    'tex '  4
  178.       ;  cmp     [emboss_flag],1                         ;    'pos '  5
  179.       ;  je      @f                                      ;    'dots'  6
  180.       ;  cmp     [dr_flag],8
  181.       ;  jge     @f
  182.       ;  cmp     [dr_flag],2                             ;    'txgr'  7
  183.       ;  jl      .next_m5                            ;    '2tex'  8
  184.       ;  cmp     [dr_flag],3                             ;    'btex'  9
  185.       ;  jg      .next_m5
  186.     ; @@:
  187.         call    init_envmap2    ; update env map if shading model = environment or bump
  188.     .next_m5:
  189.         cmp      ah,11
  190.         je       @f
  191.         cmp      ah,12
  192.         je       @f
  193.         cmp      ah,13
  194.         jne      .next_m4
  195.       @@:
  196.         call     mirror
  197.      .next_m4:
  198.         cmp      ah,14
  199.         jne      @f
  200.         call     exchange
  201.      @@:
  202.         cmp      ah,15
  203.         jne      @f
  204.         cmp      [emboss_flag],1
  205.         call     init_envmap2
  206.      @@:
  207. ;        cmp      ah,17
  208. ;        jne      .next_m
  209. ;        cmp      [move_flag],2
  210. ;        jne      @f
  211. ;        call     draw_window             ; redraw other labels to navigation buttons
  212. ;      @@:
  213. ;        cmp      [move_flag],0
  214. ;        jne      .next_m
  215. ;        call     draw_window             ; redraw other labels to navigation buttons
  216.      .next_m:
  217.         cmp      ah,18
  218.         jne      .next_m2
  219.      if USE_LFN
  220.         mov      [re_alloc_flag],1       ; reallocate memory
  221.         mov      [triangles_count_var],1000
  222.         mov      [points_count_var],1000
  223.         call     alloc_mem_for_tp
  224.         mov      [re_alloc_flag],0
  225.      end if
  226.         mov      bl,[generator_flag]
  227.         or       bl,bl
  228.         jz       .next_m2
  229.         cmp      bl,1
  230.         jne      @f
  231.         call     generate_object
  232.         jmp      .calc_norm
  233.       @@:
  234.         cmp      bl,4
  235.         jg       @f
  236.         movzx    ax,bl                ; ax < - object number
  237.         call     generate_object2
  238.         jmp     .calc_norm
  239.       @@:
  240.         call    generate_object3
  241.       .calc_norm:
  242.         call    optimize_object1
  243.         call    init_triangles_normals2
  244.         call    init_point_normals
  245.         call    calc_bumpmap_coords   ; bump and texture mapping
  246.  
  247.      .next_m2:
  248.         cmp      ah,19
  249.         je       @f
  250.         cmp      ah,20
  251.         jne      .next_m3
  252.      @@:
  253.         mov      edi,bumpmap
  254.         call     calc_bumpmap
  255.      .next_m3:
  256.         cmp     ah,21            ; re map bumps, texture coordinates
  257.         jne     @f
  258.         call    calc_bumpmap_coords
  259.       @@:
  260.         jmp     noclose
  261.  
  262.  
  263.                                ; there are 6 navigation buttons each
  264.    add_vec_buttons:            ; can move: object, camera,.. list is open
  265.                                ;
  266.         cmp     ah,30
  267.         jne     .next
  268.         cmp     [move_flag],1
  269.         je      @f
  270. ;        cmp     [move_flag],2
  271. ;        je      .set_light1
  272.         sub     [vect_y],10
  273.         jmp     .next
  274.       @@:
  275.         sub     [yobs],10   ;  observator = camera position
  276.         jmp     .next
  277.  
  278. ;--------------------------------------------------
  279. ;      .set_light1:          ;  r -
  280. ;        movzx   ebx,[light_no_flag]  ; * 22
  281. ;        mov     ecx,ebx
  282. ;        shl     ebx,4
  283. ;        shl     ecx,1
  284. ;        add     ebx,ecx
  285. ;        shl     ecx,1
  286. ;        add     ebx,ecx
  287. ;        add     ebx,lights+6    ; 6 -> light vector size
  288. ;
  289. ;        movzx   ecx,[light_comp_flag]
  290. ;        lea     ecx,[ecx*3}
  291. ;        add     ebx,ecx         ; ebx ->  color to set
  292.  
  293. ;---------------------------------------------------
  294.       .next:
  295.         cmp     ah,31
  296.         jne     .next1
  297.         cmp     [move_flag],1
  298.         je      @f
  299.  
  300.         add     [vect_z],10
  301.         jmp     .next1
  302.       @@:
  303.         add     [zobs],10         ;  observator = camera position
  304.      .next1:
  305.         cmp     ah,33
  306.         jne     .next2
  307.         cmp     [move_flag],1
  308.         je      @f
  309.  
  310.         sub     [vect_x],10
  311.         jmp     .next2
  312.       @@:
  313.         sub     [xobs],10         ;  observator = camera position
  314.       .next2:
  315.         cmp     ah,32
  316.         jne     .next3
  317.         cmp     [move_flag],1
  318.         je      @f
  319.  
  320.         add     [vect_x],10
  321.         jmp     .next3
  322.       @@:
  323.         add     [xobs],10         ;  observator = camera position
  324.       .next3:
  325.         cmp     ah,34
  326.         jne     .next4
  327.         cmp     [move_flag],1
  328.         je      @f
  329.  
  330.         sub     [vect_z],10
  331.         jmp     .next4
  332.       @@:
  333.         sub     [zobs],10         ;  observator = camera position
  334.       .next4:
  335.         cmp     ah,35
  336.         jne     .next5
  337.         cmp     [move_flag],1
  338.         je      @f
  339.  
  340.       ;  call    add_vector
  341.         add     [vect_y],10
  342.         jmp     .next5
  343.       @@:
  344.         add     [yobs],10         ;  observator = camera position
  345.       .next5:
  346.  
  347.  
  348.  
  349.     noclose:
  350.  
  351.         cmp     [r_flag],2
  352.         jne     .no_x
  353.         inc     [angle_x]
  354.         and     [angle_x],0xff
  355.         mov     [angle_z],0
  356.         jmp     .end_rot
  357.  
  358.       .no_x:
  359.         cmp     [r_flag],0
  360.         jne     .no_y
  361.         inc     [angle_y]
  362.         and     [angle_y],0xff
  363.         mov     [angle_z],0
  364.         jmp     .end_rot
  365.  
  366.       .no_y:
  367.         cmp     [r_flag],1
  368.         jne     .end_rot
  369.         mov     cx,[angle_x]
  370.         inc     cx
  371.         and     cx,0xff
  372.         mov     [angle_z],0
  373.         mov     [angle_y],cx
  374.         mov     [angle_x],cx
  375.      .end_rot:
  376.  
  377.         mov     esi,angle_x
  378.         mov     edi,matrix
  379.         call    make_rotation_matrix
  380.     RDTSC
  381.     push eax
  382.         mov     esi,[points_normals_ptr]
  383.         mov     edi,[points_normals_rot_ptr]
  384.         mov     ebx,matrix
  385.         movzx   ecx,[points_count_var]
  386.         call    rotary
  387.  
  388.         mov     esi,matrix
  389.         call    add_scale_to_matrix
  390.  
  391.         mov     esi,[points_ptr]
  392.         mov     edi,[points_rotated_ptr]
  393.         mov     ebx,matrix
  394.         movzx   ecx,[points_count_var]
  395.         call    rotary
  396.  
  397. ;    RDTSC
  398. ;    pop    ebx
  399. ;    sub    eax,ebx
  400. ;    sub    eax,41
  401. ;    push   eax
  402.  
  403.         mov     esi,[points_rotated_ptr]
  404.         mov     edi,[points_translated_ptr]
  405.         movzx   ecx,[points_count_var]
  406.         call    translate_points
  407.  
  408. ;        cmp     [dr_flag],5
  409. ;        jne     @f
  410. ;        call    calc_attenuation_light
  411. ;     @@:
  412.         cmp     [fire_flag],0
  413.         jne     @f
  414.         call    clrscr          ; clear the screen
  415.      @@:
  416.         cmp     [catmull_flag],1  ;non sort if Catmull = on
  417.         je      .no_sort
  418.         call    sort_triangles
  419.       .no_sort:
  420.         cmp     [dr_flag],7       ; fill if 2tex and texgrd
  421.         jge     @f
  422.         cmp     [catmull_flag],0  ;non fill if Catmull = off
  423.         je      .non_f
  424.         cmp     [dr_flag],6       ; non fill if dots
  425.         je      .non_f
  426.       @@:
  427.         call    fill_Z_buffer     ; make background
  428.      .non_f:
  429. ;    RDTSC
  430. ;    push eax
  431.         cmp     [dr_flag],6
  432.         jne     @f
  433.         call     draw_dots
  434.         jmp      .blurrr
  435.       @@:
  436.         call    draw_triangles  ; draw all triangles from the list
  437.       .blurrr:
  438.         cmp     [fire_flag],0
  439.         jne     @f
  440.         cmp     [blur_flag],0
  441.         je      .no_blur  ; no blur, no fire
  442.         movzx   ecx,[blur_flag]
  443.         call    blur_screen    ; blur and fire
  444.         jmp     .no_blur
  445.     @@:
  446.         cmp     [emboss_flag],0
  447.         jne     .emb           ; if emboss=true -> no fire
  448.         movzx   ecx,[fire_flag]
  449.         call    blur_screen    ; blur and fire
  450.     .no_blur:                  ; no blur, no fire
  451.         cmp     [emboss_flag],0
  452.         je      @f
  453.      .emb:
  454.         call    do_emboss
  455.  
  456.       @@:
  457.  
  458.  
  459.     cmp     [inc_bright_flag],0           ; increase brightness
  460.     je      .no_inc_bright
  461.     movzx   ebx,[inc_bright_flag]
  462.     shl     ebx,4
  463.     mov     esi,screen
  464.     mov     ecx,SIZE_X*SIZE_Y*3
  465. if (Ext = MMX)|(Ext = SSE)
  466.     mov      bh,bl
  467.     push     bx
  468.     shl      ebx,16
  469.     pop      bx
  470.     push     ebx
  471.     push     ebx
  472.     movq     mm0,[esp]
  473.     add      esp,8
  474. else if Ext >= SSE2
  475.     mov      bh,bl
  476.     push     bx
  477.     shl      ebx,16
  478.     pop      bx
  479.     movd     xmm0,ebx
  480.     shufps   xmm0,xmm0,0
  481. end if
  482.   .oop:
  483. if Ext=NON
  484.     lodsb
  485.     add     al,bl
  486.     jnc     @f
  487.     mov     byte[esi-1],255
  488.     loop    .oop
  489.    @@:
  490.     mov     [esi-1],al
  491.     loop    .oop
  492. else if (Ext=MMX)|(Ext=SSE)
  493.     movq    mm1,[esi]
  494.     movq    mm2,[esi+8]
  495.     paddusb mm1,mm0
  496.     paddusb mm2,mm0
  497.     movq    [esi],mm1
  498.     movq    [esi+8],mm2
  499.     add     esi,16
  500.     sub     ecx,16
  501.     jnz     .oop
  502. else
  503.     movaps  xmm1,[esi]
  504.     paddusb xmm1,xmm0
  505.     movaps  [esi],xmm1
  506.     add     esi,16
  507.     sub     ecx,16
  508.     jnz     .oop
  509. end if
  510.  
  511. .no_inc_bright:
  512.  
  513.  
  514.     cmp     [dec_bright_flag],0
  515.     je      .no_dec_bright
  516.     movzx   ebx,[dec_bright_flag]
  517.     shl     ebx,4
  518.     mov     esi,screen
  519.     mov     ecx,SIZE_X*SIZE_Y*3
  520. if (Ext = MMX)|(Ext = SSE)
  521.     mov      bh,bl
  522.     push     bx
  523.     shl      ebx,16
  524.     pop      bx
  525.     push     ebx
  526.     push     ebx
  527.     movq     mm0,[esp]
  528.     add      esp,8
  529. else if Ext >=SSE2
  530.     mov      bh,bl
  531.     push     bx
  532.     shl      ebx,16
  533.     pop      bx
  534.     movd     xmm0,ebx
  535.     shufps   xmm0,xmm0,0
  536. end if
  537.  .oop1:
  538. if Ext=NON
  539.     lodsb
  540.     sub     al,bl
  541.     jb      @f
  542.     mov     [esi-1],al
  543.     loop    .oop1
  544.    @@:
  545.     mov     byte[esi-1],0
  546.     loop    .oop1
  547. else if (Ext = MMX)|(Ext=SSE)
  548.     movq    mm1,[esi]
  549.     psubusb mm1,mm0
  550.     movq    [esi],mm1
  551.     add     esi,8
  552.     sub     ecx,8
  553.     jnz     .oop1
  554. else
  555.     movaps  xmm1,[esi]
  556.     psubusb xmm1,xmm0
  557.     movaps  [esi],xmm1
  558.     add     esi,16
  559.     sub     ecx,16
  560.     jnz     .oop1
  561. end if
  562.   .no_dec_bright:
  563. ;======================================commmented====================
  564. if 0
  565. if Ext >= SSE
  566.     cmp     [max_flag],0
  567.     je      .no_max
  568.     ;movzx   ebx,[max_flag]
  569.  .again_max:
  570. ;    push       ecx
  571.     mov        edi,screen
  572.     mov        ecx,SIZE_X*3/4
  573.   ;   ;    pxor       mm5,mm5
  574.     xor        eax,eax
  575.     rep        stosd
  576.  
  577.     mov        ecx,(SIZE_X*(SIZE_Y-3))*3/8
  578.   .calc_max:
  579.   @@:
  580.     movq       mm0,[edi+SIZE_X*3]
  581.     movq       mm1,[edi-SIZE_X*3]
  582.     movq       mm2,[edi-3]
  583.     movq       mm3,[edi+3]
  584.  
  585.     pmaxub      mm0,mm1
  586.     pmaxub      mm2,mm3
  587.     pmaxub      mm0,mm2
  588.  
  589.     movq       [edi],mm0
  590.     add         edi,8
  591.     loop        @b
  592.  
  593.     xor        eax,eax
  594.     mov        ecx,SIZE_X*3/4
  595.     rep        stosd
  596. end if
  597.  
  598. .no_max:
  599.  
  600. if Ext >= SSE
  601.     cmp     [min_flag],0
  602.     je      .no_min
  603. ;    push       ecx
  604.     mov        edi,screen
  605.     mov        ecx,SIZE_X*3/4
  606.   ;   ;    pxor       mm5,mm5
  607.     xor        eax,eax
  608.     rep        stosd
  609.  
  610.     mov        ecx,(SIZE_X*(SIZE_Y-3))*3/8
  611.   @@:
  612.     movq       mm0,[edi+SIZE_X*3]
  613.     movq       mm1,[edi-SIZE_X*3]
  614.     movq       mm2,[edi-3]
  615.     movq       mm3,[edi+3]
  616.  
  617.     pminub      mm0,mm1
  618.     pminub      mm2,mm3
  619.     pminub      mm0,mm2
  620.  
  621.     movq       [edi],mm0
  622.     add         edi,8
  623.     loop        @b
  624.  
  625.     xor        eax,eax
  626.  
  627.     mov        ecx,SIZE_X*3/4
  628.     rep        stosd
  629. end if
  630. .no_min:
  631. end if
  632. ;========================commented=====================================
  633.     RDTSC
  634.     sub eax,[esp]
  635.     sub eax,41
  636. ;    pop     eax
  637.  
  638.     mov     ecx,10
  639.   .dc:
  640.     xor     edx,edx
  641.     mov     edi,10
  642.     div     edi
  643.     add     dl,30h
  644.     mov     [STRdata+ecx-1],dl
  645.     loop    .dc
  646.     pop eax
  647.  
  648.     mov     eax,7           ; put image
  649.     mov     ebx,screen
  650.     mov     ecx,SIZE_X shl 16 + SIZE_Y
  651.     mov     edx,5 shl 16 + 23
  652.     int     0x40
  653.  
  654.         mov  eax,13
  655.     mov  ebx,530*65536+60
  656.     mov  ecx,510*65536+9
  657.     xor  edx,edx
  658.     int  40h
  659.        
  660.     mov  eax,4                     ; function 4 : write text to window
  661.     mov  ebx,530*65536+510         ; [x start] *65536 + [y start]
  662.     mov  ecx,0x00888888
  663.     mov  edx,STRdata               ; pointer to text beginning
  664.     mov  esi,10                    ; text length
  665.     int  40h
  666.  
  667.  
  668.    ; addsubps xmm0,xmm0
  669.  
  670.  
  671.    jmp     still
  672.  
  673. ;--------------------------------------------------------------------------------
  674. ;-------------------------PROCEDURES---------------------------------------------
  675. ;--------------------------------------------------------------------------------
  676. include "TEX3.INC"
  677. include "FLAT_CAT.INC"
  678. include "TEX_CAT.INC"
  679. include "BUMP_CAT.INC"
  680. include "3DMATH.INC"
  681. include "GRD_LINE.INC"
  682. include "GRD3.INC"
  683. include "FLAT3.INC"
  684. include "BUMP3.INC"
  685. include "B_PROCS.INC"
  686. include "A_PROCS.INC"
  687. include "GRD_CAT.INC"
  688. include "BUMP_TEX.INC"
  689. include "GRD_TEX.INC"
  690. include "TWO_TEX.INC"
  691.  
  692.  
  693.  
  694. alloc_buffer_mem:
  695.     movzx    ecx,[size_x]
  696.     movzx    eax,[size_y]
  697.     mul      ecx
  698.     lea      ecx,[eax*3]
  699.     push     ecx
  700.     shl      eax,2
  701.     add      ecx,eax
  702.     add      ecx,MEM_END
  703.     mov      ebx,1
  704.     mov      eax,64     ; allocate mem  - resize app mem
  705.     int      0x40
  706.     mov      [screen_ptr],MEM_END
  707.     mov      [Zbuffer_ptr],MEM_END
  708.     pop      ecx
  709.     add      [Zbuffer_ptr],ecx
  710. ret
  711.  
  712. update_flags:
  713. ; updates flags and writing flag description
  714. ; in    ah - button number
  715.         push    ax
  716.         mov     edi,menu
  717.       .ch_another:
  718.         cmp     ah,byte[edi]     ; ah = button id
  719.         jne     @f
  720.         mov     bl,byte[edi+11]  ; max_flag + 1
  721.         cmp     bl,255
  722.         je      .no_write
  723.         inc     byte[edi+12]     ; flag
  724.         cmp     byte[edi+12],bl
  725.         jne     .write
  726.         mov     byte[edi+12],0
  727.         jmp     .write
  728.       @@:
  729.         add     edi,17
  730.         cmp     byte[edi],-1
  731.         jne     .ch_another
  732.      .write:
  733. ;     clreol   {pascal never dies}
  734. ;          * eax = 13 - function number
  735. ;  * ebx = [coordinate on axis x]*65536 + [size on axis x]
  736. ;  * ecx = [coordinate on axis y]*65536 + [size on axis y]
  737. ;  * edx = color 0xRRGGBB or 0x80RRGGBB for gradient fill
  738.  
  739.         mov     eax,13                           ; function 13 write rectangle
  740.         movzx   ecx,byte[edi]
  741.         sub     cl,2
  742.         lea     ecx,[ecx*3]
  743.         lea     ecx,[ecx*5]
  744.         add     ecx,28
  745.         shl     ecx,16
  746.         add     ecx,14                          ;  ecx = [coord y]*65536 + [size y]
  747.         mov     ebx,(SIZE_X+12+70)*65536+25     ; [x start] *65536 + [size x]
  748.         mov     edx,0x00000000                  ;  color  0x00RRGGBB
  749.         int     0x40
  750.  
  751.         mov     eax,4                           ; function 4 : write text to window
  752.         movzx   ebx,byte[edi]
  753.         sub     bl,2
  754.         lea     ebx,[ebx*3]
  755.         lea     ebx,[ebx*5]
  756.         add     ebx,(SIZE_X+12+70)*65536+28     ; [x start] *65536 + [y start]
  757.         mov     ecx,0x20ddeeff                  ; font 1 & color ( 0xF0RRGGBB )
  758.         movzx   edx,byte[edi+12]                ; current flag
  759.         shl     edx,2                           ; * 4 = text length
  760.         add     edx,dword[edi+13]               ; pointer to text beginning
  761.         mov     esi,4                           ; text length -
  762.                                                 ; flag description 4 characters
  763.         int     0x40
  764.  
  765.      .no_write:
  766.         pop     ax
  767. ret
  768. normalize_all_light_vectors:
  769.         mov     edi,lights
  770.      @@:
  771.         call    normalize_vector           ;       3dmath.inc
  772.         add     edi,LIGHT_SIZE
  773.         cmp     edi,lightsend   ;ecx
  774.         jl      @b
  775. ret
  776.  
  777. calc_bumpmap_coords:      ; map texture, bump
  778. ;macro .comment222
  779. ;                                ; planar mapping
  780. ;        mov     esi,points
  781. ;        mov     edi,tex_points
  782. ;      @@:
  783. ;         add     esi,2
  784. ;         movsd
  785. ;         cmp     dword[esi],dword -1
  786. ;         jne     @b
  787.  
  788. ;      .Pi2  equ dword[ebp-4]
  789.  
  790. ;      mov   ebp,esp
  791. ;      sub   esp,4
  792.  
  793.       fninit
  794.       fldpi
  795.       fadd      st,st
  796.       mov       esi,[points_ptr]
  797.       mov       edi,tex_points
  798.       movzx     ecx,[points_count_var]
  799.       inc       ecx
  800. ;      cmp       [map_tex_flag],1
  801. ;      jne       .cylindric
  802.       ; spherical mapping around y axle
  803.  
  804.    @@:
  805.       fld       dword[esi]     ; x coord
  806.       fld       dword[esi+8]   ; z coord
  807.       fpatan                   ; arctg(st1/st)
  808. ;      fdiv      .Pi2
  809.       fdiv      st0,st1
  810.       fimul     [tex_x_div2]
  811.       fiadd     [tex_x_div2]
  812.       fistp     word[edi]      ; x
  813.  
  814.       fld       dword[esi+4]   ; y coord
  815.       fld       dword[esi]     ; x
  816.       fmul      st,st0
  817.       fld       dword[esi+4]   ; y
  818.       fmul      st,st0
  819.       fld       dword[esi+8]   ; z
  820.       fmul      st,st0
  821.       faddp
  822.       faddp
  823.       fsqrt
  824.       fpatan
  825.       fldpi
  826.       fdivp
  827.       fimul    [tex_y_div2]
  828.       fiadd    [tex_y_div2]
  829.       fistp    word[edi+2]     ; y
  830.  
  831.       add      esi,12
  832.       add      edi,4
  833.       loop     @b
  834.       ffree    st0
  835. ;      jmp      .end_map
  836. ;  .cylindric:
  837. ;       fld     dword[esi]     ; around y axle
  838. ;       fld     dword[esi+8]
  839. ;       fpatan
  840. ;       fdiv    st0,st1
  841. ;       fimul   [tex_x_div2]
  842. ;       fiadd   [tex_x_div2]
  843. ;       fistp   word[edi]
  844.  
  845. ;       fld     dword[esi+4]
  846. ;       fimul   [tex_y_div2]
  847. ;       fiadd   [tex_y_div2]
  848. ;       fistp   word[edi+2]
  849.  
  850. ;       add     esi,12
  851. ;       add     edi,4
  852. ;       loop    .cylindric
  853. ;       ffree    st0
  854. ;;      mov      esp,ebp
  855. ;   .end_map:
  856. ret
  857.  
  858.  
  859. init_envmap2:         ; do env_map using many light sources
  860. ;env_map 512 x 512 x 3 bytes
  861. .temp  equ word   [ebp-2]
  862. .nEy   equ word  [ebp-4]
  863. .nEx   equ word  [ebp-6]
  864. .col_r equ    [ebp-8]
  865. .col_g equ    [ebp-9]
  866. .col_b equ    [ebp-10]
  867.  
  868.          push     ebp
  869.          mov      ebp,esp
  870.          sub      esp,20
  871.          mov      edi,envmap
  872.          fninit
  873.  
  874.          mov      dx,- TEX_Y / 2 ;256   ; dx - vertical coordinate = y
  875.     .ie_ver:
  876.          mov      cx,- TEX_X / 2 ;256   ; cx - horizontal coord = x
  877.     .ie_hor:
  878.          xor      ebx,ebx
  879.          mov      dword .col_b, 0
  880.      .light:
  881.          lea      esi,[lights+ebx]
  882.          fld      dword[esi]     ; light vector x cooficient
  883.          fimul    [tex_x_div2] ;[i256]
  884.          mov      .temp,cx
  885.          fisubr   .temp
  886.          fistp    .nEx
  887.          fld      dword[esi+4]   ; light vector y cooficient
  888.          fimul    [tex_y_div2] ;[i256]
  889.          mov      .temp,dx
  890.          fisubr   .temp
  891.          fistp    .nEy
  892.  
  893.          cmp      .nEx,- TEX_X / 2 ;256
  894.          jl       .update_counters
  895.          cmp      .nEy,- TEX_Y / 2 ;256
  896.          jl       .update_counters
  897.          cmp      .nEx,TEX_X / 2 ;256
  898.          jg       .update_counters
  899.          cmp      .nEy,TEX_Y / 2 ;256
  900.          jg       .update_counters
  901.  
  902.          fild     .nEx
  903.          fmul     st,st0
  904.          fild     .nEy
  905.          fmul     st,st0
  906.          faddp
  907.          fsqrt
  908.          fisubr   [i256]
  909.          fmul     [env_const]
  910.          fidiv    [i256]   ; st - 'virtual' dot product
  911.  
  912.          fcom     [dot_max]
  913.          fstsw    ax
  914.          sahf
  915.          jb       @f
  916.          ffree    st
  917.          fld1     ;[dot_max]
  918.       @@:
  919.          fcom     [dot_min]
  920.          fstsw    ax
  921.          sahf
  922.          ja       @f
  923.          ffree    st
  924.          fldz     ;[dot_min]
  925.       @@:
  926.          push     ebp
  927.          movzx    ax,byte[esi+21]
  928.          push     ax  ;- shines
  929.          mov      al,byte[esi+14]   ; b    orginal color
  930.          push     ax
  931.          mov      al,byte[esi+13]   ; g
  932.          push     ax
  933.          mov      al,byte[esi+12]   ; r
  934.          push     ax
  935.          mov      al,byte[esi+20]   ; b     max color
  936.          push     ax
  937.          mov      al,byte[esi+19]   ; g
  938.          push     ax
  939.          mov      al,byte[esi+18]   ; r
  940.          push     ax
  941.          mov      al,byte[esi+17]   ; b    min col
  942.          push     ax
  943.          mov      al,byte[esi+16]   ; g
  944.          push     ax
  945.          mov      al,byte[esi+15]   ; r
  946.          push     ax
  947.          push     eax         ; earlier - dot pr
  948.       ;  fstp     .dot_product
  949.       ;  push     .dot_product
  950.          call     calc_one_col
  951.          pop      ebp
  952.          ; eax-0x00rrggbb
  953.          cmp      al,.col_b
  954.          jbe      @f
  955.          mov      .col_b,al
  956.    @@:                        ;  eax - ggbb00rr
  957.          shr      ax,8
  958.          cmp      al,.col_g
  959.          jbe      @f
  960.          mov      .col_g,al
  961.    @@:                        ;  eax - bb0000gg
  962.          shr      eax,16
  963.          cmp      al,.col_r
  964.          jbe      @f
  965.          mov      .col_r,al
  966.    @@:
  967.    .update_counters:                     ; update and jump when neccesery
  968.          add      ebx,LIGHT_SIZE
  969.          cmp      bx,[all_lights_size]
  970.          jl       .light    ; next_light
  971.          mov      eax,dword .col_b
  972.          stosd
  973.          dec      edi
  974.  
  975.          inc      cx
  976.          cmp      cx,TEX_X / 2 ;256
  977.          jne      .ie_hor
  978.  
  979.          inc      dx
  980.          cmp      dx,TEX_Y / 2 ;256
  981.          jne     .ie_ver
  982.  
  983.          mov     esp,ebp
  984.          pop     ebp
  985. ret
  986.  
  987.  
  988.  
  989. do_color_buffer:         ; do color buffer for Gouraud, flat shading
  990. ;env_map 512 x 512 x 3 bytes    ; many lights using
  991. .temp  equ word   [ebp-2]
  992. .nz    equ dword  [ebp-6]  ; dword
  993. .ny    equ dword  [ebp-10]
  994. .nx    equ dword  [ebp-14]
  995. .col_r equ    [ebp-16]
  996. .col_g equ    [ebp-17]
  997. .col_b equ    [ebp-18]
  998.  
  999.          push     ebp
  1000.          mov      ebp,esp
  1001.          sub      esp,20
  1002.          mov      edi,color_map
  1003.          fninit
  1004.  
  1005.          mov      dx,- TEX_Y / 2 ;-256   ; dx - vertical coordinate = y
  1006.     .ie_ver:
  1007.          mov      cx,- TEX_X / 2 ;256   ; cx - horizontal coord = x
  1008.     .ie_hor:
  1009.          mov      .temp,cx
  1010.          fild     .temp
  1011.          fidiv    [i256]   ;st = Nx - vector normal x cooficient
  1012.          fst      .nx
  1013.          fmul     st,st0
  1014.          mov      .temp,dx
  1015.          fild     .temp
  1016.          fidiv    [i256]   ; st = Ny - vector normal y coeficient
  1017.          fst      .ny
  1018.          fmul     st,st0
  1019.          faddp
  1020.          fld1
  1021.          fchs
  1022.          faddp
  1023.          fabs
  1024.          fsqrt
  1025.          fchs
  1026.          fstp     .nz              ; st - Nz - vect normal z coeficient
  1027.          xor      ebx,ebx
  1028.          mov      dword .col_b, 0
  1029.      .light:
  1030.          push     edi   ;env_map
  1031.          lea      esi,[lights+ebx]
  1032.          lea      edi,.nx
  1033.          call     dot_product
  1034.          pop      edi
  1035.          fcom     [dot_min]
  1036.          fstsw    ax
  1037.          sahf
  1038.          ja       .env_ok1  ;compare with dot_max
  1039.          ffree    st
  1040.  
  1041.         jmp       .update_counters
  1042.       .env_ok1:
  1043.          fcom    [dot_max]
  1044.          fstsw   ax
  1045.          sahf
  1046.          jb      .env_ok2     ; calc col
  1047.          ffree   st
  1048.          jmp     .update_counters
  1049.       .env_ok2:            ;calc col
  1050.          push     ebp
  1051.          movzx    ax,byte[esi+21]
  1052.          push     ax  ;- shines
  1053.          mov      al,byte[esi+14]   ; b    orginal color
  1054.          push     ax
  1055.          mov      al,byte[esi+13]   ; g
  1056.          push     ax
  1057.          mov      al,byte[esi+12]   ; r
  1058.          push     ax
  1059.          mov      al,byte[esi+20]   ; b     max color
  1060.          push     ax
  1061.          mov      al,byte[esi+19]   ; g
  1062.          push     ax
  1063.          mov      al,byte[esi+18]   ; r
  1064.          push     ax
  1065.          mov      al,byte[esi+17]   ; b    min col
  1066.          push     ax
  1067.          mov      al,byte[esi+16]   ; g
  1068.          push     ax
  1069.          mov      al,byte[esi+15]   ; r
  1070.          push     ax
  1071.          push     eax         ; earlier - dot pr
  1072.       ;  fstp     .dot_product
  1073.       ;  push     .dot_product
  1074.          call     calc_one_col
  1075.          pop      ebp
  1076.          ; eax-0x00rrggbb
  1077.          cmp      al,.col_b
  1078.          jbe      @f
  1079.          mov      .col_b,al
  1080.    @@:
  1081.          shr      ax,8
  1082.          cmp      al,.col_g
  1083.          jbe      @f
  1084.          mov      .col_g,al
  1085.    @@:
  1086.          shr      eax,16
  1087.          cmp      al,.col_r
  1088.          jbe      @f
  1089.          mov      .col_r,al
  1090.   @@:
  1091.  .update_counters:                                  ; update and jump when neccesery
  1092.         add     ebx,LIGHT_SIZE
  1093.         cmp     bx,[all_lights_size]
  1094.         jl      .light    ; next_light
  1095.         mov     eax,dword .col_b
  1096.         stosd
  1097.         dec     edi
  1098.  
  1099.         inc     cx
  1100.         cmp     cx,TEX_X / 2 ;256
  1101.         jne     .ie_hor
  1102.  
  1103.         inc     dx
  1104.         cmp     dx,TEX_X / 2 ;256
  1105.         jne     .ie_ver
  1106.  
  1107.     .env_done:
  1108.          mov     esp,ebp
  1109.          pop     ebp
  1110. ret
  1111. if 0
  1112. init_triangles_normals:
  1113.         mov     ebx,triangles_normals
  1114.         mov     ebp,triangles
  1115.      @@:
  1116.         push    ebx
  1117.         mov     ebx,vectors
  1118.         movzx   esi,word[ebp]          ; first point index
  1119.         lea     esi,[esi*3]
  1120.         lea     esi,[points+esi*2]     ; esi - pointer to 1st 3d point
  1121.         movzx   edi,word[ebp+2]        ; second point index
  1122.         lea     edi,[edi*3]
  1123.         lea     edi,[points+edi*2]     ; edi - pointer to 2nd 3d point
  1124.         call    make_vector
  1125.         add     ebx,12
  1126.         mov     esi,edi
  1127.         movzx   edi,word[ebp+4]        ; third point index
  1128.         lea     edi,[edi*3]
  1129.         lea     edi,[points+edi*2]
  1130.         call    make_vector
  1131.         mov     edi,ebx                 ; edi - pointer to 2nd vector
  1132.         mov     esi,ebx
  1133.         sub     esi,12                  ; esi - pointer to 1st vector
  1134.         pop     ebx
  1135.         call    cross_product
  1136.         mov     edi,ebx
  1137.         call    normalize_vector
  1138.         add     ebp,6
  1139.         add     ebx,12
  1140.         cmp     dword[ebp],-1
  1141.         jne     @b
  1142. ret
  1143. end if
  1144. init_point_normals:
  1145. .x equ dword [ebp-4]
  1146. .y equ dword [ebp-8]
  1147. .z equ dword [ebp-12]
  1148. .point_number equ word [ebp-26]
  1149. .hit_faces    equ word [ebp-28]
  1150.  
  1151.         fninit
  1152.         mov       ebp,esp
  1153.         sub       esp,28
  1154.         mov       edi,[points_normals_ptr]
  1155.         mov       .point_number,0
  1156.     .ipn_loop:
  1157.         mov       .hit_faces,0
  1158.         mov       .x,0
  1159.         mov       .y,0
  1160.         mov       .z,0
  1161.         mov       esi,[triangles_ptr]
  1162.         xor       ecx,ecx              ; ecx - triangle number
  1163.     .ipn_check_face:
  1164.         xor       ebx,ebx              ; ebx - 'position' in one triangle
  1165.     .ipn_check_vertex:
  1166.         movzx     eax,word[esi+ebx]    ;  eax - point_number
  1167.         cmp       ax,.point_number
  1168.         jne       .ipn_next_vertex
  1169.         push      esi
  1170.         mov       esi,ecx
  1171.         lea       esi,[esi*3]
  1172.        ; lea       esi,[triangles_normals+esi*4]
  1173.         shl       esi,2
  1174.         add       esi,[triangles_normals_ptr]
  1175.  
  1176.         fld       .x
  1177.         fadd      dword[esi+vec_x]       ; vec_x this defined in 3dmath.asm - x cooficient
  1178.         fstp      .x                     ; of normal vactor
  1179.         fld       .y
  1180.         fadd      dword[esi+vec_y]
  1181.         fstp      .y
  1182.         fld       .z
  1183.         fadd      dword[esi+vec_z]
  1184.         fstp      .z
  1185.         pop       esi
  1186.         inc       .hit_faces
  1187.         jmp       .ipn_next_face
  1188.     .ipn_next_vertex:
  1189.         add       ebx,2
  1190.         cmp       ebx,6
  1191.         jne       .ipn_check_vertex
  1192.     .ipn_next_face:
  1193.         add       esi,6
  1194.         inc       ecx
  1195.         cmp       cx,[triangles_count_var]
  1196.         jne       .ipn_check_face
  1197.  
  1198.         fld       .x
  1199.         fidiv     .hit_faces
  1200.         fstp      dword[edi+vec_x]
  1201.         fld       .y
  1202.         fidiv     .hit_faces
  1203.         fstp      dword[edi+vec_y]
  1204.         fld       .z
  1205.         fidiv     .hit_faces
  1206.         fstp      dword[edi+vec_z]
  1207.         call      normalize_vector
  1208.         add       edi,12  ;type vector 3d
  1209.         inc       .point_number
  1210.         mov       dx,.point_number
  1211.         cmp       dx,[points_count_var]
  1212.         jne       .ipn_loop
  1213.  
  1214.         mov       esp,ebp
  1215. ret
  1216. ;===============================================================
  1217.  
  1218. init_triangles_normals2:
  1219.         mov     ebx,[triangles_normals_ptr]
  1220.         mov     ebp,[triangles_ptr]
  1221.      @@:
  1222.         push    ebx
  1223.         mov     ebx,vectors
  1224.         movzx   esi,word[ebp]          ; first point index
  1225.         lea     esi,[esi*3]
  1226. ;        lea     esi,[points+esi*2]     ; esi - pointer to 1st 3d point
  1227.         shl     esi,2
  1228.         add     esi,[points_ptr]
  1229.         movzx   edi,word[ebp+2]          ; first point index
  1230.         lea     edi,[edi*3]
  1231.         shl     edi,2
  1232.         add     edi,[points_ptr]
  1233. ;        movzx   edi,word[ebp+2]        ; second point index
  1234. ;        lea     edi,[edi*3]
  1235. ;        lea     edi,[points+edi*2]     ; edi - pointer to 2nd 3d point
  1236.         call    make_vector_r
  1237.         add     ebx,12
  1238.         mov     esi,edi
  1239.         movzx   edi,word[ebp+4]        ; third point index
  1240.         lea     edi,[edi*3]
  1241.         shl     edi,2
  1242.         add     edi,[points_ptr]
  1243. ;        lea     edi,[points+edi*2]
  1244.         call    make_vector_r
  1245.         mov     edi,ebx                 ; edi - pointer to 2nd vector
  1246.         mov     esi,ebx
  1247.         sub     esi,12                  ; esi - pointer to 1st vector
  1248.         pop     ebx
  1249.         call    cross_product
  1250.         mov     edi,ebx
  1251.         call    normalize_vector
  1252.         add     ebp,6
  1253.         add     ebx,12
  1254.         cmp     dword[ebp],-1
  1255.         jne     @b
  1256. ret
  1257.  
  1258.  
  1259. ;=================================================================
  1260. sort_triangles:
  1261.         mov     esi,[triangles_ptr]
  1262.         mov     edi,triangles_with_z
  1263.         mov     ebp,[points_translated_ptr]
  1264.  
  1265.     make_triangle_with_z:       ;makes list with triangles and z position
  1266.         movzx   eax,word[esi]
  1267.         lea     eax,[eax*3]
  1268.         movzx   ecx,word[ebp+eax*2+4]
  1269.  
  1270.         movzx   eax,word[esi+2]
  1271.         lea     eax,[eax*3]
  1272.         add     cx,word[ebp+eax*2+4]
  1273.  
  1274.         movzx   eax,word[esi+4]
  1275.         lea     eax,[eax*3]
  1276.         add     cx,word[ebp+eax*2+4]
  1277.  
  1278.         mov     ax,cx
  1279.        ; cwd
  1280.        ; idiv    word[i3]
  1281.         movsd                   ; store vertex coordinates
  1282.         movsw
  1283.         stosw                   ; middle vertex coordinate  'z' in triangles_with_z list
  1284.         cmp     dword[esi],-1
  1285.         jne     make_triangle_with_z
  1286.         movsd                   ; copy end mark
  1287.         mov     eax,4
  1288.         lea     edx,[edi-8-trizdd]
  1289.      ;   lea     edx, [edi-8]
  1290.      ;   sub     edx,[triangles_w_z_ptr]
  1291.         mov     [high],edx
  1292.         call    quicksort
  1293.         mov     eax,4
  1294.         mov     edx,[high]
  1295.         call    insertsort
  1296.         jmp     end_sort
  1297.  
  1298.     quicksort:
  1299.         mov     ecx,edx
  1300.         sub     ecx,eax
  1301.         cmp     ecx,32
  1302.         jc      .exit
  1303.         lea     ecx,[eax+edx]
  1304.         shr     ecx,4
  1305.         lea     ecx,[ecx*8-4];
  1306. ;        mov     edi,[triangles_w_z_ptr]
  1307. ;        mov     ebx,[edi+eax]
  1308. ;        mov     esi,[edi+ecx]
  1309. ;        mov     edi,[edi+edx]
  1310.         mov     ebx,[trizdd+eax]; trizdd[l]
  1311.         mov     esi,[trizdd+ecx]; trizdd[i]
  1312.         mov     edi,[trizdd+edx]; trizdd[h]
  1313.         cmp     ebx,esi
  1314.         jg      @f              ; direction NB! you need to negate these to invert the order
  1315.       if Ext=NON
  1316.         mov     [trizdd+eax],esi
  1317.         mov     [trizdd+ecx],ebx
  1318.         mov     ebx,[trizdd+eax-4]
  1319.         mov     esi,[trizdd+ecx-4]
  1320.         mov     [trizdd+eax-4],esi
  1321.         mov     [trizdd+ecx-4],ebx
  1322.         mov     ebx,[trizdd+eax]
  1323.         mov     esi,[trizdd+ecx]
  1324.       else
  1325. ;        push    ebx
  1326. ;        mov     ebx,[triangles_w_z_ptr]
  1327. ;        movq    mm0,[ebx+eax-4]
  1328. ;        movq    mm1,[ebx+ecx-4]
  1329. ;        movq    [ebx+ecx-4],mm0
  1330. ;        movq    [ebx+eax-4],mm1
  1331. ;        pop     ebx
  1332.         movq    mm0,[trizdq+eax-4]
  1333.         movq    mm1,[trizdq+ecx-4]
  1334.         movq    [trizdq+ecx-4],mm0
  1335.         movq    [trizdq+eax-4],mm1
  1336.         xchg    ebx,esi
  1337.       end if
  1338.       @@:
  1339.         cmp     ebx,edi
  1340.         jg      @f              ; direction
  1341.       if Ext=NON
  1342.         mov     [trizdd+eax],edi
  1343.         mov     [trizdd+edx],ebx
  1344.         mov     ebx,[trizdd+eax-4]
  1345.         mov     edi,[trizdd+edx-4]
  1346.         mov     [trizdd+eax-4],edi
  1347.         mov     [trizdd+edx-4],ebx
  1348.         mov     ebx,[trizdd+eax]
  1349.         mov     edi,[trizdd+edx]
  1350.       else
  1351. ;        push    ebx
  1352. ;        mov     ebx,[triangles_w_z_ptr]
  1353. ;        movq    mm0,[ebx+eax-4]
  1354. ;        movq    mm1,[ebx+edx-4]
  1355. ;        movq    [ebx+edx-4],mm0
  1356. ;        movq    [ebx+eax-4],mm1
  1357.         movq    mm0,[trizdq+eax-4]
  1358.         movq    mm1,[trizdq+edx-4]
  1359.         movq    [trizdq+edx-4],mm0
  1360.         movq    [trizdq+eax-4],mm1
  1361. ;        pop     ebx
  1362.         xchg    ebx,edi
  1363.       end if
  1364.       @@:
  1365.         cmp     esi,edi
  1366.         jg      @f              ; direction
  1367.       if Ext=NON
  1368.         mov     [trizdd+ecx],edi
  1369.         mov     [trizdd+edx],esi
  1370.         mov     esi,[trizdd+ecx-4]
  1371.         mov     edi,[trizdd+edx-4]
  1372.         mov     [trizdd+ecx-4],edi
  1373.         mov     [trizdd+edx-4],esi
  1374.       else
  1375. ;        push    ebx
  1376. ;        mov     ebx,[triangles_w_z_ptr]
  1377. ;        movq    mm0,[ebx+ecx-4]
  1378. ;        movq    mm1,[ebx+edx-4]
  1379. ;        movq    [ebx+edx-4],mm0
  1380. ;        movq    [ebx+ecx-4],mm1
  1381. ;        pop     ebx
  1382.  
  1383.         movq    mm0,[trizdq+ecx-4]
  1384.         movq    mm1,[trizdq+edx-4]
  1385.         movq    [trizdq+edx-4],mm0
  1386.         movq    [trizdq+ecx-4],mm1
  1387.         xchg    ebx,esi
  1388.       end if
  1389.       @@:
  1390.         mov     ebp,eax         ; direction
  1391.         add     ebp,8      ;   j
  1392.       if Ext=NON
  1393.         mov     esi,[trizdd+ebp]
  1394.         mov     edi,[trizdd+ecx]
  1395.         mov     [trizdd+ebp],edi
  1396.         mov     [trizdd+ecx],esi
  1397.         mov     esi,[trizdd+ebp-4]
  1398.         mov     edi,[trizdd+ecx-4]
  1399.         mov     [trizdd+ecx-4],esi
  1400.         mov     [trizdd+ebp-4],edi
  1401.       else
  1402. ;        push    ebx
  1403. ;        mov     ebx,[triangles_w_z_ptr]
  1404. ;        movq    mm0,[ebx+ebp-4]
  1405. ;        movq    mm1,[ebx+ecx-4]
  1406. ;        movq    [ebx+ecx-4],mm0
  1407. ;        movq    [ebx+ebp-4],mm1
  1408. ;        pop     ebx
  1409.  
  1410.         movq    mm0,[trizdq+ebp-4]
  1411.         movq    mm1,[trizdq+ecx-4]
  1412.         movq    [trizdq+ecx-4],mm0
  1413.         movq    [trizdq+ebp-4],mm1
  1414.       end if
  1415.         mov     ecx,edx    ;   i; direction
  1416.         mov     ebx,[trizdd+ebp]; trizdd[j]
  1417. ;        mov     ebx, [triangles_w_z_ptr]
  1418. ;        add     ebx, ebp
  1419.  
  1420.  ;       push    eax
  1421.  ;       mov     eax, [triangles_w_z_ptr]
  1422.       .loop:
  1423.         sub     ecx,8           ; direction
  1424.         cmp     [trizdd+ecx],ebx
  1425. ;        cmp     [eax+ecx],ebx
  1426.         jl      .loop           ; direction
  1427.       @@:
  1428.         add     ebp,8           ; direction
  1429.         cmp     [trizdd+ebp],ebx
  1430. ;        cmp     [eax+ebp],ebx
  1431.         jg      @b              ; direction
  1432.         cmp     ebp,ecx
  1433.         jge     @f              ; direction
  1434.       if Ext=NON
  1435.         mov     esi,[trizdd+ecx]
  1436.         mov     edi,[trizdd+ebp]
  1437.         mov     [trizdd+ebp],esi
  1438.         mov     [trizdd+ecx],edi
  1439.         mov     edi,[trizdd+ecx-4]
  1440.         mov     esi,[trizdd+ebp-4]
  1441.         mov     [trizdd+ebp-4],edi
  1442.         mov     [trizdd+ecx-4],esi
  1443.       else
  1444. ;        movq    mm0,[eax+ecx-4]
  1445. ;        movq    mm1,[eax+ebp-4]
  1446. ;        movq    [eax+ebp-4],mm0
  1447. ;        movq    [eax+ecx-4],mm1
  1448.         movq    mm0,[trizdq+ecx-4]
  1449.         movq    mm1,[trizdq+ebp-4]
  1450.         movq    [trizdq+ebp-4],mm0
  1451.         movq    [trizdq+ecx-4],mm1
  1452.       end if
  1453.         jmp     .loop
  1454. ;        pop     eax
  1455.       @@:
  1456.       if Ext=NON
  1457.         mov     esi,[trizdd+ecx]
  1458.         mov     edi,[trizdd+eax+8]
  1459.         mov     [trizdd+eax+8],esi
  1460.         mov     [trizdd+ecx],edi
  1461.         mov     edi,[trizdd+ecx-4]
  1462.         mov     esi,[trizdd+eax+4]
  1463.         mov     [trizdd+eax+4],edi
  1464.         mov     [trizdd+ecx-4],esi
  1465.       else
  1466. ;        push    edx
  1467. ;        mov     edx,[triangles_w_z_ptr]
  1468. ;        movq    mm0,[edx+ecx-4]
  1469. ;        movq    mm1,[edx+eax+4]; dir
  1470. ;        movq    [edx+eax+4],mm0; dir
  1471. ;        movq    [edx+ecx-4],mm1
  1472. ;        pop     edx
  1473.  
  1474.         movq    mm0,[trizdq+ecx-4]
  1475.         movq    mm1,[trizdq+eax+4]; dir
  1476.         movq    [trizdq+eax+4],mm0; dir
  1477.         movq    [trizdq+ecx-4],mm1
  1478.       end if
  1479.         add     ecx,8
  1480.         push    ecx edx
  1481.         mov     edx,ebp
  1482.         call    quicksort
  1483.         pop     edx eax
  1484.         call    quicksort
  1485.       .exit:
  1486.     ret
  1487.     insertsort:
  1488.         mov     esi,eax
  1489.       .start:
  1490.         add     esi,8
  1491.         cmp     esi,edx
  1492.         ja      .exit
  1493.         mov     ebx,[trizdd+esi]
  1494. ;        mov     ebx,[triangles_w_z_ptr]
  1495. ;        add     ebx,esi
  1496.       if Ext=NON
  1497.         mov     ecx,[trizdd+esi-4]
  1498.       else
  1499. ;        push    ebx
  1500. ;        mov     ebx,[triangles_w_z_ptr]
  1501. ;        movq    mm1,[ebx+esi-4]
  1502.         movq    mm1,[trizdq+esi-4]
  1503. ;        pop     ebx
  1504.       end if
  1505.         mov     edi,esi
  1506.       @@:
  1507.         cmp     edi,eax
  1508.         jna     @f
  1509. ;        push    eax
  1510. ;        mov     eax,[triangles_w_z_ptr]
  1511. ;        cmp     [eax+edi-8],ebx
  1512. ;        pop     eax
  1513.        cmp     [trizdd+edi-8],ebx
  1514.         jg      @f                 ; direction
  1515.       if Ext=NON
  1516.         mov     ebp,[trizdd+edi-8]
  1517.         mov     [trizdd+edi],ebp
  1518.         mov     ebp,[trizdd+edi-12]
  1519.         mov     [trizdd+edi-4],ebp
  1520.       else
  1521. ;        push    eax
  1522. ;        mov     eax,[triangles_w_z_ptr]
  1523. ;        movq    mm0,[eax+edi-12]
  1524. ;        movq    [eax+edi-4],mm0
  1525.         movq    mm0,[trizdq+edi-12]
  1526.         movq    [trizdq+edi-4],mm0
  1527. ;        pop     eax
  1528.       end if
  1529.         sub     edi,8
  1530.         jmp     @b
  1531.       @@:
  1532.       if Ext=NON
  1533.         mov     [trizdd+edi],ebx
  1534.         mov     [trizdd+edi-4],ecx
  1535.       else
  1536. ;        push    eax
  1537. ;        mov     eax,[triangles_w_z_ptr]
  1538. ;        movq    [eax+edi-4],mm1
  1539.         movq    [trizdq+edi-4],mm1
  1540. ;        pop     eax
  1541.       end if
  1542.         jmp     .start
  1543.       .exit:
  1544.     ret
  1545.    end_sort:
  1546.     ; translate triangles_with_z to sorted_triangles
  1547.         mov     esi,triangles_with_z
  1548. ;        mov      esi,[triangles_w_z_ptr]
  1549.       ;  mov     edi,sorted_triangles
  1550.         mov      edi,[triangles_ptr]
  1551.     again_copy:
  1552.       if Ext=NON
  1553.         movsd
  1554.         movsw
  1555.         add     esi,2
  1556.       else
  1557.         movq    mm0,[esi]
  1558.         movq    [edi],mm0
  1559.         add     esi,8
  1560.         add     edi,6
  1561.       end if
  1562.         cmp     dword[esi],-1
  1563.         jne     again_copy
  1564. ;      if Ext=MMX
  1565. ;        emms
  1566. ;      end if
  1567.         movsd  ; copy end mark too
  1568. ret
  1569.  
  1570. clrscr:
  1571.         mov     edi,screen
  1572.         mov     ecx,SIZE_X*SIZE_Y*3/4
  1573.         xor     eax,eax
  1574.       if Ext=NON
  1575.         rep     stosd
  1576.       else
  1577.         pxor    mm0,mm0
  1578.       @@:
  1579.         movq    [edi+00],mm0
  1580.         movq    [edi+08],mm0
  1581.         movq    [edi+16],mm0
  1582.         movq    [edi+24],mm0
  1583.         add     edi,32
  1584.         sub     ecx,8
  1585.         jnc     @b
  1586.       end if
  1587. ret
  1588.  
  1589.  
  1590. draw_triangles:
  1591.         mov esi,[triangles_ptr]
  1592.         mov [edges_counter],0
  1593.     .again_dts:
  1594.         mov ebp,[points_translated_ptr]
  1595.       if Ext=NON
  1596.         movzx   eax,word[esi]
  1597.         mov     [point_index1],ax
  1598.         lea     eax,[eax*3]
  1599.         add     eax,eax
  1600.         push    ebp
  1601.         add     ebp,eax
  1602.         mov     eax,[ebp]
  1603.         mov     dword[xx1],eax
  1604.         mov     eax,[ebp+4]
  1605.         mov     [zz1],ax
  1606.         pop     ebp
  1607.  
  1608.  
  1609.         movzx   eax,word[esi+2]
  1610.         mov     [point_index2],ax
  1611.         lea     eax,[eax*3]
  1612.         add     eax,eax
  1613.         push    ebp
  1614.         add     ebp,eax
  1615.         mov     eax,[ebp]
  1616.         mov     dword[xx2],eax
  1617.         mov     eax,[ebp+4]
  1618.         mov     [zz2],ax
  1619.         pop     ebp
  1620.  
  1621.  
  1622.         movzx   eax,word[esi+4]        ; xyz3 = [ebp+[esi+4]*6]
  1623.         mov     [point_index3],ax
  1624.         lea     eax,[eax*3]
  1625.         add     eax,eax
  1626.     ;    push    ebp
  1627.         add     ebp,eax
  1628.         mov     eax,[ebp]
  1629.         mov     dword[xx3],eax
  1630.         mov     eax,[ebp+4]
  1631.         mov     [zz3],ax
  1632.       else
  1633.         mov     eax,dword[esi]           ; don't know MMX
  1634.         mov     dword[point_index1],eax
  1635.        ; shr     eax,16
  1636.        ; mov     [point_index2],ax
  1637.         mov     ax,word[esi+4]
  1638.         mov     [point_index3],ax
  1639.         movq    mm0,[esi]
  1640.         pmullw  mm0,qword[const6]
  1641.         movd    eax,mm0
  1642.         psrlq   mm0,16
  1643.         movd    ebx,mm0
  1644.         psrlq   mm0,16
  1645.         movd    ecx,mm0
  1646.         and     eax,0FFFFh
  1647.         and     ebx,0FFFFh
  1648.         and     ecx,0FFFFh
  1649.         movq    mm0,[ebp+eax]
  1650.         movq    mm1,[ebp+ebx]
  1651.         movq    mm2,[ebp+ecx]
  1652.         movq    qword[xx1],mm0
  1653.         movq    qword[xx2],mm1
  1654.         movq    qword[xx3],mm2
  1655. ;        emms
  1656.       end if                              ; *********************************
  1657.         push esi                          ;
  1658.         fninit                            ; DO culling AT FIRST
  1659.         cmp     [culling_flag],1          ; (if culling_flag = 1)
  1660.         jne     .no_culling
  1661.         mov     esi,point_index1          ; *********************************
  1662.         mov     ecx,3                     ;
  1663.       @@:
  1664.         movzx   eax,word[esi]
  1665.         lea     eax,[eax*3]
  1666.         shl     eax,2
  1667.         add     eax,[points_normals_rot_ptr]
  1668. ;        lea     eax,[eax+point_normals_rotated]
  1669.         fld     dword[eax+8]             ; *****************************
  1670.         ftst                             ; CHECKING OF Z COOFICIENT OF
  1671.         fstsw   ax                       ; NORMAL VECTOR
  1672.         sahf
  1673.         jb      @f
  1674.         ffree   st
  1675.         loop    @b
  1676.         jmp     .end_draw   ; non visable
  1677.       @@:
  1678.         ffree   st  ;is visable
  1679.       .no_culling:
  1680.         cmp     [dr_flag],0               ; draw type flag
  1681.         je      .flat_draw
  1682.         cmp     [dr_flag],2
  1683.         je      .env_mapping
  1684.         cmp     [dr_flag],3
  1685.         je      .bump_mapping
  1686.         cmp     [dr_flag],4
  1687.         je      .tex_mapping
  1688.         cmp     [dr_flag],5
  1689.         je      .rainbow
  1690.         cmp     [dr_flag],7
  1691.         je      .grd_tex
  1692.         cmp     [dr_flag],8
  1693.         je      .two_tex
  1694.         cmp     [dr_flag],9
  1695.         je      .bump_tex
  1696.         cmp     [dr_flag],10
  1697.         je      .cubic_env_mapping
  1698.         cmp     [dr_flag],11
  1699.         je      .draw_smooth_line
  1700.                                       ; ****************
  1701.         mov     esi,point_index3      ; do Gouraud shading
  1702.         mov     ecx,3
  1703.       .again_grd_draw:
  1704.         movzx   eax,word[esi]
  1705.         shl     eax,2
  1706.         lea     eax,[eax*3]
  1707.         add     eax,[points_normals_rot_ptr]
  1708.         ; texture x=(rotated point normal -> x * 255)+255
  1709.         fld     dword[eax]       ; x cooficient of normal vector
  1710.         fimul   [correct_tex]
  1711.         fiadd   [correct_tex]
  1712.         fistp   word[esp-2]
  1713.         ; texture y=(rotated point normal -> y * 255)+255
  1714.         fld     dword[eax+4]      ; y cooficient
  1715.         fimul   [correct_tex]
  1716.         fiadd   [correct_tex]
  1717.         fistp   word[esp-4]
  1718.  
  1719.         movzx    eax,word[esp-4]
  1720.         movzx    ebx,word[esp-2]
  1721.         shl      eax,TEX_SHIFT
  1722.         add      eax,ebx
  1723.         lea      eax,[eax*3+color_map]
  1724.         mov      eax,dword[eax]
  1725.         cmp     [catmull_flag],1      ; put on stack z coordinate if necessary
  1726.         jne      @f
  1727.         lea      edx,[ecx*3]
  1728.         push     word[edx*2+xx1-2]    ; zz1 ,2 ,3
  1729.       @@:
  1730.         ror      eax,16               ; eax -0xxxrrggbb -> 0xggbbxxrr
  1731.         xor      ah,ah
  1732.         push     ax         ;r
  1733.         rol      eax,8                ; eax-0xggbb00rr -> 0xbb00rrgg
  1734.         xor      ah,ah
  1735.         push     ax         ;g
  1736.         shr      eax,24
  1737.         push     ax         ;b
  1738.  
  1739.         sub      esi,2
  1740.         dec      cx
  1741.         jnz      .again_grd_draw
  1742.         jmp      .both_draw
  1743.  
  1744.    ;     movzx   edi,[point_index3]   ;gouraud shading according to light vector
  1745.    ;     lea     edi,[edi*3]
  1746.    ;     lea     edi,[4*edi+point_normals_rotated] ; edi - normal
  1747.    ;     mov     esi,light_vector
  1748.    ;     call    dot_product
  1749.    ;     fabs
  1750.    ;     fimul   [orginal_color_r]
  1751.    ;     fistp   [temp_col]
  1752.    ;     and     [temp_col],0x00ff
  1753.    ;     push    [temp_col]
  1754.    ;     push    [temp_col]
  1755.    ;     push    [temp_col]
  1756.  
  1757.    ;     movzx   edi,[point_index2]
  1758.    ;     lea     edi,[edi*3]
  1759.    ;     lea     edi,[4*edi+point_normals_rotated] ; edi - normal
  1760.    ;     mov     esi,light_vector
  1761.    ;     call    dot_product
  1762.    ;     fabs
  1763.    ;     fimul   [orginal_color_r]
  1764.    ;     fistp    [temp_col]
  1765.    ;     and     [temp_col],0x00ff
  1766.    ;     push    [temp_col]
  1767.    ;     push    [temp_col]
  1768.    ;     push    [temp_col]
  1769.  
  1770.    ;     movzx   edi,[point_index1]
  1771.    ;     lea     edi,[edi*3]
  1772.    ;     lea     edi,[4*edi+point_normals_rotated] ; edi - normal
  1773.    ;     mov     esi,light_vector
  1774.    ;     call    dot_product
  1775.    ;     fabs
  1776.    ;     fimul   [orginal_color_r]
  1777.    ;     fistp   [temp_col]
  1778.    ;     and     [temp_col],0x00ff
  1779.    ;     push    [temp_col]
  1780.    ;     push    [temp_col]
  1781.    ;     push    [temp_col]
  1782.    .rainbow:
  1783.         cmp     [catmull_flag],1      ; put on stack z coordinate if necessary
  1784.         jne      @f
  1785.         push     [zz3]
  1786.       @@:
  1787.         mov      eax,dword[yy3]
  1788.         mov      ebx,0x00ff00ff
  1789.         and      eax,ebx
  1790.         push     eax
  1791.         neg      al
  1792.         push     ax
  1793.         cmp     [catmull_flag],1
  1794.         jne      @f
  1795.         push     [zz2]
  1796.       @@:
  1797.         mov      eax,dword[yy2]
  1798.         and      eax,ebx
  1799.         push     eax
  1800.         neg      al
  1801.         push     ax
  1802.         cmp     [catmull_flag],1
  1803.         jne      @f
  1804.         push     [zz1]
  1805.       @@:
  1806.         mov      eax,dword[yy1]
  1807.         and      eax,ebx
  1808.         push     eax
  1809.         neg      al
  1810.         push     ax
  1811.     .both_draw:
  1812.         mov     eax,dword[xx1]
  1813.         ror     eax,16
  1814.         mov     ebx,dword[xx2]
  1815.         ror     ebx,16
  1816.         mov     ecx,dword[xx3]
  1817.         ror     ecx,16
  1818.         lea     edi,[screen]
  1819.         cmp     [catmull_flag],0
  1820.         je      @f
  1821.   ;      lea     esi,[Z_buffer]
  1822.         mov     esi,[Zbuffer_ptr]
  1823.         call    gouraud_triangle_z
  1824.         jmp     .end_draw
  1825.        @@:
  1826.         call    gouraud_triangle
  1827.         jmp     .end_draw
  1828.  
  1829.      .flat_draw:                     ;**************************
  1830.                                      ; FLAT DRAWING
  1831.         movzx   eax,[point_index1]
  1832.         movzx   ebx,[point_index2]
  1833.         movzx   ecx,[point_index3]
  1834.         shl     eax,2
  1835.         shl     ebx,2
  1836.         shl     ecx,2
  1837.         lea     eax,[eax*3]  ;+point_normals_rotated]
  1838.         add     eax,[points_normals_rot_ptr]
  1839.         lea     ebx,[ebx*3]  ;+point_normals_rotated]
  1840.         add     ebx,[points_normals_rot_ptr]
  1841.         lea     ecx,[ecx*3]  ;+point_normals_rotated]
  1842.         add     ecx,[points_normals_rot_ptr]
  1843.         fld     dword[eax]      ; x cooficient of normal vector
  1844.         fadd    dword[ebx]
  1845.         fadd    dword[ecx]
  1846.         fidiv   [i3]
  1847.         fimul   [correct_tex]
  1848.         fiadd   [correct_tex]
  1849.         fistp   dword[esp-4]    ; x temp variables
  1850.         fld     dword[eax+4]    ; y cooficient of normal vector
  1851.         fadd    dword[ebx+4]
  1852.         fadd    dword[ecx+4]
  1853.         fidiv   [i3]
  1854.         fimul   [correct_tex]
  1855.         fiadd   [correct_tex]
  1856.         fistp   dword[esp-8]   ;  y
  1857.         mov     edx,dword[esp-8]
  1858.         shl     edx,TEX_SHIFT
  1859.         add     edx,dword[esp-4]
  1860.         lea     eax,[3*edx+color_map]
  1861.         mov     edx,dword[eax]
  1862.  
  1863.         and     edx,0x00ffffff    ; edx = 0x00rrggbb
  1864.  
  1865.  
  1866.  
  1867.      ;   mov     ax,[zz1]      ; z position depend draw
  1868.      ;   add     ax,[zz2]
  1869.      ;   add     ax,[zz3]
  1870.      ;   cwd
  1871.      ;   idiv    [i3] ;    = -((a+b+c)/3+130)
  1872.      ;   add     ax,130
  1873.      ;   neg     al
  1874.      ;   xor     edx,edx
  1875.      ;   mov     ah,al           ;set color according to z position
  1876.      ;   shl     eax,8
  1877.      ;   mov     edx,eax
  1878.  
  1879.         mov     eax,dword[xx1]
  1880.         ror     eax,16
  1881.         mov     ebx,dword[xx2]
  1882.         ror     ebx,16
  1883.         mov     ecx,dword[xx3]
  1884.         ror     ecx,16
  1885.        ; mov     edi,screen
  1886.         lea     edi,[screen]
  1887.         cmp     [catmull_flag],0
  1888.         je      @f
  1889.    ;     lea     esi,[Z_buffer]
  1890.         mov     esi,[Zbuffer_ptr]
  1891.         push    word[zz3]
  1892.         push    word[zz2]
  1893.         push    word[zz1]
  1894.         call    flat_triangle_z
  1895.         jmp     .end_draw
  1896.       @@:
  1897.         call    draw_triangle
  1898.         jmp     .end_draw
  1899.       .env_mapping:
  1900.        ; fninit
  1901.         cmp     [catmull_flag],0
  1902.         je      @f
  1903.         push    [zz3]
  1904.         push    [zz2]
  1905.         push    [zz1]
  1906.       @@:
  1907.         mov     esi,point_index1
  1908.         sub     esp,12
  1909.         mov     edi,esp
  1910.         mov     ecx,3
  1911.       @@:
  1912.         movzx   eax,word[esi]
  1913.         lea     eax,[eax*3]
  1914.         shl     eax,2
  1915.         add     eax,[points_normals_rot_ptr]       ;point_normals_rotated
  1916. ; #
  1917. ;        fld     dword[eax]
  1918. ;        fmul    dword[eax+4]
  1919. ;        fld1
  1920. ;        fld1
  1921. ;        faddp
  1922. ;        fmulp
  1923. ;        fimul   [correct_tex]
  1924. ;        fiadd   [correct_tex]
  1925. ;        fistp   word[edi]
  1926. ;        mov     word[edi+2],0
  1927. ;;        fistp   word[edi+2]
  1928. ; # last change
  1929.         ; texture x=(rotated point normal -> x * 255)+255
  1930.         fld     dword[eax]
  1931.         fimul   [correct_tex]
  1932.         fiadd   [correct_tex]
  1933.         fistp   word[edi]
  1934.         ; texture y=(rotated point normal -> y * 255)+255
  1935.         fld     dword[eax+4]
  1936.         fimul   [correct_tex]
  1937.         fiadd   [correct_tex]
  1938.         fistp   word[edi+2]
  1939. ; # end of last ch.
  1940.         add     edi,4
  1941.         add     esi,2
  1942.         loop    @b
  1943.  
  1944.         mov     eax,dword[xx1]
  1945.         ror     eax,16
  1946.         mov     ebx,dword[xx2]
  1947.         ror     ebx,16
  1948.         mov     ecx,dword[xx3]
  1949.         ror     ecx,16
  1950.         mov     edi,screen
  1951.         mov     esi,envmap
  1952.         cmp     [catmull_flag],0
  1953.         je      @f
  1954.   ;      mov     edx,Z_buffer
  1955.         mov     edx,[Zbuffer_ptr]
  1956.         call    tex_triangle_z
  1957.         jmp     .end_draw
  1958.       @@:
  1959.         call    tex_triangle
  1960.         jmp     .end_draw
  1961. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1962.      .cubic_env_mapping:
  1963.        ; fninit
  1964.         cmp     [catmull_flag],0
  1965.         je      @f
  1966.         push    [zz3]
  1967.         push    [zz2]
  1968.         push    [zz1]
  1969.       @@:
  1970.         mov     esi,point_index1
  1971.         sub     esp,12
  1972.         mov     edi,esp
  1973.         mov     ecx,3
  1974.       @@:
  1975.         movzx   eax,word[esi]
  1976.         lea     eax,[eax*3]
  1977.         shl     eax,2
  1978.         add     eax,[points_normals_rot_ptr]  ;point_normals_rotated
  1979. ; #
  1980.         fld     dword[eax]
  1981.         fmul    dword[eax+4]
  1982.         fld1
  1983.         fld1
  1984.         faddp
  1985.         fmulp
  1986.         fimul   [correct_tex]
  1987.         fiadd   [correct_tex]
  1988.         fistp   word[edi]
  1989.         mov     word[edi+2],0
  1990. ;        fistp   word[edi+2]
  1991. ; # last change
  1992. ;        ; texture x=(rotated point normal -> x * 255)+255
  1993. ;        fld     dword[eax]
  1994. ;        fimul   [correct_tex]
  1995. ;        fiadd   [correct_tex]
  1996. ;        fistp   word[edi]
  1997. ;        ; texture y=(rotated point normal -> y * 255)+255
  1998. ;        fld     dword[eax+4]
  1999. ;        fimul   [correct_tex]
  2000. ;        fiadd   [correct_tex]
  2001. ;        fistp   word[edi+2]
  2002. ; # end of last ch.
  2003.         add     edi,4
  2004.         add     esi,2
  2005.         loop    @b
  2006.  
  2007.         mov     eax,dword[xx1]
  2008.         ror     eax,16
  2009.         mov     ebx,dword[xx2]
  2010.         ror     ebx,16
  2011.         mov     ecx,dword[xx3]
  2012.         ror     ecx,16
  2013.         mov     edi,screen
  2014.         mov     esi,envmap_cub
  2015.         cmp     [catmull_flag],0
  2016.         je      @f
  2017.   ;      mov     edx,Z_buffer
  2018.         mov     edx,[Zbuffer_ptr]
  2019.         call    tex_triangle_z
  2020.         jmp     .end_draw
  2021.       @@:
  2022.         call    tex_triangle
  2023.         jmp     .end_draw
  2024.  
  2025. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2026.  
  2027.       .bump_mapping:
  2028.         ; fninit
  2029.         cmp     [catmull_flag],0
  2030.         je      @f
  2031. ;        push    Z_buffer
  2032.         push    [Zbuffer_ptr]
  2033.         push    [zz3]
  2034.         push    [zz2]
  2035.         push    [zz1]
  2036.       @@:
  2037.         mov     esi,point_index1
  2038.         sub     esp,12
  2039.         mov     edi,esp
  2040.         mov     ecx,3
  2041.       @@:
  2042.         movzx   eax,word[esi]
  2043.         lea     eax,[eax*3]
  2044.         shl     eax,2
  2045.         add     eax,[points_normals_rot_ptr]  ;point_normals_rotated
  2046.         ; texture x=(rotated point normal -> x * 255)+255
  2047.         fld     dword[eax]
  2048.         fimul   [correct_tex]
  2049.         fiadd   [correct_tex]
  2050.         fistp   word[edi]
  2051.         ; texture y=(rotated point normal -> y * 255)+255
  2052.         fld     dword[eax+4]
  2053.         fimul   [correct_tex]
  2054.         fiadd   [correct_tex]
  2055.         fistp   word[edi+2]
  2056.  
  2057.         add     edi,4
  2058.         add     esi,2
  2059.         loop    @b
  2060.  
  2061.         movzx  esi,[point_index3]      ; bump map coords
  2062.         shl    esi,2
  2063.         add    esi,tex_points
  2064.         push   dword[esi]
  2065.         movzx  esi,[point_index2]
  2066.         shl    esi,2
  2067.         add    esi,tex_points
  2068. ;       lea    esi,[esi*3]
  2069. ;       lea    esi,[points+2+esi*2]
  2070.         push   dword[esi]
  2071.   ;     push   dword[xx2]
  2072.         movzx  esi,[point_index1]
  2073.         shl    esi,2
  2074.         add    esi,tex_points
  2075. ;       lea     esi,[esi*3]
  2076. ;       lea     esi,[points+2+esi*2]
  2077.         push   dword[esi]
  2078.    ;    push     dword[xx1]
  2079.  
  2080.         mov     eax,dword[xx1]
  2081.         ror     eax,16
  2082.         mov     ebx,dword[xx2]
  2083.         ror     ebx,16
  2084.         mov     ecx,dword[xx3]
  2085.         ror     ecx,16
  2086.         mov     edi,screen
  2087.         mov     esi,envmap
  2088.         mov     edx,bumpmap            ;BUMP_MAPPING
  2089.  
  2090.         cmp     [catmull_flag],0
  2091.         je      @f
  2092.         call    bump_triangle_z
  2093.         jmp     .end_draw
  2094.       @@:
  2095.         call    bump_triangle
  2096.         jmp     .end_draw
  2097.  
  2098.       .tex_mapping:
  2099.  
  2100.         ; fninit
  2101.         cmp     [catmull_flag],0
  2102.         je      @f
  2103.         push    [zz3]
  2104.         push    [zz2]
  2105.         push    [zz1]
  2106.       @@:
  2107.         movzx  esi,[point_index3]      ; tex map coords
  2108.         shl    esi,2
  2109.         add    esi,tex_points
  2110.         push   dword[esi]
  2111.         movzx  esi,[point_index2]
  2112.         shl    esi,2
  2113.         add    esi,tex_points
  2114.         push   dword[esi]
  2115.         movzx  esi,[point_index1]
  2116.         shl    esi,2
  2117.         add    esi,tex_points
  2118.         push   dword[esi]
  2119.  
  2120.         mov     eax,dword[xx1]
  2121.         ror     eax,16
  2122.         mov     ebx,dword[xx2]
  2123.         ror     ebx,16
  2124.         mov     ecx,dword[xx3]
  2125.         ror     ecx,16
  2126.         mov     edi,screen
  2127.         mov     esi,texmap
  2128.         cmp     [catmull_flag],0
  2129.         je      @f
  2130.    ;     mov     edx,Z_buffer
  2131.         mov     edx,[Zbuffer_ptr]
  2132.         call    tex_triangle_z
  2133.  ;       call    tex_plus_grd_trianlgle
  2134.         jmp     .end_draw
  2135.       @@:
  2136.         call    tex_triangle
  2137.         jmp     .end_draw
  2138. ;      .ray:
  2139. ;        grd_triangle according to points index
  2140. ;        cmp     [catmull_flag],0
  2141. ;        je      @f
  2142. ;        push    [zz3]                   ; spot light with attenuation
  2143. ;     @@:
  2144. ;        movzx   eax,[point_index3]      ; env_map - points color list
  2145. ;        shl     eax,1                   ; each color as word, 0x00rr00gg00bb..
  2146. ;        lea     eax,[3*eax+bumpmap]
  2147. ;        push    word[eax]
  2148. ;        push    word[eax+2]
  2149. ;        push    word[eax+4]
  2150. ;        cmp     [catmull_flag],0
  2151. ;        je      @f
  2152. ;        push    [zz2]
  2153. ;    @@:
  2154. ;        movzx   eax,[point_index2]      ; env_map - points color list
  2155. ;        shl     eax,1                   ; each color as word, 0x00rr00gg00bb..
  2156. ;        lea     eax,[eax*3+bumpmap]
  2157. ;        push    word[eax]
  2158. ;        push    word[eax+2]
  2159. ;        push    word[eax+4]
  2160. ;        cmp     [catmull_flag],0
  2161. ;        je      @f
  2162. ;        push    [zz1]
  2163. ;     @@:
  2164. ;        movzx   eax,[point_index1]      ; env_map - points color list
  2165. ;        shl     eax,1                   ; each color as word, 0xrr00gg00bb00..
  2166. ;        lea     eax,[eax*3+bumpmap]
  2167. ;        push    word[eax]
  2168. ;        push    word[eax+2]
  2169. ;        push    word[eax+4]
  2170. ;        jmp     .both_draw
  2171.  
  2172.      .grd_tex:            ; smooth shading + texture
  2173.          push   ebp
  2174.          mov    ebp,esp
  2175.          sub    esp,4
  2176.          push   ebp
  2177.  
  2178.          movzx  esi,[point_index3]      ; tex map coords
  2179.          shl    esi,2
  2180.          add    esi,tex_points
  2181.          push   dword[esi]              ; texture coords as first
  2182.          movzx  esi,[point_index2]      ; group of parameters
  2183.          shl    esi,2
  2184.          add    esi,tex_points
  2185.          push   dword[esi]
  2186.          movzx  esi,[point_index1]
  2187.          shl    esi,2
  2188.          add    esi,tex_points
  2189.          push   dword[esi]
  2190.  
  2191.          mov     esi,point_index3
  2192.          mov     ecx,3
  2193.  
  2194.       .aagain_grd_draw:
  2195.  
  2196.         lea      edx,[ecx*3]
  2197.         push     word[edx*2+xx1-2]    ; zz1 ,2 ,3
  2198.  
  2199.         movzx   eax,word[esi]
  2200.         shl     eax,2
  2201.         lea     eax,[eax*3] ;+point_normals_rotated]
  2202.         add     eax,[points_normals_rot_ptr]
  2203.         ; texture x=(rotated point normal -> x * 255)+255
  2204.         fld     dword[eax]       ; x cooficient of normal vector
  2205.         fimul   [correct_tex]
  2206.         fiadd   [correct_tex]
  2207.         fistp   word[ebp-2]
  2208.         ; texture y=(rotated point normal -> y * 255)+255
  2209.         fld     dword[eax+4]      ; y cooficient
  2210.         fimul   [correct_tex]
  2211.         fiadd   [correct_tex]
  2212.         fistp   word[ebp-4]
  2213.  
  2214.         movzx    eax,word[ebp-4]
  2215.         movzx    ebx,word[ebp-2]
  2216.         shl      eax,TEX_SHIFT
  2217.         add      eax,ebx
  2218.         lea      eax,[eax*3+color_map]
  2219.         mov      eax,dword[eax]
  2220.  
  2221.         ror      eax,16               ; eax -0xxxrrggbb -> 0xggbbxxrr
  2222.         xor      ah,ah
  2223.         push     ax         ;r
  2224.         rol      eax,8                ; eax-0xggbb00rr -> 0xbb00rrgg
  2225.         xor      ah,ah
  2226.         push     ax         ;g
  2227.         shr      eax,24
  2228.         push     ax         ;b
  2229.  
  2230.         sub      esi,2
  2231.         dec      cx
  2232.         jnz      .aagain_grd_draw
  2233.  
  2234.         mov     eax,dword[xx1]
  2235.         ror     eax,16
  2236.         mov     ebx,dword[xx2]
  2237.         ror     ebx,16
  2238.         mov     ecx,dword[xx3]
  2239.         ror     ecx,16
  2240.         mov     edi,screen
  2241.         mov     edx,texmap
  2242.         mov     esi,[Zbuffer_ptr]
  2243.  
  2244.         call    tex_plus_grd_triangle
  2245.  
  2246.         pop     ebp
  2247.         mov     esp,ebp
  2248.         pop     ebp
  2249.         jmp     .end_draw
  2250.  
  2251.       .two_tex:
  2252.         push    [Zbuffer_ptr]
  2253.  
  2254.         push    word[zz3]
  2255.         push    word[zz2]
  2256.         push    word[zz1]
  2257.  
  2258.         movzx  esi,[point_index3]      ; tex map coords
  2259.         shl    esi,2
  2260.         add    esi,tex_points
  2261.         push   dword[esi]
  2262.         movzx  esi,[point_index2]
  2263.         shl    esi,2
  2264.         add    esi,tex_points
  2265.         push   dword[esi]
  2266.         movzx  esi,[point_index1]
  2267.         shl    esi,2
  2268.         add    esi,tex_points
  2269.         push   dword[esi]
  2270.  
  2271.         mov     esi,point_index1     ; env coords
  2272.         sub     esp,12
  2273.         mov     edi,esp
  2274.         mov     ecx,3
  2275.       @@:
  2276.         movzx   eax,word[esi]
  2277.         lea     eax,[eax*3]
  2278.         shl     eax,2
  2279.         add     eax,[points_normals_rot_ptr]
  2280.         ; texture x=(rotated point normal -> x * 255)+255
  2281.         fld     dword[eax]
  2282.         fimul   [correct_tex]
  2283.         fiadd   [correct_tex]
  2284.         fistp   word[edi]
  2285.         ; texture y=(rotated point normal -> y * 255)+255
  2286.         fld     dword[eax+4]
  2287.         fimul   [correct_tex]
  2288.         fiadd   [correct_tex]
  2289.         fistp   word[edi+2]
  2290.  
  2291.         add     edi,4
  2292.         add     esi,2
  2293.         loop    @b
  2294.  
  2295.         mov     eax,dword[xx1]
  2296.         ror     eax,16
  2297.         mov     ebx,dword[xx2]
  2298.         ror     ebx,16
  2299.         mov     ecx,dword[xx3]
  2300.         ror     ecx,16
  2301.         mov     edi,screen
  2302.         mov     esi,texmap
  2303.         mov     edx,envmap
  2304.  
  2305.         call    two_tex_triangle_z
  2306.         jmp     .end_draw
  2307.  
  2308.    .bump_tex:
  2309.         movzx  esi,[point_index3]      ; tex map coords
  2310.         shl    esi,2
  2311.         add    esi,tex_points
  2312.         push   dword[esi]
  2313.         movzx  esi,[point_index2]
  2314.         shl    esi,2
  2315.         add    esi,tex_points
  2316.         push   dword[esi]
  2317.         movzx  esi,[point_index1]
  2318.         shl    esi,2
  2319.         add    esi,tex_points
  2320.         push   dword[esi]
  2321.  
  2322.         push  dword texmap
  2323.  
  2324.         push  [Zbuffer_ptr]
  2325.         xor   edi,edi
  2326.  
  2327.         push    word[zz3]
  2328.         push    word[zz2]
  2329.         push    word[zz1]
  2330.  
  2331.         mov     esi,point_index1     ; env coords
  2332.         sub     esp,12
  2333.         mov     edi,esp
  2334.         mov     ecx,3
  2335.       @@:
  2336.         movzx   eax,word[esi]
  2337.         lea     eax,[eax*3]
  2338.         shl     eax,2
  2339.         add     eax,[points_normals_rot_ptr]
  2340.         ; texture x=(rotated point normal -> x * 255)+255
  2341.         fld     dword[eax]
  2342.         fimul   [correct_tex]
  2343.         fiadd   [correct_tex]
  2344.         fistp   word[edi]
  2345.         ; texture y=(rotated point normal -> y * 255)+255
  2346.         fld     dword[eax+4]
  2347.         fimul   [correct_tex]
  2348.         fiadd   [correct_tex]
  2349.         fistp   word[edi+2]
  2350.  
  2351.         add     edi,4
  2352.         add     esi,2
  2353.         loop    @b
  2354.  
  2355. ;        push  dword 1 shl 16 + 1  ; emap coords
  2356. ;        push  dword 127 shl 16 + 1
  2357. ;        push  dword 127 shl 16 + 127
  2358.  
  2359.         movzx  esi,[point_index3]      ; bump map coords
  2360.         shl    esi,2
  2361.         add    esi,tex_points
  2362.         push   dword[esi]
  2363.         movzx  esi,[point_index2]
  2364.         shl    esi,2
  2365.         add    esi,tex_points
  2366.         push   dword[esi]
  2367.  
  2368.         movzx  esi,[point_index1]
  2369.         shl    esi,2
  2370.         add    esi,tex_points
  2371.         push   dword[esi]
  2372.  
  2373. ;        push  dword 1 shl 16 + 127
  2374. ;        push  dword 127 shl 16 + 127
  2375. ;        push  dword 1 shl 16 + 1  ; bump coords
  2376.  
  2377.         mov     eax,dword[xx1]
  2378.         ror     eax,16
  2379.         mov     ebx,dword[xx2]
  2380.         ror     ebx,16
  2381.         mov     ecx,dword[xx3]
  2382.         ror     ecx,16
  2383.         mov     edi,screen
  2384.         mov     esi,envmap
  2385.         mov     edx,bumpmap
  2386.  
  2387.         call bump_tex_triangle_z
  2388.  
  2389.         jmp     .end_draw
  2390.  
  2391.       .draw_smooth_line:
  2392.         mov     esi,point_index3
  2393.         mov     ecx,3
  2394.       .again_line_param:
  2395.         movzx   eax,word[esi]
  2396.         shl     eax,2
  2397.         lea     eax,[eax*3]
  2398.         add     eax,[points_normals_rot_ptr]
  2399.         ; texture ;x=(rotated point normal -> x * 255)+255
  2400.         fld     dword[eax]       ; x cooficient of normal vector
  2401.         fimul   [correct_tex]
  2402.         fiadd   [correct_tex]
  2403.         fistp   word[esp-2]
  2404.         ; texture y=(rotated point normal -> y * 255)+255
  2405.         fld     dword[eax+4]      ; y cooficient
  2406.         fimul   [correct_tex]
  2407.         fiadd   [correct_tex]
  2408.         fistp   word[esp-4]
  2409.  
  2410.         movzx    eax,word[esp-4]
  2411.         movzx    ebx,word[esp-2]
  2412.         shl      eax,TEX_SHIFT
  2413.         add      eax,ebx
  2414.         lea      eax,[eax*3+color_map]
  2415.         mov      eax,dword[eax]
  2416.         lea      ebx,[ecx-1]
  2417.         shl      ebx,2
  2418.         mov      [ebx+col1],eax
  2419.  
  2420.         sub      esi,2
  2421.         dec      ecx
  2422.         jnz      .again_line_param
  2423.  
  2424.         mov     eax,[edges_ptr]
  2425.         add     eax,[edges_counter]
  2426.         mov     bl,[eax]
  2427.         test    bl,00000001b
  2428.         jz      @f
  2429.         mov     edi,screen
  2430.         mov     esi,[Zbuffer_ptr]
  2431.  
  2432.         mov     eax,[col1]
  2433.         movzx   bx,al
  2434.         push    bx                ; b
  2435.         movzx   bx,ah
  2436.         push    bx
  2437.         rol     eax,16
  2438.         xor     ah,ah
  2439.         push    ax
  2440.         push    [zz1]
  2441.         push    [yy1]
  2442.         push    [xx1]
  2443.  
  2444.         mov     eax,[col2]
  2445.         movzx   bx,al
  2446.         push    bx                ; b
  2447.         movzx   bx,ah
  2448.         push    bx
  2449.         rol     eax,16
  2450.         xor     ah,ah
  2451.         push    ax
  2452.         push    [zz2]
  2453.         push    [yy2]
  2454.         push    [xx2]
  2455.  
  2456.         call    smooth_line
  2457.      @@:
  2458.         mov     eax,[edges_ptr]
  2459.         add     eax,[edges_counter]
  2460.         mov     bl,[eax]
  2461.         test    bl,00000010b
  2462.         jz      @f
  2463.  
  2464.         mov     edi,screen
  2465.         mov     esi,[Zbuffer_ptr]
  2466.  
  2467.         mov     eax,[col1]
  2468.         movzx   bx,al
  2469.         push    bx                ; b
  2470.         movzx   bx,ah
  2471.         push    bx
  2472.         rol     eax,16
  2473.         xor     ah,ah
  2474.         push    ax
  2475.         push    [zz1]
  2476.         push    [yy1]
  2477.         push    [xx1]
  2478.  
  2479.         mov     eax,[col3]
  2480.         movzx   bx,al
  2481.         push    bx                ; b
  2482.         movzx   bx,ah
  2483.         push    bx
  2484.         rol     eax,16
  2485.         xor     ah,ah
  2486.         push    ax
  2487.         push    [zz3]
  2488.         push    [yy3]
  2489.         push    [xx3]
  2490.  
  2491.         call    smooth_line
  2492.       @@:
  2493.  
  2494.         mov     eax,[edges_ptr]
  2495.         add     eax,[edges_counter]
  2496.         mov     bl,[eax]
  2497.         test    bl,00000100b
  2498.         jz      @f
  2499.  
  2500.         mov     edi,screen
  2501.         mov     esi,[Zbuffer_ptr]
  2502.  
  2503.         mov     eax,[col3]
  2504.         movzx   bx,al
  2505.         push    bx                ; b
  2506.         movzx   bx,ah
  2507.         push    bx
  2508.         rol     eax,16
  2509.         xor     ah,ah
  2510.         push    ax
  2511.         push    [zz3]
  2512.         push    [yy3]
  2513.         push    [xx3]
  2514.  
  2515.         mov     eax,[col2]
  2516.         movzx   bx,al
  2517.         push    bx                ; b
  2518.         movzx   bx,ah
  2519.         push    bx
  2520.         rol     eax,16
  2521.         xor     ah,ah
  2522.         push    ax
  2523.         push    [zz2]
  2524.         push    [yy2]
  2525.         push    [xx2]
  2526.  
  2527.         call    smooth_line
  2528.       @@:
  2529.  
  2530.       .end_draw:
  2531.         pop     esi
  2532.         add     esi,6
  2533.         inc     [edges_counter]
  2534.         cmp     dword[esi],-1
  2535.         jne     .again_dts
  2536. ret
  2537.  
  2538.  
  2539. fill_Z_buffer:
  2540.         mov     eax,0x70000000
  2541.       ;  mov     edi,Z_buffer
  2542.         mov     edi,[Zbuffer_ptr]
  2543.         mov     ecx,SIZE_X*SIZE_Y
  2544.         rep     stosd
  2545. ret
  2546.  
  2547. read_from_file:
  2548.         fninit
  2549.         mov     edi,[triangles_ptr]
  2550.         xor     ebx,ebx
  2551.         xor     ebp,ebp
  2552.         mov     [points_count_var],0
  2553.         mov     [triangles_count_var],0
  2554.    if USE_LFN = 0
  2555.         mov     esi,SourceFile
  2556.    else
  2557.         mov     esi,[fptr]
  2558.    end if
  2559.         cmp     [esi],word 4D4Dh
  2560.         jne     .exit ;Must be legal .3DS file
  2561. ;        cmp     dword[esi+2],EndFile-SourceFile
  2562. ;        jne     .exit ;This must tell the length
  2563.         mov     eax,dword[esi+2]
  2564.   ;      cmp     eax,[fsize]
  2565.   ;      jne     .exit
  2566.  
  2567.         add     eax,esi
  2568.         mov     [EndFile],eax    ;
  2569.  
  2570.         add     esi,6
  2571.         mov     eax,[edges_ptr]
  2572.       @@:
  2573.         cmp     [esi],word 3D3Dh
  2574.         je      @f
  2575.         add     esi,[esi+2]
  2576.         jmp     @b
  2577.       @@:
  2578.         add     esi,6
  2579.       .find4k:
  2580.         cmp     [esi],word 4000h
  2581.         je      @f
  2582.         add     esi,[esi+2]
  2583.         cmp     esi,[EndFile]
  2584.         jc      .find4k
  2585.         jmp     .exit
  2586.       @@:
  2587.         add     esi,6
  2588.       @@:
  2589.         cmp     [esi],byte 0
  2590.         je      @f
  2591.         inc     esi
  2592.         jmp     @b
  2593.       @@:
  2594.         inc     esi
  2595.       @@:
  2596.         cmp     [esi],word 4100h
  2597.         je      @f
  2598.         add     esi,[esi+2]
  2599.         jmp     @b
  2600.       @@:
  2601.         add     esi,6
  2602.       @@:
  2603.         cmp     [esi],word 4110h
  2604.         je      @f
  2605.         add     esi,[esi+2]
  2606.         jmp     @b
  2607.       @@:
  2608.         movzx   ecx,word[esi+6]
  2609.         add     [points_count_var],cx
  2610.  
  2611.         mov     edx,ecx
  2612.         add     esi,8
  2613.      @@:
  2614.         push    edi
  2615.         mov     edi,[points_ptr]
  2616.         push    dword[esi+4]
  2617.         pop     dword[edi+ebx*2+0]
  2618.         push    dword[esi+8]
  2619.         pop     dword[edi+ebx*2+4]
  2620.         push    dword[esi+0]
  2621.         pop     dword[edi+ebx*2+8]
  2622.         pop     edi
  2623. ;        fld     dword[esi+4]
  2624. ;        fstp    dword[real_points+ebx*2+0]  ; x
  2625. ;        fld     dword[esi+8]
  2626. ;        fstp   dword[real_points+ebx*2+4]  ; y
  2627. ;        fld     dword[esi+0]
  2628. ;        fstp   dword[real_points+ebx*2+8]  ; z
  2629.  
  2630.         add     ebx,6
  2631.         add     esi,12
  2632.         dec     ecx
  2633.         jnz     @b
  2634.       @@:
  2635.   ;      mov     dword[points+ebx],-1
  2636.         push    edi
  2637.         mov     edi,[points_ptr]
  2638.         mov     dword[edi+ebx*2],-1        ; end mark (not always in use)
  2639.         pop     edi
  2640.       @@:
  2641.         cmp     [esi],word 4120h
  2642.         je      @f
  2643.         add     esi,[esi+2]
  2644.         jmp     @b
  2645.       @@:
  2646.         movzx   ecx,word[esi+6]
  2647.         add     [triangles_count_var],cx
  2648.         add     esi,8
  2649.         ;mov     edi,triangles
  2650.       @@:
  2651.         movsd
  2652.         movsw
  2653.         add     word[edi-6],bp
  2654.         add     word[edi-4],bp
  2655.         add     word[edi-2],bp
  2656.         mov     dl,byte[esi]
  2657.         mov     [eax],dl
  2658.         inc     eax
  2659.         add     esi,2
  2660.         dec     ecx
  2661.         jnz     @b
  2662.         add     ebp,edx
  2663.         jmp     .find4k
  2664.         mov     eax,-1 ;<---mark if OK
  2665.       .exit:
  2666.         mov     dword[edi],-1
  2667. ret
  2668.  
  2669. read_tp_variables:            ; read [triangles_count_var] and  [points_count_var]
  2670.                               ; and  allocate memory
  2671.         xor     ebx,ebx
  2672.         xor     ebp,ebp
  2673.         mov     [points_count_var],bx
  2674.         mov     [triangles_count_var],bx
  2675.    if USE_LFN = 0
  2676.         mov     esi,SourceFile
  2677.    else
  2678.         mov     esi,[fptr]
  2679.    end if
  2680.  
  2681.         cmp     [esi],word 4D4Dh
  2682.         je      @f ;Must be legal .3DS file
  2683.         xor     eax,eax
  2684.         ret
  2685.     @@:
  2686.         mov     eax,dword[esi+2]
  2687.         cmp     eax,[fsize] ;This must tell the length
  2688.         je      @f
  2689.         xor     eax,eax
  2690.         ret
  2691.      @@:
  2692.         add     eax,esi
  2693.         mov     [EndFile],eax    ;
  2694.  
  2695.         add     esi,6
  2696.       @@:
  2697.         cmp     [esi],word 3D3Dh
  2698.         je      @f
  2699.         add     esi,[esi+2]
  2700.         jmp     @b
  2701.       @@:
  2702.         add     esi,6
  2703.       .find4k:
  2704.         cmp     [esi],word 4000h
  2705.         je      @f
  2706.         add     esi,[esi+2]
  2707.         cmp     esi,[EndFile]
  2708.         jc      .find4k
  2709.         jmp     .exit
  2710.       @@:
  2711.         add     esi,6
  2712.       @@:
  2713.         cmp     [esi],byte 0
  2714.         je      @f
  2715.         inc     esi
  2716.         jmp     @b
  2717.       @@:
  2718.         inc     esi
  2719.       @@:
  2720.         cmp     [esi],word 4100h
  2721.         je      @f
  2722.         add     esi,[esi+2]
  2723.         jmp     @b
  2724.       @@:
  2725.         add     esi,6
  2726.       @@:
  2727.         cmp     [esi],word 4110h
  2728.         je      @f
  2729.         add     esi,[esi+2]
  2730.         jmp     @b
  2731.       @@:
  2732.         movzx   ecx,word[esi+6]
  2733.         add     [points_count_var],cx
  2734.  
  2735.         mov     edx,ecx
  2736.         add     esi,8
  2737.      @@:
  2738.  
  2739.         add     ebx,6
  2740.         add     esi,12
  2741.      ;   dec     ecx
  2742.         loop     @b
  2743.       @@:
  2744.  
  2745.       @@:
  2746.         cmp     [esi],word 4120h
  2747.         je      @f
  2748.         add     esi,[esi+2]
  2749.         jmp     @b
  2750.       @@:
  2751.         movzx   ecx,word[esi+6]
  2752.         add     [triangles_count_var],cx
  2753.         add     esi,8
  2754.  
  2755.       @@:
  2756.         add     esi,8
  2757.         dec     ecx
  2758.         jnz     @b
  2759. ;        xor     ecx,ecx
  2760.         add     ebp,edx
  2761.         jmp     .find4k
  2762.         mov     eax,-1 ;<---mark if OK
  2763.       .exit:
  2764. ret
  2765.  
  2766. if USE_LFN
  2767. alloc_mem_for_tp:
  2768.         mov     eax, 68
  2769.         cmp     [re_alloc_flag],1
  2770.         jz      @f
  2771.         mov     ebx, 12
  2772.         jmp     .alloc
  2773.     @@:
  2774.         mov     ebx,20
  2775.     .alloc:
  2776.  
  2777.         movzx   ecx, [triangles_count_var]
  2778.         inc     ecx
  2779.         lea     ecx, [ecx*3]
  2780.         add     ecx, ecx
  2781.         mov     edx,[triangles_ptr]
  2782.         int     0x40                   ;  -> allocate memory to triangles
  2783.         mov     [triangles_ptr], eax   ;  -> eax = pointer to allocated mem
  2784.  
  2785.         mov     eax, 68
  2786.         movzx   ecx, [triangles_count_var]
  2787.         inc     ecx
  2788.         mov     edx,[edges_ptr]
  2789.         int     0x40                   ;  -> allocate memory to edges
  2790.         mov     [edges_ptr], eax   ;  -> eax = pointer to allocated mem
  2791.  
  2792.  
  2793. ;        mov     eax, 68
  2794. ;        mov     ebx, 12
  2795. ;        movzx   ecx, [triangles_count_var]
  2796. ;        shl     ecx, 4
  2797. ;        int     0x40
  2798. ;        mov     [triangles_w_z_ptr], eax   ; for trainagles_with_z list
  2799.                                             ; ststic  memory
  2800.  
  2801.         mov     eax, 68
  2802.         movzx   ecx, [triangles_count_var]
  2803.         lea     ecx, [3+ecx*3]
  2804.         shl     ecx, 2
  2805.         mov     edx,[triangles_normals_ptr]
  2806.         int     0x40                           ;  -> allocate memory for triangles normals
  2807.         mov     [triangles_normals_ptr], eax   ;  -> eax = pointer to allocated mem
  2808.  
  2809.         mov     eax, 68
  2810.         movzx   ecx, [points_count_var]
  2811.         lea     ecx,[3+ecx*3]
  2812.         shl     ecx, 2
  2813.         mov     edx,[points_normals_ptr]
  2814.         int     0x40
  2815.         mov     [points_normals_ptr], eax
  2816.  
  2817.         mov     eax, 68
  2818.     ;    mov     ebx, 12
  2819.         movzx   ecx, [points_count_var]
  2820.         lea     ecx,[3+ecx*3]
  2821.         shl     ecx, 2
  2822.         mov     edx,[points_normals_rot_ptr]
  2823.         int     0x40
  2824.         mov     [points_normals_rot_ptr], eax
  2825.  
  2826.         mov     eax, 68
  2827.         mov     edx,[points_ptr]
  2828.         int     0x40
  2829.         mov     [points_ptr], eax
  2830.  
  2831.         mov     eax, 68
  2832.         mov     edx,[points_rotated_ptr]
  2833.         int     0x40
  2834.         mov     [points_rotated_ptr], eax
  2835.  
  2836.         mov     eax, 68
  2837.         movzx   ecx, [points_count_var]
  2838.         inc     ecx
  2839.         shl     ecx, 3
  2840.         mov     edx,[points_translated_ptr]
  2841.         int     0x40
  2842.         mov     [points_translated_ptr], eax
  2843. ret
  2844. end if
  2845.  
  2846.  
  2847. read_from_disk:
  2848. if USE_LFN
  2849. ;-
  2850.     mov     eax, 70
  2851.     mov     ebx, file_info
  2852.     mov     dword[ebx], 5          ;  -> subfunction number
  2853.     int     0x40                   ;  -> read file size
  2854.     mov     ebx, [fptr]
  2855.     mov     ebx, dword[ebx+32]
  2856.     inc     ebx
  2857.     mov     [fsize], ebx
  2858.  
  2859.     mov     eax, 68
  2860.     mov     ebx, 11
  2861.     int     0x40                   ;  -> create heap
  2862.  
  2863.     mov     eax, 68
  2864.     mov     ebx, 12
  2865.     mov     ecx, [fsize]
  2866.     int     0x40                   ;  -> allocate memory for file
  2867.     mov     [fptr], eax            ;  -> eax = pointer to allocated mem
  2868.  
  2869.     mov     eax, 70
  2870.     mov     ebx, file_info
  2871.     mov     dword[ebx],0
  2872.     int     0x40                   ; -> read file
  2873.  
  2874.     mov     [fsize],ebx
  2875.  
  2876.     cmp     eax,6
  2877.     jnz     @f
  2878.     xor     eax,eax     ;;;;---
  2879.   @@:
  2880. else
  2881.     mov      eax,58
  2882.     mov      ebx,file_info
  2883.     int      0x40
  2884.  
  2885.     mov      eax,ebx
  2886.     shr      eax,9
  2887.     inc      eax
  2888.     mov      [fsize],eax
  2889. ;    mov      ecx,ebx
  2890. ;    add      ecx,MEM_END
  2891. ;    mov      ebx,1
  2892. ;    mov      eax,64     ; allocate mem  - resize app mem
  2893.                         ; for points and  triangles
  2894.     int      0x40
  2895.  
  2896.     mov      eax,58
  2897.     mov      ebx,file_info
  2898.     int      0x40
  2899. end if
  2900.   ;  eax = 0   -> ok file loaded
  2901. ret
  2902. read_param:
  2903.     mov        esi,I_Param
  2904.     cmp        dword[esi],0
  2905.     je         .end
  2906.     cmp        byte[esi],'/'
  2907.     je         .copy
  2908.     mov        edi,esi
  2909.     mov        ecx,25   ; 25 - would be enought
  2910.     repe       scasb
  2911.     jne        .end
  2912.     dec        edi
  2913.     mov        esi,edi
  2914.  .copy:
  2915.     mov         edi,file_name
  2916.     mov         ecx,50
  2917.     rep         movsd
  2918.  .end:
  2919. ret
  2920. buttons:                                      ; draw some buttons (all but navigation and close )
  2921.         mov     edi,menu
  2922.       .again:
  2923.         mov     eax,8                         ; function 8 : define and draw button
  2924.         mov     ebx,(SIZE_X+10)*65536+62      ; [x start] *65536 + [x size]
  2925.         movzx   ecx,byte[edi]                 ; button id = position+2
  2926.         sub     cl,2
  2927.         lea     ecx,[ecx*5]
  2928.         lea     ecx,[ecx*3]
  2929.         add     ecx,25
  2930.         shl     ecx,16
  2931.         add     ecx,12
  2932.         movzx   edx,byte[edi]                   ; button id
  2933.         mov     esi,0x6688dd                    ; button color RRGGBB
  2934.         int     0x40
  2935.          ; BUTTON  LABEL
  2936.         mov     eax,4                           ; function 4 : write text to window
  2937.         movzx   ebx,byte[edi]
  2938.         sub     bl,2                            ; button id, according to position
  2939.         lea     ebx,[ebx*3]
  2940.         lea     ebx,[ebx*5]
  2941.         add     ebx,(SIZE_X+12)*65536+28        ; [x start] *65536 + [y start]
  2942.         mov     ecx,0x20ddeeff                  ; font 1 & color ( 0xF0RRGGBB )
  2943.         lea     edx,[edi+1]                     ; pointer to text beginning
  2944.         mov     esi,10                          ; text length
  2945.         int     0x40
  2946.         cmp     byte[edi+11],255                ; if max_flag=255
  2947.         je      @f                              ; skip
  2948.         ; flag description
  2949. ;       mov     eax,4                           ; function 4 : write text to window
  2950. ;       movzx   ebx,byte[edi]
  2951. ;       sub     bl,2
  2952. ;       lea     ebx,[ebx*3]
  2953. ;       lea     ebx,[ebx*5]
  2954. ;       add     ebx,(SIZE_X+12+70)*65536+28     ; [x start] *65536 + [y start]
  2955.         add     ebx,70*65536
  2956. ;       mov     ecx,0x20ddeeff                  ; font 1 & color ( 0xF0RRGGBB )
  2957.         movzx   edx,byte[edi+12]                ; current flag
  2958.         shl     edx,2                           ; * 4 = text length
  2959.         add     edx,dword[edi+13]               ; pointer to text beginning
  2960.         mov     esi,4                           ; text length
  2961.         int     0x40
  2962.  
  2963.     @@:
  2964.         add     edi,17
  2965.         cmp     byte[edi],-1
  2966.         jnz     .again
  2967. ret
  2968. ;   *********************************************
  2969. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  2970. ;   *********************************************
  2971.     draw_window:
  2972.         mov     eax,12          ; function 12:tell os about windowdraw
  2973.         mov     ebx,1           ; 1, start of draw
  2974.         int     0x40
  2975.  
  2976.         ; DRAW WINDOW
  2977.         mov     eax,0           ; function 0 : define and draw window
  2978.         mov     ebx,100*65536;+SIZE_X;+80+30 ; [x start] *65536 + [x size]
  2979.         mov     ecx,100*65536;+SIZE_Y;+30    ; [y start] *65536 + [y size]
  2980.         mov     bx,[size_x]
  2981.         add     bx,114
  2982.         mov     cx,[size_y]
  2983.         add     cx,30
  2984.         mov     edx,0x14000000  ; color of work area RRGGBB,8->color gl
  2985.         mov     edi,labelt      ; WINDOW LABEL
  2986.         int     0x40
  2987.  
  2988.         call    buttons         ; more buttons
  2989.  
  2990.         .Y_ADD equ 1   ;-> offset of 'add vector' buttons
  2991.  
  2992.         ; ADD VECTOR LABEL      ; add vector buttons - 30 ++
  2993.         mov     eax,4           ; function 4 : write text to window
  2994.         mov     ebx,(SIZE_X+12)*65536+(168+15*(13+.Y_ADD))   ; [x start] *65536 + [y start]
  2995.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2996.         mov     edx,labelvector      ; pointer to text beginning
  2997.         mov     esi,labelvectorend-labelvector     ; text length
  2998.     ;    cmp     [move_flag],2
  2999.     ;    jne     @f
  3000.     ;    add     edx,navigation_size
  3001.     ;  @@:
  3002.         int     0x40
  3003.          ; VECTOR Y- BUTTON
  3004.         mov     eax,8           ; function 8 : define and draw button
  3005.         mov     ebx,(SIZE_X+30)*65536+20     ; [x start] *65536 + [x size]
  3006.         mov     ecx,(165+15*(14+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  3007.         mov     edx,30           ; button id
  3008.         mov     esi,0x6688dd    ; button color RRGGBB
  3009.         int     0x40
  3010.         ;VECTOR Y- LABEL
  3011.         mov     eax,4           ; function 4 : write text to window
  3012.         mov     ebx,(SIZE_X+32)*65536+(168+15*(14+.Y_ADD))   ; [x start] *65536 + [y start]
  3013.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  3014.         mov     edx,labelyminus      ; pointer to text beginning
  3015.         mov     esi,labelyminusend-labelyminus     ; text length
  3016.         cmp     [move_flag],2
  3017.    ;     jne     @f
  3018.    ;     add     edx,navigation_size
  3019.    ;   @@:
  3020.         int     0x40
  3021.         ; VECTOR Z+ BUTTON
  3022.         mov     eax,8           ; function 8 : define and draw button
  3023.         mov     ebx,(SIZE_X+51)*65536+21     ; [x start] *65536 + [x size]
  3024.         mov     ecx,(165+15*(14+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  3025.         mov     edx,31           ; button id
  3026.         mov     esi,0x6688dd    ; button color RRGGBB
  3027.         int     0x40
  3028.         ;VECTOR Z+ LABEL
  3029.         mov     eax,4           ; function 4 : write text to window
  3030.         mov     ebx,(SIZE_X+53)*65536+(168+15*(14+.Y_ADD))   ; [x start] *65536 + [y start]
  3031.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  3032.         mov     edx,labelzplus      ; pointer to text beginning
  3033.         mov     esi,labelzplusend-labelzplus     ; text length
  3034.    ;     cmp     [move_flag],2
  3035.    ;     jne     @f
  3036.    ;     add     edx,navigation_size
  3037.    ;   @@:
  3038.  
  3039.         int     0x40
  3040.         ; VECTOR x- BUTTON
  3041.         mov     eax,8           ; function 8 : define and draw button
  3042.         mov     ebx,(SIZE_X+10)*65536+21     ; [x start] *65536 + [x size]
  3043.         mov     ecx,(165+15*(15+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  3044.         mov     edx,32           ; button id
  3045.         mov     esi,0x6688dd    ; button color RRGGBB
  3046.         int     0x40
  3047.         ;VECTOR x- LABEL
  3048.         mov     eax,4           ; function 4 : write text to window
  3049.         mov     ebx,(SIZE_X+12)*65536+(168+15*(15+.Y_ADD))   ; [x start] *65536 + [y start]
  3050.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  3051.         mov     edx,labelxminus      ; pointer to text beginning
  3052.         mov     esi,labelxminusend-labelxminus     ; text length
  3053.    ;     cmp     [move_flag],2
  3054.    ;     jne     @f
  3055.    ;     add     edx,navigation_size
  3056.    ;   @@:
  3057.         int     0x40
  3058.         ; VECTOR x+ BUTTON
  3059.         mov     eax,8           ; function 8 : define and draw button
  3060.         mov     ebx,(SIZE_X+51)*65536+21     ; [x start] *65536 + [x size]
  3061.         mov     ecx,(165+15*(15+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  3062.         mov     edx,33           ; button id
  3063.         mov     esi,0x6688dd    ; button color RRGGBB
  3064.         int     0x40
  3065.         ;VECTOR x+ LABEL
  3066.         mov     eax,4           ; function 4 : write text to window
  3067.         mov     ebx,(SIZE_X+53)*65536+(168+15*(15+.Y_ADD))   ; [x start] *65536 + [y start]
  3068.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  3069.         mov     edx,labelxplus      ; pointer to text beginning
  3070.         mov     esi,labelxplusend-labelxplus     ; text length
  3071.    ;     cmp     [move_flag],2
  3072.    ;     jne     @f
  3073.    ;     add     edx,navigation_size
  3074.    ;   @@:
  3075.         int     0x40
  3076.         ; VECTOR z- BUTTON
  3077.         mov     eax,8           ; function 8 : define and draw button
  3078.         mov     ebx,(SIZE_X+10)*65536+62-41     ; [x start] *65536 + [x size]
  3079.         mov     ecx,(25+140+15*(16+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  3080.         mov     edx,34           ; button id
  3081.         mov     esi,0x6688dd    ; button color RRGGBB
  3082.         int     0x40
  3083.         ;VECTOR z- LABEL
  3084.         mov     eax,4           ; function 4 : write text to window
  3085.         mov     ebx,(SIZE_X+12)*65536+(168+15*(16+.Y_ADD))   ; [x start] *65536 + [y start]
  3086.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  3087.         mov     edx,labelzminus      ; pointer to text beginning
  3088.         mov     esi,labelzminusend-labelzminus     ; text length
  3089.    ;     cmp     [move_flag],2
  3090.    ;     jne     @f
  3091.    ;     add     edx,navigation_size
  3092.    ;   @@:
  3093.         int     0x40
  3094.        ;VECTOR Y+ BUTTON
  3095.         mov     eax,8           ; function 8 : define and draw button
  3096.         mov     ebx,(SIZE_X+10+20)*65536+20     ; [x start] *65536 + [x size]
  3097.         mov     ecx,(165+15*(16+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  3098.         mov     edx,35           ; button id
  3099.         mov     esi,0x6688dd    ; button color RRGGBB
  3100.         int     0x40
  3101.         ;VECTOR Y+ LABEL
  3102.         mov     eax,4           ; function 4 : write text to window
  3103.         mov     ebx,(SIZE_X+32)*65536+(168+15*(16+.Y_ADD))   ; [x start] *65536 + [y start]
  3104.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  3105.         mov     edx,labelyplus      ; pointer to text beginning
  3106.         mov     esi,labelyplusend-labelyplus     ; text length
  3107.    ;     cmp     [move_flag],2
  3108.    ;     jne     @f
  3109.    ;     add     edx,navigation_size
  3110.    ;   @@:
  3111.         int     0x40
  3112.  
  3113.         mov     eax,12          ; function 12:tell os about windowdraw
  3114.         mov     ebx,2           ; 2, end of draw
  3115.         int     0x40
  3116.         ret
  3117.  
  3118.  
  3119.    ; DATA AREA  ************************************
  3120.  
  3121.    include 'DATA.INC'
  3122.  
  3123. MEM_END:
  3124.