# HG changeset patch # User Radek Brich # Date 1206809667 -3600 # Node ID d4481fc439526fa7d70907a87d5f7c58657ab8ff # Parent f6d75ae82c88767733c97f3219e25b1ccab1c6cb fix sphere transmissivity, rename demo.py to spheres_glass.py diff -r f6d75ae82c88 -r d4481fc43952 demos/demo.py --- a/demos/demo.py Sat Mar 29 12:09:50 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -#!/usr/bin/python - -import sys -sys.path.append(open('ModulePath').read().strip()) - -from raytracer import Raytracer, Material, Box, Sphere, Light -#, SphericalLight -import Image - -rt = Raytracer() - -light1 = Light(position=(0.0, 4.0, -3.0), colour=(0.9, 0.3, 0.6)) -rt.addlight(light1) - -#light2 = SphericalLight(position=(-2.0, 5.0, 1.0), radius=3.0, colour=(0.7, 1.0, 0.3)) -light2 = Light(position=(-2.0, -4.0, -1.0), colour=(0.7, 1.0, 0.3)) -rt.addlight(light2) - -light2 = Light(position=(2.0, 5.0, 1.0), colour=(0.8, 0.9, 1.0)) -rt.addlight(light2) - -mat0 = Material(colour=(0.1, 0.2, 0.9)) -box = Box(L=(-20.0, -2.2, -20.0), H=(20.0, -2.0, 20.0), material=mat0) -rt.addshape(box) - -mat1 = Material(colour=(1.0, 0.2, 0.1)) -mat1.setReflectivity(0.7) -bigsphere = Sphere(centre=(2.0, 1.0, -5.0), radius=2.5, material=mat1) -# reflection=0.6) -rt.addshape(bigsphere) - -mat2 = Material(colour=(0.1, 0.7, 1.0)) -smallsphere = Sphere(centre=(-5.5, 0.5, -8.0), radius=2.0, material=mat2) -# reflection=1.0, diffuse=0.1) -rt.addshape(smallsphere) - -mat3 = Material(colour=(0.9, 0.9, 1.0)) -mat3.setPhong(0.2, 1.0, 0.2) -mat3.setTransmissivity(0.9) -mat3.setReflectivity(0.0) -for i in range(10): - sph = Sphere(centre=(-5.0+i, -1.5, -4.0), radius=0.5, material=mat3) - rt.addshape(sph) - -rendersize = (800, 600) -data = rt.render(rendersize) -img = Image.fromstring("RGB", rendersize, data) -img.save('demo.png') diff -r f6d75ae82c88 -r d4481fc43952 demos/spheres_glass.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/spheres_glass.py Sat Mar 29 17:54:27 2008 +0100 @@ -0,0 +1,47 @@ +#!/usr/bin/python + +import sys +sys.path.append(open('ModulePath').read().strip()) + +from raytracer import Raytracer, Material, Box, Sphere, Light +#, SphericalLight +import Image + +rt = Raytracer() + +light1 = Light(position=(0.0, 4.0, -3.0), colour=(0.9, 0.3, 0.6)) +rt.addlight(light1) + +#light2 = SphericalLight(position=(-2.0, 5.0, 1.0), radius=3.0, colour=(0.7, 1.0, 0.3)) +light2 = Light(position=(-2.0, -4.0, -1.0), colour=(0.7, 1.0, 0.3)) +rt.addlight(light2) + +light2 = Light(position=(2.0, 5.0, 1.0), colour=(0.8, 0.9, 1.0)) +rt.addlight(light2) + +mat0 = Material(colour=(0.1, 0.2, 0.9)) +box = Box(L=(-20.0, -2.2, -20.0), H=(20.0, -2.0, 20.0), material=mat0) +rt.addshape(box) + +mat1 = Material(colour=(1.0, 0.2, 0.1)) +mat1.setReflectivity(0.7) +bigsphere = Sphere(centre=(2.0, 1.0, -5.0), radius=2.5, material=mat1) +rt.addshape(bigsphere) + +mat2 = Material(colour=(0.1, 0.4, 0.2)) +mat2.setReflectivity(0.6) +smallsphere = Sphere(centre=(-4.5, 0.5, -8.0), radius=2.0, material=mat2) +rt.addshape(smallsphere) + +mat3 = Material(colour=(0.9, 0.9, 1.0)) +mat3.setPhong(0.2, 1.0, 0.2) +mat3.setTransmissivity(0.88) +mat3.setReflectivity(0.1) +for i in range(10): + sph = Sphere(centre=(-5.0+i, -1.5, -4.0), radius=0.5, material=mat3) + rt.addshape(sph) + +rendersize = (800, 600) +data = rt.render(rendersize) +img = Image.fromstring("RGB", rendersize, data) +img.save('spheres_glass.png') diff -r f6d75ae82c88 -r d4481fc43952 src/raytracer.cc --- a/src/raytracer.cc Sat Mar 29 12:09:50 2008 +0100 +++ b/src/raytracer.cc Sat Mar 29 17:54:27 2008 +0100 @@ -206,8 +206,8 @@ refl += R*trans; trans = (1-R)*trans; Vector3 newdir = n * ray.dir + (n*cos_i - cos_t) * normal; - Ray newray = Ray(P, newdir); - trans_col = raytrace(newray, depth + 1, nearest_shape); + Ray newray = Ray(P + 0.001*newdir, newdir); + trans_col = raytrace(newray, depth + 1, NULL); } } col = (1-refl-trans)*col + refl*refl_col + trans*trans_col; diff -r f6d75ae82c88 -r d4481fc43952 src/scene.cc --- a/src/scene.cc Sat Mar 29 12:09:50 2008 +0100 +++ b/src/scene.cc Sat Mar 29 17:54:27 2008 +0100 @@ -114,13 +114,24 @@ bool Sphere::intersect(const Ray &ray, Float &dist) const { Vector3 V = ray.o - center; - register Float d = dot(V, ray.dir); + register Float d = -dot(V, ray.dir); register Float Det = d * d - (dot(V,V) - sqr_radius); + register Float t1,t2; if (Det > 0) { - d = -d - sqrtf(Det); - if (d > 0 && d < dist) + Det = sqrtf(Det); + t1 = d - Det; + t2 = d + Det; + if (t1 > 0) { - dist = d; + if (t1 < dist) + { + dist = t1; + return true; + } + } + else if (t2 > 0 && t2 < dist) + { + dist = t2; return true; } }