Subversion Repositories Kolibri OS

Rev

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

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