Subversion Repositories Kolibri OS

Rev

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