Subversion Repositories Kolibri OS

Rev

Rev 2564 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. ; application : View3ds ver. 0.061 - 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.  
  669.    jmp     still
  670.  
  671. ;--------------------------------------------------------------------------------
  672. ;-------------------------PROCEDURES---------------------------------------------
  673. ;--------------------------------------------------------------------------------
  674. include "TEX3.INC"
  675. include "FLAT_CAT.INC"
  676. include "TEX_CAT.INC"
  677. include "BUMP_CAT.INC"
  678. include "3DMATH.INC"
  679. include "GRD3.INC"
  680. include "FLAT3.INC"
  681. include "BUMP3.INC"
  682. include "B_PROCS.INC"
  683. include "A_PROCS.INC"
  684. include "GRD_CAT.INC"
  685. include "BUMP_TEX.INC"
  686. include "GRD_TEX.INC"
  687. include "TWO_TEX.INC"
  688.  
  689.  
  690. alloc_buffer_mem:
  691.     movzx    ecx,[size_x]
  692.     movzx    eax,[size_y]
  693.     mul      ecx
  694.     lea      ecx,[eax*3]
  695.     push     ecx
  696.     shl      eax,2
  697.     add      ecx,eax
  698.     add      ecx,MEM_END
  699.     mov      ebx,1
  700.     mov      eax,64     ; allocate mem  - resize app mem
  701.     int      0x40
  702.     mov      [screen_ptr],MEM_END
  703.     mov      [Zbuffer_ptr],MEM_END
  704.     pop      ecx
  705.     add      [Zbuffer_ptr],ecx
  706. ret
  707.  
  708. update_flags:
  709. ; updates flags and writing flag description
  710. ; in    ah - button number
  711.         push    ax
  712.         mov     edi,menu
  713.       .ch_another:
  714.         cmp     ah,byte[edi]     ; ah = button id
  715.         jne     @f
  716.         mov     bl,byte[edi+11]  ; max_flag + 1
  717.         cmp     bl,255
  718.         je      .no_write
  719.         inc     byte[edi+12]     ; flag
  720.         cmp     byte[edi+12],bl
  721.         jne     .write
  722.         mov     byte[edi+12],0
  723.         jmp     .write
  724.       @@:
  725.         add     edi,17
  726.         cmp     byte[edi],-1
  727.         jne     .ch_another
  728.      .write:
  729. ;     clreol   {pascal never dies}
  730. ;          * eax = 13 - function number
  731. ;  * ebx = [coordinate on axis x]*65536 + [size on axis x]
  732. ;  * ecx = [coordinate on axis y]*65536 + [size on axis y]
  733. ;  * edx = color 0xRRGGBB or 0x80RRGGBB for gradient fill
  734.  
  735.         mov     eax,13                           ; function 13 write rectangle
  736.         movzx   ecx,byte[edi]
  737.         sub     cl,2
  738.         lea     ecx,[ecx*3]
  739.         lea     ecx,[ecx*5]
  740.         add     ecx,28
  741.         shl     ecx,16
  742.         add     ecx,14                          ;  ecx = [coord y]*65536 + [size y]
  743.         mov     ebx,(SIZE_X+12+70)*65536+25     ; [x start] *65536 + [size x]
  744.         mov     edx,0x00000000                  ;  color  0x00RRGGBB
  745.         int     0x40
  746.  
  747.         mov     eax,4                           ; function 4 : write text to window
  748.         movzx   ebx,byte[edi]
  749.         sub     bl,2
  750.         lea     ebx,[ebx*3]
  751.         lea     ebx,[ebx*5]
  752.         add     ebx,(SIZE_X+12+70)*65536+28     ; [x start] *65536 + [y start]
  753.         mov     ecx,0x20ddeeff                  ; font 1 & color ( 0xF0RRGGBB )
  754.         movzx   edx,byte[edi+12]                ; current flag
  755.         shl     edx,2                           ; * 4 = text length
  756.         add     edx,dword[edi+13]               ; pointer to text beginning
  757.         mov     esi,4                           ; text length -
  758.                                                 ; flag description 4 characters
  759.         int     0x40
  760.  
  761.      .no_write:
  762.         pop     ax
  763. ret
  764. normalize_all_light_vectors:
  765.         mov     edi,lights
  766.      @@:
  767.         call    normalize_vector           ;       3dmath.inc
  768.         add     edi,LIGHT_SIZE
  769.         cmp     edi,lightsend   ;ecx
  770.         jl      @b
  771. ret
  772.  
  773. calc_bumpmap_coords:      ; map texture, bump
  774. ;macro .comment222
  775. ;                                ; planar mapping
  776. ;        mov     esi,points
  777. ;        mov     edi,tex_points
  778. ;      @@:
  779. ;         add     esi,2
  780. ;         movsd
  781. ;         cmp     dword[esi],dword -1
  782. ;         jne     @b
  783.  
  784. ;      .Pi2  equ dword[ebp-4]
  785.  
  786. ;      mov   ebp,esp
  787. ;      sub   esp,4
  788.  
  789.       fninit
  790.       fldpi
  791.       fadd      st,st
  792.       mov       esi,[points_ptr]
  793.       mov       edi,tex_points
  794.       movzx     ecx,[points_count_var]
  795.       inc       ecx
  796. ;      cmp       [map_tex_flag],1
  797. ;      jne       .cylindric
  798.       ; spherical mapping around y axle
  799.  
  800.    @@:
  801.       fld       dword[esi]     ; x coord
  802.       fld       dword[esi+8]   ; z coord
  803.       fpatan                   ; arctg(st1/st)
  804. ;      fdiv      .Pi2
  805.       fdiv      st0,st1
  806.       fimul     [tex_x_div2]
  807.       fiadd     [tex_x_div2]
  808.       fistp     word[edi]      ; x
  809.  
  810.       fld       dword[esi+4]   ; y coord
  811.       fld       dword[esi]     ; x
  812.       fmul      st,st0
  813.       fld       dword[esi+4]   ; y
  814.       fmul      st,st0
  815.       fld       dword[esi+8]   ; z
  816.       fmul      st,st0
  817.       faddp
  818.       faddp
  819.       fsqrt
  820.       fpatan
  821.       fldpi
  822.       fdivp
  823.       fimul    [tex_y_div2]
  824.       fiadd    [tex_y_div2]
  825.       fistp    word[edi+2]     ; y
  826.  
  827.       add      esi,12
  828.       add      edi,4
  829.       loop     @b
  830.       ffree    st0
  831. ;      jmp      .end_map
  832. ;  .cylindric:
  833. ;       fld     dword[esi]     ; around y axle
  834. ;       fld     dword[esi+8]
  835. ;       fpatan
  836. ;       fdiv    st0,st1
  837. ;       fimul   [tex_x_div2]
  838. ;       fiadd   [tex_x_div2]
  839. ;       fistp   word[edi]
  840.  
  841. ;       fld     dword[esi+4]
  842. ;       fimul   [tex_y_div2]
  843. ;       fiadd   [tex_y_div2]
  844. ;       fistp   word[edi+2]
  845.  
  846. ;       add     esi,12
  847. ;       add     edi,4
  848. ;       loop    .cylindric
  849. ;       ffree    st0
  850. ;;      mov      esp,ebp
  851. ;   .end_map:
  852. ret
  853.  
  854.  
  855. init_envmap2:         ; do env_map using many light sources
  856. ;env_map 512 x 512 x 3 bytes
  857. .temp  equ word   [ebp-2]
  858. .nEy   equ word  [ebp-4]
  859. .nEx   equ word  [ebp-6]
  860. .col_r equ    [ebp-8]
  861. .col_g equ    [ebp-9]
  862. .col_b equ    [ebp-10]
  863.  
  864.          push     ebp
  865.          mov      ebp,esp
  866.          sub      esp,20
  867.          mov      edi,envmap
  868.          fninit
  869.  
  870.          mov      dx,- TEX_Y / 2 ;256   ; dx - vertical coordinate = y
  871.     .ie_ver:
  872.          mov      cx,- TEX_X / 2 ;256   ; cx - horizontal coord = x
  873.     .ie_hor:
  874.          xor      ebx,ebx
  875.          mov      dword .col_b, 0
  876.      .light:
  877.          lea      esi,[lights+ebx]
  878.          fld      dword[esi]     ; light vector x cooficient
  879.          fimul    [tex_x_div2] ;[i256]
  880.          mov      .temp,cx
  881.          fisubr   .temp
  882.          fistp    .nEx
  883.          fld      dword[esi+4]   ; light vector y cooficient
  884.          fimul    [tex_y_div2] ;[i256]
  885.          mov      .temp,dx
  886.          fisubr   .temp
  887.          fistp    .nEy
  888.  
  889.          cmp      .nEx,- TEX_X / 2 ;256
  890.          jl       .update_counters
  891.          cmp      .nEy,- TEX_Y / 2 ;256
  892.          jl       .update_counters
  893.          cmp      .nEx,TEX_X / 2 ;256
  894.          jg       .update_counters
  895.          cmp      .nEy,TEX_Y / 2 ;256
  896.          jg       .update_counters
  897.  
  898.          fild     .nEx
  899.          fmul     st,st0
  900.          fild     .nEy
  901.          fmul     st,st0
  902.          faddp
  903.          fsqrt
  904.          fisubr   [i256]
  905.          fmul     [env_const]
  906.          fidiv    [i256]   ; st - 'virtual' dot product
  907.  
  908.          fcom     [dot_max]
  909.          fstsw    ax
  910.          sahf
  911.          jb       @f
  912.          ffree    st
  913.          fld1     ;[dot_max]
  914.       @@:
  915.          fcom     [dot_min]
  916.          fstsw    ax
  917.          sahf
  918.          ja       @f
  919.          ffree    st
  920.          fldz     ;[dot_min]
  921.       @@:
  922.          push     ebp
  923.          movzx    ax,byte[esi+21]
  924.          push     ax  ;- shines
  925.          mov      al,byte[esi+14]   ; b    orginal color
  926.          push     ax
  927.          mov      al,byte[esi+13]   ; g
  928.          push     ax
  929.          mov      al,byte[esi+12]   ; r
  930.          push     ax
  931.          mov      al,byte[esi+20]   ; b     max color
  932.          push     ax
  933.          mov      al,byte[esi+19]   ; g
  934.          push     ax
  935.          mov      al,byte[esi+18]   ; r
  936.          push     ax
  937.          mov      al,byte[esi+17]   ; b    min col
  938.          push     ax
  939.          mov      al,byte[esi+16]   ; g
  940.          push     ax
  941.          mov      al,byte[esi+15]   ; r
  942.          push     ax
  943.          push     eax         ; earlier - dot pr
  944.       ;  fstp     .dot_product
  945.       ;  push     .dot_product
  946.          call     calc_one_col
  947.          pop      ebp
  948.          ; eax-0x00rrggbb
  949.          cmp      al,.col_b
  950.          jbe      @f
  951.          mov      .col_b,al
  952.    @@:                        ;  eax - ggbb00rr
  953.          shr      ax,8
  954.          cmp      al,.col_g
  955.          jbe      @f
  956.          mov      .col_g,al
  957.    @@:                        ;  eax - bb0000gg
  958.          shr      eax,16
  959.          cmp      al,.col_r
  960.          jbe      @f
  961.          mov      .col_r,al
  962.    @@:
  963.    .update_counters:                     ; update and jump when neccesery
  964.          add      ebx,LIGHT_SIZE
  965.          cmp      bx,[all_lights_size]
  966.          jl       .light    ; next_light
  967.          mov      eax,dword .col_b
  968.          stosd
  969.          dec      edi
  970.  
  971.          inc      cx
  972.          cmp      cx,TEX_X / 2 ;256
  973.          jne      .ie_hor
  974.  
  975.          inc      dx
  976.          cmp      dx,TEX_Y / 2 ;256
  977.          jne     .ie_ver
  978.  
  979.          mov     esp,ebp
  980.          pop     ebp
  981. ret
  982.  
  983.  
  984.  
  985. do_color_buffer:         ; do color buffer for Gouraud, flat shading
  986. ;env_map 512 x 512 x 3 bytes    ; many lights using
  987. .temp  equ word   [ebp-2]
  988. .nz    equ dword  [ebp-6]  ; dword
  989. .ny    equ dword  [ebp-10]
  990. .nx    equ dword  [ebp-14]
  991. .col_r equ    [ebp-16]
  992. .col_g equ    [ebp-17]
  993. .col_b equ    [ebp-18]
  994.  
  995.          push     ebp
  996.          mov      ebp,esp
  997.          sub      esp,20
  998.          mov      edi,color_map
  999.          fninit
  1000.  
  1001.          mov      dx,- TEX_Y / 2 ;-256   ; dx - vertical coordinate = y
  1002.     .ie_ver:
  1003.          mov      cx,- TEX_X / 2 ;256   ; cx - horizontal coord = x
  1004.     .ie_hor:
  1005.          mov      .temp,cx
  1006.          fild     .temp
  1007.          fidiv    [i256]   ;st = Nx - vector normal x cooficient
  1008.          fst      .nx
  1009.          fmul     st,st0
  1010.          mov      .temp,dx
  1011.          fild     .temp
  1012.          fidiv    [i256]   ; st = Ny - vector normal y coeficient
  1013.          fst      .ny
  1014.          fmul     st,st0
  1015.          faddp
  1016.          fld1
  1017.          fchs
  1018.          faddp
  1019.          fabs
  1020.          fsqrt
  1021.          fchs
  1022.          fstp     .nz              ; st - Nz - vect normal z coeficient
  1023.          xor      ebx,ebx
  1024.          mov      dword .col_b, 0
  1025.      .light:
  1026.          push     edi   ;env_map
  1027.          lea      esi,[lights+ebx]
  1028.          lea      edi,.nx
  1029.          call     dot_product
  1030.          pop      edi
  1031.          fcom     [dot_min]
  1032.          fstsw    ax
  1033.          sahf
  1034.          ja       .env_ok1  ;compare with dot_max
  1035.          ffree    st
  1036.  
  1037.         jmp       .update_counters
  1038.       .env_ok1:
  1039.          fcom    [dot_max]
  1040.          fstsw   ax
  1041.          sahf
  1042.          jb      .env_ok2     ; calc col
  1043.          ffree   st
  1044.          jmp     .update_counters
  1045.       .env_ok2:            ;calc col
  1046.          push     ebp
  1047.          movzx    ax,byte[esi+21]
  1048.          push     ax  ;- shines
  1049.          mov      al,byte[esi+14]   ; b    orginal color
  1050.          push     ax
  1051.          mov      al,byte[esi+13]   ; g
  1052.          push     ax
  1053.          mov      al,byte[esi+12]   ; r
  1054.          push     ax
  1055.          mov      al,byte[esi+20]   ; b     max color
  1056.          push     ax
  1057.          mov      al,byte[esi+19]   ; g
  1058.          push     ax
  1059.          mov      al,byte[esi+18]   ; r
  1060.          push     ax
  1061.          mov      al,byte[esi+17]   ; b    min col
  1062.          push     ax
  1063.          mov      al,byte[esi+16]   ; g
  1064.          push     ax
  1065.          mov      al,byte[esi+15]   ; r
  1066.          push     ax
  1067.          push     eax         ; earlier - dot pr
  1068.       ;  fstp     .dot_product
  1069.       ;  push     .dot_product
  1070.          call     calc_one_col
  1071.          pop      ebp
  1072.          ; eax-0x00rrggbb
  1073.          cmp      al,.col_b
  1074.          jbe      @f
  1075.          mov      .col_b,al
  1076.    @@:
  1077.          shr      ax,8
  1078.          cmp      al,.col_g
  1079.          jbe      @f
  1080.          mov      .col_g,al
  1081.    @@:
  1082.          shr      eax,16
  1083.          cmp      al,.col_r
  1084.          jbe      @f
  1085.          mov      .col_r,al
  1086.   @@:
  1087.  .update_counters:                                  ; update and jump when neccesery
  1088.         add     ebx,LIGHT_SIZE
  1089.         cmp     bx,[all_lights_size]
  1090.         jl      .light    ; next_light
  1091.         mov     eax,dword .col_b
  1092.         stosd
  1093.         dec     edi
  1094.  
  1095.         inc     cx
  1096.         cmp     cx,TEX_X / 2 ;256
  1097.         jne     .ie_hor
  1098.  
  1099.         inc     dx
  1100.         cmp     dx,TEX_X / 2 ;256
  1101.         jne     .ie_ver
  1102.  
  1103.     .env_done:
  1104.          mov     esp,ebp
  1105.          pop     ebp
  1106. ret
  1107. if 0
  1108. init_triangles_normals:
  1109.         mov     ebx,triangles_normals
  1110.         mov     ebp,triangles
  1111.      @@:
  1112.         push    ebx
  1113.         mov     ebx,vectors
  1114.         movzx   esi,word[ebp]          ; first point index
  1115.         lea     esi,[esi*3]
  1116.         lea     esi,[points+esi*2]     ; esi - pointer to 1st 3d point
  1117.         movzx   edi,word[ebp+2]        ; second point index
  1118.         lea     edi,[edi*3]
  1119.         lea     edi,[points+edi*2]     ; edi - pointer to 2nd 3d point
  1120.         call    make_vector
  1121.         add     ebx,12
  1122.         mov     esi,edi
  1123.         movzx   edi,word[ebp+4]        ; third point index
  1124.         lea     edi,[edi*3]
  1125.         lea     edi,[points+edi*2]
  1126.         call    make_vector
  1127.         mov     edi,ebx                 ; edi - pointer to 2nd vector
  1128.         mov     esi,ebx
  1129.         sub     esi,12                  ; esi - pointer to 1st vector
  1130.         pop     ebx
  1131.         call    cross_product
  1132.         mov     edi,ebx
  1133.         call    normalize_vector
  1134.         add     ebp,6
  1135.         add     ebx,12
  1136.         cmp     dword[ebp],-1
  1137.         jne     @b
  1138. ret
  1139. end if
  1140. init_point_normals:
  1141. .x equ dword [ebp-4]
  1142. .y equ dword [ebp-8]
  1143. .z equ dword [ebp-12]
  1144. .point_number equ word [ebp-26]
  1145. .hit_faces    equ word [ebp-28]
  1146.  
  1147.         fninit
  1148.         mov       ebp,esp
  1149.         sub       esp,28
  1150.         mov       edi,[points_normals_ptr]
  1151.         mov       .point_number,0
  1152.     .ipn_loop:
  1153.         mov       .hit_faces,0
  1154.         mov       .x,0
  1155.         mov       .y,0
  1156.         mov       .z,0
  1157.         mov       esi,[triangles_ptr]
  1158.         xor       ecx,ecx              ; ecx - triangle number
  1159.     .ipn_check_face:
  1160.         xor       ebx,ebx              ; ebx - 'position' in one triangle
  1161.     .ipn_check_vertex:
  1162.         movzx     eax,word[esi+ebx]    ;  eax - point_number
  1163.         cmp       ax,.point_number
  1164.         jne       .ipn_next_vertex
  1165.         push      esi
  1166.         mov       esi,ecx
  1167.         lea       esi,[esi*3]
  1168.        ; lea       esi,[triangles_normals+esi*4]
  1169.         shl       esi,2
  1170.         add       esi,[triangles_normals_ptr]
  1171.  
  1172.         fld       .x
  1173.         fadd      dword[esi+vec_x]       ; vec_x this defined in 3dmath.asm - x cooficient
  1174.         fstp      .x                     ; of normal vactor
  1175.         fld       .y
  1176.         fadd      dword[esi+vec_y]
  1177.         fstp      .y
  1178.         fld       .z
  1179.         fadd      dword[esi+vec_z]
  1180.         fstp      .z
  1181.         pop       esi
  1182.         inc       .hit_faces
  1183.         jmp       .ipn_next_face
  1184.     .ipn_next_vertex:
  1185.         add       ebx,2
  1186.         cmp       ebx,6
  1187.         jne       .ipn_check_vertex
  1188.     .ipn_next_face:
  1189.         add       esi,6
  1190.         inc       ecx
  1191.         cmp       cx,[triangles_count_var]
  1192.         jne       .ipn_check_face
  1193.  
  1194.         fld       .x
  1195.         fidiv     .hit_faces
  1196.         fstp      dword[edi+vec_x]
  1197.         fld       .y
  1198.         fidiv     .hit_faces
  1199.         fstp      dword[edi+vec_y]
  1200.         fld       .z
  1201.         fidiv     .hit_faces
  1202.         fstp      dword[edi+vec_z]
  1203.         call      normalize_vector
  1204.         add       edi,12  ;type vector 3d
  1205.         inc       .point_number
  1206.         mov       dx,.point_number
  1207.         cmp       dx,[points_count_var]
  1208.         jne       .ipn_loop
  1209.  
  1210.         mov       esp,ebp
  1211. ret
  1212. ;===============================================================
  1213.  
  1214. init_triangles_normals2:
  1215.         mov     ebx,[triangles_normals_ptr]
  1216.         mov     ebp,[triangles_ptr]
  1217.      @@:
  1218.         push    ebx
  1219.         mov     ebx,vectors
  1220.         movzx   esi,word[ebp]          ; first point index
  1221.         lea     esi,[esi*3]
  1222. ;        lea     esi,[points+esi*2]     ; esi - pointer to 1st 3d point
  1223.         shl     esi,2
  1224.         add     esi,[points_ptr]
  1225.         movzx   edi,word[ebp+2]          ; first point index
  1226.         lea     edi,[edi*3]
  1227.         shl     edi,2
  1228.         add     edi,[points_ptr]
  1229. ;        movzx   edi,word[ebp+2]        ; second point index
  1230. ;        lea     edi,[edi*3]
  1231. ;        lea     edi,[points+edi*2]     ; edi - pointer to 2nd 3d point
  1232.         call    make_vector_r
  1233.         add     ebx,12
  1234.         mov     esi,edi
  1235.         movzx   edi,word[ebp+4]        ; third point index
  1236.         lea     edi,[edi*3]
  1237.         shl     edi,2
  1238.         add     edi,[points_ptr]
  1239. ;        lea     edi,[points+edi*2]
  1240.         call    make_vector_r
  1241.         mov     edi,ebx                 ; edi - pointer to 2nd vector
  1242.         mov     esi,ebx
  1243.         sub     esi,12                  ; esi - pointer to 1st vector
  1244.         pop     ebx
  1245.         call    cross_product
  1246.         mov     edi,ebx
  1247.         call    normalize_vector
  1248.         add     ebp,6
  1249.         add     ebx,12
  1250.         cmp     dword[ebp],-1
  1251.         jne     @b
  1252. ret
  1253.  
  1254.  
  1255. ;=================================================================
  1256. sort_triangles:
  1257.         mov     esi,[triangles_ptr]
  1258.         mov     edi,triangles_with_z
  1259.         mov     ebp,[points_translated_ptr]
  1260.  
  1261.     make_triangle_with_z:       ;makes list with triangles and z position
  1262.         movzx   eax,word[esi]
  1263.         lea     eax,[eax*3]
  1264.         movzx   ecx,word[ebp+eax*2+4]
  1265.  
  1266.         movzx   eax,word[esi+2]
  1267.         lea     eax,[eax*3]
  1268.         add     cx,word[ebp+eax*2+4]
  1269.  
  1270.         movzx   eax,word[esi+4]
  1271.         lea     eax,[eax*3]
  1272.         add     cx,word[ebp+eax*2+4]
  1273.  
  1274.         mov     ax,cx
  1275.        ; cwd
  1276.        ; idiv    word[i3]
  1277.         movsd                   ; store vertex coordinates
  1278.         movsw
  1279.         stosw                   ; middle vertex coordinate  'z' in triangles_with_z list
  1280.         cmp     dword[esi],-1
  1281.         jne     make_triangle_with_z
  1282.         movsd                   ; copy end mark
  1283.         mov     eax,4
  1284.         lea     edx,[edi-8-trizdd]
  1285.      ;   lea     edx, [edi-8]
  1286.      ;   sub     edx,[triangles_w_z_ptr]
  1287.         mov     [high],edx
  1288.         call    quicksort
  1289.         mov     eax,4
  1290.         mov     edx,[high]
  1291.         call    insertsort
  1292.         jmp     end_sort
  1293.  
  1294.     quicksort:
  1295.         mov     ecx,edx
  1296.         sub     ecx,eax
  1297.         cmp     ecx,32
  1298.         jc      .exit
  1299.         lea     ecx,[eax+edx]
  1300.         shr     ecx,4
  1301.         lea     ecx,[ecx*8-4];
  1302. ;        mov     edi,[triangles_w_z_ptr]
  1303. ;        mov     ebx,[edi+eax]
  1304. ;        mov     esi,[edi+ecx]
  1305. ;        mov     edi,[edi+edx]
  1306.         mov     ebx,[trizdd+eax]; trizdd[l]
  1307.         mov     esi,[trizdd+ecx]; trizdd[i]
  1308.         mov     edi,[trizdd+edx]; trizdd[h]
  1309.         cmp     ebx,esi
  1310.         jg      @f              ; direction NB! you need to negate these to invert the order
  1311.       if Ext=NON
  1312.         mov     [trizdd+eax],esi
  1313.         mov     [trizdd+ecx],ebx
  1314.         mov     ebx,[trizdd+eax-4]
  1315.         mov     esi,[trizdd+ecx-4]
  1316.         mov     [trizdd+eax-4],esi
  1317.         mov     [trizdd+ecx-4],ebx
  1318.         mov     ebx,[trizdd+eax]
  1319.         mov     esi,[trizdd+ecx]
  1320.       else
  1321. ;        push    ebx
  1322. ;        mov     ebx,[triangles_w_z_ptr]
  1323. ;        movq    mm0,[ebx+eax-4]
  1324. ;        movq    mm1,[ebx+ecx-4]
  1325. ;        movq    [ebx+ecx-4],mm0
  1326. ;        movq    [ebx+eax-4],mm1
  1327. ;        pop     ebx
  1328.         movq    mm0,[trizdq+eax-4]
  1329.         movq    mm1,[trizdq+ecx-4]
  1330.         movq    [trizdq+ecx-4],mm0
  1331.         movq    [trizdq+eax-4],mm1
  1332.         xchg    ebx,esi
  1333.       end if
  1334.       @@:
  1335.         cmp     ebx,edi
  1336.         jg      @f              ; direction
  1337.       if Ext=NON
  1338.         mov     [trizdd+eax],edi
  1339.         mov     [trizdd+edx],ebx
  1340.         mov     ebx,[trizdd+eax-4]
  1341.         mov     edi,[trizdd+edx-4]
  1342.         mov     [trizdd+eax-4],edi
  1343.         mov     [trizdd+edx-4],ebx
  1344.         mov     ebx,[trizdd+eax]
  1345.         mov     edi,[trizdd+edx]
  1346.       else
  1347. ;        push    ebx
  1348. ;        mov     ebx,[triangles_w_z_ptr]
  1349. ;        movq    mm0,[ebx+eax-4]
  1350. ;        movq    mm1,[ebx+edx-4]
  1351. ;        movq    [ebx+edx-4],mm0
  1352. ;        movq    [ebx+eax-4],mm1
  1353.         movq    mm0,[trizdq+eax-4]
  1354.         movq    mm1,[trizdq+edx-4]
  1355.         movq    [trizdq+edx-4],mm0
  1356.         movq    [trizdq+eax-4],mm1
  1357. ;        pop     ebx
  1358.         xchg    ebx,edi
  1359.       end if
  1360.       @@:
  1361.         cmp     esi,edi
  1362.         jg      @f              ; direction
  1363.       if Ext=NON
  1364.         mov     [trizdd+ecx],edi
  1365.         mov     [trizdd+edx],esi
  1366.         mov     esi,[trizdd+ecx-4]
  1367.         mov     edi,[trizdd+edx-4]
  1368.         mov     [trizdd+ecx-4],edi
  1369.         mov     [trizdd+edx-4],esi
  1370.       else
  1371. ;        push    ebx
  1372. ;        mov     ebx,[triangles_w_z_ptr]
  1373. ;        movq    mm0,[ebx+ecx-4]
  1374. ;        movq    mm1,[ebx+edx-4]
  1375. ;        movq    [ebx+edx-4],mm0
  1376. ;        movq    [ebx+ecx-4],mm1
  1377. ;        pop     ebx
  1378.  
  1379.         movq    mm0,[trizdq+ecx-4]
  1380.         movq    mm1,[trizdq+edx-4]
  1381.         movq    [trizdq+edx-4],mm0
  1382.         movq    [trizdq+ecx-4],mm1
  1383.         xchg    ebx,esi
  1384.       end if
  1385.       @@:
  1386.         mov     ebp,eax         ; direction
  1387.         add     ebp,8      ;   j
  1388.       if Ext=NON
  1389.         mov     esi,[trizdd+ebp]
  1390.         mov     edi,[trizdd+ecx]
  1391.         mov     [trizdd+ebp],edi
  1392.         mov     [trizdd+ecx],esi
  1393.         mov     esi,[trizdd+ebp-4]
  1394.         mov     edi,[trizdd+ecx-4]
  1395.         mov     [trizdd+ecx-4],esi
  1396.         mov     [trizdd+ebp-4],edi
  1397.       else
  1398. ;        push    ebx
  1399. ;        mov     ebx,[triangles_w_z_ptr]
  1400. ;        movq    mm0,[ebx+ebp-4]
  1401. ;        movq    mm1,[ebx+ecx-4]
  1402. ;        movq    [ebx+ecx-4],mm0
  1403. ;        movq    [ebx+ebp-4],mm1
  1404. ;        pop     ebx
  1405.  
  1406.         movq    mm0,[trizdq+ebp-4]
  1407.         movq    mm1,[trizdq+ecx-4]
  1408.         movq    [trizdq+ecx-4],mm0
  1409.         movq    [trizdq+ebp-4],mm1
  1410.       end if
  1411.         mov     ecx,edx    ;   i; direction
  1412.         mov     ebx,[trizdd+ebp]; trizdd[j]
  1413. ;        mov     ebx, [triangles_w_z_ptr]
  1414. ;        add     ebx, ebp
  1415.  
  1416.  ;       push    eax
  1417.  ;       mov     eax, [triangles_w_z_ptr]
  1418.       .loop:
  1419.         sub     ecx,8           ; direction
  1420.         cmp     [trizdd+ecx],ebx
  1421. ;        cmp     [eax+ecx],ebx
  1422.         jl      .loop           ; direction
  1423.       @@:
  1424.         add     ebp,8           ; direction
  1425.         cmp     [trizdd+ebp],ebx
  1426. ;        cmp     [eax+ebp],ebx
  1427.         jg      @b              ; direction
  1428.         cmp     ebp,ecx
  1429.         jge     @f              ; direction
  1430.       if Ext=NON
  1431.         mov     esi,[trizdd+ecx]
  1432.         mov     edi,[trizdd+ebp]
  1433.         mov     [trizdd+ebp],esi
  1434.         mov     [trizdd+ecx],edi
  1435.         mov     edi,[trizdd+ecx-4]
  1436.         mov     esi,[trizdd+ebp-4]
  1437.         mov     [trizdd+ebp-4],edi
  1438.         mov     [trizdd+ecx-4],esi
  1439.       else
  1440. ;        movq    mm0,[eax+ecx-4]
  1441. ;        movq    mm1,[eax+ebp-4]
  1442. ;        movq    [eax+ebp-4],mm0
  1443. ;        movq    [eax+ecx-4],mm1
  1444.         movq    mm0,[trizdq+ecx-4]
  1445.         movq    mm1,[trizdq+ebp-4]
  1446.         movq    [trizdq+ebp-4],mm0
  1447.         movq    [trizdq+ecx-4],mm1
  1448.       end if
  1449.         jmp     .loop
  1450. ;        pop     eax
  1451.       @@:
  1452.       if Ext=NON
  1453.         mov     esi,[trizdd+ecx]
  1454.         mov     edi,[trizdd+eax+8]
  1455.         mov     [trizdd+eax+8],esi
  1456.         mov     [trizdd+ecx],edi
  1457.         mov     edi,[trizdd+ecx-4]
  1458.         mov     esi,[trizdd+eax+4]
  1459.         mov     [trizdd+eax+4],edi
  1460.         mov     [trizdd+ecx-4],esi
  1461.       else
  1462. ;        push    edx
  1463. ;        mov     edx,[triangles_w_z_ptr]
  1464. ;        movq    mm0,[edx+ecx-4]
  1465. ;        movq    mm1,[edx+eax+4]; dir
  1466. ;        movq    [edx+eax+4],mm0; dir
  1467. ;        movq    [edx+ecx-4],mm1
  1468. ;        pop     edx
  1469.  
  1470.         movq    mm0,[trizdq+ecx-4]
  1471.         movq    mm1,[trizdq+eax+4]; dir
  1472.         movq    [trizdq+eax+4],mm0; dir
  1473.         movq    [trizdq+ecx-4],mm1
  1474.       end if
  1475.         add     ecx,8
  1476.         push    ecx edx
  1477.         mov     edx,ebp
  1478.         call    quicksort
  1479.         pop     edx eax
  1480.         call    quicksort
  1481.       .exit:
  1482.     ret
  1483.     insertsort:
  1484.         mov     esi,eax
  1485.       .start:
  1486.         add     esi,8
  1487.         cmp     esi,edx
  1488.         ja      .exit
  1489.         mov     ebx,[trizdd+esi]
  1490. ;        mov     ebx,[triangles_w_z_ptr]
  1491. ;        add     ebx,esi
  1492.       if Ext=NON
  1493.         mov     ecx,[trizdd+esi-4]
  1494.       else
  1495. ;        push    ebx
  1496. ;        mov     ebx,[triangles_w_z_ptr]
  1497. ;        movq    mm1,[ebx+esi-4]
  1498.         movq    mm1,[trizdq+esi-4]
  1499. ;        pop     ebx
  1500.       end if
  1501.         mov     edi,esi
  1502.       @@:
  1503.         cmp     edi,eax
  1504.         jna     @f
  1505. ;        push    eax
  1506. ;        mov     eax,[triangles_w_z_ptr]
  1507. ;        cmp     [eax+edi-8],ebx
  1508. ;        pop     eax
  1509.        cmp     [trizdd+edi-8],ebx
  1510.         jg      @f                 ; direction
  1511.       if Ext=NON
  1512.         mov     ebp,[trizdd+edi-8]
  1513.         mov     [trizdd+edi],ebp
  1514.         mov     ebp,[trizdd+edi-12]
  1515.         mov     [trizdd+edi-4],ebp
  1516.       else
  1517. ;        push    eax
  1518. ;        mov     eax,[triangles_w_z_ptr]
  1519. ;        movq    mm0,[eax+edi-12]
  1520. ;        movq    [eax+edi-4],mm0
  1521.         movq    mm0,[trizdq+edi-12]
  1522.         movq    [trizdq+edi-4],mm0
  1523. ;        pop     eax
  1524.       end if
  1525.         sub     edi,8
  1526.         jmp     @b
  1527.       @@:
  1528.       if Ext=NON
  1529.         mov     [trizdd+edi],ebx
  1530.         mov     [trizdd+edi-4],ecx
  1531.       else
  1532. ;        push    eax
  1533. ;        mov     eax,[triangles_w_z_ptr]
  1534. ;        movq    [eax+edi-4],mm1
  1535.         movq    [trizdq+edi-4],mm1
  1536. ;        pop     eax
  1537.       end if
  1538.         jmp     .start
  1539.       .exit:
  1540.     ret
  1541.    end_sort:
  1542.     ; translate triangles_with_z to sorted_triangles
  1543.         mov     esi,triangles_with_z
  1544. ;        mov      esi,[triangles_w_z_ptr]
  1545.       ;  mov     edi,sorted_triangles
  1546.         mov      edi,[triangles_ptr]
  1547.     again_copy:
  1548.       if Ext=NON
  1549.         movsd
  1550.         movsw
  1551.         add     esi,2
  1552.       else
  1553.         movq    mm0,[esi]
  1554.         movq    [edi],mm0
  1555.         add     esi,8
  1556.         add     edi,6
  1557.       end if
  1558.         cmp     dword[esi],-1
  1559.         jne     again_copy
  1560. ;      if Ext=MMX
  1561. ;        emms
  1562. ;      end if
  1563.         movsd  ; copy end mark too
  1564. ret
  1565.  
  1566. clrscr:
  1567.         mov     edi,screen
  1568.         mov     ecx,SIZE_X*SIZE_Y*3/4
  1569.         xor     eax,eax
  1570.       if Ext=NON
  1571.         rep     stosd
  1572.       else
  1573.         pxor    mm0,mm0
  1574.       @@:
  1575.         movq    [edi+00],mm0
  1576.         movq    [edi+08],mm0
  1577.         movq    [edi+16],mm0
  1578.         movq    [edi+24],mm0
  1579.         add     edi,32
  1580.         sub     ecx,8
  1581.         jnc     @b
  1582.       end if
  1583. ret
  1584.  
  1585.  
  1586. draw_triangles:
  1587.         mov esi,[triangles_ptr]
  1588.     .again_dts:
  1589.         mov ebp,[points_translated_ptr]
  1590.       if Ext=NON
  1591.         movzx   eax,word[esi]
  1592.         mov     [point_index1],ax
  1593.         lea     eax,[eax*3]
  1594.         add     eax,eax
  1595.         push    ebp
  1596.         add     ebp,eax
  1597.         mov     eax,[ebp]
  1598.         mov     dword[xx1],eax
  1599.         mov     eax,[ebp+4]
  1600.         mov     [zz1],ax
  1601.         pop     ebp
  1602.  
  1603.  
  1604.         movzx   eax,word[esi+2]
  1605.         mov     [point_index2],ax
  1606.         lea     eax,[eax*3]
  1607.         add     eax,eax
  1608.         push    ebp
  1609.         add     ebp,eax
  1610.         mov     eax,[ebp]
  1611.         mov     dword[xx2],eax
  1612.         mov     eax,[ebp+4]
  1613.         mov     [zz2],ax
  1614.         pop     ebp
  1615.  
  1616.  
  1617.         movzx   eax,word[esi+4]        ; xyz3 = [ebp+[esi+4]*6]
  1618.         mov     [point_index3],ax
  1619.         lea     eax,[eax*3]
  1620.         add     eax,eax
  1621.     ;    push    ebp
  1622.         add     ebp,eax
  1623.         mov     eax,[ebp]
  1624.         mov     dword[xx3],eax
  1625.         mov     eax,[ebp+4]
  1626.         mov     [zz3],ax
  1627.       else
  1628.         mov     eax,dword[esi]           ; don't know MMX
  1629.         mov     dword[point_index1],eax
  1630.        ; shr     eax,16
  1631.        ; mov     [point_index2],ax
  1632.         mov     ax,word[esi+4]
  1633.         mov     [point_index3],ax
  1634.         movq    mm0,[esi]
  1635.         pmullw  mm0,qword[const6]
  1636.         movd    eax,mm0
  1637.         psrlq   mm0,16
  1638.         movd    ebx,mm0
  1639.         psrlq   mm0,16
  1640.         movd    ecx,mm0
  1641.         and     eax,0FFFFh
  1642.         and     ebx,0FFFFh
  1643.         and     ecx,0FFFFh
  1644.         movq    mm0,[ebp+eax]
  1645.         movq    mm1,[ebp+ebx]
  1646.         movq    mm2,[ebp+ecx]
  1647.         movq    qword[xx1],mm0
  1648.         movq    qword[xx2],mm1
  1649.         movq    qword[xx3],mm2
  1650. ;        emms
  1651.       end if                              ; *********************************
  1652.         push esi                          ;
  1653.         fninit                            ; DO culling AT FIRST
  1654.         cmp     [culling_flag],1          ; (if culling_flag = 1)
  1655.         jne     .no_culling
  1656.         mov     esi,point_index1          ; *********************************
  1657.         mov     ecx,3                     ;
  1658.       @@:
  1659.         movzx   eax,word[esi]
  1660.         lea     eax,[eax*3]
  1661.         shl     eax,2
  1662.         add     eax,[points_normals_rot_ptr]
  1663. ;        lea     eax,[eax+point_normals_rotated]
  1664.         fld     dword[eax+8]             ; *****************************
  1665.         ftst                             ; CHECKING OF Z COOFICIENT OF
  1666.         fstsw   ax                       ; NORMAL VECTOR
  1667.         sahf
  1668.         jb      @f
  1669.         ffree   st
  1670.         loop    @b
  1671.         jmp     .end_draw   ; non visable
  1672.       @@:
  1673.         ffree   st  ;is visable
  1674.       .no_culling:
  1675.         cmp     [dr_flag],0               ; draw type flag
  1676.         je      .flat_draw
  1677.         cmp     [dr_flag],2
  1678.         je      .env_mapping
  1679.         cmp     [dr_flag],3
  1680.         je      .bump_mapping
  1681.         cmp     [dr_flag],4
  1682.         je      .tex_mapping
  1683.         cmp     [dr_flag],5
  1684.         je      .rainbow
  1685.         cmp     [dr_flag],7
  1686.         je      .grd_tex
  1687.         cmp     [dr_flag],8
  1688.         je      .two_tex
  1689.         cmp     [dr_flag],9
  1690.         je      .bump_tex
  1691.         cmp     [dr_flag],10
  1692.         je      .cubic_env_mapping
  1693.                                       ; ****************
  1694.         mov     esi,point_index3      ; do Gouraud shading
  1695.         mov     ecx,3
  1696.       .again_grd_draw:
  1697.         movzx   eax,word[esi]
  1698.         shl     eax,2
  1699.         lea     eax,[eax*3]
  1700.         add     eax,[points_normals_rot_ptr]
  1701.         ; texture x=(rotated point normal -> x * 255)+255
  1702.         fld     dword[eax]       ; x cooficient of normal vector
  1703.         fimul   [correct_tex]
  1704.         fiadd   [correct_tex]
  1705.         fistp   word[esp-2]
  1706.         ; texture y=(rotated point normal -> y * 255)+255
  1707.         fld     dword[eax+4]      ; y cooficient
  1708.         fimul   [correct_tex]
  1709.         fiadd   [correct_tex]
  1710.         fistp   word[esp-4]
  1711.  
  1712.         movzx    eax,word[esp-4]
  1713.         movzx    ebx,word[esp-2]
  1714.         shl      eax,TEX_SHIFT
  1715.         add      eax,ebx
  1716.         lea      eax,[eax*3+color_map]
  1717.         mov      eax,dword[eax]
  1718.         cmp     [catmull_flag],1      ; put on stack z coordinate if necessary
  1719.         jne      @f
  1720.         lea      edx,[ecx*3]
  1721.         push     word[edx*2+xx1-2]    ; zz1 ,2 ,3
  1722.       @@:
  1723.         ror      eax,16               ; eax -0xxxrrggbb -> 0xggbbxxrr
  1724.         xor      ah,ah
  1725.         push     ax         ;r
  1726.         rol      eax,8                ; eax-0xggbb00rr -> 0xbb00rrgg
  1727.         xor      ah,ah
  1728.         push     ax         ;g
  1729.         shr      eax,24
  1730.         push     ax         ;b
  1731.  
  1732.         sub      esi,2
  1733.         dec      cx
  1734.         jnz      .again_grd_draw
  1735.         jmp      .both_draw
  1736.  
  1737.    ;     movzx   edi,[point_index3]   ;gouraud shading according to light vector
  1738.    ;     lea     edi,[edi*3]
  1739.    ;     lea     edi,[4*edi+point_normals_rotated] ; edi - normal
  1740.    ;     mov     esi,light_vector
  1741.    ;     call    dot_product
  1742.    ;     fabs
  1743.    ;     fimul   [orginal_color_r]
  1744.    ;     fistp   [temp_col]
  1745.    ;     and     [temp_col],0x00ff
  1746.    ;     push    [temp_col]
  1747.    ;     push    [temp_col]
  1748.    ;     push    [temp_col]
  1749.  
  1750.    ;     movzx   edi,[point_index2]
  1751.    ;     lea     edi,[edi*3]
  1752.    ;     lea     edi,[4*edi+point_normals_rotated] ; edi - normal
  1753.    ;     mov     esi,light_vector
  1754.    ;     call    dot_product
  1755.    ;     fabs
  1756.    ;     fimul   [orginal_color_r]
  1757.    ;     fistp    [temp_col]
  1758.    ;     and     [temp_col],0x00ff
  1759.    ;     push    [temp_col]
  1760.    ;     push    [temp_col]
  1761.    ;     push    [temp_col]
  1762.  
  1763.    ;     movzx   edi,[point_index1]
  1764.    ;     lea     edi,[edi*3]
  1765.    ;     lea     edi,[4*edi+point_normals_rotated] ; edi - normal
  1766.    ;     mov     esi,light_vector
  1767.    ;     call    dot_product
  1768.    ;     fabs
  1769.    ;     fimul   [orginal_color_r]
  1770.    ;     fistp   [temp_col]
  1771.    ;     and     [temp_col],0x00ff
  1772.    ;     push    [temp_col]
  1773.    ;     push    [temp_col]
  1774.    ;     push    [temp_col]
  1775.    .rainbow:
  1776.         cmp     [catmull_flag],1      ; put on stack z coordinate if necessary
  1777.         jne      @f
  1778.         push     [zz3]
  1779.       @@:
  1780.         mov      eax,dword[yy3]
  1781.         mov      ebx,0x00ff00ff
  1782.         and      eax,ebx
  1783.         push     eax
  1784.         neg      al
  1785.         push     ax
  1786.         cmp     [catmull_flag],1
  1787.         jne      @f
  1788.         push     [zz2]
  1789.       @@:
  1790.         mov      eax,dword[yy2]
  1791.         and      eax,ebx
  1792.         push     eax
  1793.         neg      al
  1794.         push     ax
  1795.         cmp     [catmull_flag],1
  1796.         jne      @f
  1797.         push     [zz1]
  1798.       @@:
  1799.         mov      eax,dword[yy1]
  1800.         and      eax,ebx
  1801.         push     eax
  1802.         neg      al
  1803.         push     ax
  1804.     .both_draw:
  1805.         mov     eax,dword[xx1]
  1806.         ror     eax,16
  1807.         mov     ebx,dword[xx2]
  1808.         ror     ebx,16
  1809.         mov     ecx,dword[xx3]
  1810.         ror     ecx,16
  1811.         lea     edi,[screen]
  1812.         cmp     [catmull_flag],0
  1813.         je      @f
  1814.   ;      lea     esi,[Z_buffer]
  1815.         mov     esi,[Zbuffer_ptr]
  1816.         call    gouraud_triangle_z
  1817.         jmp     .end_draw
  1818.        @@:
  1819.         call    gouraud_triangle
  1820.         jmp     .end_draw
  1821.  
  1822.      .flat_draw:                     ;**************************
  1823.                                      ; FLAT DRAWING
  1824.         movzx   eax,[point_index1]
  1825.         movzx   ebx,[point_index2]
  1826.         movzx   ecx,[point_index3]
  1827.         shl     eax,2
  1828.         shl     ebx,2
  1829.         shl     ecx,2
  1830.         lea     eax,[eax*3]  ;+point_normals_rotated]
  1831.         add     eax,[points_normals_rot_ptr]
  1832.         lea     ebx,[ebx*3]  ;+point_normals_rotated]
  1833.         add     ebx,[points_normals_rot_ptr]
  1834.         lea     ecx,[ecx*3]  ;+point_normals_rotated]
  1835.         add     ecx,[points_normals_rot_ptr]
  1836.         fld     dword[eax]      ; x cooficient of normal vector
  1837.         fadd    dword[ebx]
  1838.         fadd    dword[ecx]
  1839.         fidiv   [i3]
  1840.         fimul   [correct_tex]
  1841.         fiadd   [correct_tex]
  1842.         fistp   dword[esp-4]    ; x temp variables
  1843.         fld     dword[eax+4]    ; y cooficient of normal vector
  1844.         fadd    dword[ebx+4]
  1845.         fadd    dword[ecx+4]
  1846.         fidiv   [i3]
  1847.         fimul   [correct_tex]
  1848.         fiadd   [correct_tex]
  1849.         fistp   dword[esp-8]   ;  y
  1850.         mov     edx,dword[esp-8]
  1851.         shl     edx,TEX_SHIFT
  1852.         add     edx,dword[esp-4]
  1853.         lea     eax,[3*edx+color_map]
  1854.         mov     edx,dword[eax]
  1855.  
  1856.         and     edx,0x00ffffff    ; edx = 0x00rrggbb
  1857.  
  1858.  
  1859.  
  1860.      ;   mov     ax,[zz1]      ; z position depend draw
  1861.      ;   add     ax,[zz2]
  1862.      ;   add     ax,[zz3]
  1863.      ;   cwd
  1864.      ;   idiv    [i3] ;    = -((a+b+c)/3+130)
  1865.      ;   add     ax,130
  1866.      ;   neg     al
  1867.      ;   xor     edx,edx
  1868.      ;   mov     ah,al           ;set color according to z position
  1869.      ;   shl     eax,8
  1870.      ;   mov     edx,eax
  1871.  
  1872.         mov     eax,dword[xx1]
  1873.         ror     eax,16
  1874.         mov     ebx,dword[xx2]
  1875.         ror     ebx,16
  1876.         mov     ecx,dword[xx3]
  1877.         ror     ecx,16
  1878.        ; mov     edi,screen
  1879.         lea     edi,[screen]
  1880.         cmp     [catmull_flag],0
  1881.         je      @f
  1882.    ;     lea     esi,[Z_buffer]
  1883.         mov     esi,[Zbuffer_ptr]
  1884.         push    word[zz3]
  1885.         push    word[zz2]
  1886.         push    word[zz1]
  1887.         call    flat_triangle_z
  1888.         jmp     .end_draw
  1889.       @@:
  1890.         call    draw_triangle
  1891.         jmp     .end_draw
  1892.       .env_mapping:
  1893.        ; fninit
  1894.         cmp     [catmull_flag],0
  1895.         je      @f
  1896.         push    [zz3]
  1897.         push    [zz2]
  1898.         push    [zz1]
  1899.       @@:
  1900.         mov     esi,point_index1
  1901.         sub     esp,12
  1902.         mov     edi,esp
  1903.         mov     ecx,3
  1904.       @@:
  1905.         movzx   eax,word[esi]
  1906.         lea     eax,[eax*3]
  1907.         shl     eax,2
  1908.         add     eax,[points_normals_rot_ptr]       ;point_normals_rotated
  1909. ; #
  1910. ;        fld     dword[eax]
  1911. ;        fmul    dword[eax+4]
  1912. ;        fld1
  1913. ;        fld1
  1914. ;        faddp
  1915. ;        fmulp
  1916. ;        fimul   [correct_tex]
  1917. ;        fiadd   [correct_tex]
  1918. ;        fistp   word[edi]
  1919. ;        mov     word[edi+2],0
  1920. ;;        fistp   word[edi+2]
  1921. ; # last change
  1922.         ; texture x=(rotated point normal -> x * 255)+255
  1923.         fld     dword[eax]
  1924.         fimul   [correct_tex]
  1925.         fiadd   [correct_tex]
  1926.         fistp   word[edi]
  1927.         ; texture y=(rotated point normal -> y * 255)+255
  1928.         fld     dword[eax+4]
  1929.         fimul   [correct_tex]
  1930.         fiadd   [correct_tex]
  1931.         fistp   word[edi+2]
  1932. ; # end of last ch.
  1933.         add     edi,4
  1934.         add     esi,2
  1935.         loop    @b
  1936.  
  1937.         mov     eax,dword[xx1]
  1938.         ror     eax,16
  1939.         mov     ebx,dword[xx2]
  1940.         ror     ebx,16
  1941.         mov     ecx,dword[xx3]
  1942.         ror     ecx,16
  1943.         mov     edi,screen
  1944.         mov     esi,envmap
  1945.         cmp     [catmull_flag],0
  1946.         je      @f
  1947.   ;      mov     edx,Z_buffer
  1948.         mov     edx,[Zbuffer_ptr]
  1949.         call    tex_triangle_z
  1950.         jmp     .end_draw
  1951.       @@:
  1952.         call    tex_triangle
  1953.         jmp     .end_draw
  1954. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1955.      .cubic_env_mapping:
  1956.        ; fninit
  1957.         cmp     [catmull_flag],0
  1958.         je      @f
  1959.         push    [zz3]
  1960.         push    [zz2]
  1961.         push    [zz1]
  1962.       @@:
  1963.         mov     esi,point_index1
  1964.         sub     esp,12
  1965.         mov     edi,esp
  1966.         mov     ecx,3
  1967.       @@:
  1968.         movzx   eax,word[esi]
  1969.         lea     eax,[eax*3]
  1970.         shl     eax,2
  1971.         add     eax,[points_normals_rot_ptr]  ;point_normals_rotated
  1972. ; #
  1973.         fld     dword[eax]
  1974.         fmul    dword[eax+4]
  1975.         fld1
  1976.         fld1
  1977.         faddp
  1978.         fmulp
  1979.         fimul   [correct_tex]
  1980.         fiadd   [correct_tex]
  1981.         fistp   word[edi]
  1982.         mov     word[edi+2],0
  1983. ;        fistp   word[edi+2]
  1984. ; # last change
  1985. ;        ; texture x=(rotated point normal -> x * 255)+255
  1986. ;        fld     dword[eax]
  1987. ;        fimul   [correct_tex]
  1988. ;        fiadd   [correct_tex]
  1989. ;        fistp   word[edi]
  1990. ;        ; texture y=(rotated point normal -> y * 255)+255
  1991. ;        fld     dword[eax+4]
  1992. ;        fimul   [correct_tex]
  1993. ;        fiadd   [correct_tex]
  1994. ;        fistp   word[edi+2]
  1995. ; # end of last ch.
  1996.         add     edi,4
  1997.         add     esi,2
  1998.         loop    @b
  1999.  
  2000.         mov     eax,dword[xx1]
  2001.         ror     eax,16
  2002.         mov     ebx,dword[xx2]
  2003.         ror     ebx,16
  2004.         mov     ecx,dword[xx3]
  2005.         ror     ecx,16
  2006.         mov     edi,screen
  2007.         mov     esi,envmap_cub
  2008.         cmp     [catmull_flag],0
  2009.         je      @f
  2010.   ;      mov     edx,Z_buffer
  2011.         mov     edx,[Zbuffer_ptr]
  2012.         call    tex_triangle_z
  2013.         jmp     .end_draw
  2014.       @@:
  2015.         call    tex_triangle
  2016.         jmp     .end_draw
  2017.  
  2018. ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2019.  
  2020.       .bump_mapping:
  2021.         ; fninit
  2022.         cmp     [catmull_flag],0
  2023.         je      @f
  2024. ;        push    Z_buffer
  2025.         push    [Zbuffer_ptr]
  2026.         push    [zz3]
  2027.         push    [zz2]
  2028.         push    [zz1]
  2029.       @@:
  2030.         mov     esi,point_index1
  2031.         sub     esp,12
  2032.         mov     edi,esp
  2033.         mov     ecx,3
  2034.       @@:
  2035.         movzx   eax,word[esi]
  2036.         lea     eax,[eax*3]
  2037.         shl     eax,2
  2038.         add     eax,[points_normals_rot_ptr]  ;point_normals_rotated
  2039.         ; texture x=(rotated point normal -> x * 255)+255
  2040.         fld     dword[eax]
  2041.         fimul   [correct_tex]
  2042.         fiadd   [correct_tex]
  2043.         fistp   word[edi]
  2044.         ; texture y=(rotated point normal -> y * 255)+255
  2045.         fld     dword[eax+4]
  2046.         fimul   [correct_tex]
  2047.         fiadd   [correct_tex]
  2048.         fistp   word[edi+2]
  2049.  
  2050.         add     edi,4
  2051.         add     esi,2
  2052.         loop    @b
  2053.  
  2054.         movzx  esi,[point_index3]      ; bump map coords
  2055.         shl    esi,2
  2056.         add    esi,tex_points
  2057.         push   dword[esi]
  2058.         movzx  esi,[point_index2]
  2059.         shl    esi,2
  2060.         add    esi,tex_points
  2061. ;       lea    esi,[esi*3]
  2062. ;       lea    esi,[points+2+esi*2]
  2063.         push   dword[esi]
  2064.   ;     push   dword[xx2]
  2065.         movzx  esi,[point_index1]
  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[xx1]
  2072.  
  2073.         mov     eax,dword[xx1]
  2074.         ror     eax,16
  2075.         mov     ebx,dword[xx2]
  2076.         ror     ebx,16
  2077.         mov     ecx,dword[xx3]
  2078.         ror     ecx,16
  2079.         mov     edi,screen
  2080.         mov     esi,envmap
  2081.         mov     edx,bumpmap            ;BUMP_MAPPING
  2082.  
  2083.         cmp     [catmull_flag],0
  2084.         je      @f
  2085.         call    bump_triangle_z
  2086.         jmp     .end_draw
  2087.       @@:
  2088.         call    bump_triangle
  2089.         jmp     .end_draw
  2090.  
  2091.       .tex_mapping:
  2092.  
  2093.         ; fninit
  2094.         cmp     [catmull_flag],0
  2095.         je      @f
  2096.         push    [zz3]
  2097.         push    [zz2]
  2098.         push    [zz1]
  2099.       @@:
  2100.         movzx  esi,[point_index3]      ; tex map coords
  2101.         shl    esi,2
  2102.         add    esi,tex_points
  2103.         push   dword[esi]
  2104.         movzx  esi,[point_index2]
  2105.         shl    esi,2
  2106.         add    esi,tex_points
  2107.         push   dword[esi]
  2108.         movzx  esi,[point_index1]
  2109.         shl    esi,2
  2110.         add    esi,tex_points
  2111.         push   dword[esi]
  2112.  
  2113.         mov     eax,dword[xx1]
  2114.         ror     eax,16
  2115.         mov     ebx,dword[xx2]
  2116.         ror     ebx,16
  2117.         mov     ecx,dword[xx3]
  2118.         ror     ecx,16
  2119.         mov     edi,screen
  2120.         mov     esi,texmap
  2121.         cmp     [catmull_flag],0
  2122.         je      @f
  2123.    ;     mov     edx,Z_buffer
  2124.         mov     edx,[Zbuffer_ptr]
  2125.         call    tex_triangle_z
  2126.  ;       call    tex_plus_grd_trianlgle
  2127.         jmp     .end_draw
  2128.       @@:
  2129.         call    tex_triangle
  2130.         jmp     .end_draw
  2131. ;      .ray:
  2132. ;        grd_triangle according to points index
  2133. ;        cmp     [catmull_flag],0
  2134. ;        je      @f
  2135. ;        push    [zz3]                   ; spot light with attenuation
  2136. ;     @@:
  2137. ;        movzx   eax,[point_index3]      ; env_map - points color list
  2138. ;        shl     eax,1                   ; each color as word, 0x00rr00gg00bb..
  2139. ;        lea     eax,[3*eax+bumpmap]
  2140. ;        push    word[eax]
  2141. ;        push    word[eax+2]
  2142. ;        push    word[eax+4]
  2143. ;        cmp     [catmull_flag],0
  2144. ;        je      @f
  2145. ;        push    [zz2]
  2146. ;    @@:
  2147. ;        movzx   eax,[point_index2]      ; env_map - points color list
  2148. ;        shl     eax,1                   ; each color as word, 0x00rr00gg00bb..
  2149. ;        lea     eax,[eax*3+bumpmap]
  2150. ;        push    word[eax]
  2151. ;        push    word[eax+2]
  2152. ;        push    word[eax+4]
  2153. ;        cmp     [catmull_flag],0
  2154. ;        je      @f
  2155. ;        push    [zz1]
  2156. ;     @@:
  2157. ;        movzx   eax,[point_index1]      ; env_map - points color list
  2158. ;        shl     eax,1                   ; each color as word, 0xrr00gg00bb00..
  2159. ;        lea     eax,[eax*3+bumpmap]
  2160. ;        push    word[eax]
  2161. ;        push    word[eax+2]
  2162. ;        push    word[eax+4]
  2163. ;        jmp     .both_draw
  2164.  
  2165.      .grd_tex:            ; smooth shading + texture
  2166.          push   ebp
  2167.          mov    ebp,esp
  2168.          sub    esp,4
  2169.          push   ebp
  2170.  
  2171.          movzx  esi,[point_index3]      ; tex map coords
  2172.          shl    esi,2
  2173.          add    esi,tex_points
  2174.          push   dword[esi]              ; texture coords as first
  2175.          movzx  esi,[point_index2]      ; group of parameters
  2176.          shl    esi,2
  2177.          add    esi,tex_points
  2178.          push   dword[esi]
  2179.          movzx  esi,[point_index1]
  2180.          shl    esi,2
  2181.          add    esi,tex_points
  2182.          push   dword[esi]
  2183.  
  2184.          mov     esi,point_index3
  2185.          mov     ecx,3
  2186.  
  2187.       .aagain_grd_draw:
  2188.  
  2189.         lea      edx,[ecx*3]
  2190.         push     word[edx*2+xx1-2]    ; zz1 ,2 ,3
  2191.  
  2192.         movzx   eax,word[esi]
  2193.         shl     eax,2
  2194.         lea     eax,[eax*3] ;+point_normals_rotated]
  2195.         add     eax,[points_normals_rot_ptr]
  2196.         ; texture x=(rotated point normal -> x * 255)+255
  2197.         fld     dword[eax]       ; x cooficient of normal vector
  2198.         fimul   [correct_tex]
  2199.         fiadd   [correct_tex]
  2200.         fistp   word[ebp-2]
  2201.         ; texture y=(rotated point normal -> y * 255)+255
  2202.         fld     dword[eax+4]      ; y cooficient
  2203.         fimul   [correct_tex]
  2204.         fiadd   [correct_tex]
  2205.         fistp   word[ebp-4]
  2206.  
  2207.         movzx    eax,word[ebp-4]
  2208.         movzx    ebx,word[ebp-2]
  2209.         shl      eax,TEX_SHIFT
  2210.         add      eax,ebx
  2211.         lea      eax,[eax*3+color_map]
  2212.         mov      eax,dword[eax]
  2213.  
  2214.         ror      eax,16               ; eax -0xxxrrggbb -> 0xggbbxxrr
  2215.         xor      ah,ah
  2216.         push     ax         ;r
  2217.         rol      eax,8                ; eax-0xggbb00rr -> 0xbb00rrgg
  2218.         xor      ah,ah
  2219.         push     ax         ;g
  2220.         shr      eax,24
  2221.         push     ax         ;b
  2222.  
  2223.         sub      esi,2
  2224.         dec      cx
  2225.         jnz      .aagain_grd_draw
  2226.  
  2227.         mov     eax,dword[xx1]
  2228.         ror     eax,16
  2229.         mov     ebx,dword[xx2]
  2230.         ror     ebx,16
  2231.         mov     ecx,dword[xx3]
  2232.         ror     ecx,16
  2233.         mov     edi,screen
  2234.         mov     edx,texmap
  2235.         mov     esi,[Zbuffer_ptr]
  2236.  
  2237.         call    tex_plus_grd_triangle
  2238.  
  2239.         pop     ebp
  2240.         mov     esp,ebp
  2241.         pop     ebp
  2242.         jmp     .end_draw
  2243.  
  2244.       .two_tex:
  2245.         push    [Zbuffer_ptr]
  2246.  
  2247.         push    word[zz3]
  2248.         push    word[zz2]
  2249.         push    word[zz1]
  2250.  
  2251.         movzx  esi,[point_index3]      ; tex map coords
  2252.         shl    esi,2
  2253.         add    esi,tex_points
  2254.         push   dword[esi]
  2255.         movzx  esi,[point_index2]
  2256.         shl    esi,2
  2257.         add    esi,tex_points
  2258.         push   dword[esi]
  2259.         movzx  esi,[point_index1]
  2260.         shl    esi,2
  2261.         add    esi,tex_points
  2262.         push   dword[esi]
  2263.  
  2264.         mov     esi,point_index1     ; env coords
  2265.         sub     esp,12
  2266.         mov     edi,esp
  2267.         mov     ecx,3
  2268.       @@:
  2269.         movzx   eax,word[esi]
  2270.         lea     eax,[eax*3]
  2271.         shl     eax,2
  2272.         add     eax,[points_normals_rot_ptr]
  2273.         ; texture x=(rotated point normal -> x * 255)+255
  2274.         fld     dword[eax]
  2275.         fimul   [correct_tex]
  2276.         fiadd   [correct_tex]
  2277.         fistp   word[edi]
  2278.         ; texture y=(rotated point normal -> y * 255)+255
  2279.         fld     dword[eax+4]
  2280.         fimul   [correct_tex]
  2281.         fiadd   [correct_tex]
  2282.         fistp   word[edi+2]
  2283.  
  2284.         add     edi,4
  2285.         add     esi,2
  2286.         loop    @b
  2287.  
  2288.         mov     eax,dword[xx1]
  2289.         ror     eax,16
  2290.         mov     ebx,dword[xx2]
  2291.         ror     ebx,16
  2292.         mov     ecx,dword[xx3]
  2293.         ror     ecx,16
  2294.         mov     edi,screen
  2295.         mov     esi,texmap
  2296.         mov     edx,envmap
  2297.  
  2298.         call    two_tex_triangle_z
  2299.         jmp     .end_draw
  2300.  
  2301.    .bump_tex:
  2302.         movzx  esi,[point_index3]      ; tex map coords
  2303.         shl    esi,2
  2304.         add    esi,tex_points
  2305.         push   dword[esi]
  2306.         movzx  esi,[point_index2]
  2307.         shl    esi,2
  2308.         add    esi,tex_points
  2309.         push   dword[esi]
  2310.         movzx  esi,[point_index1]
  2311.         shl    esi,2
  2312.         add    esi,tex_points
  2313.         push   dword[esi]
  2314.  
  2315.         push  dword texmap
  2316.  
  2317.         push  [Zbuffer_ptr]
  2318.         xor   edi,edi
  2319.  
  2320.         push    word[zz3]
  2321.         push    word[zz2]
  2322.         push    word[zz1]
  2323.  
  2324.         mov     esi,point_index1     ; env coords
  2325.         sub     esp,12
  2326.         mov     edi,esp
  2327.         mov     ecx,3
  2328.       @@:
  2329.         movzx   eax,word[esi]
  2330.         lea     eax,[eax*3]
  2331.         shl     eax,2
  2332.         add     eax,[points_normals_rot_ptr]
  2333.         ; texture x=(rotated point normal -> x * 255)+255
  2334.         fld     dword[eax]
  2335.         fimul   [correct_tex]
  2336.         fiadd   [correct_tex]
  2337.         fistp   word[edi]
  2338.         ; texture y=(rotated point normal -> y * 255)+255
  2339.         fld     dword[eax+4]
  2340.         fimul   [correct_tex]
  2341.         fiadd   [correct_tex]
  2342.         fistp   word[edi+2]
  2343.  
  2344.         add     edi,4
  2345.         add     esi,2
  2346.         loop    @b
  2347.  
  2348. ;        push  dword 1 shl 16 + 1  ; emap coords
  2349. ;        push  dword 127 shl 16 + 1
  2350. ;        push  dword 127 shl 16 + 127
  2351.  
  2352.         movzx  esi,[point_index3]      ; bump map coords
  2353.         shl    esi,2
  2354.         add    esi,tex_points
  2355.         push   dword[esi]
  2356.         movzx  esi,[point_index2]
  2357.         shl    esi,2
  2358.         add    esi,tex_points
  2359.         push   dword[esi]
  2360.  
  2361.         movzx  esi,[point_index1]
  2362.         shl    esi,2
  2363.         add    esi,tex_points
  2364.         push   dword[esi]
  2365.  
  2366. ;        push  dword 1 shl 16 + 127
  2367. ;        push  dword 127 shl 16 + 127
  2368. ;        push  dword 1 shl 16 + 1  ; bump coords
  2369.  
  2370.         mov     eax,dword[xx1]
  2371.         ror     eax,16
  2372.         mov     ebx,dword[xx2]
  2373.         ror     ebx,16
  2374.         mov     ecx,dword[xx3]
  2375.         ror     ecx,16
  2376.         mov     edi,screen
  2377.         mov     esi,envmap
  2378.         mov     edx,bumpmap
  2379.  
  2380.         call bump_tex_triangle_z
  2381.  
  2382.       .end_draw:
  2383.         pop     esi
  2384.         add     esi,6
  2385.         cmp     dword[esi],-1
  2386.         jne     .again_dts
  2387. ret
  2388.  
  2389.  
  2390. fill_Z_buffer:
  2391.         mov     eax,0x70000000
  2392.       ;  mov     edi,Z_buffer
  2393.         mov     edi,[Zbuffer_ptr]
  2394.         mov     ecx,SIZE_X*SIZE_Y
  2395.         rep     stosd
  2396. ret
  2397.  
  2398. read_from_file:
  2399.         fninit
  2400.         mov     edi,[triangles_ptr]
  2401.         xor     ebx,ebx
  2402.         xor     ebp,ebp
  2403.         mov     [points_count_var],0
  2404.         mov     [triangles_count_var],0
  2405.    if USE_LFN = 0
  2406.         mov     esi,SourceFile
  2407.    else
  2408.         mov     esi,[fptr]
  2409.    end if
  2410.         cmp     [esi],word 4D4Dh
  2411.         jne     .exit ;Must be legal .3DS file
  2412. ;        cmp     dword[esi+2],EndFile-SourceFile
  2413. ;        jne     .exit ;This must tell the length
  2414.         mov     eax,dword[esi+2]
  2415.   ;      cmp     eax,[fsize]
  2416.   ;      jne     .exit
  2417.  
  2418.         add     eax,esi
  2419.         mov     [EndFile],eax    ;
  2420.  
  2421.         add     esi,6
  2422.       @@:
  2423.         cmp     [esi],word 3D3Dh
  2424.         je      @f
  2425.         add     esi,[esi+2]
  2426.         jmp     @b
  2427.       @@:
  2428.         add     esi,6
  2429.       .find4k:
  2430.         cmp     [esi],word 4000h
  2431.         je      @f
  2432.         add     esi,[esi+2]
  2433.         cmp     esi,[EndFile]
  2434.         jc      .find4k
  2435.         jmp     .exit
  2436.       @@:
  2437.         add     esi,6
  2438.       @@:
  2439.         cmp     [esi],byte 0
  2440.         je      @f
  2441.         inc     esi
  2442.         jmp     @b
  2443.       @@:
  2444.         inc     esi
  2445.       @@:
  2446.         cmp     [esi],word 4100h
  2447.         je      @f
  2448.         add     esi,[esi+2]
  2449.         jmp     @b
  2450.       @@:
  2451.         add     esi,6
  2452.       @@:
  2453.         cmp     [esi],word 4110h
  2454.         je      @f
  2455.         add     esi,[esi+2]
  2456.         jmp     @b
  2457.       @@:
  2458.         movzx   ecx,word[esi+6]
  2459.         add     [points_count_var],cx
  2460.  
  2461.         mov     edx,ecx
  2462.         add     esi,8
  2463.      @@:
  2464.         push    edi
  2465.         mov     edi,[points_ptr]
  2466.         push    dword[esi+4]
  2467.         pop     dword[edi+ebx*2+0]
  2468.         push    dword[esi+8]
  2469.         pop     dword[edi+ebx*2+4]
  2470.         push    dword[esi+0]
  2471.         pop     dword[edi+ebx*2+8]
  2472.         pop     edi
  2473. ;        fld     dword[esi+4]
  2474. ;        fstp    dword[real_points+ebx*2+0]  ; x
  2475. ;        fld     dword[esi+8]
  2476. ;        fstp   dword[real_points+ebx*2+4]  ; y
  2477. ;        fld     dword[esi+0]
  2478. ;        fstp   dword[real_points+ebx*2+8]  ; z
  2479.  
  2480.         add     ebx,6
  2481.         add     esi,12
  2482.         dec     ecx
  2483.         jnz     @b
  2484.       @@:
  2485.   ;      mov     dword[points+ebx],-1
  2486.         push    edi
  2487.         mov     edi,[points_ptr]
  2488.         mov     dword[edi+ebx*2],-1        ; end mark (not always in use)
  2489.         pop     edi
  2490.       @@:
  2491.         cmp     [esi],word 4120h
  2492.         je      @f
  2493.         add     esi,[esi+2]
  2494.         jmp     @b
  2495.       @@:
  2496.         movzx   ecx,word[esi+6]
  2497.         add     [triangles_count_var],cx
  2498.         add     esi,8
  2499.         ;mov     edi,triangles
  2500.       @@:
  2501.         movsd
  2502.         movsw
  2503.         add     word[edi-6],bp
  2504.         add     word[edi-4],bp
  2505.         add     word[edi-2],bp
  2506.         add     esi,2
  2507.         dec     ecx
  2508.         jnz     @b
  2509.         add     ebp,edx
  2510.         jmp     .find4k
  2511.         mov     eax,-1 ;<---mark if OK
  2512.       .exit:
  2513.         mov     dword[edi],-1
  2514. ret
  2515.  
  2516. read_tp_variables:            ; read [triangles_count_var] and  [points_count_var]
  2517.                               ; and  allocate memory
  2518.         xor     ebx,ebx
  2519.         xor     ebp,ebp
  2520.         mov     [points_count_var],bx
  2521.         mov     [triangles_count_var],bx
  2522.    if USE_LFN = 0
  2523.         mov     esi,SourceFile
  2524.    else
  2525.         mov     esi,[fptr]
  2526.    end if
  2527.  
  2528.         cmp     [esi],word 4D4Dh
  2529.         je      @f ;Must be legal .3DS file
  2530.         xor     eax,eax
  2531.         ret
  2532.     @@:
  2533.         mov     eax,dword[esi+2]
  2534.         cmp     eax,[fsize] ;This must tell the length
  2535.         je      @f
  2536.         xor     eax,eax
  2537.         ret
  2538.      @@:
  2539.         add     eax,esi
  2540.         mov     [EndFile],eax    ;
  2541.  
  2542.         add     esi,6
  2543.       @@:
  2544.         cmp     [esi],word 3D3Dh
  2545.         je      @f
  2546.         add     esi,[esi+2]
  2547.         jmp     @b
  2548.       @@:
  2549.         add     esi,6
  2550.       .find4k:
  2551.         cmp     [esi],word 4000h
  2552.         je      @f
  2553.         add     esi,[esi+2]
  2554.         cmp     esi,[EndFile]
  2555.         jc      .find4k
  2556.         jmp     .exit
  2557.       @@:
  2558.         add     esi,6
  2559.       @@:
  2560.         cmp     [esi],byte 0
  2561.         je      @f
  2562.         inc     esi
  2563.         jmp     @b
  2564.       @@:
  2565.         inc     esi
  2566.       @@:
  2567.         cmp     [esi],word 4100h
  2568.         je      @f
  2569.         add     esi,[esi+2]
  2570.         jmp     @b
  2571.       @@:
  2572.         add     esi,6
  2573.       @@:
  2574.         cmp     [esi],word 4110h
  2575.         je      @f
  2576.         add     esi,[esi+2]
  2577.         jmp     @b
  2578.       @@:
  2579.         movzx   ecx,word[esi+6]
  2580.         add     [points_count_var],cx
  2581.  
  2582.         mov     edx,ecx
  2583.         add     esi,8
  2584.      @@:
  2585.  
  2586.         add     ebx,6
  2587.         add     esi,12
  2588.      ;   dec     ecx
  2589.         loop     @b
  2590.       @@:
  2591.  
  2592.       @@:
  2593.         cmp     [esi],word 4120h
  2594.         je      @f
  2595.         add     esi,[esi+2]
  2596.         jmp     @b
  2597.       @@:
  2598.         movzx   ecx,word[esi+6]
  2599.         add     [triangles_count_var],cx
  2600.         add     esi,8
  2601.  
  2602.       @@:
  2603.         add     esi,8
  2604.         dec     ecx
  2605.         jnz     @b
  2606. ;        xor     ecx,ecx
  2607.         add     ebp,edx
  2608.         jmp     .find4k
  2609.         mov     eax,-1 ;<---mark if OK
  2610.       .exit:
  2611. ret
  2612.  
  2613. if USE_LFN
  2614. alloc_mem_for_tp:
  2615.         mov     eax, 68
  2616.         cmp     [re_alloc_flag],1
  2617.         jz      @f
  2618.         mov     ebx, 12
  2619.         jmp     .alloc
  2620.     @@:
  2621.         mov     ebx,20
  2622.     .alloc:
  2623.         movzx   ecx, [triangles_count_var]
  2624.         inc     ecx
  2625.         lea     ecx, [ecx*3]
  2626.         add     ecx, ecx
  2627.         mov     edx,[triangles_ptr]
  2628.         int     0x40                   ;  -> allocate memory to triangles
  2629.         mov     [triangles_ptr], eax   ;  -> eax = pointer to allocated mem
  2630.  
  2631. ;        mov     eax, 68
  2632. ;        mov     ebx, 12
  2633. ;        movzx   ecx, [triangles_count_var]
  2634. ;        shl     ecx, 4
  2635. ;        int     0x40
  2636. ;        mov     [triangles_w_z_ptr], eax   ; for trainagles_with_z list
  2637.                                             ; ststic  memory
  2638.  
  2639.         mov     eax, 68
  2640.         movzx   ecx, [triangles_count_var]
  2641.         lea     ecx, [3+ecx*3]
  2642.         shl     ecx, 2
  2643.         mov     edx,[triangles_normals_ptr]
  2644.         int     0x40                           ;  -> allocate memory for triangles normals
  2645.         mov     [triangles_normals_ptr], eax   ;  -> eax = pointer to allocated mem
  2646.  
  2647.         mov     eax, 68
  2648.         movzx   ecx, [points_count_var]
  2649.         lea     ecx,[3+ecx*3]
  2650.         shl     ecx, 2
  2651.         mov     edx,[points_normals_ptr]
  2652.         int     0x40
  2653.         mov     [points_normals_ptr], eax
  2654.  
  2655.         mov     eax, 68
  2656.     ;    mov     ebx, 12
  2657.         movzx   ecx, [points_count_var]
  2658.         lea     ecx,[3+ecx*3]
  2659.         shl     ecx, 2
  2660.         mov     edx,[points_normals_rot_ptr]
  2661.         int     0x40
  2662.         mov     [points_normals_rot_ptr], eax
  2663.  
  2664.         mov     eax, 68
  2665.         mov     edx,[points_ptr]
  2666.         int     0x40
  2667.         mov     [points_ptr], eax
  2668.  
  2669.         mov     eax, 68
  2670.         mov     edx,[points_rotated_ptr]
  2671.         int     0x40
  2672.         mov     [points_rotated_ptr], eax
  2673.  
  2674.         mov     eax, 68
  2675.         movzx   ecx, [points_count_var]
  2676.         inc     ecx
  2677.         shl     ecx, 3
  2678.         mov     edx,[points_translated_ptr]
  2679.         int     0x40
  2680.         mov     [points_translated_ptr], eax
  2681. ret
  2682. end if
  2683.  
  2684.  
  2685. read_from_disk:
  2686. if USE_LFN
  2687. ;-
  2688.     mov     eax, 70
  2689.     mov     ebx, file_info
  2690.     mov     dword[ebx], 5          ;  -> subfunction number
  2691.     int     0x40                   ;  -> read file size
  2692.     mov     ebx, [fptr]
  2693.     mov     ebx, dword[ebx+32]
  2694.     inc     ebx
  2695.     mov     [fsize], ebx
  2696.  
  2697.     mov     eax, 68
  2698.     mov     ebx, 11
  2699.     int     0x40                   ;  -> create heap
  2700.  
  2701.     mov     eax, 68
  2702.     mov     ebx, 12
  2703.     mov     ecx, [fsize]
  2704.     int     0x40                   ;  -> allocate memory for file
  2705.     mov     [fptr], eax            ;  -> eax = pointer to allocated mem
  2706.  
  2707.     mov     eax, 70
  2708.     mov     ebx, file_info
  2709.     mov     dword[ebx],0
  2710.     int     0x40                   ; -> read file
  2711.  
  2712.     mov     [fsize],ebx
  2713.  
  2714.     cmp     eax,6
  2715.     jnz     @f
  2716.     xor     eax,eax     ;;;;---
  2717.   @@:
  2718. else
  2719.     mov      eax,58
  2720.     mov      ebx,file_info
  2721.     int      0x40
  2722.  
  2723.     mov      eax,ebx
  2724.     shr      eax,9
  2725.     inc      eax
  2726.     mov      [fsize],eax
  2727. ;    mov      ecx,ebx
  2728. ;    add      ecx,MEM_END
  2729. ;    mov      ebx,1
  2730. ;    mov      eax,64     ; allocate mem  - resize app mem
  2731.                         ; for points and  triangles
  2732.     int      0x40
  2733.  
  2734.     mov      eax,58
  2735.     mov      ebx,file_info
  2736.     int      0x40
  2737. end if
  2738.   ;  eax = 0   -> ok file loaded
  2739. ret
  2740. read_param:
  2741.     mov        esi,I_Param
  2742.     cmp        dword[esi],0
  2743.     je         .end
  2744.     cmp        byte[esi],'/'
  2745.     je         .copy
  2746.     mov        edi,esi
  2747.     mov        ecx,25   ; 25 - would be enought
  2748.     repe       scasb
  2749.     jne        .end
  2750.     dec        edi
  2751.     mov        esi,edi
  2752.  .copy:
  2753.     mov         edi,file_name
  2754.     mov         ecx,50
  2755.     rep         movsd
  2756.  .end:
  2757. ret
  2758. buttons:                                      ; draw some buttons (all but navigation and close )
  2759.         mov     edi,menu
  2760.       .again:
  2761.         mov     eax,8                         ; function 8 : define and draw button
  2762.         mov     ebx,(SIZE_X+10)*65536+62      ; [x start] *65536 + [x size]
  2763.         movzx   ecx,byte[edi]                 ; button id = position+2
  2764.         sub     cl,2
  2765.         lea     ecx,[ecx*5]
  2766.         lea     ecx,[ecx*3]
  2767.         add     ecx,25
  2768.         shl     ecx,16
  2769.         add     ecx,12
  2770.         movzx   edx,byte[edi]                   ; button id
  2771.         mov     esi,0x6688dd                    ; button color RRGGBB
  2772.         int     0x40
  2773.          ; BUTTON  LABEL
  2774.         mov     eax,4                           ; function 4 : write text to window
  2775.         movzx   ebx,byte[edi]
  2776.         sub     bl,2                            ; button id, according to position
  2777.         lea     ebx,[ebx*3]
  2778.         lea     ebx,[ebx*5]
  2779.         add     ebx,(SIZE_X+12)*65536+28        ; [x start] *65536 + [y start]
  2780.         mov     ecx,0x20ddeeff                  ; font 1 & color ( 0xF0RRGGBB )
  2781.         lea     edx,[edi+1]                     ; pointer to text beginning
  2782.         mov     esi,10                          ; text length
  2783.         int     0x40
  2784.         cmp     byte[edi+11],255                ; if max_flag=255
  2785.         je      @f                              ; skip
  2786.         ; flag description
  2787. ;       mov     eax,4                           ; function 4 : write text to window
  2788. ;       movzx   ebx,byte[edi]
  2789. ;       sub     bl,2
  2790. ;       lea     ebx,[ebx*3]
  2791. ;       lea     ebx,[ebx*5]
  2792. ;       add     ebx,(SIZE_X+12+70)*65536+28     ; [x start] *65536 + [y start]
  2793.         add     ebx,70*65536
  2794. ;       mov     ecx,0x20ddeeff                  ; font 1 & color ( 0xF0RRGGBB )
  2795.         movzx   edx,byte[edi+12]                ; current flag
  2796.         shl     edx,2                           ; * 4 = text length
  2797.         add     edx,dword[edi+13]               ; pointer to text beginning
  2798.         mov     esi,4                           ; text length
  2799.         int     0x40
  2800.  
  2801.     @@:
  2802.         add     edi,17
  2803.         cmp     byte[edi],-1
  2804.         jnz     .again
  2805. ret
  2806. ;   *********************************************
  2807. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  2808. ;   *********************************************
  2809.     draw_window:
  2810.         mov     eax,12          ; function 12:tell os about windowdraw
  2811.         mov     ebx,1           ; 1, start of draw
  2812.         int     0x40
  2813.  
  2814.         ; DRAW WINDOW
  2815.         mov     eax,0           ; function 0 : define and draw window
  2816.         mov     ebx,100*65536;+SIZE_X;+80+30 ; [x start] *65536 + [x size]
  2817.         mov     ecx,100*65536;+SIZE_Y;+30    ; [y start] *65536 + [y size]
  2818.         mov     bx,[size_x]
  2819.         add     bx,114
  2820.         mov     cx,[size_y]
  2821.         add     cx,30
  2822.         mov     edx,0x14000000  ; color of work area RRGGBB,8->color gl
  2823.         mov     edi,labelt      ; WINDOW LABEL
  2824.         int     0x40
  2825.  
  2826.         call    buttons         ; more buttons
  2827.  
  2828.         .Y_ADD equ 1   ;-> offset of 'add vector' buttons
  2829.  
  2830.         ; ADD VECTOR LABEL      ; add vector buttons - 30 ++
  2831.         mov     eax,4           ; function 4 : write text to window
  2832.         mov     ebx,(SIZE_X+12)*65536+(168+15*(13+.Y_ADD))   ; [x start] *65536 + [y start]
  2833.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2834.         mov     edx,labelvector      ; pointer to text beginning
  2835.         mov     esi,labelvectorend-labelvector     ; text length
  2836.     ;    cmp     [move_flag],2
  2837.     ;    jne     @f
  2838.     ;    add     edx,navigation_size
  2839.     ;  @@:
  2840.         int     0x40
  2841.          ; VECTOR Y- BUTTON
  2842.         mov     eax,8           ; function 8 : define and draw button
  2843.         mov     ebx,(SIZE_X+30)*65536+20     ; [x start] *65536 + [x size]
  2844.         mov     ecx,(165+15*(14+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  2845.         mov     edx,30           ; button id
  2846.         mov     esi,0x6688dd    ; button color RRGGBB
  2847.         int     0x40
  2848.         ;VECTOR Y- LABEL
  2849.         mov     eax,4           ; function 4 : write text to window
  2850.         mov     ebx,(SIZE_X+32)*65536+(168+15*(14+.Y_ADD))   ; [x start] *65536 + [y start]
  2851.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2852.         mov     edx,labelyminus      ; pointer to text beginning
  2853.         mov     esi,labelyminusend-labelyminus     ; text length
  2854.         cmp     [move_flag],2
  2855.    ;     jne     @f
  2856.    ;     add     edx,navigation_size
  2857.    ;   @@:
  2858.         int     0x40
  2859.         ; VECTOR Z+ BUTTON
  2860.         mov     eax,8           ; function 8 : define and draw button
  2861.         mov     ebx,(SIZE_X+51)*65536+21     ; [x start] *65536 + [x size]
  2862.         mov     ecx,(165+15*(14+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  2863.         mov     edx,31           ; button id
  2864.         mov     esi,0x6688dd    ; button color RRGGBB
  2865.         int     0x40
  2866.         ;VECTOR Z+ LABEL
  2867.         mov     eax,4           ; function 4 : write text to window
  2868.         mov     ebx,(SIZE_X+53)*65536+(168+15*(14+.Y_ADD))   ; [x start] *65536 + [y start]
  2869.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2870.         mov     edx,labelzplus      ; pointer to text beginning
  2871.         mov     esi,labelzplusend-labelzplus     ; text length
  2872.    ;     cmp     [move_flag],2
  2873.    ;     jne     @f
  2874.    ;     add     edx,navigation_size
  2875.    ;   @@:
  2876.  
  2877.         int     0x40
  2878.         ; VECTOR x- BUTTON
  2879.         mov     eax,8           ; function 8 : define and draw button
  2880.         mov     ebx,(SIZE_X+10)*65536+21     ; [x start] *65536 + [x size]
  2881.         mov     ecx,(165+15*(15+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  2882.         mov     edx,32           ; button id
  2883.         mov     esi,0x6688dd    ; button color RRGGBB
  2884.         int     0x40
  2885.         ;VECTOR x- LABEL
  2886.         mov     eax,4           ; function 4 : write text to window
  2887.         mov     ebx,(SIZE_X+12)*65536+(168+15*(15+.Y_ADD))   ; [x start] *65536 + [y start]
  2888.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2889.         mov     edx,labelxminus      ; pointer to text beginning
  2890.         mov     esi,labelxminusend-labelxminus     ; text length
  2891.    ;     cmp     [move_flag],2
  2892.    ;     jne     @f
  2893.    ;     add     edx,navigation_size
  2894.    ;   @@:
  2895.         int     0x40
  2896.         ; VECTOR x+ BUTTON
  2897.         mov     eax,8           ; function 8 : define and draw button
  2898.         mov     ebx,(SIZE_X+51)*65536+21     ; [x start] *65536 + [x size]
  2899.         mov     ecx,(165+15*(15+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  2900.         mov     edx,33           ; button id
  2901.         mov     esi,0x6688dd    ; button color RRGGBB
  2902.         int     0x40
  2903.         ;VECTOR x+ LABEL
  2904.         mov     eax,4           ; function 4 : write text to window
  2905.         mov     ebx,(SIZE_X+53)*65536+(168+15*(15+.Y_ADD))   ; [x start] *65536 + [y start]
  2906.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2907.         mov     edx,labelxplus      ; pointer to text beginning
  2908.         mov     esi,labelxplusend-labelxplus     ; text length
  2909.    ;     cmp     [move_flag],2
  2910.    ;     jne     @f
  2911.    ;     add     edx,navigation_size
  2912.    ;   @@:
  2913.         int     0x40
  2914.         ; VECTOR z- BUTTON
  2915.         mov     eax,8           ; function 8 : define and draw button
  2916.         mov     ebx,(SIZE_X+10)*65536+62-41     ; [x start] *65536 + [x size]
  2917.         mov     ecx,(25+140+15*(16+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  2918.         mov     edx,34           ; button id
  2919.         mov     esi,0x6688dd    ; button color RRGGBB
  2920.         int     0x40
  2921.         ;VECTOR z- LABEL
  2922.         mov     eax,4           ; function 4 : write text to window
  2923.         mov     ebx,(SIZE_X+12)*65536+(168+15*(16+.Y_ADD))   ; [x start] *65536 + [y start]
  2924.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2925.         mov     edx,labelzminus      ; pointer to text beginning
  2926.         mov     esi,labelzminusend-labelzminus     ; text length
  2927.    ;     cmp     [move_flag],2
  2928.    ;     jne     @f
  2929.    ;     add     edx,navigation_size
  2930.    ;   @@:
  2931.         int     0x40
  2932.        ;VECTOR Y+ BUTTON
  2933.         mov     eax,8           ; function 8 : define and draw button
  2934.         mov     ebx,(SIZE_X+10+20)*65536+20     ; [x start] *65536 + [x size]
  2935.         mov     ecx,(165+15*(16+.Y_ADD))*65536+12  ; [y start] *65536 + [y size]
  2936.         mov     edx,35           ; button id
  2937.         mov     esi,0x6688dd    ; button color RRGGBB
  2938.         int     0x40
  2939.         ;VECTOR Y+ LABEL
  2940.         mov     eax,4           ; function 4 : write text to window
  2941.         mov     ebx,(SIZE_X+32)*65536+(168+15*(16+.Y_ADD))   ; [x start] *65536 + [y start]
  2942.         mov     ecx,0x20ddeeff  ; font 1 & color ( 0xF0RRGGBB )
  2943.         mov     edx,labelyplus      ; pointer to text beginning
  2944.         mov     esi,labelyplusend-labelyplus     ; text length
  2945.    ;     cmp     [move_flag],2
  2946.    ;     jne     @f
  2947.    ;     add     edx,navigation_size
  2948.    ;   @@:
  2949.         int     0x40
  2950.  
  2951.         mov     eax,12          ; function 12:tell os about windowdraw
  2952.         mov     ebx,2           ; 2, end of draw
  2953.         int     0x40
  2954.         ret
  2955.  
  2956.  
  2957.    ; DATA AREA  ************************************
  2958.  
  2959.    include 'DATA.INC'
  2960.  
  2961. MEM_END:
  2962.