这个程序是巧妙的利用了坐标的不断变化_第1页
这个程序是巧妙的利用了坐标的不断变化_第2页
这个程序是巧妙的利用了坐标的不断变化_第3页
这个程序是巧妙的利用了坐标的不断变化_第4页
这个程序是巧妙的利用了坐标的不断变化_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、;这个程序是巧妙的利用了坐标的不断变化,从而实现了由星星构成的箱子3D转动!;   Assembler Program By Vulture.                                    

2、60;         ;   3D-system example. Use the following formulas to rotate a point:           ;        Rotate around x-axis       

3、60;                                          ;        YT = Y

4、 * COS(xang) - Z * SIN(xang) / 256                              ;        ZT = Y * SIN(xang) + Z * COS(xang) / 256 &

5、#160;                            ;        Y = YT             

6、0;                                                  ;&#

7、160;       Z = ZT                                          &

8、#160;                     ;                             

9、;                                                 ; 

10、60;      Rotate around y-axis                                         

11、         ;        XT = X * COS(yang) - Z * SIN(yang) / 256                            

12、;  ;        ZT = X * SIN(yang) + Z * COS(yang) / 256                              ;     &#

13、160;  X = XT                                               &

14、#160;                ;        Z = ZT                         

15、0;                                      ;        Rotate around z-axis  

16、;                                                ;  

17、60;     XT = X * COS(zang) - Y * SIN(zang) / 256                              ;        YT = X * SIN(

18、zang) + Y * COS(zang) / 256                              ;        X = XT        

19、                                                  

20、      ;        Y = YT                                    

21、60;                           ;   Divide by 256 coz we have multiplyd our sin values with 256 too.           ;

22、   This example isn't too fast right now but it'll work just fine.            ;       Current Date: 6-9-95         Vulture      

23、;                             ;IDEAL                    

24、0;      ; Ideal modeP386                            ; Allow 80386 instructionsJUMPS         

25、60;                 ; Tasm handles out of range jumps (rulez!:)  SEGMENT CODE                    ; Code segment startsAS

26、SUME cs:code,ds:code          ; Let cs and ds point to code segment ORG 100h                        ; Make a .COM file START: 

27、;                         ; Main program     mov     ax,0013h            ; Init vga&#

28、160;   int     10h    mov     ax,cs    mov     ds,ax               ; ds points to codesegment    mov  

29、60;  ax,0a000h    mov     es,ax               ; es points to vga    lea     si,Palette        ; Set palette  &

30、#160; mov     dx,3c8h    xor     al,al    out     dx,al    mov     dx,3c9h    mov     cx,189*3    repz    outs

31、b; = Set some variables =    mov     DeltaX,1          ; Initial speed of rotation    mov     DeltaY,1          ; Change this and watch what

32、    mov     DeltaZ,1          ; happens. It's fun!    mov     Xoff,256    mov     Yoff,256          ;

33、 Used for calculating vga-pos    mov     Zoff,300          ; Distance from viewerMainLoop:    call    MainProgram         ; Yep. do it all. ;-) &

34、#160;  in      al,60h              ; Scan keyboard    cmp     al,1                ; Test on ESC

35、APE    jne     MainLoop            ; Continue if not keypressed; = Quit to DOS =    mov     ax,0003h            ; Back t

36、o textmode    int     10h    lea     dx,Credits    mov     ah,9    int     21h    mov     ax,4c00h    

37、60;       ; Return control to DOS    int     21h                 ; Call DOS interrupt; = Sub-routines =        

38、0;  PROC WaitVrt                    ; Waits for vertical retrace to reduce "snow"    mov     dx,3dahVrt:    in     

39、 al,dx    test    al,8    jnz     Vrt                 ; Wait until Verticle Retrace startsNoVrt:    in      al,dx&#

40、160;   test    al,8    jz      NoVrt               ; Wait until Verticle Retrace ends    ret         

41、0;               ; Return to main programENDP WaitVrtPROC UpdateAngles; Calculates new x,y,z angles; to rotate around    mov     ax,XAngle         ; Load c

42、urrent angles    mov     bx,YAngle    mov     cx,ZAngle               add     ax,DeltaX         ; Add vel

43、ocity    and     ax,11111111b        ; Range from 0.255    mov     XAngle,ax         ; Update X    add     bx,DeltaY

44、0;        ; Add velocity    and     bx,11111111b        ; Range from 0.255    mov     YAngle,bx         ; Update Y 

45、   add     cx,DeltaZ         ; Add velocity    and     cx,11111111b        ; Range from 0.255    mov     ZAngle,cx  

46、;       ; Update Z    retENDP UpdateAnglesPROC GetSinCos; Needed : bx=angle (0.255); Returns: ax=Sin   bx=Cos    push    bx               &#

