A.D.A. Amiga Demoscene Archive

  Welcome guest! Please register a new account or log in

  

  

  

log in with SceneID

  

Demos Amiga Demoscene Archive Forum / Coding / Sinus / cosinus calculation
 Page:  ««  1  2  
Author Message
ZEROblue
Member
#1 - Posted: 14 Jun 2009 11:25 - Edited
Reply Quote
Since you get a long word result you have to use asr.l to shift all 32 bits and not just the lower 16 bits.

EDIT: maybe I missed something in the context, but your sinus table seems wrong as well. If you want values between -256 and 256 you need to set the amplitude to 256 and multiplier to 1.
z5_
Member
#2 - Posted: 17 Jun 2009 19:48
Reply Quote
@ZEROblue: off-topic but can i contact you somewhere. Drop me a mail if interested (email at ada info page)

sorry bout that. Please continue this interesting discussion.

On topic, when i was doing my first sinus/cosinus stuff for Walking in circles, a world of effects opened up for me. Or rather, i had a feeling that sin/cos is the base of a whole lot of effects. But i never really found the missing link (if that makes sense): that is to pinpoint which effects and to actually understand how they are done.
Vektor
Member
#3 - Posted: 19 Jun 2009 19:01
Reply Quote
That's a very good question. I dugged up my old code to see how I did it.

Instead of shifting right (dividing) I shifted left and swapped the register

** Rotate Around Z **
;d0=x
;d1=y
;d2=z
movem.W (a0)+,d0-d2 ;X,Y,Z coordinates
move.W d1,d3 ;Y coordinat
muls.W (a2),d3 ;sinus Alpha * Y

move.W d0,d4 ;X coordinate
muls.W (a3),d4 ;X * cos Alpha

add.L d4,d3 ;sinus Alpha * Y+X * cos Alpha

lsl.L #2,d3 ;x4
swap d3 ;/2^16 = NEW X

muls.W (a3),d1 ;cosinus Alpha * Y
muls.W (a2),d0 ;sinus Alpha * X
sub.L d0,d1

lsl.L @2,d1
swap d1 ;Store NEW Y
Vektor
Member
#4 - Posted: 19 Jun 2009 19:04
Reply Quote
I also dugged up my precalculated rotation and plotter....

Eye = 750

Number_of_pixels = 144;182= max
screenwidth = 48
screenheight = 270
maxx = [screenwidth*8]-1
minx = 0
maxy = screenheight-1
miny = 0

CODE:
lea code(pc),a5
bsr.w Make_rotation_table
bsr.w Init_shift_table_x
bsr.w Init_mulu_table_y

Main:
; bsr.W wait_vert_blank
bsr.W rotate
bsr.B setpoint
btst #6,$bfe001
bne.B main
exit:
rts



Setpoint:
lea Rotated_Pos_tab(pc),a0
lea Shift_Table_x(pc),a1
lea Mulu_Table_y(pc),a3
move.L screen(pc),a2

move.w (a0)+,d7
moveq #0,d0
moveq #0,d1
moveq #0,d2
Plot_loop:
movem.w (a0)+,d0-d2

; cmp.W #minx,d0 ;min x;
; blt.B .next
; cmp.W #maxx,d0 ;max x
; bgt.B .next
; cmp.W #miny,d1 ;min y
; blt.B .next
; cmp.W #maxy,d1 ;max y
; bgt.B .next

lsl.w #1,d1
move.w (a3,d1.w),d1

moveq #0,d3
move.b (a1,d0.w),d3 ;add x-word
add.l d3,d1
not.w d0 ;shift bit

cmp.W #Eye+50,d2
bcc.B .third_plane

cmp.W #Eye-50,d2
bcc.B .second_plane

bset d0,(a2,d1.W)
.next
dbf d7,plot_loop
rts

.second_plane:
bset d0,(a2,d1.W)
bset d0,screenwidth(a2,d1.W)
dbf d7,plot_loop
rts

.third_plane:
bset d0,screenwidth(a2,d1.W)
dbf d7,plot_loop
rts

Init_shift_table_x:
lea Shift_Table_x(pc),a0
moveq #screenwidth-1,d7
moveq #0,d0
.Shift_loop:
rept 8
move.b d0,(a0)+
endr
addq.w #1,d0
dbf d7,.shift_loop
rts

Shift_Table_x:
dcb.w screenwidth*8,0

Init_mulu_table_y:
lea Mulu_Table_y(pc),a0
moveq #0,d7
move.w #screenheight-1,d7
moveq #0,d0
moveq #screenwidth*2,d1
.Shift_loop:
move.w d0,(a0)+
add.w d1,d0
dbf d7,.shift_loop
rts

