-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from zhenyatos/5-normal-mappings
Add normal mapping with example
- Loading branch information
Showing
5 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Example 18: 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 spheres 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") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |