Hmm, i tried 2 examples from
http://wiki.tcl.tk/10857 and
http://www.devmaster.net/articles/raytracing/
Both were failing to work
here is some code that i have that i did from the devmaster tutorial
Code: Select all
#ScreenWidth=320
#ScreenHieght=240
#Tolarance=0.0001
Structure xyz
x.f
y.f
z.f
EndStructure
Structure Sphere
center.xyz
radius.f
EndStructure
Procedure VectorNormalize(*this.xyz)
m.f = Sqr(*this\x * *this\x + *this\y * *this\y + *this\z * *this\z)
If m < =#Tolarance
m = 1
EndIf
*this\x = *this\x / m
*this\y = *this\y / m
*this\z = *this\z / m
If *this\x < #Tolarance
*this\x = 0
EndIf
If *this\y < #Tolarance
*this\y = 0
EndIf
If *this\z < #Tolarance
*this\z = 0
EndIf
EndProcedure
Procedure.f TestSphere(*Direction.xyz,*Origin.xyz,*Sphere.Sphere)
dirx=*Direction\x
diry=*Direction\y
dirz=*Direction\z
Originx=*Origin\x
Originy=*Origin\y
Originz=*Origin\z
Spherex=*Sphere\center\x
Spherey=*Sphere\center\y
Spherez=*Sphere\center\z
;a.f=Pow(*Direction\x,2)+Pow(*Direction\y,2)+Pow(*Direction\z,2)
;b.f=2*(*Direction\x*(*Origin\x-*Sphere\center\x)+*Direction\y*(*Origin\y-*Sphere\center\y)+*Direction\z*(*Origin\z-*Sphere\center\z))
;c.f=Pow(*Origin\x-*Sphere\center\x,2)+Pow(*Origin\y-*Sphere\center\y,2)+Pow(*Origin\z-*Sphere\center\z,2)
a.f=Pow(dirx,2)+Pow(diry,2)+Pow(dirz,2)
b.f=2*(dirx*(Originx-Spherex)+diry*(Originy-Spherey)+dirz*(Originz-Spherez))
c.f=Pow(Originx-Spherex,2)+Pow(Originy-Spherey,2)+Pow(Originz-Spherez,2)
D.f=Pow(b,2)-4*a*c
If D>0 ;hit the sphere
t.f=(-b-Sqr(D))/(2*a)
ElseIf D=0 ;glazing the sphere
t.f=-1
ElseIf D<0 ;missed completely
t.f=-2
EndIf
ProcedureReturn t
EndProcedure
Origin.xyz
Direction.xyz
Test.Sphere
Origin\x=0
Origin\y=0
Origin\z=-256
InitSprite()
OpenWindow(0,0,0,#ScreenWidth,#ScreenHieght,#PB_Window_ScreenCentered|#PB_Window_SystemMenu,"Ray Test")
StartDrawing(WindowOutput())
Repeat
For y=-#ScreenHieght/2 To (#ScreenHieght/2)-1 Step 1
For x=-#ScreenWidth/2 To (#ScreenWidth/2)-1 Step 1
Direction\x=x
Direction\y=y
Direction\z=256
VectorNormalize(Direction)
t.f=TestSphere(Direction,Origin,Test)
If t>0
posx=x+(#ScreenWidth/2)
posy=y+(#ScreenHieght/2)
Plot(posx,posy,RGB(255,0,0))
ElseIf t=-1
posx=x+(#ScreenWidth/2)
posy=y+(#ScreenHieght/2)
Plot(posx,posy,RGB(0,255,0))
ElseIf t=-2
posx=x+(#ScreenWidth/2)
posy=y+(#ScreenHieght/2)
Plot(posx,posy,RGB(0,0,255))
Else
posx=x+(#ScreenWidth/2)
posy=y+(#ScreenHieght/2)
Plot(posx,posy,RGB(255,255,255))
EndIf
Next x
Next y
Until WindowEvent()=#PB_Event_CloseWindow
oddly window rendering gives a bit of the clear screen color o_O
anyways, most of the main code is from the document the vector procedure is from another program i did while back
the problem i see with the code is that radius it not account for in the equasion im wondering where does it go? the tutorial only has a small part at the end of the sphere intersection about it but thats for the normal but that is only for lighting calulcations.
the other tutoral with the code when i converted it returned "behind origin" which i thought i converted wrong, because the code is very odd
im wondering whats missing...
edit : changed the code to fix a few dumb problems but it still returns -2 (glazing the sphere) until the right side of the screen where it returns "missed completely"