Mulu_Table_y:
dcb.w screenheight,0

********************************
* *
* 3D Rotate + Perspective V5.0 *
* *
* Precalc with a 100Kb Table *
* *
********************************

rotate:
moveq #0,d0
moveq #0,d1
moveq #0,d2

movem.w Angles,d0-d2
addq.w #1,d0
addq.w #1,d1
addq.w #1,d2
bsr.w Test_angles
movem.w d0-d2,Angles-code(a5)

bsr.w Convert_angles

New_rotate:
moveq #0,d0
moveq #0,d1
moveq #0,d2
moveq #0,d3
moveq #0,d4
moveq #0,d5
movem.w Angle_save_table,d0-d5
mulu #screenwidth*8*2,d0
mulu #screenwidth*8*2,d1
mulu #screenwidth*8*2,d2
mulu #screenwidth*8*2,d3
mulu #screenwidth*8*2,d4
mulu #screenwidth*8*2,d5

lea Rotate_table+[[screenwidth*8*2]/2],a1
lea (a1,d5.l),a6
lea (a1,d4.l),a5
lea (a1,d3.l),a4
lea (a1,d2.l),a3
lea (a1,d1.l),a2
lea (a1,d0.l),a1


lea Pos_tab(pc),a0
moveq #0,d7
move.w (a0)+,d7
exg.l a0,d6
lea Rotated_pos_tab(pc),a0
move.w d7,(a0)+
exg.l a0,d6

moveq #0,d0
moveq #0,d1
moveq #0,d2
New_rotate_loop:
************************************************** ***************
movem.w (a0)+,d0-d2 ;get coords
************************************************** ***************
add.w d0,d0 ;make index from x
add.w d1,d1 ;make index from y
add.w d2,d2 ;make index from z
************************************************** ***************
; Y = X * sin A + Y * cos A
move.w (a1,d0.w),d3
add.w (a2,d1.w),d3 ;New y
; X = X * cos A - Y * sin A
move.w (a2,d0.w),d0
sub.w (a1,d1.w),d0 ;New x
************************************************** ***************
add.w d3,d3 ;make index from new y
add.w d0,d0 ;make index from new x
************************************************** ***************
; X = Z * cos B - X * sin B
move.w (a4,d2.w),d1
sub.w (a3,d0.w),d1 ;New x
; Z = Z * sin B + X * cos B
move.w (a3,d2.w),d2
add.w (a4,d0.w),d2 ;New z
************************************************** ***************
add.w d2,d2 ;make index from new z
************************************************** ***************
; Z = Y * cos C - Z * sin C
move.w (a6,d3.w),d0
sub.w (a5,d2.w),d0 ;New Z
; Y = Y * sin C + Z * cos C
move.w (a5,d3.w),d3
add.w (a6,d2.w),d3 ;New Y
************************************************** ***************

;d1=x/d3=y/d0=z

Calc_perspective:
add.w #Eye,d0
beq.s ._0
muls.w #Eye,d1
muls.w #-Eye,d3
divs.w d0,d1
divs.w d0,d3
._0

add.w #[maxx/2]+16,d1 ;x mid
add.w #maxy/2,d3 ;y mid

exg.l a0,d6
move.w d1,(a0)+ ;store x
move.w d3,(a0)+ ;store y
move.w d0,(a0)+ ;store z
exg.l a0,d6
dbf d7,new_rotate_loop

lea code(pc),a5
lea $dff000,a6
rts

Convert_angles:
lea Angle_save_table(pc),a0
Save_alpha
move.w d0,(a0)+ ;sin a
add.w #90,d0
cmp.w #360,d0
blt.b .not_more
sub.w #360,d0 ;cos a (in sin )
.not_more
move.w d0,(a0)+

Save_beta
move.w d1,(a0)+
add.w #90,d1
cmp.w #360,d1
blt.b .not_more
sub.w #360,d1
.not_more
move.w d1,(a0)+

Save_gamma
move.w d2,(a0)+
add.w #90,d2
cmp.w #360,d2
blt.b .not_more
sub.w #360,d2
.not_more
move.w d2,(a0)+

lea Angle_save_table(pc),a0
moveq #6-1,d7
.convert_loop
move.w (a0),d0
bsr.b Do_conversion
move.w d0,(a0)+
dbf d7,.convert_loop
rts

Angle_save_table:
dc.w 0,0,0
dc.w 0,0,0

Do_conversion:
cmp.w #90,d0
ble.b .not_more_than_90
cmp.w #180,d0
ble.b .invert_angle_090_180
cmp.w #270,d0
bgt.b .invert_angle_180_270
sub.w #90-1,d0
.not_more_than_90:
rts

.invert_angle_180_270
moveq #0,d1
move.w #180,d1
sub.w d0,d1
exg d0,d1 ;exg
tst.w d0
add.w #360,d0
sub.w #90-1,d0
rts

.invert_angle_090_180:
moveq #0,d1
move.w #180,d1
sub.w d0,d1
exg d0,d1 ;exg
rts

test_angles:
moveq #0,d3
move.w #360,d3
TST.W D0
BGE.S Alpha_not_less_zero
add.W d3,D0
Alpha_not_less_zero:
CMP.W d3,D0
BLT.S Alpha_not_more_360
sub.W d3,D0
Alpha_not_more_360:
TST.W D1
BGE.S Beta_not_less_zero
add.W d3,D1
Beta_not_less_zero:
CMP.W d3,D1
BLT.S Beta_not_more_360
sub.W d3,D1
Beta_not_more_360:
TST.W D2
BGE.S Gamma_not_less_zero
add.W d3,D2
Gamma_not_less_zero:
CMP.W d3,D2
BLT.S Gamma_not_more_360
sub.W d3,D2
Gamma_not_more_360:
RTS

Make_rotation_table:
lea sintab(pc),a0 ;Sin
lea Rotate_table,a1
lea Rotate_table+[[screenwidth*8*2]*91],a2
move.w #91-1,d7 ;0-90 degrees
.Make_Loop:
moveq #0,d0
move.w #-[screenwidth*8/2],d0 ;X
.loop
move.l d0,d1
muls (a0),d1 ;Sin x * C
add.l d1,d1
swap d1
move.w d1,(a1)+
neg d1
move.w d1,(a2)+
move.w d1,$dff180
addq.w #1,d0
cmp.w #[screenwidth*8/2],d0
bne.b .loop
addq.l #2,a0
dbf d7,.Make_loop
rts


Rotating_point:
Rotating_point_X:
dc.w 0
Rotating_point_Y:
dc.w 0;20
Rotating_point_Z:
dc.w 0;200

Angles:
Angle_alpha:
dc.w 0
Angle_Beta:
dc.w 0
Angle_Gamma:
dc.w 0
Sintab:
dc.l $23B,$47706B2,$8ED0B27,$D610F99,$11D01406
dc.l $163A186C,$1A9C1CCB,$1EF72120,$2348256C,$278 D29AC
dc.l $2BC72DDF,$2FF33203,$340F3618,$381C3A1C,$3C1 73E0E
dc.l $3FFF41EC,$43D445B6,$4793496A,$4B3C4D08,$4EC D508D
dc.l $524653F9,$55A6574B,$58EA5A82,$5C135D9C,$5F1 F609A
dc.l $620D6379,$64DD6639,$678D68D9,$6A1D6B59,$6C8 C6DB7
dc.l $6ED96FF3,$7104720C,$730B7401,$74EF75D3,$76A D777F
dc.l $78477906,$79BC7A68,$7B0A7BA3,$7C327CB8,$7D3 37DA5
dc.l $7E0E7E6C,$7EC17F0B,$7F4C7F83,$7FB07FD3,$7FE C7FFB
dc.l $7FFF7FFB,$7FEC7FD3,$7FB07F83,$7F4C7F0B,$7EC 17E6C
dc.l $7E0E7DA5,$7D337CB8,$7C327BA3,$7B0A7A68,$79B C7906
dc.l $7847777F,$76AD75D3,$74EF7401,$730B720C,$710 46FF3
dc.l $6ED96DB7,$6C8C6B59,$6A1D68D9,$678D6639,$64D D6379
dc.l $620D609A,$5F1F5D9C,$5C135A82,$58EA574B,$55A 653F9
dc.l $5246508D,$4ECD4D08,$4B3C496A,$479345B6,$43D 441EC
dc.l $40003E0E,$3C173A1C,$381C3618,$340F3203,$2FF 32DDF
dc.l $2BC729AC,$278D256C,$23482120,$1EF71CCB,$1A9 C186C
dc.l $163A1406,$11D00F99,$D610B27,$8ED06B2,$47702 3B
dc.l $FDC5,$FB89F94E,$F713F4D9,$F29FF067,$EE30EBF A
dc.l $E9C6E794,$E564E335,$E109DEE0,$DCB8DA94,$D87 3D654
dc.l $D439D221,$D00DCDFD,$CBF1C9E8,$C7E4C5E4,$C3E 9C1F2
dc.l $C001BE14,$BC2CBA4A,$B86DB696,$B4C4B2F8,$B13 3AF73
dc.l $ADBAAC07,$AA5AA8B5,$A716A57E,$A3EDA264,$A0E 19F66
dc.l $9DF39C87,$9B2399C7,$98739727,$95E394A7,$937 49249
dc.l $9127900D,$8EFC8DF4,$8CF58BFF,$8B118A2D,$895 38881
dc.l $87B986FA,$86448598,$84F6845D,$83CE8348,$82C D825B
dc.l $81F28194,$813F80F5,$80B4807D,$8050802D,$801 48005
dc.l $80018005,$8014802D,$8050807D,$80B480F5,$813 F8194
dc.l $81F2825B,$82CD8348,$83CE845D,$84F68598,$864 486FA
dc.l $87B98881,$89538A2D,$8B118BFF,$8CF58DF4,$8EF C900D
dc.l $91279249,$937494A7,$95E39727,$987399C7,$9B2 39C87
dc.l $9DF39F66,$A0E1A264,$A3EDA57E,$A716A8B5,$AA5 AAC07
dc.l $ADBAAF73,$B133B2F8,$B4C4B696,$B86DBA4A,$BC2 CBE14
dc.l $C000C1F2,$C3E9C5E4,$C7E4C9E8,$CBF1CDFD,$D00 DD221
dc.l $D439D654,$D873DA94,$DCB8DEE0,$E109E335,$E56 4E794
dc.l $E9C6EBFA,$EE30F067,$F29FF4D9,$F713F94E,$FB8 9FDC5
dc.l $23B,$47706B2,$8ED0B27,$D610F99,$11D01406
dc.l $163A186C,$1A9C1CCB,$1EF72120,$2348256C,$278 D29AC
dc.l $2BC72DDF,$2FF33203,$340F3618,$381C3A1C,$3C1 73E0E
dc.l $3FFF41EC,$43D445B6,$4793496A,$4B3C4D08,$4EC D508D
dc.l $524653F9,$55A6574B,$58EA5A82,$5C135D9C,$5F1 F609A
dc.l $620D6379,$64DD6639,$678D68D9,$6A1D6B59,$6C8 C6DB7
dc.l $6ED96FF3,$7104720C,$730B7401,$74EF75D3,$76A D777F
dc.l $78477906,$79BC7A68,$7B0A7BA3,$7C327CB8,$7D3 37DA5
dc.l $7E0E7E6C,$7EC17F0B,$7F4C7F83,$7FB07FD3,$7FE C7FFB
dc.w $7FFF

