Пример #1
0
def parseRect(entity):
    '''{"type":"rectangle","A":"1.2,1.5,3","B":"1.0,6,1","C":'0,0,0','color':'0,1,0'}'''

    if entity['type'] != 'rectangle':
        raise ValueError("entity passed to parseTriangle is not a rectangle")
    A_pos = entity['A'].split(',')
    B_pos = entity['B'].split(',')
    C_pos = entity['C'].split(',')
    assert len(A_pos) == 3
    A = Vertex(position=A_pos)
    B = Vertex(position=B_pos)
    C = Vertex(position=C_pos)
    D = Vertex(position=Vec3(A_pos) + Vec3(C_pos) - Vec3(B_pos))
    print(D.position)
    r_color_txt = entity['color'].split(',')
    r_color = Vec3(r_color_txt)
    r_color.normalize()
    if "reflectivity" in entity:
        r_reflect = float(entity['reflectivity'])
        r_material = PhongMaterial(color=r_color, reflectivity=r_reflect)
    else:
        r_material = PhongMaterial(color=r_color)
    return [
        Triangle(A, B, C, material=r_material),
        Triangle(A, C, D, material=r_material)
    ]
Пример #2
0
def parseSphere(entity):
    """process a dictionary (entity) containing 
    info about a sphere and return a pyrt.geometry.Sphere() object
    
    entity=
    {
        "type":"sphere",
        "radius":"string representing a float number",
        "center":"x,y,z",#string of float numbers seperated by commas
        "color":"r,g,b" #int  0-255 seperated by commas,
        "reflectivity":"string representing a float number 0-1"
    }
    """

    if entity['type'] != "sphere":
        raise ValueError("entity passed to parseSphere() is not a sphere")

    s_center = Vec3(entity['center'].split(','))
    s_color = entity['color'].split(',')
    s_color = Vec3(s_color)
    s_color.normalize()
    if "reflectivity" in entity:
        s_reflect = float(entity['reflectivity'])
        s_material = PhongMaterial(color=s_color, reflectivity=s_reflect)
    else:
        s_material = PhongMaterial(color=s_color)
    s_radius = float(entity['radius'])
    return Sphere(center=s_center, radius=s_radius, material=s_material)
Пример #3
0
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
Пример #4
0
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
Пример #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)
Пример #7
0
def render(ents):
    """entities is dictionary built in obj_parser.parse() representing objects in scene"""
    #create scene and add objects to scene
    scene=Scene()
    for obj in ents['objects']:
        if isinstance(obj,list):
            for o in obj:
                scene.add(o)
        else:
            scene.add(obj)
    if ents['light'] is None:
        ents['light']=PointLight(Vec3(-1,-8,1))
    scene.addLight(ents['light'])
    scene.setCamera(ents['camera'])
    engine=SimpleRT(shadow=True,iterations=3)
    image=engine.render(scene)
 
    return image
Пример #8
0
def parseTriangle(entity):
    '''{"type":"triangle","A":"1.2,1.5,3","B":"1.0,6,1","C":'0,0,0','color':'0,1,0'}'''

    if entity['type'] != 'triangle':
        raise ValueError("entity passed to parseTriangle is not a triangle")
    A_pos = entity['A'].split(',')
    B_pos = entity['B'].split(',')
    C_pos = entity['C'].split(',')
    assert len(A_pos) == 3
    A = Vertex(position=A_pos)
    B = Vertex(position=B_pos)
    C = Vertex(position=C_pos)
    t_color_txt = entity['color'].split(',')
    t_color = Vec3(t_color_txt)
    t_color.normalize()
    if "reflectivity" in entity:
        t_reflect = float(entity['reflectivity'])
        t_material = PhongMaterial(color=t_color, reflectivity=s_reflect)
    else:
        t_material = PhongMaterial(color=t_color)
    return Triangle(A, B, C, material=t_material)
Пример #9
0
# Example 4: **Introduction to the image class**
# This example draws 20 rectangles and stores the image using PIL

from pyrt.renderer import RGBImage
from pyrt.math import Vec2, Vec3
import random
from PIL import Image


w = 320
h = 240
image = RGBImage(w, h)

for i in range(100):
    image.drawRectangle(Vec2(random.randint(0, w - 1), random.randint(0, h - 1)),
                        random.randint(1, w / 2), random.randint(1, h / 2),
                        Vec3(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)))

image.save("04.png")
Пример #10
0
    if lev == 0:
        image.drawLine(Vec2(x1, y1), Vec2(x5, y5), color)
    else:
        deltaX = x5 - x1
        deltaY = y5 - y1

        x2 = int(x1 + deltaX / 3.)
        y2 = int(y1 + deltaY / 3.)

        x3 = int(0.5 * (x1 + x5) + math.sqrt(3.) * (y1 - y5) / 6.)
        y3 = int(0.5 * (y1 + y5) + math.sqrt(3.) * (x5 - x1) / 6.)

        x4 = int(x1 + 2. * deltaX / 3.)
        y4 = int(y1 + 2. * deltaY / 3.)

        snowflake(image, lev - 1, x1, y1, x2, y2, color)
        snowflake(image, lev - 1, x2, y2, x3, y3, color)
        snowflake(image, lev - 1, x3, y3, x4, y4, color)
        snowflake(image, lev - 1, x4, y4, x5, y5, color)


w = 512
h = 512
image = RGBImage(w, h)

level = 5  # Change max recursion depth here

snowflake(image, level, 0, 255, 511, 255, Vec3(1, 0, 0))

image.save("02.png")
Пример #11
0
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 
# width = 320
# height = 240

width = 1280
height = 720

# now create a camera and a view :
camera = PerspectiveCamera(width, height, 90)
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,0,15)))

# 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"
Пример #12
0
def parseLight(entity):
    if entity['type'] != 'light':
        raise ValueError("entity passed to parseLight is not a light")
    pos = Vec3(entity['position'].split(','))
    return PointLight(pos)
Пример #13
0
from pyrt.renderer import RGBImage
from pyrt.math import Vec2, Vec3
from pyrt.camera import PerspectiveCamera
from pyrt.geometry import Triangle, Vertex
from PIL import Image

w = 320
h = 240
image = RGBImage(w, h)

# 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))

#  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),
Пример #14
0
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.))
sphere2material = PhongMaterial(color=Vec3(0., 0., 1.), reflectivity=0.5)
Пример #15
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)
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))
Пример #17
0
from pyrt.math import Vec3
from pyrt.scene import Scene
from pyrt.light import PointLight
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 light to the scene
scene.addLight(PointLight(Vec3(-1, -8, 1)))

# 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)
Пример #18
0
# Example 3: **Introduction to the image class**
# This example draws 100 circles and stores the image using PIL

from pyrt.renderer import RGBImage
from pyrt.math import Vec2, Vec3
import random
from PIL import Image

w = 320
h = 240
image = RGBImage(w, h)

for i in range(100):
    image.drawCircle(
        Vec2(random.randint(0, w - 1), random.randint(0, h - 1)),  # Center
        random.randint(3, 100),  #  Radius
        Vec3(random.uniform(0, 1), random.uniform(0, 1),
             random.uniform(0, 1)),  # Color
        random.randint(1, 3))  # Size

image.drawCircle(Vec2(159, 119), 100, Vec3(1, 0, 0))

image.save("03.png")
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)),
             Vertex(position=(0, 1, 5), texcoord=(1, 0)),
             Vertex(position=(5, 1, 0), texcoord=(1, 1)),
             material=TextureMaterial(texturepath=['tex16.png']))
Пример #20
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