3D Sine Wave v1.4 [Windows only]

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: 3D Sine Wave [Windows only]

Post by UEZ »

jj2007 wrote:
UEZ wrote:the sin / cos asm variants give a real boost (from 50 fps to 70 fps on my notebook).
Yes, that can give some extra performance. For example, Sinus() is a factor 5.5 faster than the FPU's fsin. I am curious to see which sinus algo you are using - can you post it?
Look to the functions _Sin6th / _Cos6th in the code from post #1. ;-)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: 3D Sine Wave [Windows only]

Post by jj2007 »

UEZ wrote:Look to the functions _Sin6th / _Cos6th in the code from post #1. ;-)
Yes, that's almost twice as fast as the FPU fsin. But it could be done a little bit faster ;-)
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: 3D Sine Wave [Windows only]

Post by UEZ »

@jj2007: have a look here for some more variants coded by eukalyptus: https://forum.qbasic.at/viewtopic.php?t ... nparabolic
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: 3D Sine Wave [Windows only]

Post by dodicat »

Nice UEZ.
I was inspired.

Code: Select all




Type V3
    As Single x,y,z
End Type

Type float As V3

Type box
    As v3 p(1 To 4)
    As Ulong c    'colour
End Type

Type angle3D             'FLOATS for angles
    As Single sx,sy,sz
    As Single cx,cy,cz
    Declare Static Function construct(As Single,As Single,As Single) As Angle3D
End Type

Screenres 1024,768,32,2
width 1024\8,768\16 'max dos font size

'============ globals =============
Const pi=4*Atn(1)
Redim shared As box b()
Redim shared As box rot1()
Dim shared As Angle3D A3d
dim shared as V3 CC       'grid centre
dim shared as double df   'for inputfunction()
'==================================

Function Angle3D.construct(x As Single,y As Single,z As Single) As Angle3D
    Return   Type (Sin(x),Sin(y),Sin(z), _
                   Cos(x),Cos(y),Cos(z))
End Function

Function Rotate(c As V3,p As V3,a As Angle3D,scale As float=Type(1,1,1)) As V3
    Dim As Single dx=p.x-c.x,dy=p.y-c.y,dz=p.z-c.z
    Return Type<V3>((scale.x)*((a.cy*a.cz)*dx+(-a.cx*a.sz+a.sx*a.sy*a.cz)*dy+(a.sx*a.sz+a.cx*a.sy*a.cz)*dz)+c.x,_
    (scale.y)*((a.cy*a.sz)*dx+(a.cx*a.cz+a.sx*a.sy*a.sz)*dy+(-a.sx*a.cz+a.cx*a.sy*a.sz)*dz)+c.y,_
    (scale.z)*((-a.sy)*dx+(a.sx*a.cy)*dy+(a.cx*a.cy)*dz)+c.z)
End Function 

Function perspective(p As V3,eyepoint As V3) As V3
    Dim As Single   w=1+(p.z/eyepoint.z)
    Return Type<V3>((p.x-eyepoint.x)/w+eyepoint.x,_
    (p.y-eyepoint.y)/w+eyepoint.y,_
    (p.z-eyepoint.z)/w+eyepoint.z)
End Function 

Function setgrid(sx As Single,bx As Single,sy As Single,by As Single,st As Single,p() As box,fn As Function(x As Double) As Double) As v3
    #define U Ubound(p)
    #define dst(p1,p2) Sqr( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) + (p1.z-p2.z)*(p1.z-p2.z) )   
    Redim p(0)
    static As v3 centre
    Dim As Single cx,cy,ctr
    For y As Single=sy To by Step st
        For x As Single=sx To bx Step st
            ctr+=1
            cx+=x
            cy+=y
        Next
    Next
    static as single q=15
    centre=Type(cx/ctr,cy/ctr)
    'randomize 1
    For y As Single=sy To by Step st
        For x As Single=sx To bx Step st
            Redim Preserve p(1 To U+1)
            p(u).p(1)=Type<v3>(x,y,      fn(dst( p(u).p(1),centre)))
            p(u).p(2)=Type<v3>(x+st,y,   fn(dst( p(u).p(2),centre)))
            p(u).p(3)=Type<v3>(x+st,y+st,fn(dst( p(u).p(3),centre)))
            p(u).p(4)=Type<v3>(x,y+st,   fn(dst( p(u).p(4),centre)))
            p(u).c=Rgb(x*q, x*q xor y*q,y*q)'(Rnd*255,Rnd*255,Rnd*255)
        Next
        
    Next
    Return centre
End Function

Sub drawboxes(b() As box)
    For n As Long=Lbound(b) To Ubound(b)
        Line(b(n).p(1).x,b(n).p(1).y)-(b(n).p(2).x,b(n).p(2).y),b(n).c  'optional
        Line(b(n).p(2).x,b(n).p(2).y)-(b(n).p(3).x,b(n).p(3).y),b(n).c  'optional
        Line(b(n).p(3).x,b(n).p(3).y)-(b(n).p(4).x,b(n).p(4).y),b(n).c
        Line(b(n).p(4).x,b(n).p(4).y)-(b(n).p(1).x,b(n).p(1).y),b(n).c
    Next
End Sub

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
    Static As Double timervalue,_lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    _lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function

Function InputFunction(x As Double) As Double
    Return Sin(x-df)' << --------------- INPUT function -----------
End Function


sub setup(x1 as single,x2 as single,y1 as single,y2 as single,meshsize as single)
 CC= setgrid(x1,x2,y1,y2,meshsize,b(),@InputFunction)'create grid, CC is the centre
Redim rot1(Lbound(b) To Ubound(b))                   'working array
A3d=angle3D.construct(0,-pi/2,0)  
For n As Long=Lbound(b) To Ubound(b)
    For m As Long=1 To 4
        rot1(n).p(m)=rotate(CC,B(n).p(m),A3D,Type(10,10,10)) 'align boxes horizontally based
        rot1(n).c=B(n).c
        B(n).p(m)=rot1(n).p(m)
    Next m
Next n
end sub

sub display()
#define resetwheel(w,fl) fl=w
#define wheel(w,f) w-f
screenset 1,0
static As float ang=(0,-pi/7,pi/2)  'default
static As Long fps
static As String key
static As Long mx,my,mw,mb,rflag
static As Single sc
Do
    df+=.03                   'push the wave out
    setup(485,515,385-50,415-50,.6) 'reset the grid
    
    Getmouse mx,my,mw,mb
    If mb=2 Then 'reset
    ang.z=pi/2:ang.y=-pi/7
    resetwheel(mw,rflag) 
    end if
      mw=wheel(mw,rflag)
   if mx>0 then sc=2+(mw/10)'scaler
    key=Inkey
    If key=Chr(255)+"K" Then ang.z-=.05     'left
    If key=Chr(255)+"M" Then ang.z+=.05     'right
    If key=Chr(255)+"P" Then ang.y-=.05     'down
    If key=Chr(255)+"H" Then ang.y+=.05     'up 
    
    ang.x+=.01  'the orbiting speed 
    
    A3D=Angle3D.construct(ang.x,ang.y,ang.z)      'set the rotate trigs
    
    For n As Long=Lbound(b) To Ubound(b)
        For m As Long=1 To 4
            rot1(n).p(m) =rotate(CC,B(n).p(m),A3D,Type(sc,sc,sc))
            rot1(n).p(m) =perspective(rot1(n).p(m),Type(cc.x,cc.y,400*sc))'eyepoint
            if mb=1 then rot1(n).p(m).x-=cc.x-mx: rot1(n).p(m).y-=cc.y-my'follow the mouse
        Next m
    Next n
     cls
     Draw String(50,50),"Framerate "&fps
     Draw String(50,150),"Use the arrow keys and wheel"
      Draw String(50,250),"Right mouse click to reset"
     drawboxes(rot1())
     flip
    Sleep regulate(80,fps),1
Loop Until key=Chr(27)
end sub

display()

Sleep




 
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: 3D Sine Wave [Windows only]