Pos_tab:
dc.W Number_of_pixels-1

* 137 dots torus
x
dc.w -40,0,0
dc.w -35,-20,0
dc.w -20,-35,0
dc.w 0,-40,0
dc.w 20,-35,0
dc.w 35,-20,0
dc.w 40,0,0
dc.w 35,20,0
dc.w 20,35,0
dc.w 0,40,0
dc.w -20,35,0
dc.w -35,20,0
dc.w -45,0,-20
dc.w -39,-23,-20
dc.w -23,-39,-20
dc.w 0,-45,-20
dc.w 23,-39,-20
dc.w 39,-23,-20
dc.w 45,0,-20
dc.w 39,23,-20
dc.w 23,39,-20
dc.w 0,45,-20
dc.w -23,39,-20
dc.w -39,23,-20
dc.w -60,0,-35
dc.w -52,-30,-35
dc.w -30,-52,-35
dc.w 0,-60,-35
dc.w 30,-52,-35
dc.w 52,-30,-35
dc.w 60,0,-35
dc.w 52,30,-35
dc.w 30,52,-35
dc.w 0,60,-35
dc.w -30,52,-35
dc.w -52,30,-35
dc.w -80,0,-40
dc.w -69,-40,-40
dc.w -40,-69,-40
dc.w 0,-80,-40
dc.w 40,-69,-40
dc.w 69,-40,-40
dc.w 80,0,-40
dc.w 69,40,-40
dc.w 40,69,-40
dc.w 0,80,-40
dc.w -40,69,-40
dc.w -69,40,-40
dc.w -100,0,-35
dc.w -87,-50,-35
dc.w -
LaBodilsen
Member
#5 - Posted: 7 Jan 2016 21:12
Reply Quote
hi
just want to say thanks for this thread..

as i'm slowly getting into assembler programming again after a 20 year break, i was looking for a sinus generator, and because of this thread, i found that i already had the needed Tool in asm-one :D


nonarkitten
Member
#6 - Posted: 28 Aug 2018 02:09 - Edited
Reply Quote
If 100% precision is not needed, then it's best to just do it without any table at all. Something like this:
	int d1 = phase & 511;
int d0 = d1 * (512 - d1);
return (phase & 512) ? -d0 : d0;
 Page:  ««  1  2  

  Please register a new account or log in to comment

  

  

  

 

A.D.A. Amiga Demoscene Archive, Version 3.0