Subversion Repositories Kolibri OS

Rev

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

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