def parse(request_dic):

    id = request_dic['id']
    entities = request_dic['entities']
    objs = []
    camera = None
    light = None
    for ent in entities:
        if ent['type'] == 'sphere':
            objs.append(parseSphere(ent))
        elif ent['type'] == 'triangle':
            objs.append(parseTriangle(ent))
        elif ent['type'] == 'rectangle':
            objs += parseRect(ent)
        elif ent['type'] == 'light':
            light = parseLight(ent)
        elif ent['type'] == 'camera':
            camera = parseCamera(ent)
    #create default camera
    if camera is None:
        camera = PerspectiveCamera(400, 300, 45)
        camera.setView(Vec3(0., -10., 10.), Vec3(0., 0., 0.), Vec3(0., 0., 1.))

    ret = {'id': id, "objects": objs, 'camera': camera, 'light': light}
    return ret
def make_frame(t):
    # Specify width/height as in example 5
    width = 320
    height = 240

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

    # Create a scene
    scene = Scene()

    # Add a light to the scene
    radius = 3.0
    p = 2*pi/4
    x = radius * cos(t*p)
    y = radius * sin(t*p)

    scene.addLight(PointLight(Vec3(x, y, 10)))


    # create some materials:
    floormaterial = PhongMaterial(color=Vec3(0.5,0.5,0.5))
    sphere0material = PhongMaterial(color=Vec3(1.,0.,0.))
    sphere1material = PhongMaterial(color=Vec3(0.,1.,0.))
    sphere2material = PhongMaterial(color=Vec3(0.,0.,1.))
    sphere3material = PhongMaterial(color=Vec3(1.,1.,0.))

    # 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(A,C,D, material=floormaterial))

    # Add some spheres

    scene.add(Sphere(center=Vec3(-2.5,-2.5,1.75), radius=1.75, material=sphere0material))
    scene.add(Sphere(center=Vec3( 2.5,-2.5,1.75), radius=1.75, material=sphere1material))
    scene.add(Sphere(center=Vec3( 2.5, 2.5,1.75), radius=1.75, material=sphere2material))
    scene.add(Sphere(center=Vec3(-2.5, 2.5,1.75), radius=1.75, material=sphere3material))

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

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

    # Render the scene:
    image = engine.render(scene)
    return image.data
Beispiel #3
0
    def testCamera(self):
        w=320
        h=240

        # Create a camere with width, height and field of view:
        camera = PerspectiveCamera(w, h, 60)

        # Set View matrix of camera: define where to look at
        camera.setView(Vec3(0, -10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1))

        #  Get the view-projection matrix
        vp = camera.getMatrix()

        # Test view-projection matrix
        '''self.assertEqual(vp, (1.2990381056766582, 0.0, 0.0, 0.0,
                         0.0, 0.0, 1.7320508075688774, 0.0,
                         0.0, 1.0002000200020003, 0.0, 9.801980198019804,
                         0.0, 1.0, 0.0, 10.0))
        '''
        # Camera Matrices:
        identity = camera.matrixinv * camera.matrix
        self.assertEqual(identity, (1.0, 0.0, 0.0, 0.0,
                                    0.0, 1.0, 0.0, 0.0,
                                    0.0, 0.0, 1.0, 0.0,
                                    0.0, 0.0, 0.0, 1.0))


        # Check calculated camera position (with inverse view matrix)
        pos = camera.viewinv * Vec3(0,0,0)
        self.assertEqual(pos, Vec3(0.,-10.,0.0))

        # Check front frustum plane:
        pos0 = camera.matrixinv * Vec3(-1,-1,-1)
        pos1 = camera.matrixinv * Vec3( 1,-1,-1)
        pos2 = camera.matrixinv * Vec3( 1, 1,-1)
        pos3 = camera.matrixinv * Vec3(-1, 1,-1)

        self.assertEqual(pos0, Vec3(-0.07698003589194975, -9.9, -0.05773502691896232))
        self.assertEqual(pos1, Vec3(0.07698003589194975, -9.9, -0.05773502691896232))
        self.assertEqual(pos2, Vec3(0.07698003589194975, -9.9, 0.05773502691896232))
        self.assertEqual(pos3, Vec3(-0.07698003589194975, -9.9, 0.05773502691896232))

        # Check back frustum plane:
        pos4 = camera.matrixinv * Vec3(-1, -1, 1)
        pos5 = camera.matrixinv * Vec3(1, -1, 1)
        pos6 = camera.matrixinv * Vec3(1, 1, 1)
        pos7 = camera.matrixinv * Vec3(-1, 1, 1)

        self.assertEqual(pos4, Vec3(-769.8003589185585, 989.9999999987921, -577.3502691889189))
        self.assertEqual(pos5, Vec3(769.8003589185585, 989.9999999987921, -577.3502691889189))
        self.assertEqual(pos6, Vec3(769.8003589185585, 989.9999999987921, 577.3502691889189))
        self.assertEqual(pos7, Vec3(-769.8003589185585, 989.9999999987921, 577.3502691889189))