Post by srvaldez »

very nice dodicat :-)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: 3D Sine Wave [Windows only]

Post by srvaldez »

@jj2007
what's the range of your fast sin and where is the code? all I see is the timing code.
Sin6th seems to reduce the range, don't understand the code though.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: 3D Sine Wave [Windows only]

Post by jj2007 »

srvaldez wrote:@jj2007
what's the range of your fast sin and where is the code?
Range is -400 ... 400:

Code: Select all

include \masm32\MasmBasic\MasmBasic.inc	; http://masm32.com/board/index.php?topic=94.0
  Init
  Dim MySinus() As REAL10	; can be REAL4 (single), REAL8 (double) or REAL10
  For_ ct=-400 To 400
	SetFloat MySinus(ct+400)=Sinus(ct)
	PrintLine Str$("%i\t", ct), Str$(MySinus(ct+400))
  Next
  Inkey "hit any key"
EndOfCode
Output:

Code: Select all

-400    -0.6427876096865393264
-399    -0.6293203910498374527
-398    -0.6156614753256582797
-397    -0.6018150231520482799
-396    -0.5877852522924731293
-395    -0.5735764363510460962
-394    -0.5591929034707468302
-393    -0.5446390350150270823
-392    -0.5299192642332049541
-391    -0.5150380749100542101
-390    -0.5000000000000000001
-389    -0.4848096202463370291
-388    -0.4694715627858907760
-387    -0.4539904997395467916
-386    -0.4383711467890774175
-385    -0.4226182617406994363
-384    -0.4067366430758002078
-383    -0.3907311284892737551
-382    -0.3746065934159120355
-381    -0.3583679495453002735
-380    -0.3420201433256687331
-379    -0.3255681544571566688
-378    -0.3090169943749474241
-377    -0.2923717047227367281
-376    -0.2756373558169991857
-375    -0.2588190451025207624
-374    -0.2419218955996677226
-373    -0.2249510543438649981
-372    -0.2079116908177593371
-371    -0.1908089953765448124
-370    -0.1736481776669303489
-369    -0.1564344650402308690
-368    -0.1391731009600654441
-367    -0.1218693434051474811
-366    -0.1045284632676534714
-365    -0.08715574274765817356
-364    -0.06975647374412530078
-363    -0.05233595624294383272
-362    -0.03489949670250097165
-361    -0.01745240643728351282
-360    0.0
-359    0.01745240643728351282
-358    0.03489949670250097165
-357    0.05233595624294383272
-356    0.06975647374412530078
-355    0.08715574274765817356
-354    0.1045284632676534714
-353    0.1218693434051474811
-352    0.1391731009600654441
-351    0.1564344650402308690
-350    0.1736481776669303489
-349    0.1908089953765448124
-348    0.2079116908177593371
-347    0.2249510543438649981
-346    0.2419218955996677226
-345    0.2588190451025207624
-344    0.2756373558169991857
-343    0.2923717047227367281
-342    0.3090169943749474241
-341    0.3255681544571566688
-340    0.3420201433256687331
-339    0.3583679495453002735
-338    0.3746065934159120355
-337    0.3907311284892737551
-336    0.4067366430758002078
-335    0.4226182617406994363
-334    0.4383711467890774175
-333    0.4539904997395467916
-332    0.4694715627858907760
-331    0.4848096202463370291
-330    0.5000000000000000001
-329    0.5150380749100542101
-328    0.5299192642332049541
-327    0.5446390350150270823
-326    0.5591929034707468302
-325    0.5735764363510460962
-324    0.5877852522924731293
-323    0.6018150231520482799
-322    0.6156614753256582797
-321    0.6293203910498374527
-320    0.6427876096865393264
-319    0.6560590289905072849
-318    0.6691306063588582139
-317    0.6819983600624985005
-316    0.6946583704589972868
-315    0.7071067811865475244
-314    0.7193398003386511395
-313    0.7313537016191704833
-312    0.7431448254773942351
-311    0.7547095802227719981
-310    0.7660444431189780353
-309    0.7771459614569708801
-308    0.7880107536067219568
-307    0.7986355100472928463
-306    0.8090169943749474242
-305    0.8191520442889917897
-304    0.8290375725550416921
-303    0.8386705679454240297
-302    0.8480480961564259706
-301    0.8571673007021122875
-300    0.8660254037844386468
-299    0.8746197071393958004
-298    0.8829475928589269422
-297    0.8910065241883678624
-296    0.8987940462991669929
-295    0.9063077870366499633
-294    0.9135454576426008956
-293    0.9205048534524403275
-292    0.9271838545667874009
-291    0.9335804264972017491
-290    0.9396926207859083841
-289    0.9455185755993168104
-288    0.9510565162951535722
-287    0.9563047559630354813
-286    0.9612616959383188619
-285    0.9659258262890682867
-284    0.9702957262759964724
-283    0.9743700647852352287
-282    0.9781476007338056380
-281    0.9816271834476639537
-280    0.9848077530122080594
-279    0.9876883405951377263
-278    0.9902680687415703152
-277    0.9925461516413220351
-276    0.9945218953682733370
-275    0.9961946980917455322
-274    0.9975640502598242478
-273    0.9986295347545738739
-272    0.9993908270190957301
-271    0.9998476951563912392
-270    1.000000000000000000
-269    0.9998476951563912392
-268    0.9993908270190957301
-267    0.9986295347545738739
-266    0.9975640502598242478
-265    0.9961946980917455322
-264    0.9945218953682733370
-263    0.9925461516413220351
-262    0.9902680687415703152
-261    0.9876883405951377263
-260    0.9848077530122080594
-259    0.9816271834476639537
-258    0.9781476007338056380
-257    0.9743700647852352287
-256    0.9702957262759964724
-255    0.9659258262890682867
-254    0.9612616959383188619
-253    0.9563047559630354813
-252    0.9510565162951535722
-251    0.9455185755993168104
-250    0.9396926207859083841
-249    0.9335804264972017491
-248    0.9271838545667874009
-247    0.9205048534524403275
-246    0.9135454576426008956
-245    0.9063077870366499633
-244    0.8987940462991669929
-243    0.8910065241883678624
-242    0.8829475928589269422
-241    0.8746197071393958004
-240    0.8660254037844386468
-239    0.8571673007021122875
-238    0.8480480961564259706
-237    0.8386705679454240297
-236    0.8290375725550416921
-235    0.8191520442889917897
-234    0.8090169943749474242
-233    0.7986355100472928463
-232    0.7880107536067219568
-231    0.7771459614569708801
-230    0.7660444431189780353
-229    0.7547095802227719981
-228    0.7431448254773942351
-227    0.7313537016191704833
-226    0.7193398003386511395
-225    0.7071067811865475244
-224    0.6946583704589972868
-223    0.6819983600624985005
-222    0.6691306063588582139
-221    0.6560590289905072849
-220    0.6427876096865393264
-219    0.6293203910498374527
-218    0.6156614753256582797
-217    0.6018150231520482799
-216    0.5877852522924731293
-215    0.5735764363510460962
-214    0.5591929034707468302
-213    0.5446390350150270823
-212    0.5299192642332049541
-211    0.5150380749100542101
-210    0.5000000000000000001
-209    0.4848096202463370291
-208    0.4694715627858907760
-207    0.4539904997395467916
-206    0.4383711467890774175
-205    0.4226182617406994363
-204    0.4067366430758002078
-203    0.3907311284892737551
-202    0.3746065934159120355
-201    0.3583679495453002735
-200    0.3420201433256687331
-199    0.3255681544571566688
-198    0.3090169943749474241
-197    0.2923717047227367281
-196    0.2756373558169991857
-195    0.2588190451025207624
-194    0.2419218955996677226
-193    0.2249510543438649981
-192    0.2079116908177593371
-191    0.1908089953765448124
-190    0.1736481776669303489
-189    0.1564344650402308690
-188    0.1391731009600654441
-187    0.1218693434051474811
-186    0.1045284632676534714
-185    0.08715574274765817356
-184    0.06975647374412530078
-183    0.05233595624294383272
-182    0.03489949670250097165
-181    0.01745240643728351282
-180    0.0
-179    -0.01745240643728351282
-178    -0.03489949670250097165
-177    -0.05233595624294383272
-176    -0.06975647374412530078
-175    -0.08715574274765817356
-174    -0.1045284632676534714
-173    -0.1218693434051474811
-172    -0.1391731009600654441
-171    -0.1564344650402308690
-170    -0.1736481776669303489
-169    -0.1908089953765448124
-168    -0.2079116908177593371
-167    -0.2249510543438649981
-166    -0.2419218955996677226
-165    -0.2588190451025207624
-164    -0.2756373558169991857
-163    -0.2923717047227367281
-162    -0.3090169943749474241
-161    -0.3255681544571566688
-160    -0.3420201433256687331
-159    -0.3583679495453002735
-158    -0.3746065934159120355
-157    -0.3907311284892737551
-156    -0.4067366430758002078
-155    -0.4226182617406994363
-154    -0.4383711467890774175
-153    -0.4539904997395467916
-152    -0.4694715627858907760
-151    -0.4848096202463370291
-150    -0.5000000000000000001
-149    -0.5150380749100542101
-148    -0.5299192642332049541
-147    -0.5446390350150270823
-146    -0.5591929034707468302
-145    -0.5735764363510460962
-144    -0.5877852522924731293
-143    -0.6018150231520482799
-142    -0.6156614753256582797
-141    -0.6293203910498374527
-140    -0.6427876096865393264
-139    -0.6560590289905072849
-138    -0.6691306063588582139
-137    -0.6819983600624985005
-136    -0.6946583704589972868
-135    -0.7071067811865475244
-134    -0.7193398003386511395
-133    -0.7313537016191704833
-132    -0.7431448254773942351
-131    -0.7547095802227719981
-130    -0.7660444431189780353
-129    -0.7771459614569708801
-128    -0.7880107536067219568
-127    -0.7986355100472928463
-126    -0.8090169943749474242
-125    -0.8191520442889917897
-124    -0.8290375725550416921
-123    -0.8386705679454240297
-122    -0.8480480961564259706
-121    -0.8571673007021122875
-120    -0.8660254037844386468
-119    -0.8746197071393958004
-118    -0.8829475928589269422
-117    -0.8910065241883678624
-116    -0.8987940462991669929
-115    -0.9063077870366499633
-114    -0.9135454576426008956
-113    -0.9205048534524403275
-112    -0.9271838545667874009
-111    -0.9335804264972017491
-110    -0.9396926207859083841
-109    -0.9455185755993168104
-108    -0.9510565162951535722
-107    -0.9563047559630354813
-106    -0.9612616959383188619
-105    -0.9659258262890682867
-104    -0.9702957262759964724
-103    -0.9743700647852352287
-102    -0.9781476007338056380
-101    -0.9816271834476639537
-100    -0.9848077530122080594
-99     -0.9876883405951377263
-98     -0.9902680687415703152
-97     -0.9925461516413220351
-96     -0.9945218953682733370
-95     -0.9961946980917455322
-94     -0.9975640502598242478
-93     -0.9986295347545738739
-92     -0.9993908270190957301
-91     -0.9998476951563912392
-90     -1.000000000000000000
-89     -0.9998476951563912392
-88     -0.9993908270190957301
-87     -0.9986295347545738739
-86     -0.9975640502598242478
-85     -0.9961946980917455322
-84     -0.9945218953682733370
-83     -0.9925461516413220351
-82     -0.9902680687415703152
-81     -0.9876883405951377263
-80     -0.9848077530122080594
-79     -0.9816271834476639537
-78     -0.9781476007338056380
-77     -0.9743700647852352287
-76     -0.9702957262759964724
-75     -0.9659258262890682867
-74     -0.9612616959383188619
-73     -0.9563047559630354813
-72     -0.9510565162951535722
-71     -0.9455185755993168104
-70     -0.9396926207859083841
-69     -0.9335804264972017491
-68     -0.9271838545667874009
-67     -0.9205048534524403275
-66     -0.9135454576426008956
-65     -0.9063077870366499633
-64     -0.8987940462991669929
-63     -0.8910065241883678624
-62     -0.8829475928589269422
-61     -0.8746197071393958004
-60     -0.8660254037844386468
-59     -0.8571673007021122875
-58     -0.8480480961564259706
-57     -0.8386705679454240297
-56     -0.8290375725550416921
-55     -0.8191520442889917897
-54     -0.8090169943749474242
-53     -0.7986355100472928463
-52     -0.7880107536067219568
-51     -0.7771459614569708801
-50     -0.7660444431189780353
-49     -0.7547095802227719981
-48     -0.7431448254773942351
-47     -0.7313537016191704833
-46     -0.7193398003386511395
-45     -0.7071067811865475244
-44     -0.6946583704589972868
-43     -0.6819983600624985005
-42     -0.6691306063588582139
-41     -0.6560590289905072849
-40     -0.6427876096865393264
-39     -0.6293203910498374527
-38     -0.6156614753256582797
-37     -0.6018150231520482799
-36     -0.5877852522924731293
-35     -0.5735764363510460962
-34     -0.5591929034707468302
-33     -0.5446390350150270823
-32     -0.5299192642332049541
-31     -0.5150380749100542101
-30     -0.5000000000000000001
-29     -0.4848096202463370291
-28     -0.4694715627858907760
-27     -0.4539904997395467916
-26     -0.4383711467890774175
-25     -0.4226182617406994363
-24     -0.4067366430758002078
-23     -0.3907311284892737551
-22     -0.3746065934159120355
-21     -0.3583679495453002735
-20     -0.3420201433256687331
-19     -0.3255681544571566688
-18     -0.3090169943749474241
-17     -0.2923717047227367281
-16     -0.2756373558169991857
-15     -0.2588190451025207624
-14     -0.2419218955996677226
-13     -0.2249510543438649981
-12     -0.2079116908177593371
-11     -0.1908089953765448124
-10     -0.1736481776669303489
-9      -0.1564344650402308690
-8      -0.1391731009600654441
-7      -0.1218693434051474811
-6      -0.1045284632676534714
-5      -0.08715574274765817356
-4      -0.06975647374412530078
-3      -0.05233595624294383272
-2      -0.03489949670250097165
-1      -0.01745240643728351282
0       0.0
1       0.01745240643728351282
2       0.03489949670250097165
3       0.05233595624294383272
4       0.06975647374412530078
5       0.08715574274765817356
6       0.1045284632676534714
7       0.1218693434051474811
8       0.1391731009600654441
9       0.1564344650402308690
10      0.1736481776669303489
11      0.1908089953765448124
12      0.2079116908177593371
13      0.2249510543438649981
14      0.2419218955996677226
15      0.2588190451025207624
16      0.2756373558169991857
17      0.2923717047227367281
18      0.3090169943749474241
19      0.3255681544571566688
20      0.3420201433256687331
21      0.3583679495453002735
22      0.3746065934159120355
23      0.3907311284892737551
24      0.4067366430758002078
25      0.4226182617406994363
26      0.4383711467890774175
27      0.4539904997395467916
28      0.4694715627858907760
29      0.4848096202463370291
30      0.5000000000000000001
31      0.5150380749100542101
32      0.5299192642332049541
33      0.5446390350150270823
34      0.5591929034707468302
35      0.5735764363510460962
36      0.5877852522924731293
37      0.6018150231520482799
38      0.6156614753256582797
39      0.6293203910498374527
40      0.6427876096865393264
41      0.6560590289905072849
42      0.6691306063588582139
43      0.6819983600624985005
44      0.6946583704589972868
45      0.7071067811865475244
46      0.7193398003386511395
47      0.7313537016191704833
48      0.7431448254773942351
49      0.7547095802227719981
50      0.7660444431189780353
51      0.7771459614569708801
52      0.7880107536067219568
53      0.7986355100472928463
54      0.8090169943749474242
55      0.8191520442889917897
56      0.8290375725550416921
57      0.8386705679454240297
58      0.8480480961564259706
59      0.8571673007021122875
60      0.8660254037844386468
61      0.8746197071393958004
62      0.8829475928589269422
63      0.8910065241883678624
64      0.8987940462991669929
65      0.9063077870366499633
66      0.9135454576426008956
67      0.9205048534524403275
68      0.9271838545667874009
69      0.9335804264972017491
70      0.9396926207859083841
71      0.9455185755993168104
72      0.9510565162951535722
73      0.9563047559630354813
74      0.9612616959383188619
75      0.9659258262890682867
76      0.9702957262759964724
77      0.9743700647852352287
78      0.9781476007338056380
79      0.9816271834476639537
80      0.9848077530122080594
81      0.9876883405951377263
82      0.9902680687415703152
83      0.9925461516413220351
84      0.9945218953682733370
85      0.9961946980917455322
86      0.9975640502598242478
87      0.9986295347545738739
88      0.9993908270190957301
89      0.9998476951563912392
90      1.000000000000000000
91      0.9998476951563912392
92      0.9993908270190957301
93      0.9986295347545738739
94      0.9975640502598242478
95      0.9961946980917455322
96      0.9945218953682733370
97      0.9925461516413220351
98      0.9902680687415703152
99      0.9876883405951377263
100     0.9848077530122080594
101     0.9816271834476639537
102     0.9781476007338056380
103     0.9743700647852352287
104     0.9702957262759964724
105     0.9659258262890682867
106     0.9612616959383188619
107     0.9563047559630354813
108     0.9510565162951535722
109     0.9455185755993168104
110     0.9396926207859083841
111     0.9335804264972017491
112     0.9271838545667874009
113     0.9205048534524403275
114     0.9135454576426008956
115     0.9063077870366499633
116     0.8987940462991669929
117     0.8910065241883678624
118     0.8829475928589269422
119     0.8746197071393958004
120     0.8660254037844386468
121     0.8571673007021122875
122     0.8480480961564259706
123     0.8386705679454240297
124     0.8290375725550416921
125     0.8191520442889917897
126     0.8090169943749474242
127     0.7986355100472928463
128     0.7880107536067219568
129     0.7771459614569708801
130     0.7660444431189780353
131     0.7547095802227719981
132     0.7431448254773942351
133     0.7313537016191704833
134     0.7193398003386511395
135     0.7071067811865475244
136     0.6946583704589972868
137     0.6819983600624985005
138     0.6691306063588582139
139     0.6560590289905072849
140     0.6427876096865393264
141     0.6293203910498374527
142     0.6156614753256582797
143     0.6018150231520482799
144     0.5877852522924731293
145     0.5735764363510460962
146     0.5591929034707468302
147     0.5446390350150270823
148     0.5299192642332049541
149     0.5150380749100542101
150     0.5000000000000000001
151     0.4848096202463370291
152     0.4694715627858907760
153     0.4539904997395467916
154     0.4383711467890774175
155     0.4226182617406994363
156     0.4067366430758002078
157     0.3907311284892737551
158     0.3746065934159120355
159     0.3583679495453002735
160     0.3420201433256687331
161     0.3255681544571566688
162     0.3090169943749474241
163     0.2923717047227367281
164     0.2756373558169991857
165     0.2588190451025207624
166     0.2419218955996677226
167     0.2249510543438649981
168     0.2079116908177593371
169     0.1908089953765448124
170     0.1736481776669303489
171     0.1564344650402308690
172     0.1391731009600654441
173     0.1218693434051474811
174     0.1045284632676534714
175     0.08715574274765817356
176     0.06975647374412530078
177     0.05233595624294383272
178     0.03489949670250097165
179     0.01745240643728351282
180     0.0
181     -0.01745240643728351282
182     -0.03489949670250097165
183     -0.05233595624294383272
184     -0.06975647374412530078
185     -0.08715574274765817356
186     -0.1045284632676534714
187     -0.1218693434051474811
188     -0.1391731009600654441
189     -0.1564344650402308690
190     -0.1736481776669303489
191     -0.1908089953765448124
192     -0.2079116908177593371
193     -0.2249510543438649981
194     -0.2419218955996677226
195     -0.2588190451025207624
196     -0.2756373558169991857
197     -0.2923717047227367281
198     -0.3090169943749474241
199     -0.3255681544571566688
200     -0.3420201433256687331
201     -0.3583679495453002735
202     -0.3746065934159120355
203     -0.3907311284892737551
204     -0.4067366430758002078
205     -0.4226182617406994363
206     -0.4383711467890774175
207     -0.4539904997395467916
208     -0.4694715627858907760
209     -0.4848096202463370291
210     -0.5000000000000000001
211     -0.5150380749100542101
212     -0.5299192642332049541
213     -0.5446390350150270823
214     -0.5591929034707468302
215     -0.5735764363510460962
216     -0.5877852522924731293
217     -0.6018150231520482799
218     -0.6156614753256582797
219     -0.6293203910498374527
220     -0.6427876096865393264
221     -0.6560590289905072849
222     -0.6691306063588582139
223     -0.6819983600624985005
224     -0.6946583704589972868
225     -0.7071067811865475244
226     -0.7193398003386511395
227     -0.7313537016191704833
228     -0.7431448254773942351
229     -0.7547095802227719981
230     -0.7660444431189780353
231     -0.7771459614569708801
232     -0.7880107536067219568
233     -0.7986355100472928463
234     -0.8090169943749474242
235     -0.8191520442889917897
236     -0.8290375725550416921
237     -0.8386705679454240297
238     -0.8480480961564259706
239     -0.8571673007021122875
240     -0.8660254037844386468
241     -0.8746197071393958004
242     -0.8829475928589269422
243     -0.8910065241883678624
244     -0.8987940462991669929
245     -0.9063077870366499633
246     -0.9135454576426008956
247     -0.9205048534524403275
248     -0.9271838545667874009
249     -0.9335804264972017491
250     -0.9396926207859083841
251     -0.9455185755993168104
252     -0.9510565162951535722
253     -0.9563047559630354813
254     -0.9612616959383188619
255     -0.9659258262890682867
256     -0.9702957262759964724
257     -0.9743700647852352287
258     -0.9781476007338056380
259     -0.9816271834476639537
260     -0.9848077530122080594
261     -0.9876883405951377263
262     -0.9902680687415703152
263     -0.9925461516413220351
264     -0.9945218953682733370
265     -0.9961946980917455322
266     -0.9975640502598242478
267     -0.9986295347545738739
268     -0.9993908270190957301
269     -0.9998476951563912392
270     -1.000000000000000000
271     -0.9998476951563912392
272     -0.9993908270190957301
273     -0.9986295347545738739
274     -0.9975640502598242478
275     -0.9961946980917455322
276     -0.9945218953682733370
277     -0.9925461516413220351
278     -0.9902680687415703152
279     -0.9876883405951377263
280     -0.9848077530122080594
281     -0.9816271834476639537
282     -0.9781476007338056380
283     -0.9743700647852352287
284     -0.9702957262759964724
285     -0.9659258262890682867
286     -0.9612616959383188619
287     -0.9563047559630354813
288     -0.9510565162951535722
289     -0.9455185755993168104
290     -0.9396926207859083841
291     -0.9335804264972017491
292     -0.9271838545667874009
293     -0.9205048534524403275
294     -0.9135454576426008956
295     -0.9063077870366499633
296     -0.8987940462991669929
297     -0.8910065241883678624
298     -0.8829475928589269422
299     -0.8746197071393958004
300     -0.8660254037844386468
301     -0.8571673007021122875
302     -0.8480480961564259706
303     -0.8386705679454240297
304     -0.8290375725550416921
305     -0.8191520442889917897
306     -0.8090169943749474242
307     -0.7986355100472928463
308     -0.7880107536067219568
309     -0.7771459614569708801
310     -0.7660444431189780353
311     -0.7547095802227719981
312     -0.7431448254773942351
313     -0.7313537016191704833
314     -0.7193398003386511395
315     -0.7071067811865475244
316     -0.6946583704589972868
317     -0.6819983600624985005
318     -0.6691306063588582139
319     -0.6560590289905072849
320     -0.6427876096865393264
321     -0.6293203910498374527
322     -0.6156614753256582797
323     -0.6018150231520482799
324     -0.5877852522924731293
325     -0.5735764363510460962
326     -0.5591929034707468302
327     -0.5446390350150270823
328     -0.5299192642332049541
329     -0.5150380749100542101
330     -0.5000000000000000001
331     -0.4848096202463370291
332     -0.4694715627858907760
333     -0.4539904997395467916
334     -0.4383711467890774175
335     -0.4226182617406994363
336     -0.4067366430758002078
337     -0.3907311284892737551
338     -0.3746065934159120355
339     -0.3583679495453002735
340     -0.3420201433256687331
341     -0.3255681544571566688
342     -0.3090169943749474241
343     -0.2923717047227367281
344     -0.2756373558169991857
345     -0.2588190451025207624
346     -0.2419218955996677226
347     -0.2249510543438649981
348     -0.2079116908177593371
349     -0.1908089953765448124
350     -0.1736481776669303489
351     -0.1564344650402308690
352     -0.1391731009600654441
353     -0.1218693434051474811
354     -0.1045284632676534714
355     -0.08715574274765817356
356     -0.06975647374412530078
357     -0.05233595624294383272
358     -0.03489949670250097165
359     -0.01745240643728351282
360     0.0
361     0.01745240643728351282
362     0.03489949670250097165
363     0.05233595624294383272
364     0.06975647374412530078
365     0.08715574274765817356
366     0.1045284632676534714
367     0.1218693434051474811
368     0.1391731009600654441
369     0.1564344650402308690
370     0.1736481776669303489
371     0.1908089953765448124
372     0.2079116908177593371
373     0.2249510543438649981
374     0.2419218955996677226
375     0.2588190451025207624
376     0.2756373558169991857
377     0.2923717047227367281
378     0.3090169943749474241
379     0.3255681544571566688
380     0.3420201433256687331
381     0.3583679495453002735
382     0.3746065934159120355
383     0.3907311284892737551
384     0.4067366430758002078
385     0.4226182617406994363
386     0.4383711467890774175
387     0.4539904997395467916
388     0.4694715627858907760
389     0.4848096202463370291
390     0.5000000000000000001
391     0.5150380749100542101
392     0.5299192642332049541
393     0.5446390350150270823
394     0.5591929034707468302
395     0.5735764363510460962
396     0.5877852522924731293
397     0.6018150231520482799
398     0.6156614753256582797
399     0.6293203910498374527
400     0.6427876096865393264
hit any key
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: 3D Sine Wave [Windows only]