47、160;  ; Save angle (use as pointer)    shl     bx,1                ; Grab a word so bx=bx*2    mov     ax,SinCos + bx    ; Get sine

48、0;   pop     bx                  ; Restore pointer into bx    push    ax              

49、;    ; Save sine on stack    add     bx,64               ; Add 64 to get cosine    and     bx,11111111b       

50、 ; Range from 0.255    shl     bx,1                ; *2 coz it's a word    mov     ax,SinCos + bx    ; Get cosine    mov&

51、#160;    bx,ax               ; Save it   bx=Cos    pop     ax                  ; Rest

52、ore   ax=Sin    retENDP GetSinCosPROC SetRotation; Set sine & cosine of x,y,z    mov     bx,XAngle         ; Grab angle    call    GetSinCos    &#

53、160;      ; Get the sine&cosine    mov     Xsin,ax           ; Save sin    mov     Xcos,bx          

54、; Save cos    mov     bx,Yangle    call    GetSinCos    mov     Ysin,ax    mov     Ycos,bx    mov     bx,Zangle   

55、call    GetSinCos    mov     Zsin,ax    mov     Zcos,bx    retENDP SetRotationPROC RotatePoint            ; Rotates the point around x,y,z; Gets

56、original x,y,z values; This can be done elsewhere    movsx   ax,Cube+si    ; si = X        (movsx coz of byte)    mov     X,ax    movsx   ax,Cube+si+1  ; si+1 =

57、 Y    mov     Y,ax    movsx   ax,Cube+si+2  ; si+2 = Z    mov     Z,ax; Rotate around x-axis; YT = Y * COS(xang) - Z * SIN(xang) / 256; ZT = Y * SIN(xang) + Z * COS(xang) / 256; Y = YT; Z = ZT &#

58、160;  mov     ax,Y    mov     bx,XCos    imul    bx               ; ax = Y * Cos(xang)    mov     bp,ax&#

59、160;   mov     ax,Z    mov     bx,XSin    imul    bx               ; ax = Z * Sin(xang)    sub     b

60、p,ax            ; bp = Y * Cos(xang) - Z * Sin(xang)    sar     bp,8             ; bp = Y * Cos(xang) - Z * Sin(xang) / 256    mov 

61、;    Yt,bp    mov     ax,Y    mov     bx,XSin    imul    bx               ; ax = Y * Sin(xang)    mo

62、v     bp,ax    mov     ax,Z    mov     bx,XCos    imul    bx               ; ax = Z * Cos(xang)  

63、60; add     bp,ax            ; bp = Y * SIN(xang) + Z * COS(xang)    sar     bp,8             ; bp = Y * SIN(xang) + Z * COS(xang)

64、 / 256    mov     Zt,bp    mov     ax,Yt          ; Switch values    mov     Y,ax    mov     ax,Zt   

65、; mov     Z,ax; Rotate around y-axis; XT = X * COS(yang) - Z * SIN(yang) / 256; ZT = X * SIN(yang) + Z * COS(yang) / 256; X = XT; Z = ZT    mov     ax,X    mov     bx,YCos    imul  

66、0; bx               ; ax = X * Cos(yang)    mov     bp,ax    mov     ax,Z    mov     bx,YSin    imul 

67、0;  bx               ; ax = Z * Sin(yang)    sub     bp,ax            ; bp = X * Cos(yang) - Z * Sin(yang)    sar &

68、#160;   bp,8             ; bp = X * Cos(yang) - Z * Sin(yang) / 256    mov     Xt,bp    mov     ax,X    mov     bx,YSin 

69、   imul    bx               ; ax = X * Sin(yang)    mov     bp,ax    mov     ax,Z    mov     bx,YCos

70、    imul    bx               ; ax = Z * Cos(yang)    add     bp,ax            ; bp = X * SIN(yang) + Z * COS(y

71、ang)    sar     bp,8             ; bp = X * SIN(yang) + Z * COS(yang) / 256    mov     Zt,bp    mov     ax,Xt    &#

72、160;     ; Switch values    mov     X,ax    mov     ax,Zt    mov     Z,ax; Rotate around z-axis; XT = X * COS(zang) - Y * SIN(zang) / 256; YT = X * SIN(zang) + Y * COS(zang) /

73、 256; X = XT; Y = YT    mov     ax,X    mov     bx,ZCos    imul    bx               ; ax = X * Cos(zang)    mov

74、0;    bp,ax    mov     ax,Y    mov     bx,ZSin    imul    bx               ; ax = Y * Sin(zang)    s

