Skip to content

Commit

Permalink
Merge branch 'refraction' of https://github.com/slmtnm/pyRT into refr…
Browse files Browse the repository at this point in the history
…action
  • Loading branch information
PinkOink committed Mar 21, 2021
2 parents d0a8e49 + 183b1f0 commit 2e1957d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
71 changes: 40 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
from pyrt.geometry.sphere import Sphere
from typing import List
from pyrt.math import *
from pyrt.light import *
from pyrt.geometry import Triangle, Vertex, Sphere
from pyrt.geometry.vertex import Vertex
from pyrt import material
from pyrt.math import Vec3
from pyrt.scene import Scene
from pyrt.light import PointLight
from pyrt.geometry import Sphere, Triangle
from pyrt.material import PhongMaterial
from pyrt.camera import PerspectiveCamera
from pyrt.renderer import SimpleRT
from pyrt.scene import Scene
from pyrt.renderer import SimpleRT
from PIL import Image

floormaterial = PhongMaterial(color=Vec3(0.5,0.5,0.5))
sphere0material = PhongMaterial(color=Vec3(1.,0.,0.), transparency=0.5)
sphere1material = PhongMaterial(color=Vec3(0.,0.,1.), transparency=0.1, reflectivity=0.5)
sphere2material = PhongMaterial(color=Vec3(0.,1.,0.))
width = 1000
height = 800

camera = PerspectiveCamera(640, 480, 45)
camera.setView(Vec3(0.,-10.,0.0), Vec3(0.,0.,0.), Vec3(0.,0.,1.))
camera = PerspectiveCamera(width, height, 60)
camera.setView(Vec3(0,-10,0), Vec3(0,0,0), Vec3(0,0,1))

scene = Scene()
scene.add(Triangle(Vertex(position=(0, 0, 0)),
Vertex(position=(0, 5, 0)),
Vertex(position=(1, 5, 0)), material=PhongMaterial()))
scene.setCamera(camera)
scene.addLight(PointLight(Vec3(0,0,15)))
scene.addLight(PointLight(Vec3(-1,-8,1)))

# Add "floor"
# A = Vertex(position=(-5.0, -5.0, 0.0))
# B = Vertex(position=( 5.0, -5.0, 0.0))
# C = Vertex(position=( 5.0, 5.0, 0.0))
# D = Vertex(position=(-5.0, 5.0, 0.0))
# scene.add(Triangle(A,B,C, material=floormaterial))
scene.add(
Triangle(
Vertex(position=(-1.5,10,0)),
Vertex(position=(3.5,10,0)),
Vertex(position=(1,10,5.)),
material=PhongMaterial(color=Vec3(1,1,1))
)
)

# Add spheres
scene.add(Sphere(center=Vec3(0,-5.5,0), radius=1, material=sphere0material))
scene.add(Sphere(center=Vec3(0,-0.5,0), radius=1, material=sphere1material))
scene.add(Sphere(center=Vec3(3,-0.5,0), radius=1, material=sphere2material))
scene.add(Sphere(center=Vec3(-3,-0.5,0), radius=0.5, material=sphere2material))
#scene.add(
# Sphere(
# center=Vec3(1, 10, 0),
# radius = 1.5,
# material=PhongMaterial(color=Vec3(1, 1, 0))
# )
#)

imgdata = SimpleRT(shadow=True, iterations=7).render(scene)
imgdata.save('a.png')
scene.add(
Sphere(
center=Vec3(0, 0, 0),
radius=1.5,
material=PhongMaterial(color=Vec3(.9, .9, .9), transparency=0.4, refraction=1.33)
)
)

scene.setCamera(camera)
engine = SimpleRT(iterations=10)
image = engine.render(scene)
image.save("a.png")
1 change: 1 addition & 0 deletions pyrt/math/vecops.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def refract3(N, I, eta):
return I
else:
return I * eta - N * (eta * d + math.sqrt(k))


# ---------------------------------------------------------------------------------------------------------------------

Expand Down
15 changes: 10 additions & 5 deletions pyrt/renderer/simplert.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ def __init__(self, shadow=False, iterations=1):
Renderer.__init__(self, "Simple Raytracer")
self.shadow = shadow
self.iterations = iterations
self.background = (0, 0, 0)
if self.shadow:
print("# Shadow Enabled")
if self.iterations>1:
print("# Iterations: " + str(self.iterations))

self._epsilon = 1e-4 # for float comparison
self._refract_offset = 1e-6 # refraction ray origin offset

def _shade(self, scene: Scene, ray: Ray, hitrecord: HitRecord) -> tuple:
r, g, b = self.background
r = g = b = 0 # background color
hit = False
for element in scene.nodes:
if element.hit(ray, hitrecord):
Expand Down Expand Up @@ -78,7 +80,7 @@ def _recurse_shade(self, scene: Scene, ray: Ray, iteration_num: int) -> tuple:
if not hit or iteration_num == 0:
return r, g, b

if hitrecord.material.reflectivity != 0.0:
if hitrecord.material.reflectivity > self._epsilon:
reflect_ray = Ray(hitrecord.point, reflect3(hitrecord.normal_g, ray.direction))
new_r, new_g, new_b = self._recurse_shade(scene, reflect_ray, iteration_num - 1)

Expand All @@ -90,8 +92,11 @@ def _recurse_shade(self, scene: Scene, ray: Ray, iteration_num: int) -> tuple:

self.num_secondary_rays += 1

if hitrecord.material.transparency != 0.0:
refract_ray = Ray(hitrecord.point + ray.direction * 0.01, refract3(hitrecord.normal_g, ray.direction, hitrecord.material.refraction))
if hitrecord.material.transparency > self._epsilon:
refract_ray = Ray(
hitrecord.point + normalize3(ray.direction) * self._refract_offset,
refract3(normalize3(hitrecord.normal_g), normalize3(ray.direction), hitrecord.material.refraction)
)
new_r, new_g, new_b = self._recurse_shade(scene, refract_ray, iteration_num - 1)

ref1 = hitrecord.material.transparency
Expand Down

0 comments on commit 2e1957d

Please sign in to comment.