Post by jj2007 »

UEZ wrote:@jj2007: have a look here for some more variants coded by eukalyptus: https://forum.qbasic.at/viewtopic.php?t ... nparabolic
Sieht interessant aus. Some precision loss, but on my machine, eukalyptus' "fast" variant is 4.5 faster than FPU fsin. The packed version even arrives at a factor 5.3 (but it is limited in its applications, of course).
In contrast, Sinus() runs at 6.7 times the fsin speed, and full REAL10 precision.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: 3D Sine Wave [Windows only]

Post by UEZ »

dodicat wrote:Nice UEZ.
I was inspired.
Well done dodicat, same rotating grid but different coding approach, at least for me. If I reading your code there are a lot of things to learn for me.

:-)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: 3D Sine Wave [Windows only]

Post by dodicat »

Thanks for testing UEZ/srvaldez.
Regarding the passage to the Moon using the rough inverse squareroot, I reckon it would be safe enough tonight If you left about now.
But I would not recommend it for a daytime launch.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: 3D Sine Wave [Windows only]

Post by UEZ »

Any clue why the Xiaolin Wu’s line-drawing algorithm (http://rosettacode.org/wiki/Xiaolin_Wu' ... #FreeBASIC) is not working properly?

I want to add antialiasing for better looking but no journey to the moon this time. ;-)

