Skip to content

Commit

Permalink
Add normal mapping with example
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyatos committed Feb 5, 2021
1 parent 27dcd08 commit dfa0d27
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
Binary file added examples/18.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/18_normal_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Example 16: Normal mappings
#
# Two spheres are rendered, one with normal map and the other without it

from pyrt.math import Vec3
from pyrt.scene import Scene
from pyrt.light import PointLight
from pyrt.geometry import Triangle, Sphere, Vertex
from pyrt.material import NormalMappedMaterial, TextureMaterial
from pyrt.camera import PerspectiveCamera
from pyrt.renderer import SimpleRT
from PIL import Image

# Specify width/height
width = 400
height = 300

# now create a camera and a view like in example 5:
camera = PerspectiveCamera(width, height, 60)
camera.setView(Vec3(0,-10,0), Vec3(0,0,0), Vec3(0,0,1))

# Create a scene
scene = Scene()

# Add a light to the scene
scene.addLight(PointLight(Vec3(0,0,0)))

# Create material with normal mappings
material0 = NormalMappedMaterial(texturepath='tex16.png', normalmappath='normalmap.png')
material1 = TextureMaterial(texturepath='tex16.png')

# Add a sphere to the scene:
scene.add(Sphere(center=Vec3(-3.5,0.,0.), radius=2.8, material=material0))
scene.add(Sphere(center=Vec3(3.5,0.,0.), radius=2.8, material=material1))

# Now tell the scene which camera we use
scene.setCamera(camera)

# Create a raytracer using "SimpleRT"
engine = SimpleRT()

# Render the scene:
image = engine.render(scene)

# Save the resulting image using pillow
image.save("18.png")
Binary file added examples/normalmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion pyrt/material/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
"""

from .material import Material
from .phongmaterial import PhongMaterial
from .phongmaterial import PhongMaterial
from .texturematerial import TextureMaterial
from .normalmappedmaterial import NormalMappedMaterial
23 changes: 23 additions & 0 deletions pyrt/material/normalmappedmaterial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from .texture import Texture
from .texturematerial import TextureMaterial
from ..math import Vec3, Ray, HitRecord, dot3, reflect3, normalize3, clamp3
from ..camera import Camera


class NormalMappedMaterial(TextureMaterial):
"""Texture Material Class"""

def __init__(self, color: Vec3 = Vec3(1., 1., 1.), shininess: float = 10.0, reflectivity: float = 0.0,
refraction: float = 1.0, texturepath: str = '', normalmappath: str = ''):
TextureMaterial.__init__(self, color, shininess, reflectivity, refraction, texturepath)
self.normalmap = None if len(normalmappath) == 0 else Texture(normalmappath)

def shade(self, camera: Camera, ray: Ray, hitrecord: HitRecord, lights: list) -> Vec3:
"""
Shade method: Texture
texture shader
"""
diff = 2 * (self.normalmap.color(hitrecord.texcoord) - Vec3(0.5, 0.5, 0.5))
hitrecord.normal_g = normalize3(diff)
return super().shade(camera, ray, hitrecord, lights)

0 comments on commit dfa0d27

Please sign in to comment.