def parseCamera(entity):
    '''{"type":"camera",'position':"0,-10,10",'center':"0,0,0","width":"400","height":"300"}'''
    if entity['type'] != 'camera':
        raise ValueError("entity passed to parseCamera is not a camera")
    pos = entity['position'].split(',')
    center = entity['center'].split(',')
    c_pos = Vec3(pos)
    c_center = Vec3(center)
    width = int(entity['width'])
    height = int(entity['height'])
    camera = PerspectiveCamera(width, height, 45)  #default FOV = 45
    camera.setView(c_pos, c_center, Vec3(0., 0., 1.))
    return camera
Beispiel #5
0
def make_frame(t):
    w = 320
    h = 240

    # Create a camera with width, height and field of view:
    camera = PerspectiveCamera(w, h, 60)

    # Set View matrix of camera: define where to look at
    camera.setView(Vec3(0, -t - 10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1))

    #  Create view-projection matrix (right to left)
    vp =  camera.projection * camera.view

    # Create a scene
    scene = Scene()

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

    # Create a triangle
    t1 = Triangle(Vertex(position=(-5, -4, 5), texcoord=(0, 0)),
                 Vertex(position=(5, -4, 5), texcoord=(1, 0)),
                 Vertex(position=(5, -4, -5), texcoord=(1, 1)),
                 material=
                 TextureMaterial(texturepath=['tex17-1.png', 'tex17-2.png']))

    t2 = Triangle(Vertex(position=(-5, -4, 5), texcoord=(0, 0)),
                 Vertex(position=(5, -4, -5), texcoord=(1, 1)),
                 Vertex(position=(-5,-4, -5), texcoord=(0, 1)),
                 material=
                 TextureMaterial(texturepath=['tex17-1.png', 'tex17-2.png']))

    # Add triangle and sphere to the scene:
    scene.add(t1)
    scene.add(t2)

    # 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)
    return image.data
def make_frame(t):
    w = 320
    h = 240
    image = RGBImage(w, h)

    # Create a camere with width, height and field of view:
    camera = PerspectiveCamera(w, h, 60)

    posy = -90 + t * 25  # move camera along y-axis. t is time in seconds.

    # Set View matrix of camera: define where to look at
    camera.setView(Vec3(0, posy, 0), Vec3(0, 0, 0), Vec3(0, 0, 1))

    #  Create view-projection matrix (right to left)
    vp = camera.projection * camera.view

    # Create a triangle
    t = Triangle(Vertex(position=(-5, 1, 0)), Vertex(position=(0, 1, 5)),
                 Vertex(position=(5, 1, 0)))

    # Triangle in normalized device coordinates:
    at = vp * t.a.position
    bt = vp * t.b.position
    ct = vp * t.c.position

    # Triangle in Screen coordinates:
    a_screenpos = Vec2(int(w * 0.5 * (at.x + 1.) / at.z),
                       int(h * 0.5 * (at.y + 1.) / at.z))
    b_screenpos = Vec2(int(w * 0.5 * (bt.x + 1.) / bt.z),
                       int(h * 0.5 * (bt.y + 1.) / at.z))
    c_screenpos = Vec2(int(w * 0.5 * (ct.x + 1.) / ct.z),
                       int(h * 0.5 * (ct.y + 1.) / at.z))

    # Draw the Triangle (wireframe) in image:
    color = Vec3(1, 1, 1)
    image.drawLine(a_screenpos, c_screenpos, color)
    image.drawLine(c_screenpos, b_screenpos, color)
    image.drawLine(b_screenpos, a_screenpos, color)

    return np.asarray(image.data)
from pyrt.scene import *
from pyrt.geometry import Sphere
from pyrt.material.texturematerial import TextureMaterial
from pyrt.camera import PerspectiveCamera
from pyrt.renderer import SimpleRT

from pyrt.renderer import RGBImage
from pyrt.math import Vec3
from pyrt.geometry import Triangle, Vertex
from PIL import Image

w = 320
h = 240

# Create a camera with width, height and field of view:
camera = PerspectiveCamera(w, h, 60)

# Set View matrix of camera: define where to look at
camera.setView(Vec3(0, -10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1))

#  Create view-projection matrix (right to left)
vp = camera.projection * camera.view

# Create a scene
scene = Scene()

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

# Create a triangle
t = Triangle(Vertex(position=(-5, 1, 0), texcoord=(0, 0)),
# This time we render two triangles

from pyrt.math import *
from pyrt.scene import *
from pyrt.geometry import Triangle, Vertex
from pyrt.camera import OrthographicCamera, PerspectiveCamera
from pyrt.renderer import SimpleRT
from PIL import Image


# Specify width/height as in example 5
width = 320
height = 240

# 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 triangle (same as example 5) to the scene:
scene.add(Triangle(Vertex(position=(-5, 1, 0), color=(1,1,1)),
                   Vertex(position=(0, 1, 5), color=(0,1,1)),
                   Vertex(position=(5, 1, 0), color=(1,1,1))))

scene.add(Triangle(Vertex(position=(5, 1, 0), color=(1,1,1)),
                   Vertex(position=(0, 1, -5), color=(1,1,0)),
                   Vertex(position=(-5, 1, 0), color=(1,1,1))))

# Now tell the scene which camera we use
from pyrt.math import *
from pyrt.scene import *
from pyrt.geometry import Sphere
from pyrt.material import PhongMaterial
from pyrt.camera import PerspectiveCamera
from pyrt.renderer import SimpleRT

from PIL import Image


# Specify width/height as in example 5
width = 320
height = 240

# 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 sphere to the scene:
scene.add(Sphere(center=Vec3(0.,0.,0.), radius=3., material=PhongMaterial(color=Vec3(1.,0.,0.))))

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

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

# Render the scene:
Beispiel #10
0
    def testCamera(self):
        w = 320
        h = 240

        # Create a camere with width, height and field of view:
        camera = PerspectiveCamera(w, h, 60)

        # Set View matrix of camera: define where to look at
        camera.setView(Vec3(0, -10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1))

        #  Get the view-projection matrix
        vp = camera.getMatrix()

        # Test view-projection matrix
        '''self.assertEqual(vp, (1.2990381056766582, 0.0, 0.0, 0.0,
                         0.0, 0.0, 1.7320508075688774, 0.0,
                         0.0, 1.0002000200020003, 0.0, 9.801980198019804,
                         0.0, 1.0, 0.0, 10.0))
        '''
        # Camera Matrices:
        identity = camera.matrixinv * camera.matrix
        self.assertEqual(identity, (1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
                                    0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0))

        # Check calculated camera position (with inverse view matrix)
        pos = camera.viewinv * Vec3(0, 0, 0)
        self.assertEqual(pos, Vec3(0., -10., 0.0))

        # Check front frustum plane:
        pos0 = camera.matrixinv * Vec3(-1, -1, -1)
        pos1 = camera.matrixinv * Vec3(1, -1, -1)
        pos2 = camera.matrixinv * Vec3(1, 1, -1)
        pos3 = camera.matrixinv * Vec3(-1, 1, -1)

        self.assertEqual(
            pos0, Vec3(-0.07698003589194975, -9.9, -0.05773502691896232))
        self.assertEqual(pos1,
                         Vec3(0.07698003589194975, -9.9, -0.05773502691896232))
        self.assertEqual(pos2,
                         Vec3(0.07698003589194975, -9.9, 0.05773502691896232))
        self.assertEqual(pos3,
                         Vec3(-0.07698003589194975, -9.9, 0.05773502691896232))

        # Check back frustum plane:
        pos4 = camera.matrixinv * Vec3(-1, -1, 1)
        pos5 = camera.matrixinv * Vec3(1, -1, 1)
        pos6 = camera.matrixinv * Vec3(1, 1, 1)
        pos7 = camera.matrixinv * Vec3(-1, 1, 1)

        self.assertEqual(
            pos4,
            Vec3(-769.8003589185585, 989.9999999987921, -577.3502691889189))
        self.assertEqual(
            pos5, Vec3(769.8003589185585, 989.9999999987921,
                       -577.3502691889189))
        self.assertEqual(
            pos6, Vec3(769.8003589185585, 989.9999999987921,
                       577.3502691889189))
        self.assertEqual(
            pos7, Vec3(-769.8003589185585, 989.9999999987921,
                       577.3502691889189))
from pyrt.math import Vec3
from pyrt.scene import Scene
from pyrt.light import PointLight
from pyrt.light import SpotLight
from pyrt.geometry import Triangle, Sphere, Vertex
from pyrt.material import PhongMaterial
from pyrt.camera import PerspectiveCamera
from pyrt.renderer import SimpleRT
from PIL import Image

# Specify width/height as in example 5
width = 320
height = 240

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

# Create a scene
scene = Scene()

# Add a light to the scene
#scene.addLight(PointLight(Vec3(0,5,15)))
scene.addLight(SpotLight(Vec3(0, 0, 5), Vec3(-0.5, 0.5, -1), 7))
scene.addLight(SpotLight(Vec3(0, 0, 5), Vec3(0, -1, -1), 15))
#scene.addLight(SpotLight(Vec3(2.5, -2.5, 5), Vec3(0, 0, -1), 5))

# create some materials:
floormaterial = PhongMaterial(color=Vec3(0.5, 0.5, 0.5))
sphere0material = PhongMaterial(color=Vec3(1., 0., 0.))
sphere1material = PhongMaterial(color=Vec3(0., 1., 0.))
Beispiel #12
0
from pyrt.math import Vec3
from pyrt.scene import Scene
from pyrt.light import PointLight
from pyrt.geometry import Triangle, Sphere, Vertex, TriangleMesh
from pyrt.material import PhongMaterial
from pyrt.camera import PerspectiveCamera
from pyrt.renderer import SimpleRT

# Specify width/height as in example 5
width = 320
height = 240

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

# Create a scene
scene = Scene()

trimesh = TriangleMesh()
trimesh.load("../data/teapot_simple/teapot.obj")

print(trimesh.stats())

center = trimesh.getCentroid()
print(center)

bbox = trimesh.getBBox()
print(bbox)
Beispiel #13
0
def make_frame(t):
    # Specify width/height as in example 5
    width = 320
    height = 240

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

    # Create a scene
    scene = Scene()

    # Add a light to the scene
    radius = 3.0
    p = 2 * pi / 4
    x = radius * cos(t * p)
    y = radius * sin(t * p)

    scene.addLight(PointLight(Vec3(x, y, 10)))

    # create some materials:
    floormaterial = PhongMaterial(color=Vec3(0.5, 0.5, 0.5))
    sphere0material = PhongMaterial(color=Vec3(1., 0., 0.))
    sphere1material = PhongMaterial(color=Vec3(0., 1., 0.))
    sphere2material = PhongMaterial(color=Vec3(0., 0., 1.))
    sphere3material = PhongMaterial(color=Vec3(1., 1., 0.))

    # 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(A, C, D, material=floormaterial))

    # Add some spheres

    scene.add(
        Sphere(center=Vec3(-2.5, -2.5, 1.75),
               radius=1.75,
               material=sphere0material))
    scene.add(
        Sphere(center=Vec3(2.5, -2.5, 1.75),
               radius=1.75,
               material=sphere1material))
    scene.add(
        Sphere(center=Vec3(2.5, 2.5, 1.75),
               radius=1.75,
               material=sphere2material))
    scene.add(
        Sphere(center=Vec3(-2.5, 2.5, 1.75),
               radius=1.75,
               material=sphere3material))

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

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

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