Subversion Repositories Kolibri OS

Rev

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

  1. ;
  2. ; 3D POLYGONAL CUBE - ASCL
  3. ;
  4. ; Pavlushin Evgeni
  5. ; mail: waptap@mail.ru       site: www.deck4.narod.ru
  6. ;
  7. ; Create on base 3D test sample
  8. ; Mikolaj Felix mfelix@polbox.com
  9. ;
  10.  
  11. use32
  12.                org     0x0
  13.                db     'MENUET01'              ; 8 byte id
  14.                dd     0x01                    ; header version
  15.                dd     START                   ; start of code
  16.                dd     I_END                   ; size of image
  17.                dd     0x30000                  ; memory for app
  18.                dd     0x30000                  ; esp
  19.                dd     0x0 , 0x0               ; I_Param , I_Icon
  20.  
  21. MAX_POINTS equ 8
  22. MAX_TRIANGLES equ 12
  23. SCREEN_X equ 320
  24. SCREEN_Y equ 200
  25.  
  26. include 'lang.inc'
  27. include 'ascl.inc'
  28. include 'ascgl.inc'
  29. include 'macros.inc'
  30. START:
  31.     call draw_window
  32.         call init_sin_cos
  33.  
  34. still:
  35. ;    mov eax,23  ; wait for system event with 10 ms timeout
  36. ;    mov ebx,1   ; wait 10 ms, then continue
  37. ;    int  0x40
  38.  
  39.     mov eax,11
  40.     int 0x40
  41.  
  42.     dec eax
  43. ;    cmp  eax,1                  ; window redraw request ?
  44.     jz   red
  45.     dec eax
  46. ;    cmp  eax,2                  ; key in buffer ?
  47.     jz   key
  48.     dec eax
  49. ;    cmp  eax,3                  ; button in buffer ?
  50.     jz   button
  51.  
  52.     fps  280,8,cl_White,cl_Black
  53.  
  54. main_loop:
  55.  
  56.         mov esi,object
  57.         mov edi,object_rotated
  58.         mov ecx,MAX_POINTS*3
  59.         cld
  60.         rep movsw
  61.  
  62.         mov esi,angle_x
  63.         mov edi,object_rotated
  64.         mov ecx,MAX_POINTS
  65.         call rotate_points
  66.  
  67.         mov esi,object_rotated
  68.         mov edi,object_translated
  69.         mov ecx,MAX_POINTS
  70.         call translate_points
  71.  
  72.         call draw_faces
  73.  
  74.         call clear_screen_buffer
  75.  
  76.         add [angle_x],2
  77.         add [angle_y],3
  78.         add [angle_z],1
  79.  
  80.      jmp still
  81.  
  82. red:
  83.      call draw_window
  84.      jmp still
  85. key:
  86.      mov eax,2
  87.      int 0x40
  88.      jmp still
  89. button:
  90.      mov eax,17
  91.      int 0x40
  92.      cmp ah,1
  93.      jne still
  94. exit:
  95.      mov eax,-1
  96.      int 0x40
  97.  
  98. ;Draw window
  99. draw_window:
  100.     mov eax,12  ;Start
  101.     mov ebx,1
  102.     int 0x40
  103.  
  104.     mov eax,0   ;Draw window
  105.     mov ebx,100*65536+(SCREEN_X+9) ;x start*65536+x size
  106.     mov ecx,100*65536+(SCREEN_Y+26) ;y start*65536+y size
  107.     mov edx,0x03000000         ;0x03 use skinned window
  108.     int 0x40
  109.  
  110.     mov eax,4   ;Out Text
  111.     mov ebx,8*65536+8          ;x start*65536+y start
  112.     mov ecx,0x00ffffff         ;color White
  113.     mov edx,head_label
  114.     mov esi,hl_end-head_label
  115.     int 0x40
  116.  
  117.     mov eax,12  ;End
  118.     mov ebx,2
  119.     int 0x40
  120.     ret
  121.  
  122. head_label: db "3D TEST SAMPLE FOR MENUETOS"
  123. hl_end:
  124.  
  125.  
  126.  
  127. ; Draw faces procedure
  128.  
  129. draw_faces:
  130.  
  131.         mov esi,link
  132.         mov ecx,MAX_TRIANGLES
  133. df_draw:
  134.         push ecx
  135.         mov ecx,3
  136.         mov edi,@@tx1 ;bp
  137. df_get_point:
  138.         xor bh,bh
  139.         mov bl,byte [esi]
  140.         shl bx,2
  141.         mov ax,word [object_translated+bx]
  142.         mov word [edi],ax
  143.         mov ax,word [object_translated+bx+2]
  144.         mov word [edi+2],ax
  145.         inc esi
  146.         add edi,4
  147.         dec ecx
  148.         jnz df_get_point
  149.  
  150.         mov ax,[@@ty1]
  151.         sub ax,[@@ty3]
  152.         mov bx,[@@tx2]
  153.         sub bx,[@@tx1]
  154.         imul bx
  155.         shl edx,16
  156.         mov dx,ax
  157.         push edx
  158.         mov ax,[@@tx1]
  159.         sub ax,[@@tx3]
  160.         mov bx,[@@ty2]
  161.         sub bx,[@@ty1]
  162.         imul bx
  163.         shl edx,16
  164.         mov dx,ax
  165.         pop ebx
  166.         sub ebx,edx
  167.         or ebx,ebx
  168.         jge df_next
  169.  
  170.         xor ah,ah
  171.         mov al,byte [si]
  172.        
  173.  mov  [@@xcol],ax
  174.  
  175.         call filled_triangle
  176. df_next:
  177.         inc si
  178.         pop ecx
  179.         dec ecx
  180.         jnz df_draw
  181.         ret
  182.  
  183. ;modify
  184. ;include graphlib.asm
  185.  
  186. clear_screen_buffer:
  187.  
  188. ;outscrbuf
  189.  mov ebx,scrbuf
  190.  mov ecx,SCREEN_X*65536+SCREEN_Y
  191.  mov edx,5*65536+22
  192.  mov ax,7
  193.  int 0x40
  194.  
  195. ;White background
  196.  mov edi,scrbuf
  197.  mov ecx,(SCREEN_X*SCREEN_Y*3)/4
  198.  mov eax,0xffffffff
  199.  cld
  200.  rep stosd
  201.  
  202.         ret
  203.  
  204. ;include triangle.asm
  205. ; Mikolaj Felix 14/5/2001
  206. ; mfelix@polbox.com
  207.  
  208. ;filled trangle procedure
  209.  
  210. @@tx1  dw 0
  211. @@ty1  dw 0
  212. @@tx2  dw 0
  213. @@ty2  dw 0
  214. @@tx3  dw 0
  215. @@ty3  dw 0
  216. @@xcol dw 0
  217.  
  218. @@dx12 dw 0
  219. @@dx13 dw 0
  220. @@dx23 dw 0
  221.  
  222. filled_triangle:
  223.  
  224.  mov ax,[@@xcol]  ;trnsforming color
  225.  mov bl,al        ;byte bbbggrrx
  226.  mov dl,al        ;to 3 byte
  227.  mov dh,al        ;bbbxxxxx ggxxxxxx rrxxxxxx
  228.  and dh,00000001b
  229.  
  230.  and al,11100000b
  231.  and bl,00011000b
  232.  and dl,00000110b
  233.  shl bl,3
  234.  shl dl,5
  235.  
  236.  cmp dh,1
  237.  jne no_bitup
  238.  or  al,00011111b
  239.  or  bl,00111111b
  240.  or  dl,00111111b
  241. no_bitup:
  242.  
  243.  shl eax,8 ;puck colors
  244.  mov al,bl
  245.  shl eax,8
  246.  mov al,dl
  247.  mov dword [@@rgb],eax
  248.  mov eax,0 ; for 16 bit instructions
  249.  
  250.         mov ax,[@@ty1]
  251.         cmp ax,[@@ty3]
  252.         jb ft_check1
  253.  
  254.         xchg ax,[@@ty3]
  255.         mov [@@ty1],ax
  256.  
  257.         mov ax,[@@tx1]
  258.         xchg ax,[@@tx3]
  259.         mov [@@tx1],ax
  260. ft_check1:
  261.         mov ax,[@@ty2]
  262.         cmp ax,[@@ty3]
  263.         jb ft_check2
  264.  
  265.         xchg ax,[@@ty3]
  266.         mov [@@ty2],ax
  267.  
  268.         mov ax,[@@tx2]
  269.         xchg ax,[@@tx3]
  270.         mov [@@tx2],ax
  271. ft_check2:
  272.         mov ax,[@@ty1]
  273.         cmp ax,[@@ty2]
  274.         jb ft_check3
  275.  
  276.         xchg ax,[@@ty2]
  277.         mov [@@ty1],ax
  278.  
  279.         mov ax,[@@tx1]
  280.         xchg ax,[@@tx2]
  281.         mov [@@tx1],ax
  282. ft_check3:
  283.  
  284.         mov bx,[@@ty2]
  285.         sub bx,[@@ty1]
  286.         jnz ft_dx12_make
  287.  
  288.         mov [@@dx12],word 0
  289.         jmp ft_dx12_done
  290. ft_dx12_make:
  291.         mov ax,[@@tx2]
  292.         sub ax,[@@tx1]
  293.         shl ax,7
  294.         cwd
  295.         idiv bx
  296.         mov [@@dx12],ax                 ; dx12 = (x2-x1)/(y2-y1)
  297. ft_dx12_done:
  298.  
  299.         mov bx,[@@ty3]
  300.         sub bx,[@@ty1]
  301.         jnz ft_dx13_make
  302.  
  303.         mov [@@dx13],word 0
  304.         jmp ft_dx13_done
  305. ft_dx13_make:
  306.         mov ax,[@@tx3]
  307.         sub ax,[@@tx1]
  308.         shl ax,7
  309.         cwd
  310.         idiv bx
  311.         mov [@@dx13],ax                 ; dx13 = (x3-x1)/(y3-y1)
  312. ft_dx13_done:
  313.  
  314.         mov bx,[@@ty3]
  315.         sub bx,[@@ty2]
  316.         jnz ft_dx23_make
  317.  
  318.         mov [@@dx23],word 0
  319.         jmp ft_dx23_done
  320. ft_dx23_make:
  321.         mov ax,[@@tx3]
  322.         sub ax,[@@tx2]
  323.         shl ax,7
  324.         cwd
  325.         idiv bx
  326.         mov [@@dx23],ax                 ; dx23 = (x3-x2)/(y3-y2)
  327. ft_dx23_done:
  328.  
  329.         mov ax,[@@tx1]
  330.         shl ax,7
  331.         mov bx,ax
  332.  
  333.         mov cx,[@@ty1]
  334. ft_loop1:
  335.  
  336.  pushad
  337.  
  338.         mov [@@ly],cx
  339.         mov dx,bx
  340.         shr dx,7
  341.         mov [@@lx2],dx
  342.         mov dx,ax
  343.         shr dx,7
  344.         mov [@@lx1],dx
  345.         mov ax,[@@xcol]
  346.  mov [@@lcol],ax
  347.  call horizontal_line
  348.  
  349.  popad
  350.  
  351.         add ax,[@@dx13]
  352.         add bx,[@@dx12]
  353.         inc cx
  354.         cmp cx,[@@ty2]
  355.         jb ft_loop1
  356.  
  357.        
  358.         mov bx,[@@tx2]
  359.         shl bx,7
  360.         mov cx,[@@ty2]
  361. ft_loop2:
  362.  
  363.  pushad
  364.  
  365.         mov [@@ly],cx
  366.         mov dx,bx
  367.         shr dx,7
  368.         mov [@@lx2],dx
  369.         mov dx,ax
  370.         shr dx,7
  371.         mov [@@lx1],dx
  372.  mov ax,[@@xcol]
  373.  mov [@@lcol],ax
  374.         call horizontal_line
  375.  
  376.  popad
  377.  
  378.         add ax,[@@dx13]
  379.         add bx,[@@dx23]
  380.         inc ecx
  381.         cmp cx,[@@ty3]
  382.         jb ft_loop2
  383.  
  384.         ret
  385.  
  386. ;horizontal line subproc
  387.  
  388. @@lx1  dw 0
  389. @@lx2  dw 0
  390. @@ly   dw 0
  391. @@lcol dw 0
  392.  
  393. @@rgb  dd 0
  394.  
  395. horizontal_line:
  396.  
  397.  mov ecx,0
  398.  mov cx,[@@lx1]
  399.  cmp cx,[@@lx2]
  400.  ja  x12
  401.  je  ext
  402. ; ret
  403.  mov cx,[@@lx2]
  404.  sub cx,[@@lx1]
  405.  mov edi,3
  406.  jmp xx
  407. x12:
  408.  mov cx,[@@lx1]
  409.  sub cx,[@@lx2]
  410.  mov edi,-3
  411.  jmp xx
  412. ext:
  413.  mov ecx,-1 ;1
  414. ; sub ebp,3
  415. xx:
  416.  mov eax,0
  417.  mov ax,[@@ly]
  418.  mov ebx,SCREEN_X ;320
  419.  mul ebx
  420.  mov ebp,0
  421.  mov bp,[@@lx1] ;for correct 16 bit size
  422.  add eax,ebp
  423.  mov ebx,3
  424.  mul ebx
  425.  mov ebp,eax
  426.  sub ebp,3   ;for delete  white dots
  427.  add ecx,2
  428. loo:
  429.  
  430.  mov eax,dword [@@rgb]
  431.  mov bl,al
  432.  shr eax,8 ;puck colors
  433.  
  434.  mov byte [scrbuf+ebp],ah
  435.  mov byte [scrbuf+ebp+1],al
  436.  mov byte [scrbuf+ebp+2],bl
  437.  add ebp,edi
  438.  dec ecx
  439.  jnz loo
  440.  
  441.         ret
  442.  
  443. ;include fixed3d.asm
  444. ; Mikolaj Felix 25/5/2001
  445. ; mfelix@polbox.com
  446.  
  447. ;------------------------------------------------------------
  448. ;       ds:si   - offset to angles
  449. ;       ds:di   - offset to 3d points
  450. ;       cx      - number of points
  451. ;------------------------------------------------------------
  452.  
  453. @@sin_x dw 0
  454. @@cos_x dw 0
  455. @@sin_y dw 0
  456. @@cos_y dw 0
  457. @@sin_z dw 0
  458. @@cos_z dw 0
  459.  
  460. @@px equ word [edi]
  461. @@py equ word [edi+2]
  462. @@pz equ word [edi+4]
  463.  
  464. rotate_points:
  465.  
  466.  push edi
  467.         mov edi,@@sin_x
  468.         mov edx,3
  469. rp_sin_cos:
  470.         mov bx,word [esi]
  471.         and bx,511
  472.         shl bx,1
  473.         mov ax,word [sin_table+bx]
  474.         mov word [edi],ax
  475.         mov ax,word [cos_table+bx]
  476.         mov word [edi+2],ax
  477.  
  478.         add esi,2
  479.         add edi,4
  480.         dec edx
  481.         jnz rp_sin_cos
  482.         pop edi
  483.  
  484. rp_rotate:
  485.  
  486.         ; rotate around x-axis
  487.  
  488.         mov ax,@@py
  489.         imul [@@cos_x]
  490.         mov bx,ax
  491.         mov si,dx
  492.  
  493.         mov ax,@@pz
  494.         imul [@@sin_x]
  495.         sub bx,ax
  496.         sbb si,dx
  497.         shrd bx,si,14
  498.         push bx
  499.  
  500.         mov ax,@@py
  501.         imul [@@sin_x]
  502.         mov bx,ax
  503.         mov si,dx
  504.  
  505.         mov ax,@@pz
  506.         imul [@@cos_x]
  507.         add bx,ax
  508.         adc si,dx
  509.         shrd bx,si,14
  510.  
  511.         pop @@py
  512.         mov @@pz,bx
  513.  
  514.         ; rotate around y-axis
  515.  
  516.         mov ax,@@px
  517.         imul [@@cos_y]
  518.         mov bx,ax
  519.         mov si,dx
  520.  
  521.         mov ax,@@pz
  522.         imul [@@sin_y]
  523.         sub bx,ax
  524.         sbb si,dx
  525.         shrd bx,si,14
  526.         push bx
  527.  
  528.         mov ax,@@px
  529.         imul [@@sin_y]
  530.         mov bx,ax
  531.         mov si,dx
  532.  
  533.         mov ax,@@pz
  534.         imul [@@cos_y]
  535.         add bx,ax
  536.         adc si,dx
  537.         shrd bx,si,14
  538.  
  539.         pop @@px
  540.         mov @@pz,bx
  541.  
  542.         ; rotate around z-axis
  543.  
  544.         mov ax,@@px
  545.         imul [@@cos_z]
  546.         mov bx,ax
  547.         mov si,dx
  548.  
  549.         mov ax,@@py
  550.         imul [@@sin_z]
  551.         sub bx,ax
  552.         sbb si,dx
  553.         shrd bx,si,14
  554.         push bx
  555.  
  556.         mov ax,@@px
  557.         imul [@@sin_z]
  558.         mov bx,ax
  559.         mov si,dx
  560.  
  561.         mov ax,@@py
  562.         imul [@@cos_z]
  563.         add bx,ax
  564.         adc si,dx
  565.         shrd bx,si,14
  566.  
  567.         pop @@px
  568.         mov @@py,bx
  569.  
  570.         add edi,6
  571.         dec ecx
  572.         jnz rp_rotate
  573.  
  574.         ret
  575.  
  576. ;------------------------------------------------------------
  577. ;       ds:si   - offset to 3d points
  578. ;       es:di   - offset to 2d points
  579. ;       cx      - number of points
  580. ;------------------------------------------------------------
  581.  
  582.  mx dw 0
  583.  my dw 0
  584.  
  585. translate_points:
  586.  pushad
  587.  mov eax,37
  588.  mov ebx,1
  589.  int 0x40
  590.  mov ebx,eax
  591.  shr eax,16
  592.  and ebx,0xffff
  593.  cmp ax,SCREEN_X
  594.  jna x_n
  595.  mov ax,0 ;SCREEN_X
  596. x_n:
  597.  cmp bx,SCREEN_Y
  598.  jna y_n
  599.  mov bx,0 ;SCREEN_Y
  600. y_n:
  601.  mov [mx],ax
  602.  mov [my],bx
  603.  popad
  604.  
  605.  mov ebx,0  ;?
  606.         mov bx,word [esi+4]
  607.  mov ax,[my]
  608.  cmp ax,0
  609.  jng no_m
  610.  shl ax,3
  611.  add bx,ax
  612. no_m:
  613.  add bx,256  ; Z factor (zoom)
  614.  
  615.  mov eax,0  ;?
  616.         mov ax,word [esi]
  617.         shl ax,8
  618.         cwd
  619.         idiv bx; bx
  620.         add ax,(SCREEN_X/2) ;160         ;X factor (center X)
  621.         stosw
  622.  
  623.  mov eax,0  ;?
  624.         mov ax,word [esi+2]
  625.         shl ax,8
  626.         cwd
  627.         idiv bx
  628.         add ax,(SCREEN_Y/2) ;100         ;Y factor (center Y)
  629.         stosw
  630.  
  631.         add esi,6
  632.         dec ecx
  633.         jnz translate_points
  634.         ret
  635.  
  636. init_sin_cos:
  637.         finit
  638.         fldz
  639.         fstp [temp]
  640.         xor edi,edi
  641.         mov ecx,512
  642. isc_make:
  643.         fld [temp]
  644.         fld st0
  645.         fld st0
  646.         fsin
  647.         fmul [fixed_point_const]
  648.         fistp word [sin_table+edi]
  649.         fcos
  650.         fmul [fixed_point_const]
  651.         fistp word [cos_table+edi]
  652.  
  653.         fadd [inc_angle]
  654.         fstp [temp]
  655.  
  656.         add edi,2
  657.         loop isc_make
  658.         ret
  659.  
  660. temp dd 0
  661.  
  662. fixed_point_const dd 16384.0
  663. inc_angle dd 0.01227184630309                   ; pi/256
  664.  
  665. angle_x dw 0
  666. angle_y dw 0
  667. angle_z dw 0
  668.  
  669. object  dw -50,-50,-50, 50,-50,-50, 50,50,-50, -50,50,-50
  670.         dw -50,-50, 50, 50,-50, 50, 50,50, 50, -50,50, 50
  671.  
  672. link:
  673.  db  0,1,2,10000011b, 0,2,3,10000011b ;purpure   side
  674.  db  5,4,7,00000111b, 5,7,6,00000111b ;soft-red  side
  675.  db  1,5,6,00011000b, 1,6,2,00011000b ;soft-lime side
  676.  db  4,0,3,11100001b, 4,3,7,11100001b ;soft-blue side
  677.  db  4,5,1,00011111b, 1,0,4,00011111b ;yellow    side
  678.  db  3,2,6,00000000b, 3,6,7,00000000b ;black     side
  679.  
  680. sin_table:
  681. rw 512
  682. cos_table:
  683. rw 512
  684.  
  685. object_rotated:
  686. rw MAX_POINTS*3
  687. object_translated:
  688. rw MAX_POINTS*2
  689.  
  690. scrbuf:
  691. I_END: