A raytracer problem :S

General FreeBASIC programming questions.
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

A raytracer problem :S

Post by Hezad »

Hey everyone :) I permit myself to quote the post I created on DBF interactive. I have a problem with a raytracer I started to code :

-- start of quote --
Hey everyone :)

I'm currently working on a raytracer engine. I already coded one which worked but it was far from well designed. So I started a new one to handle scene better.

The problem is it doesn't raytrace the only sphere I added to the scene. I'm totally stuck right now, I looked everywhere in the code and I don't get why and where it don't work. Here is the code :

Code: Select all

''
'' Raytracer
''

#include "vector.bi"

Const PRECISION = .1
Const MAX_RAY_LENGTH = 100

Const MAX_PRIMITIVES = 20
Const MAX_LIGHTS     = 10

Const PRIM_SPHERE = 1
Const PRIM_PLANE  = 2

Type ColorT
	as single r,g,b
End Type	

Type MaterialT
	as ColorT Diffuse,BaseColor
End type	

Type Ray
	as vector O,d
	
	as single x,y,z,t
	
	Declare Sub Update()
	Declare Function Lengthpow2() as single
End Type

Function Ray.Lengthpow2() as single
	return (this.x-this.O.x)*(this.x-this.O.x) + (this.y-this.O.y)*(this.y-this.O.y) + (this.z-this.O.z)*(this.z-this.O.z)
End Function
	

Sub Ray.Update()
	
	this.x = this.O.x + this.d.x*this.t
	this.y = this.O.y + this.d.y*this.t
	this.z = this.O.z + this.d.z*this.t
	
	this.t += PRECISION
	
End Sub

Type Primitive
	as integer TypeFlag
	
	as MaterialT Material
	
	'' for planes
	as single A,B,C,D
	
	'' for spheres
	as single R, x,y,z
	
	
	'' functions
	Declare function GetNormal(CRay as Ray) as Vector
	Declare function GetIntersection(CRay as Ray) as integer
End type

Function Primitive.GetIntersection(CRay as Ray) as integer

	Select case This.TypeFlag
	Case PRIM_SPHERE
	
		if (this.x - CRay.x)*(this.x - CRay.x) + (this.y - CRay.y)*(this.y - CRay.y) + (this.z - CRay.z)*(this.z - CRay.z) <= R*R + PRECISION then
			return 1
		else
			return 0
		end if

	Case PRIM_PLANE
		
		if (this.A*CRay.x + this.B*CRay.y + this.C*CRay.z - this.D < PRECISION) and (this.A*CRay.x + this.B*CRay.y + this.C*CRay.z - this.D > -PRECISION) then
			return 1
		else
			return 0
		end if
	
	End Select
End Function

Function Primitive.GetNormal(CRay as Ray) as Vector
	
	static as Vector normal
	
	Select case This.TypeFlag
	Case PRIM_SPHERE
	
		normal.x = CRay.x - this.x
		normal.y = CRay.y - this.y
		normal.z = CRay.z - this.z
		
		Normalize(normal)
		
		return normal

	Case PRIM_PLANE
		
		normal.x = -A
		normal.y = -B
		normal.z = -C
		
		Normalize(Normal)
		
		return normal
	
	End Select
End Function

Type LightT
	as single x,y,z
	as ColorT l_color
end type	

Type SceneT
	
	as Primitive ptr s_Primitive
	as integer nb_primitives
	
	as LightT ptr s_light
	as integer nb_lights
	
	Declare Constructor()
	Declare Destructor()
	
	Declare Sub Add_Sphere(cx as single, cy as single,cz as single, cR as single, Mat as MaterialT)
	Declare Sub Add_Plane (cA as single, cB as single,cC as single, cD as single, Mat as MaterialT)
	
	Declare Sub Add_Light(cx as single, cy as single, cz as single,col as ColorT)
	
End Type

Sub SceneT.Add_Light(cx as single, cy as single, cz as single,col as ColorT)

	if this.nb_lights>=MAX_LIGHTS then exit sub
	
	this.s_light[this.nb_lights].x = cx
	this.s_light[this.nb_lights].y = cy
	this.s_light[this.nb_lights].z = cz
	
	this.s_light[this.nb_lights].l_color = col
	
	this.nb_lights += 1
	
end sub
	

Sub SceneT.Add_Sphere(cx as single, cy as single,cz as single, cR as single, Mat as MaterialT)
	
	If this.nb_primitives >= MAX_PRIMITIVES then exit sub
	
	this.s_primitive[this.nb_primitives].typeflag = prim_sphere
	
	this.s_primitive[this.nb_primitives].x = cx
	this.s_primitive[this.nb_primitives].y = cy
	this.s_primitive[this.nb_primitives].z = cz
	this.s_primitive[this.nb_primitives].R = cR
	
	this.s_primitive[this.nb_primitives].Material = Mat
	
	this.nb_primitives += 1
	
end sub	

Sub SceneT.Add_Plane(cA as single, cB as single,cC as single, cD as single, Mat as MaterialT)

	If this.nb_primitives >= MAX_PRIMITIVES then exit sub
	
	this.s_primitive[this.nb_primitives].typeflag = prim_plane
	
	this.s_primitive[this.nb_primitives].A = cA
	this.s_primitive[this.nb_primitives].B = cB
	this.s_primitive[this.nb_primitives].C = cC
	this.s_primitive[this.nb_primitives].D = cD
	
	this.s_primitive[this.nb_primitives].Material = Mat
	
	this.nb_primitives += 1
	
end sub
	
	
Constructor SceneT()
	
	this.s_Primitive = Callocate(MAX_PRIMITIVES,Sizeof(Primitive))
	this.s_light = Callocate(MAX_LIGHTS,Sizeof(LightT))
	
End Constructor

Destructor SceneT()
	
	deallocate(this.s_Primitive)
	deallocate(this.s_light)
	
End Destructor





'' main loop


Dim as SceneT Scene
Dim as MaterialT sphereMat

SphereMat.Diffuse = type(.8,.7,.75)
SphereMat.BaseColor = type(.8,.8,.8)

Scene.Add_Light(-5,5,-5,type(1,1,1))

Scene.Add_Sphere(0,2,5,3,SphereMat)

Screenres 640,480,32,2

'' send ray

Dim as Ray MainRay
Dim as integer IntersectedPrimitive

For j as integer = 0 to 479
	For i as integer = 0 to 639
	
		MainRay.O = type( 0, 0, -5 )
		MainRay.d = type( i-320, j-240,150)
		Normalize(MainRay.d)
		
		intersectedprimitive = -1
		
		Do
		
			MainRay.update()
			
			If MainRay.z > 0 then
			
				For k as integer = 0 to Scene.nb_primitives-1
					if Scene.s_primitive[k].GetIntersection(MainRay) then
						
						IntersectedPrimitive = k
						pset(i,j),rgb(255,255,255)
						exit do
						
					End If
				next
			
			end if
			
		Loop until MainRay.Lengthpow2 >= MAX_RAY_LENGTH*MAX_RAY_LENGTH
		
		'' calc color !
		if intersectedprimitive <> -1 then
		
			static as ColorT MatCol = type(0,0,0)
		
			for k as integer = 0 to scene.nb_lights - 1
			
				static as Vector N, L
				static as single dotted
				
				L = type(Scene.s_light[k].x,Scene.s_light[k].y,Scene.s_light[k].z) - Type(MainRay.x,MainRay.y,MainRay.z)
				Normalize(L)
				
				N = Scene.s_primitive[intersectedprimitive].GetNormal(MainRay)
				
				dotted = dot(N,L)
				
				if dotted > 0 then
					MatCol.r += dotted * Scene.s_primitive[intersectedprimitive].Material.Diffuse.r * Scene.s_primitive[intersectedprimitive].Material.BaseColor.r * Scene.s_light[k].l_color.R
					MatCol.g += dotted * Scene.s_primitive[intersectedprimitive].Material.Diffuse.g * Scene.s_primitive[intersectedprimitive].Material.BaseColor.g * Scene.s_light[k].l_color.G
					MatCol.b += dotted * Scene.s_primitive[intersectedprimitive].Material.Diffuse.b * Scene.s_primitive[intersectedprimitive].Material.BaseColor.b * Scene.s_light[k].l_color.B
				end if
			next
			
			Pset(i,j),rgb(MatCol.r*255, MatCol.G*255,MatCol.b*255)
		else
			Pset(i,j),rgb(25,25,25)
		end if
	next
