Subversion Repositories Kolibri OS

Rev

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

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