75、ub     bp,ax            ; bp = X * Cos(zang) - Y * Sin(zang)    sar     bp,8             ; bp = X * Cos(zang) - Y * Sin(zang) / 25

76、6    mov     Xt,bp    mov     ax,X    mov     bx,ZSin    imul    bx               ; ax = X * Si

77、n(zang)    mov     bp,ax    mov     ax,Y    mov     bx,ZCos    imul    bx               ; ax =

78、Y * Cos(zang)    add     bp,ax            ; bp = X * SIN(zang) + Y * COS(zang)    sar     bp,8             ; bp = X

79、 * SIN(zang) + Y * COS(zang) / 256    mov     Yt,bp    mov     ax,Xt          ; Switch values    mov     X,ax    mov   &

80、#160; ax,Yt    mov     Y,ax    retENDP RotatePoint           PROC ShowPoint; Calculates screenposition and; plots the point on the screen    mov     ax,Xoff 

81、60;         ; Xoff*X / Z+Zoff = screen x    mov     bx,X    imul    bx    mov     bx,Z    add     bx,Zoff   &#

82、160;       ; Distance    idiv    bx    add     ax,Mx             ; Center on screen    mov     bp,ax 

83、0;  mov     ax,Yoff           ; Yoff*Y / Z+Zoff = screen y    mov     bx,Y    imul    bx    mov     bx,Z    ad

84、d     bx,Zoff           ; Distance    idiv    bx    add     ax,My             ; Center on screen 

85、60;             mov     bx,320    imul    bx    add     ax,bp               ; ax = (y*320)

86、+x    mov     di,ax    mov     ax,Z              ; Get color from Z    add     ax,100d       &#

87、160;     ; (This piece of code could be improved)    mov     byte ptr es:di,al ; Place a dot with color al    mov     Erase+si,di       ; Save position for erase   

88、retENDP ShowPointPROC MainProgram    call    UpdateAngles        ; Calculate new angles    call    SetRotation         ; Find sine & cosine of those angles 

89、   xor     si,si               ; First 3d-point    mov     cx,MaxPointsShowLoop:      call    RotatePoint    &

90、#160;    ; Rotates the point using above formulas    call    ShowPoint           ; Shows the point    add     si,3         &#

91、160;      ; Next 3d-point    loop    ShowLoop    call    WaitVrt             ; Wait for retrace    xor     si,si &#

92、160;             ; Starting with point 0    xor     al,al               ; Color = 0 = black    mov   

93、0; cx,MaxPointsDeletion:    mov     di,Erase+si       ; di = vgapos old point    mov     byte ptr es:di,al ; Delete it    add     si,3     &

94、#160;          ; Next point    loop    Deletion    retENDP MainProgram; = DATA =           Credits   DB   13,10,"Code by Vulture / Outl

95、aw Triad",13,10,"$"Label SinCos Word       ; 256 valuesdw 0,6,13,19,25,31,38,44,50,56dw 62,68,74,80,86,92,98,104,109,115dw 121,126,132,137,142,147,152,157,162,167dw 172,177,181,185,190,194,198,202,206,209dw 213,216,220,223,226,229,231,234,237,239dw 241,24

96、3,245,247,248,250,251,252,253,254dw 255,255,256,256,256,256,256,255,255,254dw 253,252,251,250,248,247,245,243,241,239dw 237,234,231,229,226,223,220,216,213,209dw 206,202,198,194,190,185,181,177,172,167dw 162,157,152,147,142,137,132,126,121,115dw 109,104,98,92,86,80,74,68,62,56dw 50,44,38,31,25,19,13

97、,6,0,-6dw -13,-19,-25,-31,-38,-44,-50,-56,-62,-68dw -74,-80,-86,-92,-98,-104,-109,-115,-121,-126dw -132,-137,-142,-147,-152,-157,-162,-167,-172,-177dw -181,-185,-190,-194,-198,-202,-206,-209,-213,-216dw -220,-223,-226,-229,-231,-234,-237,-239,-241,-243dw -245,-247,-248,-250,-251,-252,-253,-254,-255,

98、-255dw -256,-256,-256,-256,-256,-255,-255,-254,-253,-252dw -251,-250,-248,-247,-245,-243,-241,-239,-237,-234dw -231,-229,-226,-223,-220,-216,-213,-209,-206,-202dw -198,-194,-190,-185,-181,-177,-172,-167,-162,-157dw -152,-147,-142,-137,-132,-126,-121,-115,-109,-104dw -98,-92,-86,-80,-74,-68,-62,-56,-

99、50,-44dw -38,-31,-25,-19,-13,-6Label Cube Byte           ; The 3d points       c = -35            ; 5x*5y*5z (=125) points       rept 5         b = -35         rept 5           a = -35           rept 5    &

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论