Subversion Repositories Kolibri OS

Rev

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