Skip to content

Commit

Permalink
Fix refract3 method
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkOink authored and Makar Solomatin committed Apr 7, 2021
1 parent 53f2f3e commit 4ccfae0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 52 deletions.
Binary file added examples/19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions examples/19_refraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 PIL import Image

width = 400
height = 300

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

scene = Scene()
scene.addLight(PointLight(Vec3(-1,-8,1)))

# Triangle behind lens
A = Vertex(position=(-1.5, 6, 0))
B = Vertex(position=(3.5, 6, 0))
C = Vertex(position=(1, 6, 5.))
scene.add(Triangle(A, B, C, material=PhongMaterial(color=Vec3(1,1,1))))

# Floor
A = Vertex(position=(-5.0, -5.0, -2.0))
B = Vertex(position=( 5.0, -5.0, -2.0))
C = Vertex(position=( 5.0, 5.0, -2.0))
D = Vertex(position=(-5.0, 5.0, -2.0))

floormaterial = PhongMaterial(color=Vec3(.2,.2,.2) )
scene.add(Triangle(A,B,C, material=floormaterial))
scene.add(Triangle(A,C,D, material=floormaterial))

# Two lenses
scene.add(Sphere(Vec3(-1, 5.7, -1), 2.5, PhongMaterial(color=Vec3(.9, .9, .9),
transparency=.8, refraction=1.5)))
scene.add(Sphere(Vec3(2.8, 5, .5), 1.8, PhongMaterial(color=Vec3(.9, .9, .9),
transparency=.8, refraction=1.5)))

scene.setCamera(camera)
engine = SimpleRT(iterations=4, shadow=True)
image = engine.render(scene)
image.save("16.png")
49 changes: 0 additions & 49 deletions main.py

This file was deleted.

1 change: 1 addition & 0 deletions pyrt/math/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
"""

G_EPSILON = 1E-11
G_REFRACTION_EPSILON = 1E-4
6 changes: 4 additions & 2 deletions pyrt/math/vecops.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ def refract3(N, I, eta):
I: Incident vector
eta: Refraction koefficient
"""
d = dot3(I, N)
d = dot3(N, I)
if d > 0:
return refract3(I, N * -1, 1 / eta)
eta = 1 / eta
k = 1 - eta * eta * (1 - d * d)
if k < 0:
return I
else:
return I * eta - N * (eta * d + math.sqrt(k))


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

Expand Down
2 changes: 1 addition & 1 deletion pyrt/renderer/simplert.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _recurse_shade(self, scene: Scene, ray: Ray, iteration_num: int) -> tuple:

if hitrecord.material.transparency > G_EPSILON:
refract_ray = Ray(
hitrecord.point + normalize3(ray.direction) * G_EPSILON,
hitrecord.point + ray.direction * G_REFRACTION_EPSILON,
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)
Expand Down

0 comments on commit 4ccfae0

Please sign in to comment.