Code: Select all

'coded by UEZ build 2017-12-31, thanks to eukalyptus for the Sin / Cos ASM functions
#include "fbgfx.bi"
#include "windows.bi"

Using FB

Const As ULong iW = 1200, iH = 800, iWh = iW \ 2, iHh = iH \ 2
Const As Single fPi = ACos(-1), fRad = fPi / 180
Dim Shared As Single x1, y1, z1, x2, y2, z2, fZoom = 1.5
Dim Shared As Ulong wu_color

#Macro Round(x)
	Long((x) + .5)      ' Round off
#EndMacro
 
#Macro Frac2(x)
' 1 - Frac(x)    ' seems to give problems for very small x
	IIf(1 - Frac(x) >= 1, 1, 1 - Frac(x))
#EndMacro
 
#Macro _SetPixel(x, y , c)
' use the alpha channel to set the amount of color
	PSet(x,y), (CUbyte(c * 255) Shl 24) Or  wu_color
#EndMacro


Declare Function _Cos6th(fX As Double) As Double
Declare Function _Sin6th(fX As Double) As Double
Declare Function _ASM_Sqr(n As Single) As Single
Declare Function Dist(x1 As Single, y1 As Single, x2 As Single, y2 As Single) As Single
Declare Sub _3Dto2D2(fX As Single, fY As Single, fZ As Single, fRotX As Single, fRotY As Single, fRotZ As Single, ByRef __fXPos As Single, ByRef __fYPos As Single, ByRef __fZPos As Single, fZDeepCorrection as Single = 1024.0)
Declare Sub drawline(x0 As Single, y0 As Single, x1 As Single, y1 As Single, col As UInteger = RGB(255,255,255))