next

sleep
The point is, I tested the intersection functions with this part :

Code: Select all

Do
		
			MainRay.update()
			
			If MainRay.z > 0 then
			
				For k as integer = 0 to Scene.nb_primitives-1
					if Scene.s_primitive[k].GetIntersection(MainRay) then
						
						IntersectedPrimitive = k
						pset(i,j),rgb(255,255,255)
						exit do
						
					End If
				next
			
			end if
			
		Loop until MainRay.Lengthpow2 >= MAX_RAY_LENGTH*MAX_RAY_LENGTH
It should draw a white point if the ray intersect the primitive, but it looks like the program skips this part and directly draw the background color ...

I replaced pointers with arrays but it didn't change anything, I checked and checked again the intersection functions, the ray.update() function but everything looks okay to me ..

Does anyone have an idea of what's wrong here ?

Thanks a lot in advance :)


(oh and here is the vector.bi file if you want to compile this code :

Code: Select all

''
'' Vectors Library
'' for raytracer
''

Type Vector
	as single x,y,z
End Type

Operator +(L as Vector, R as Vector) as Vector
	return type(L.x+R.x,L.y+R.y,L.z+R.z)
End operator
	
Operator -(L as Vector, R as Vector) as Vector
	return type(L.x-R.x,L.y-R.y,L.z-R.z)
End Operator

Operator *(L as Vector, R as Vector) as Vector
	return type( L.y*R.z + L.z*R.y , _ 
				 L.z*R.x + L.x*R.z , _
				 L.x*R.y + L.y*R.x )
End operator

Function Dot(V1 as vector, V2 as vector) as single
	return V1.x*V2.x + V1.y*v2.y + v1.z*v2.z
End function

Sub Normalize(ByRef V as Vector)
	static as single _norm
	
	_norm = 1/sqr(V.x*v.x + V.y*v.y + v.z*v.z)
	
	V.x *= _norm
	V.y *= _norm
	V.z *= _norm
End Sub
	
-- end of quote --
ROMAECURSORES
Posts: 8
Joined: Dec 31, 2008 12:02
Location: Italy - Rome

Post by ROMAECURSORES »

Add

MainRay.O = Type( 0, 0, -5 )
MainRay.d = Type( i-320, j-240,150)
Normalize(MainRay.d)
MainRay.t =0

ciao
Edo
ROMAECURSORES
Posts: 8
Joined: Dec 31, 2008 12:02
Location: Italy - Rome

Post by ROMAECURSORES »

and for a correct visualization changing:

Static As ColorT MatCol = Type(0,0,0)

in:

dim As ColorT MatCol = Type(0,0,0)

ciao
Edo
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

THANKS !! rah, I had the same problem with my previous raytracer and I totally forgot it >< Well thanks a lot, I was desesperating about it :)
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

Hey :) It's me again :S

