Skip to content

Commit

Permalink
feat: use multiprocessing to create animation with -ma options
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrYxZ committed Dec 28, 2020
1 parent 9a3d49d commit 2d5e783
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getopt
import numpy as np
import os
from PIL import Image
import sys
import time
Expand All @@ -14,7 +15,7 @@
import material
from normal_map import NormalMap
from object import Cube, Plane, Sphere, Tetrahedron, Triangle
from render import render_aa, render_dof, render, render_mp
from render import render_aa, render_dof, render, render_mp, render_aa_mp
from scene import Scene
import shaders
from texture import ImageTexture, SolidImageTexture, Box
Expand All @@ -27,8 +28,8 @@
# WIDTH = 1280
# HEIGHT = 720
# Vertical and Horizontal Samples for Random Jitter Anti Aliasing
V_SAMPLES = 4
H_SAMPLES = 4
V_SAMPLES = 3
H_SAMPLES = 3
DEFAULT_KS = 0.8
DEFAULT_THICKNESS = 0.7
CHECKERS_TEXTURE_FILENAME = "textures/checkers.png"
Expand Down Expand Up @@ -73,12 +74,12 @@ def setup_lights():
# theta = utils.degree2radians(30)
# spot_light = SpotLight(light_pos, theta, nl)
# Area Light
area_light_pos = np.array([-50, 75.0, 100.0])
area_light_n0 = np.array([0.0, 0.0, -1.0])
area_light_n1 = utils.normalize(np.array([1.0, 1.0, 0.0]))
area_light = AreaLight(
area_light_pos, 50.0, 30.0, area_light_n0, area_light_n1
)
area_light_pos = np.array([0, 100, 0.0])
area_light_n0 = np.array([1.0, 0.0, 0.0])
area_light_n1 = utils.normalize(np.array([0.0, 1.0, 0.0]))
sx = 18
sy = 18
area_light = AreaLight(area_light_pos, sx, sy, area_light_n0, area_light_n1)
return [area_light]


Expand Down Expand Up @@ -106,7 +107,7 @@ def setup_objects():
plane_sy
)
# Sphere Object
sphere_pos = np.array([0, 0, 100], dtype=float)
sphere_pos = np.array([-50, 0, 100], dtype=float)
sphere_mtl = Material(
material.COLOR_BLUE,
material.TYPE_TEXTURED,
Expand All @@ -116,32 +117,33 @@ def setup_objects():
sphere_shader = shaders.TYPE_DIFFUSE_COLORS
sphere_r = 25.0
sphere = Sphere(sphere_pos, sphere_mtl, sphere_shader, sphere_r)
# Mickey Sphere
sphere_pos = np.array([-30, -5, 75], dtype=float)
sphere_mtl = Material(
material.COLOR_BLUE,
material.TYPE_TEXTURED,
specular=DEFAULT_KS
)
box = Box(sphere_pos, 40, 40, 40)
mickey_img_texture = ImageTexture(MICKEY_TEXTURE_FILENAME)
sphere_mtl.add_texture(SolidImageTexture(mickey_img_texture, box))
sphere_shader = shaders.TYPE_DIFFUSE_COLORS
sphere_r = 20.0
mickey = Sphere(sphere_pos, sphere_mtl, sphere_shader, sphere_r)
# Gray Sphere
sphere_pos = np.array([90, 15, 160], dtype=float)
sphere_mtl = Material(
material.COLOR_GRAY,
material.TYPE_DIFFUSE,
specular=DEFAULT_KS,
border=0.0,
kr=0.7, roughness=0.15
)
sphere_shader = shaders.TYPE_DIFFUSE_COLORS
sphere_r = 40.0
gray_sphere = Sphere(sphere_pos, sphere_mtl, sphere_shader, sphere_r)
return [sphere, plane, mickey, gray_sphere]
# # Mickey Sphere
# sphere_pos = np.array([-30, -5, 75], dtype=float)
# sphere_mtl = Material(
# material.COLOR_BLUE,
# material.TYPE_TEXTURED,
# specular=DEFAULT_KS
# )
# box = Box(sphere_pos, 40, 40, 40)
# mickey_img_texture = ImageTexture(MICKEY_TEXTURE_FILENAME)
# sphere_mtl.add_texture(SolidImageTexture(mickey_img_texture, box))
# sphere_shader = shaders.TYPE_DIFFUSE_COLORS
# sphere_r = 20.0
# mickey = Sphere(sphere_pos, sphere_mtl, sphere_shader, sphere_r)
# # Gray Sphere
# sphere_pos = np.array([90, 15, 160], dtype=float)
# sphere_mtl = Material(
# material.COLOR_GRAY,
# material.TYPE_DIFFUSE,
# specular=DEFAULT_KS,
# border=0.0,
# kr=0.7, roughness=0.15
# )
# sphere_shader = shaders.TYPE_DIFFUSE_COLORS
# sphere_r = 40.0
# gray_sphere = Sphere(sphere_pos, sphere_mtl, sphere_shader, sphere_r)
# return [sphere, plane, mickey, gray_sphere]
return [sphere, plane]


def set_objects_id(objects):
Expand Down Expand Up @@ -204,28 +206,33 @@ def main(argv):
print("Setting up...")
scene = setup_scene()
if multi_core:
render_function = render_mp
if debug_mode:
render_function = render_mp
else:
render_function = render_aa_mp
elif dof_mode:
render_function = render_dof
elif debug_mode:
render_function = render
else:
render_function = render_aa
render_msg = "Rendering at {}x{}".format(WIDTH, HEIGHT)
if not debug_mode:
render_msg += " with {}x{} AA".format(
H_SAMPLES, V_SAMPLES
)
if multi_core:
render_msg += " using multi-core"
if dof_mode:
render_msg += " using depth of field"
if multi_core:
render_msg += f" using {os.cpu_count()} cores"
print(render_msg)
# Raytrace one image
# -------------------------------------------------------------------------
if not animation_mode:
log.start_of_raytracing()
print("Raytracing...")
if debug_mode:
img_arr = render(scene, scene.cameras[0], HEIGHT, WIDTH)
img_arr = render_function(scene, scene.cameras[0], HEIGHT, WIDTH)
else:
img_arr = render_function(
scene, scene.cameras[0], HEIGHT, WIDTH, V_SAMPLES, H_SAMPLES
Expand Down

0 comments on commit 2d5e783

Please sign in to comment.