ScreenRes iW, iH, 32, , GFX_ALPHA_PRIMITIVES

Dim As String sTitle = "3D Sine Wave v1.4 / FPS: "
WindowTitle sTitle

Dim as ulong iDW, iDH
ScreenControl GET_DESKTOP_SIZE, iDW, iDH
Dim tWorkingArea As RECT
SystemParametersInfo(SPI_GETWORKAREA, null, @tWorkingArea, null)
ScreenControl SET_WINDOW_POS, (tWorkingArea.Right - iW) \ 2, (iDH - tWorkingArea.Bottom) \ 2

Color (, RGB(&h40, &h40, &h40))

Randomize(-1, 1)
Dim as ulong iPoints = 10 'iPoints^2 points will be generated
Dim As Single 	fEuler = 2.7182818284, fSpace = 50.0, fSpace2 = fSpace / 2, iPoints2 = iPoints / 2, _
				 	fRotX = -360 + Rnd() * 720.0, fRotY = -360 + Rnd() * 720.0, fRotZ = -360 + Rnd() * 720.0, fPower = 40.0, _
					fRx = -0.5 + Rnd() * 1.0, fRy = -0.5 + Rnd() * 1.0, fRz = -0.5 + Rnd() * 1.0, _
					c = 0, x, y, fDistance, fPowerColor = 1.0	
               			
Dim As Single aCoords(iPoints ^ 2, 3)
Dim As UByte r, g, b 
Dim As Integer iMWheel = 0
Dim as Boolean bRotate = True
Dim As ULong iFPS = 0
Dim evt As EVENT
Dim As Double t, fTimer = Timer

Do
   screenlock
	Cls
	
   'generate grid coordinates
   c = 0
	For x = 0 To iPoints - 1
		For y = 0 To iPoints - 1
			aCoords(c, 0) = fSpace * x + fSpace2 - fSpace * iPoints2
			aCoords(c, 1) = fSpace * y + fSpace2 - fSpace * iPoints2
			fDistance = Dist(0, 0, aCoords(c, 0), aCoords(c, 1))
			aCoords(c, 2) = fEuler ^ (-fDistance / 180) * _Sin6th((fDistance / 10) - t) * fPower 'generates the waves
			c += 1
			t += 0.00075
		Next
	Next
   
   'update calculated grid coordinates and draw grid to screen
	For x = 0 To iPoints - 2
		For y = 0 To iPoints - 2
			r = (&h80 + aCoords(y + iPoints * x, 0) * fPowerColor)
			g = (&h80 + aCoords(y + iPoints * x, 1) * fPowerColor)
			b = (&h80 + aCoords(y + iPoints * x, 2) * fPowerColor)
			_3Dto2D2(aCoords(y + iPoints * x, 0), aCoords(y + iPoints * x, 1), aCoords(y + iPoints * x, 2), fRotX, fRotY, fRotZ, x1, y1, z1)
			_3Dto2D2(aCoords(y + iPoints * (x + 1), 0), aCoords(y + iPoints * (x + 1), 1), aCoords(y + iPoints * (x + 1), 2), fRotX, fRotY, fRotZ, x2, y2, z2)
			drawline (iWh + x1 * fZoom, iHh + y1 * fZoom, iWh + x2 * fZoom, iHh + y2 * fZoom, RGB(r, g XOR r, b xor g))
			_3Dto2D2(aCoords((y + 1) + iPoints * x, 0), aCoords((y + 1) + iPoints * x, 1), aCoords((y + 1) + iPoints * x, 2), fRotX, fRotY, fRotZ, x2, y2, z2)
			drawline (iWh + x1 * fZoom, iHh + y1 * fZoom, iWh + x2 * fZoom, iHh + y2 * fZoom, RGB(r, r XOR g, g xor b))
		Next
	Next
   
	screenunlock
	
   If bRotate Then
      fRotX += fRx
      fRotY += fRy
      fRotZ += fRz
   End If
		
	
	If Timer - fTimer > 0.99 Then
		WindowTitle sTitle & iFPS
		iFPS = 0
		fTimer = Timer
	Else
		iFPS += 1
	EndIf
   
   If (ScreenEvent(@evt)) Then
      Select Case evt.type
      	Case EVENT_MOUSE_WHEEL
    			If evt.z > iMWheel Then
    				'up
    				fZoom += 0.25
    				If fZoom > 20 Then fZoom = 20
    			Else
    				'down
    				fZoom -= 0.25
    				If fZoom < 0.25 Then fZoom = 0.25
    			EndIf
    			iMWheel = evt.z
         Case EVENT_MOUSE_BUTTON_PRESS
            If evt.button = 1 Then
               bRotate = False
            End If
         Case EVENT_MOUSE_BUTTON_RELEASE
            If evt.button = 1 Then
               bRotate = True
            End If            
      End Select
   EndIf
	Sleep(10, 1)
Loop Until ((InKey = Chr(27)) Or (evt.Type = EVENT_WINDOW_CLOSE))

End