I have a bug I still don't understand, I checked all my vector operations but I don't get it. The phong reflection is almost okay, but for two lights, there are not 2 phong reflections but 4 ! (it seems like the vector operation with the normal*Viewvector doesn't take the direction of the normal into account :/ So both sides of the sphere present a phong reflection.

Here is the code, and below, the part which calculates the phong part of the light equation :

Code: Select all

''
'' Raytracer
''

#include "vector.bi"

Const PRECISION = .1
Const MAX_RAY_LENGTH = 100

Const MAX_PRIMITIVES = 20
Const MAX_LIGHTS     = 10

Const PRIM_SPHERE = 1
Const PRIM_PLANE  = 2

Const MAX_REFLECTIONS = 2

Const SHOW_LIGHTS = 1

Type ColorT
	as single r,g,b
End Type	

Type MaterialT
	as ColorT Diffuse, Specular
	as single Reflection, Highlight
End type	

Type Ray
	as vector O,d
	
	as single x,y,z,t
	
	Declare Sub Update()
	Declare Function Lengthpow2() as single
End Type

Function Ray.Lengthpow2() as single
	return (this.x-this.O.x)*(this.x-this.O.x) + (this.y-this.O.y)*(this.y-this.O.y) + (this.z-this.O.z)*(this.z-this.O.z)
End Function
	

Sub Ray.Update()
	
	this.x = this.O.x + this.d.x*this.t
	this.y = this.O.y + this.d.y*this.t
	this.z = this.O.z + this.d.z*this.t
	
	this.t += PRECISION
	
End Sub

Type Primitive
	as integer TypeFlag
	as integer LightFlag
	
	as MaterialT Material
	
	'' for planes
	as single A,B,C,D
	
	'' for spheres
	as single R, x,y,z
	
	
	'' functions
	Declare function GetNormal(CRay as Ray) as Vector
	Declare function GetIntersection(CRay as Ray) as integer
End type

Function Primitive.GetIntersection(CRay as Ray) as integer

	Select case This.TypeFlag
	Case PRIM_SPHERE
	
		if (this.x - CRay.x)*(this.x - CRay.x) + (this.y - CRay.y)*(this.y - CRay.y) + (this.z - CRay.z)*(this.z - CRay.z) <= R*R + PRECISION then
			return 1
		else
			return 0
		end if

	Case PRIM_PLANE
		
		if (this.A*CRay.x + this.B*CRay.y + this.C*CRay.z - this.D < PRECISION) and (this.A*CRay.x + this.B*CRay.y + this.C*CRay.z - this.D > -PRECISION) then
			return 1
		else
			return 0
		end if
	
	End Select
End Function

Function Primitive.GetNormal(CRay as Ray) as Vector
	
	static as Vector normal
	
	Select case This.TypeFlag
	Case PRIM_SPHERE
	
		normal.x = CRay.x - this.x
		normal.y = CRay.y - this.y
		normal.z = CRay.z - this.z
		
		Normalize(normal)
		
		return normal

	Case PRIM_PLANE
		
		normal.x = A
		normal.y = B
		normal.z = C
		
		Normalize(Normal)
		
		return normal
	
	End Select
End Function

Type LightT
	as single x,y,z
	as ColorT l_color
end type	

Type SceneT
	
	as Primitive ptr s_Primitive
	as integer nb_primitives
	
	as LightT ptr s_light
	as integer nb_lights
	
	Declare Constructor()
	Declare Destructor()
	
	Declare Sub Add_Sphere(cx as single, cy as single,cz as single, cR as single, Mat as MaterialT, lightf as integer=0)
	Declare Sub Add_Plane (cA as single, cB as single,cC as single, cD as single, Mat as MaterialT)
	
	Declare Sub Add_Light(cx as single, cy as single, cz as single,col as ColorT)
	
End Type

Sub SceneT.Add_Light(cx as single, cy as single, cz as single,col as ColorT)

	if this.nb_lights>=MAX_LIGHTS then exit sub
	
	this.s_light[this.nb_lights].x = cx
	this.s_light[this.nb_lights].y = cy
	this.s_light[this.nb_lights].z = cz
	
	this.s_light[this.nb_lights].l_color = col
	
	this.nb_lights += 1
	
	if SHOW_LIGHTS then
		dim as materialT lmat
		lmat.diffuse = col
		this.Add_Sphere(cx,cy,cz,.03,lmat,1)
	end if	
	
end sub
	

Sub SceneT.Add_Sphere(cx as single, cy as single,cz as single, cR as single, Mat as MaterialT, lightf as integer=0)
	
	If this.nb_primitives >= MAX_PRIMITIVES then exit sub
	
	this.s_primitive[this.nb_primitives].typeflag = prim_sphere
	
	this.s_primitive[this.nb_primitives].x = cx
	this.s_primitive[this.nb_primitives].y = cy
	this.s_primitive[this.nb_primitives].z = cz
	this.s_primitive[this.nb_primitives].R = cR
	
	this.s_primitive[this.nb_primitives].lightflag = lightf
	
	this.s_primitive[this.nb_primitives].Material = Mat
	
	this.nb_primitives += 1
	
end sub	

Sub SceneT.Add_Plane(cA as single, cB as single,cC as single, cD as single, Mat as MaterialT)

	If this.nb_primitives >= MAX_PRIMITIVES then exit sub
	
	this.s_primitive[this.nb_primitives].typeflag = prim_plane
	
	this.s_primitive[this.nb_primitives].A = cA
	this.s_primitive[this.nb_primitives].B = cB
	this.s_primitive[this.nb_primitives].C = cC
	this.s_primitive[this.nb_primitives].D = cD
	
	this.s_primitive[this.nb_primitives].Material = Mat
	
	this.nb_primitives += 1
	
end sub
	
	
Constructor SceneT()
	
	this.s_Primitive = Callocate(MAX_PRIMITIVES,Sizeof(Primitive))
	this.s_light = Callocate(MAX_LIGHTS,Sizeof(LightT))
	
End Constructor

Destructor SceneT()
	
	deallocate(this.s_Primitive)
	deallocate(this.s_light)
	
End Destructor

Dim shared as SceneT Scene

Function RayTrace(ByVal R as Ray,ByVal CurReflection as integer = 0) as ColorT
	Dim as Ray MainRay
	Dim as integer IntersectedPrimitive
	
	MainRay = R
	
	intersectedprimitive = -1
			
	Do
		
		MainRay.update()
		
		If (MainRay.z > 0) or (CurReflection > 0) then
			
			For k as integer = 0 to Scene.nb_primitives-1
					
				if Scene.s_primitive[k].GetIntersection(MainRay) then
				
					IntersectedPrimitive = k
					exit do
							
				End If
			next
		
		end if
				
	Loop until MainRay.Lengthpow2 >= MAX_RAY_LENGTH*MAX_RAY_LENGTH
			
	'' calc Diffuse and specular colors !
	if intersectedprimitive <> -1 then
			
		dim as ColorT MatCol = type(0,0,0)
		
		for k as integer = 0 to scene.nb_lights - 1
				
			dim as Vector N, L, V, R
			dim as single dotSpec, dotDiff
					
			L = type(Scene.s_light[k].x,Scene.s_light[k].y,Scene.s_light[k].z) - Type(MainRay.x,MainRay.y,MainRay.z)
			Normalize(L)
					
			N = Scene.s_primitive[intersectedprimitive].GetNormal(MainRay)
			
			V = MainRay.d
			R = GetReflectionVector(V,N)
			
			dotDiff = dot(N,L)
			dotSpec = dot(R,L)^Scene.s_primitive[intersectedprimitive].Material.Highlight
			
			'' Calculating Shadow
			dim as single shade = 1
			dim as Ray ShadowRay
			
			ShadowRay.O = type(MainRay.x,MainRay.y,MainRay.z)
			ShadowRay.d = L
			ShadowRay.t = 0
			
			Do
				ShadowRay.Update()
			
				for f as integer = 0 to Scene.nb_primitives-1
					if (f<>IntersectedPrimitive) and Scene.s_primitive[f].GetIntersection(ShadowRay) then
						
						if Scene.s_primitive[f].lightflag=0 then shade = .1
						exit do
								
					End If
				next
			Loop until (ShadowRay.x-Scene.s_light[k].x)*(ShadowRay.x-Scene.s_light[k].x)+(ShadowRay.y-Scene.s_light[k].y)*(ShadowRay.y-Scene.s_light[k].y) + (ShadowRay.z-Scene.s_light[k].z)*(ShadowRay.z-Scene.s_light[k].z) <= PRECISION
				
			'' Final Color shading
			if dotDiff > 0 then
				MatCol.r += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.r * Scene.s_light[k].l_color.r * shade
				MatCol.g += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.g * Scene.s_light[k].l_color.g * shade
				MatCol.b += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.b * Scene.s_light[k].l_color.b * shade
			end if
			
			if dotSpec > 0 then
				MatCol.r += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.r * Scene.s_light[k].l_color.r * shade
				MatCol.g += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.g * Scene.s_light[k].l_color.g * shade
				MatCol.b += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.b * Scene.s_light[k].l_color.b * shade
			end if
			
		next
		
		'' Calc reflections
		
		if Scene.s_Primitive[intersectedprimitive].Material.reflection > 0 then
			If CurReflection < MAX_REFLECTIONS then
				Dim as ColorT refCol
				Dim as Ray ReflectionRay
				Dim as vector refNormal
				
				refNormal = Scene.s_Primitive[intersectedprimitive].GetNormal(MainRay)
				
				ReflectionRay.O = type(MainRay.x,MainRay.y,MainRay.z)
				ReflectionRay.d = GetReflectionVector(MainRay.d,refNormal)
				Normalize(ReflectionRay.d)
				
				ReflectionRay.t = 0
				
				ReflectionRay.Update()
				
				'' !!! Recursion !!!
				refcol = Raytrace(ReflectionRay,CurReflection+1)
				'' !!! --------- !!!
				
				MatCol.r += Scene.s_Primitive[intersectedprimitive].Material.reflection*refcol.r
				MatCol.g += Scene.s_Primitive[intersectedprimitive].Material.reflection*refcol.g
				MatCol.b += Scene.s_Primitive[intersectedprimitive].Material.reflection*refcol.b
			end if
		end if
		
		if Scene.s_Primitive[intersectedprimitive].lightflag then
			matcol = Scene.s_Primitive[intersectedprimitive].Material.diffuse
		end if	
		
		if MatCol.r>1 then MatCol.r = 1 : if MatCol.r<0 then MatCol.r = 0
		if MatCol.g>1 then MatCol.g = 1 : if MatCol.g<0 then MatCol.g = 0
		if MatCol.b>1 then MatCol.b = 1 : if MatCol.b<0 then MatCol.b = 0
		
		return type(MatCol.r, MatCol.G,MatCol.b)
	else
		return type(.05,.05,.05)
	end if
	
End Function

'' main loop


Dim as MaterialT sphereMat, PlaneMat

PlaneMat.Diffuse = type(.5,1,.5)
PlaneMat.Reflection = .2

SphereMat.Diffuse = type(1,.5,.5)
SphereMat.Reflection = .7
SphereMat.Specular = type(1,1,1)
SphereMat.Highlight = 40

Scene.Add_Light(-7,7,3,type(.8,.8,.8))
Scene.Add_Light(5,5,12,type(.7,.7,.7))

Scene.Add_Sphere(0,0,10,3,SphereMat)
Scene.Add_Sphere(-4,-6.5,10,3,SphereMat)
Scene.Add_Sphere(4,-6.5,10,3,SphereMat)

Scene.Add_Plane(0,1,0,-10,PlaneMat)


Screenres 640,480,32,2

Dim as colorT finCol
Dim as Ray MainRay

For j as integer = 0 to 479
	For i as integer = 0 to 639
		
		MainRay.O = type( 0, 0, -10 )
		MainRay.d = type( i-320, 480-j-240,350)
		Normalize(MainRay.d)
			
		MainRay.t = 0
		
		finCol = Raytrace(MainRay)
		
		Pset(i,j),rgb(finCol.r*255,finCol.g*255,finCol.b*255)
		
	Next
Next

sleep
Phong (and diffuse) part :

Code: Select all

for k as integer = 0 to scene.nb_lights - 1
				
			dim as Vector N, L, V, R
			dim as single dotSpec, dotDiff
					
			L = type(Scene.s_light[k].x,Scene.s_light[k].y,Scene.s_light[k].z) - Type(MainRay.x,MainRay.y,MainRay.z)
			Normalize(L)
					
			N = Scene.s_primitive[intersectedprimitive].GetNormal(MainRay)
			
			V = MainRay.d
			R = GetReflectionVector(V,N)
			
			dotDiff = dot(N,L)
			dotSpec = dot(R,L)^Scene.s_primitive[intersectedprimitive].Material.Highlight

....
Any idea ? Thanks a lot in advance and sorry if it's a noobish "bug" again !



NB : the intersection function is not optimized at all but that's normal, I'm experimenting some stuff :)
ROMAECURSORES
Posts: 8
Joined: Dec 31, 2008 12:02
Location: Italy - Rome

Post by ROMAECURSORES »

Hezad, when compile your example i get these errors:

Raytracer1.bas(262) error 41: Variable not declared, GetReflectionVector in 'R = GetReflectionVector(V,N)'

Raytracer1.bas(314) error 67: Array not dimensioned, before '(' in 'ReflectionRay.d = GetReflectionVector(
MainRay.d,refNormal)'

What about GetReflectionVector?
Ciao
Edo
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

Did you download vector.bi ? GetReflectioVector is defined inside it :

Code: Select all

Function GetReflectionVector(InVec as Vector, Normal as Vector) as Vector
	return Invec - 2*dot(Normal,InVec) * Normal
End Function
ROMAECURSORES
Posts: 8
Joined: Dec 31, 2008 12:02
Location: Italy - Rome

Post by ROMAECURSORES »

and then tell me where can I download vector.bi?

ciao
Edo
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

In the first post ;)

Code: Select all

''
'' Vectors Library
'' for raytracer
''

Type Vector
	as single x,y,z
End Type

Operator +(L as Vector, R as Vector) as Vector
	return type(L.x+R.x,L.y+R.y,L.z+R.z)
End operator
	
Operator -(L as Vector, R as Vector) as Vector
	return type(L.x-R.x,L.y-R.y,L.z-R.z)
End Operator

Operator *(L as Vector, R as Vector) as Vector
	return type( L.y*R.z + L.z*R.y , _ 
				 L.z*R.x + L.x*R.z , _
				 L.x*R.y + L.y*R.x )
End operator

Operator *(L as Single, R as Vector) as Vector
	return type( L*R.x, L*R.y, L*R.z )
End operator

Operator -(V as Vector) as Vector
	return type(-V.x,-V.y,-V.z)
End Operator	

Function Dot(V1 as vector, V2 as vector) as single
	return V1.x*V2.x + V1.y*v2.y + v1.z*v2.z
End function

Sub Normalize(ByRef V as Vector)
	static as single _norm
	
	_norm = 1/sqr(V.x*v.x + V.y*v.y + v.z*v.z)
	
	V.x *= _norm
	V.y *= _norm
	V.z *= _norm
End Sub
	
Function GetReflectionVector(InVec as Vector, Normal as Vector) as Vector
	return Invec - 2*dot(Normal,InVec) * Normal
End Function
	
ROMAECURSORES
Posts: 8
Joined: Dec 31, 2008 12:02
Location: Italy - Rome

Post by ROMAECURSORES »

change:

dotSpec = dot(R,L)^Scene.s_primitive[intersectedprimitive].Material.Highlight