Function Dist(x1 As Single, y1 As Single, x2 As Single, y2 As Single) As Single
	Return _ASM_Sqr((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
End Function

Sub _3Dto2D2(fX As Single, fY As Single, fZ As Single, _
            fRotX As Single, fRotY As Single, fRotZ As Single, _
				ByRef __fXPos As Single, ByRef __fYPos As Single, ByRef __fZPos As Single, fZDeepCorrection as Single = 1024.0)
	
   Dim as Single _
      fXr = fRotX * fRad, fYr = fRotY * fRad, fZr = fRotZ * fRad, _
      fCosRotX = _Cos6th(fXr), fSinRotX = _Sin6th(fXr), _
      fCosRotY = _Cos6th(fYr), fSinRotY = _Sin6th(fYr), _
      fCosRotZ = _Cos6th(fZr), fSinRotZ = _Sin6th(fZr), _
      f1 = fCosRotY * fX, _
      f2 = fSinRotX * fY, _
      f3 = fCosRotX * fZ, _
      f4 = fCosRotX * fY, _
      f5 = fSinRotX * fZ, _
      f6 = f1 - fSinRotY * (f2 + f3), _
      f7 = f4 - f5, _
      fXPos = (fCosRotZ * f6 - fSinRotZ * (f7)), _
      fYPos = (fSinRotZ * f6 + fCosRotZ * (f7)), _
      fZPos = (fSinRotY * fX + fCosRotY * (f2 + f3)), _
      fZPerspCorrection = 1 / (fZPos / fZDeepCorrection + 1)
	__fXPos = fXPos * fZPerspCorrection
	__fYPos = fYPos * fZPerspCorrection
	__fZPos = fZPos
End Sub

Function _ASM_Sqr(n As Single) As Single
	Asm
		rsqrtss xmm0, [n]
		mulss   xmm0, [n]
		movss   [function], xmm0		
	End Asm
End Function

Function _Sin6th(fX As Double) As Double 
   Asm 
      jmp _Sin6th_Start 
         _Sin6th_Mul: .double 683565275.57643158 
         _Sin6th_Div: .double -0.0000000061763971109087229 
         _Sin6th_Rnd: .double 6755399441055744.0 
       
      _Sin6th_Start: 
         movq xmm0, [fX] 
         mulsd xmm0, [_Sin6th_Mul] 
         addsd xmm0, [_Sin6th_Rnd] 
         movd ebx, xmm0 
    
         lea  eax, [ebx*2+0x80000000] 
         sar  eax, 2 
         imul eax 
         sar  ebx, 31 
         lea  eax, [edx*2-0x70000000] 
         lea  ecx, [edx*8+edx-0x24000000] 
         imul edx 
         xor  ecx, ebx 
         lea  eax, [edx*8+edx+0x44A00000] 
         imul ecx 
          
         cvtsi2sd xmm0, edx 
         mulsd xmm0, [_Sin6th_Div] 
         movq [Function], xmm0 
   End Asm 
End Function

Function _Cos6th(fX As Double) As Double 
   Asm 
      jmp _Cos6th_Start 
         _Cos6th_Mul: .double 683565275.57643158 
         _Cos6th_Div: .double -0.0000000061763971109087229 
         _Cos6th_Rnd: .double 6755399441055744.0 
       
      _Cos6th_Start: 
         movq xmm0, [fX] 
         mulsd xmm0, [_Cos6th_Mul] 
         addsd xmm0, [_Cos6th_Rnd] 
         movd ebx, xmm0 
          
         add ebx, 0x40000000 'SinToCos 
    
         lea  eax, [ebx*2+0x80000000] 
         sar  eax, 2 
         imul eax 
         sar  ebx, 31 
         lea  eax, [edx*2-0x70000000] 
         lea  ecx, [edx*8+edx-0x24000000] 
         imul edx 
         xor  ecx, ebx 
         lea  eax, [edx*8+edx+0x44A00000] 
         imul ecx 
          
         cvtsi2sd xmm0, edx 
         mulsd xmm0, [_Cos6th_Div] 
         movq [Function], xmm0 
   End Asm 
End Function

Sub drawline(x0 As Single, y0 As Single, x1 As Single, y1 As Single, col As UInteger = RGB(255,255,255))
	
	wu_color = col And &HFFFFFF ' strip off the alpha channel information
	
	Dim As Single gradient
	Dim As Single xend, yend, xgap, intery
	Dim As Ulong xpxl1, ypxl1, xpxl2, ypxl2, x, y
	Dim As Byte steep = Abs(y1 - y0) > Abs(x1 - x0) ' boolean
	
	If steep Then
		Swap x0, y0
		Swap x1, y1
	End If
	
	If x0 > x1 Then
		Swap x0, x1
		Swap y0, y1
	End If
	
	gradient = (y1 - y0) / (x1 - x0)
	
	' first endpoint
	' xend = Round(x0)
	xend = Int(x0)
	yend = y0 + gradient * (xend - x0)
	xgap = Frac2(x0 + .5)
	xpxl1 = xend              ' this will be used in the main loop
	ypxl1 = Int(yend)
	If steep Then
		_SetPixel(ypxl1,   xpxl1, Frac2(yend) * xgap)
		_SetPixel(ypxl1+1, xpxl1,  Frac(yend) * xgap)
	Else
		_SetPixel(xpxl1, ypxl1,   Frac2(yend) * xgap)
		_SetPixel(xpxl1, ypxl1+1,  Frac(yend) * xgap)
	End If
	intery = yend + gradient  ' first y-intersecction for the main loop
	
	' handle second endpoint
	' xend = Round(x1)
	xend = Int(x1)
	yend = y1 + gradient * (xend - x1)
	xgap = Frac(x1 + .5)
	xpxl2 = xend              ' this will be used in the main loop
	ypxl2 = Int(yend)
	If steep Then
		_SetPixel(ypxl2,   xpxl2, Frac2(yend) * xgap)
		_SetPixel(ypxl2+1, xpxl2,  Frac(yend) * xgap)
	Else
		_SetPixel(xpxl2, ypxl2,   Frac2(yend) * xgap)
		_SetPixel(xpxl2, ypxl2+1,  Frac(yend) * xgap)
	End If
	
	' main loop
	If steep Then
	  For y = xpxl1 + 1 To xpxl2 - 1
		_SetPixel(Int(intery),   y, Frac2(intery))
		_SetPixel(Int(intery)+1, y,  Frac(intery))
		intery = intery + gradient
	  Next
	Else
	  For x = xpxl1 + 1 To xpxl2 - 1
			_SetPixel(x, Int(intery),   Frac2(intery))
			_SetPixel(x, Int(intery)+1,  Frac(intery))
			intery = intery + gradient
	  Next
	End If

End Sub
For me after some rotations or when I increase the iPoints variable the GUI will freeze.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: 3D Sine Wave [Windows only]

Post by dodicat »

It crashes here also.
The problem is definitely in the drawline routine.
I cannot pin point the error.
Here is another Bresenham type line.
Plotthick carries 4 pixels along a line (False antialiasing ).
Plotthin only one pixel carried.
I get about 80 fps here with plotthick.
You can speed it up by getting rid of the ifs in the plot.
I notice your colours flip a bit!
Also I notice it runs well on 64 bit, but not -O3 optimisations.
OK with -O2 though

Code: Select all



'coded by UEZ build 2017-12-31, thanks to eukalyptus for the Sin / Cos ASM functions
#include "fbgfx.bi"
#include "windows.bi"


'=========================
Type screendata
    As Integer w,h,depth,pitch
    As Any Pointer row
End Type 

#macro plotthick
For x As long=0 To 1
            For y As long=0 To 1
                    If sd.depth=8  Then ppset8((x1+x),(y1+y),col)
                    If sd.depth=16 Then ppset16((x1+x),(y1+y),col)
                    If sd.depth=32 Then ppset32((x1+x),(y1+y),col)
            Next y
        Next x
#endmacro

#macro plotthin
If sd.depth=8  Then ppset8((x1),(y1),col)
If sd.depth=16 Then ppset16((x1),(y1),col)
If sd.depth=32 Then ppset32((x1),(y1),col)
#endmacro

Sub drawline2(sd As screendata,x1 As long,y1 As long,x2 As long,y2 As long,col As Ulong)
    'line(x1,y1)-(x2,y2),col 'tester
    'exit sub
   ' col = col And &HFFFFFF '???
    #define ppset32(_x,_y,colour)    *cptr(ulong ptr,sd.row+ (_y)*sd.pitch+ (_x) shl 2)  =(colour)
    #define ppset8(_x,_y,colour)     *cptr(ubyte ptr,(sd.row+(_y)* sd.pitch+(_x)))       =(colour)
    #define ppset16(_x,_y,colour)    *cptr(ushort ptr,(sd.row+(_y)* sd.pitch+(_x) shl 1))=(colour)
    #define onscreen ((x1)>=0) And ((x1)<(sd.w-1)) And ((y1)>=0) And ((y1)<(sd.h-1))
    Var dx=Abs(x2-x1),dy=Abs(y2-y1),sx=Sgn(x2-x1),sy=Sgn(y2-y1)
    dim as long e
    If dx<dy Then  e=dx\2 Else e=dy\2
    Do
                If onscreen Then
                 plotthick  '<---      or plotthin 
                End If
        
        If x1 = x2 Then If y1 = y2 Then Exit Do
        If dx > dy Then
            x1 += sx : e -= dy : If e < 0 Then e += dx : y1 += sy
        Else
            y1 += sy : e -= dx : If e < 0 Then e += dy : x1 += sx
        End If
    Loop
End Sub
'========================
Using FB

Const As ULong iW = 1200, iH = 800, iWh = iW \ 2, iHh = iH \ 2
Const As Single fPi = ACos(-1), fRad = fPi / 180
Dim Shared As Single x1, y1, z1, x2, y2, z2, fZoom = 1.5
Dim Shared As Ulong wu_color




Declare Function _Cos6th(fX As Double) As Double
Declare Function _Sin6th(fX As Double) As Double
Declare Function _ASM_Sqr(n As Single) As Single
Declare Function Dist(x1 As Single, y1 As Single, x2 As Single, y2 As Single) As Single
Declare Sub _3Dto2D2(fX As Single, fY As Single, fZ As Single, fRotX As Single, fRotY As Single, fRotZ As Single, ByRef __fXPos As Single, ByRef __fYPos As Single, ByRef __fZPos As Single, fZDeepCorrection as Single = 1024.0)
Declare Sub drawline(x0 As Single, y0 As Single, x1 As Single, y1 As Single, col As UInteger = RGB(255,255,255))

ScreenRes iW, iH, 32, , GFX_ALPHA_PRIMITIVES
'========================
Dim As screendata S
With S
    Screeninfo .w,.h,.depth,,.pitch
    .row=Screenptr
End With
'======================
Dim As String sTitle = "3D Sine Wave v1.4 / FPS: "
WindowTitle sTitle

Dim as integer iDW, iDH
ScreenControl GET_DESKTOP_SIZE, iDW, iDH
Dim tWorkingArea As RECT
SystemParametersInfo(SPI_GETWORKAREA, null, @tWorkingArea, null)
ScreenControl SET_WINDOW_POS, (tWorkingArea.Right - iW) \ 2, (iDH - tWorkingArea.Bottom) \ 2

Color (, RGB(&h40, &h40, &h40))

Randomize(-1, 1)
Dim as ulong iPoints = 10 'iPoints^2 points will be generated
Dim As Single    fEuler = 2.7182818284, fSpace = 50.0, fSpace2 = fSpace / 2, iPoints2 = iPoints / 2, _
                fRotX = -360 + Rnd() * 720.0, fRotY = -360 + Rnd() * 720.0, fRotZ = -360 + Rnd() * 720.0, fPower = 40.0, _
               fRx = -0.5 + Rnd() * 1.0, fRy = -0.5 + Rnd() * 1.0, fRz = -0.5 + Rnd() * 1.0, _
               c = 0, x, y, fDistance, fPowerColor = 1.0   
                        
Dim As Single aCoords(iPoints ^ 2, 3)
Dim As UByte r, g, b 
Dim As Integer iMWheel = 0
Dim as Boolean bRotate = True
Dim As ULong iFPS = 0
Dim evt As EVENT
Dim As Double t, fTimer = Timer

Do
   screenlock
   Cls
   
   'generate grid coordinates
   c = 0
   For x = 0 To iPoints - 1
      For y = 0 To iPoints - 1
         aCoords(c, 0) = fSpace * x + fSpace2 - fSpace * iPoints2
         aCoords(c, 1) = fSpace * y + fSpace2 - fSpace * iPoints2
         fDistance = Dist(0, 0, aCoords(c, 0), aCoords(c, 1))
         aCoords(c, 2) = fEuler ^ (-fDistance / 180) * _Sin6th((fDistance / 10) - t) * fPower 'generates the waves
         c += 1
         t += 0.00075
      Next
   Next
   
   'update calculated grid coordinates and draw grid to screen
   For x = 0 To iPoints - 2
      For y = 0 To iPoints - 2
         r = (&h80 + aCoords(y + iPoints * x, 0) * fPowerColor)
         g = (&h80 + aCoords(y + iPoints * x, 1) * fPowerColor)
         b = (&h80 + aCoords(y + iPoints * x, 2) * fPowerColor)
         _3Dto2D2(aCoords(y + iPoints * x, 0), aCoords(y + iPoints * x, 1), aCoords(y + iPoints * x, 2), fRotX, fRotY, fRotZ, x1, y1, z1)
         _3Dto2D2(aCoords(y + iPoints * (x + 1), 0), aCoords(y + iPoints * (x + 1), 1), aCoords(y + iPoints * (x + 1), 2), fRotX, fRotY, fRotZ, x2, y2, z2)
         drawline2 (s,iWh + x1 * fZoom, iHh + y1 * fZoom, iWh + x2 * fZoom, iHh + y2 * fZoom, RGB(r, g XOR r, b xor g))
         _3Dto2D2(aCoords((y + 1) + iPoints * x, 0), aCoords((y + 1) + iPoints * x, 1), aCoords((y + 1) + iPoints * x, 2), fRotX, fRotY, fRotZ, x2, y2, z2)
         drawline2 (s,iWh + x1 * fZoom, iHh + y1 * fZoom, iWh + x2 * fZoom, iHh + y2 * fZoom, RGB(r, r XOR g, g xor b))
      Next
   Next
   
   screenunlock
   
   If bRotate Then
      fRotX += fRx
      fRotY += fRy
      fRotZ += fRz
   End If
      
   
   If Timer - fTimer > 0.99 Then
      WindowTitle sTitle & iFPS
      iFPS = 0
      fTimer = Timer
   Else
      iFPS += 1
   EndIf
   
   If (ScreenEvent(@evt)) Then
      Select Case evt.type
         Case EVENT_MOUSE_WHEEL
             If evt.z > iMWheel Then
                'up
                fZoom += 0.25
                If fZoom > 20 Then fZoom = 20
             Else
                'down
                fZoom -= 0.25
                If fZoom < 0.25 Then fZoom = 0.25
             EndIf
             iMWheel = evt.z
         Case EVENT_MOUSE_BUTTON_PRESS
            If evt.button = 1 Then
               bRotate = False
            End If
         Case EVENT_MOUSE_BUTTON_RELEASE
            If evt.button = 1 Then
               bRotate = True
            End If            
      End Select
   EndIf
   Sleep(10, 1)
Loop Until ((InKey = Chr(27)) Or (evt.Type = EVENT_WINDOW_CLOSE))

End

Function Dist(x1 As Single, y1 As Single, x2 As Single, y2 As Single) As Single
   Return _ASM_Sqr((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
End Function

Sub _3Dto2D2(fX As Single, fY As Single, fZ As Single, _
            fRotX As Single, fRotY As Single, fRotZ As Single, _
            ByRef __fXPos As Single, ByRef __fYPos As Single, ByRef __fZPos As Single, fZDeepCorrection as Single = 1024.0)
   
   Dim as Single _
      fXr = fRotX * fRad, fYr = fRotY * fRad, fZr = fRotZ * fRad, _
      fCosRotX = _Cos6th(fXr), fSinRotX = _Sin6th(fXr), _
      fCosRotY = _Cos6th(fYr), fSinRotY = _Sin6th(fYr), _
      fCosRotZ = _Cos6th(fZr), fSinRotZ = _Sin6th(fZr), _
      f1 = fCosRotY * fX, _
      f2 = fSinRotX * fY, _
      f3 = fCosRotX * fZ, _
      f4 = fCosRotX * fY, _
      f5 = fSinRotX * fZ, _
      f6 = f1 - fSinRotY * (f2 + f3), _
      f7 = f4 - f5, _
      fXPos = (fCosRotZ * f6 - fSinRotZ * (f7)), _
      fYPos = (fSinRotZ * f6 + fCosRotZ * (f7)), _
      fZPos = (fSinRotY * fX + fCosRotY * (f2 + f3)), _
      fZPerspCorrection = 1 / (fZPos / fZDeepCorrection + 1)
   __fXPos = fXPos * fZPerspCorrection
   __fYPos = fYPos * fZPerspCorrection
   __fZPos = fZPos
End Sub

Function _ASM_Sqr(n As Single) As Single
   Asm
      rsqrtss xmm0, [n]
      mulss   xmm0, [n]
      movss   [function], xmm0      
   End Asm
End Function

Function _Sin6th(fX As Double) As Double 
   Asm 
      jmp _Sin6th_Start 
         _Sin6th_Mul: .double 683565275.57643158 
         _Sin6th_Div: .double -0.0000000061763971109087229 
         _Sin6th_Rnd: .double 6755399441055744.0 
       
      _Sin6th_Start: 
         movq xmm0, [fX] 
         mulsd xmm0, [_Sin6th_Mul] 
         addsd xmm0, [_Sin6th_Rnd] 
         movd ebx, xmm0 
    
         lea  eax, [ebx*2+0x80000000] 
         sar  eax, 2 
         imul eax 
         sar  ebx, 31 
         lea  eax, [edx*2-0x70000000] 
         lea  ecx, [edx*8+edx-0x24000000] 
         imul edx 
         xor  ecx, ebx 
         lea  eax, [edx*8+edx+0x44A00000] 
         imul ecx 
          
         cvtsi2sd xmm0, edx 
         mulsd xmm0, [_Sin6th_Div] 
         movq [Function], xmm0 
   End Asm 
End Function

Function _Cos6th(fX As Double) As Double 
   Asm 
      jmp _Cos6th_Start 
         _Cos6th_Mul: .double 683565275.57643158 
         _Cos6th_Div: .double -0.0000000061763971109087229 
         _Cos6th_Rnd: .double 6755399441055744.0 
       
      _Cos6th_Start: 
         movq xmm0, [fX] 
         mulsd xmm0, [_Cos6th_Mul] 
         addsd xmm0, [_Cos6th_Rnd] 
         movd ebx, xmm0 
          
         add ebx, 0x40000000 'SinToCos 
    
         lea  eax, [ebx*2+0x80000000] 
         sar  eax, 2 
         imul eax 
         sar  ebx, 31 
         lea  eax, [edx*2-0x70000000] 
         lea  ecx, [edx*8+edx-0x24000000] 
         imul edx 
         xor  ecx, ebx 
         lea  eax, [edx*8+edx+0x44A00000] 
         imul ecx 
          
         cvtsi2sd xmm0, edx 
         mulsd xmm0, [_Cos6th_Div] 
         movq [Function], xmm0 
   End Asm 
End Function

sleep

 
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: 3D Sine Wave [Windows only]

Post by UEZ »

dodicat wrote:It crashes here also.
The problem is definitely in the drawline routine.
I cannot pin point the error.
Here is another Bresenham type line.
Plotthick carries 4 pixels along a line (False antialiasing ).
Plotthin only one pixel carried.
I get about 80 fps here with plotthick.
You can speed it up by getting rid of the ifs in the plot.
I notice your colours flip a bit!
Also I notice it runs well on 64 bit, but not -O3 optimisations.
OK with -O2 though
It works properly without crashing. I will adapt it to the original code from post #1 and update the code later.

Yes, the code is little bit truncated to show the crash only and thus the fPowerColor value wasn't set properly for this setting (color flipping).

Btw, my 2nd question I wanted to raise is how to change the line witdh -> also answered! :-)

Thanks dodicat!
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: 3D Sine Wave [Windows only]

Post by deltarho[1859] »

@UEZ

Dump all the labels and replace with local/temporary labels that we discussed the other day. -02 to -0fast gives me an increase in fps of about 25%.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: 3D Sine Wave [Windows only]

Post by UEZ »

deltarho[1859] wrote:@UEZ

Dump all the labels and replace with local/temporary labels that we discussed the other day. -02 to -0fast gives me an increase in fps of about 25%.
Thanks! I updated the code from post #1.
Post Reply