to:

if R.x*R.y*R.z>0 then dotSpec = dot(R,L)^Scene.s_primitive[intersectedprimitive].Material.Highlight else dotSpec=0


ciao
Edo
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

Thanks :) But it doesn't work, in fact it suppress the highlight(s) from a certain viewpoint but it doesn't suppress the "double" highlight :S

Thanks anyway ! :)
ROMAECURSORES
Posts: 8
Joined: Dec 31, 2008 12:02
Location: Italy - Rome

Post by ROMAECURSORES »

Hezad, this should work (I hope)

from:
If dotDiff > 0 Then
MatCol.r += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.r * Scene.s_light[k].l_color.r * shade
MatCol.g += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.g * Scene.s_light[k].l_color.g * shade
MatCol.b += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.b * Scene.s_light[k].l_color.b * shade
End If

If dotSpec > 0 Then
MatCol.r += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.r * Scene.s_light[k].l_color.r * shade
MatCol.g += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.g * Scene.s_light[k].l_color.g * shade
MatCol.b += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.b * Scene.s_light[k].l_color.b * shade
End If


to (If dotSpec > 0 inside If dotDiff > 0) :

If dotDiff > 0 Then
MatCol.r += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.r * Scene.s_light[k].l_color.r * shade
MatCol.g += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.g * Scene.s_light[k].l_color.g * shade
MatCol.b += dotDiff * Scene.s_primitive[intersectedprimitive].Material.Diffuse.b *
Scene.s_light[k].l_color.b * shade

If dotSpec > 0 then
MatCol.r += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.r * Scene.s_light[k].l_color.r * shade
MatCol.g += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.g * Scene.s_light[k].l_color.g * shade
MatCol.b += dotSpec * Scene.s_primitive[intersectedprimitive].Material.Specular.b * Scene.s_light[k].l_color.b * shade
End If

End If


Ciao Edo
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

Of Course !

Thanks A LOT again, it works perfectly ;)
ROMAECURSORES
Posts: 8
Joined: Dec 31, 2008 12:02
Location: Italy - Rome

Post by ROMAECURSORES »

I am glad.
Congratulations on the program. Despite the difficulty of the "rendering problem" it was not difficult to understand the algorithm. is written in clear and elegant way. I am waiting its development and its optimization.
Ciao
Edo
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

Well thanks :) I'll post some news here then ;)
Post Reply