Example #1
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)
    
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    num_frames,basename,shading_mode,ambient = first_pass(commands)
    frames = second_pass(commands,num_frames)

    lights = []
    for s in symbols:
        if s[0] == 'light': lights.append(s[1])

    env = {}
    env["shading_mode"] = shading_mode
    env["ambient"] = ambient
    env["lights"] = lights
    
    if num_frames > 1 and not os.path.exists("anim"):
        os.makedirs("anim")

    for frame,i in zip(frames,range(len(frames))):
        print i,frame
        screen = run_frame(commands,frame,env)
        if num_frames > 1:
            save_ppm(screen, "anim/"+basename+("%03d"%i)+".ppm")
Example #2
0
def run(filename):
    global basename
    global frames
    global knobs

    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    commands = list(commands)
    cur = 0
    for x in commands:
        commands[cur] = list(commands[cur])
        cur += 1

    animated = first_pass(commands)
    second_pass(commands, frames)
    print frames
    print knobs
    print "basename " + basename

    #    runCommands(commands, 30, animated)

    for frameNum in range(frames):
        runCommands(commands, frameNum, animated)
Example #3
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()
        
    for command in commands:

        if command[0] = "push":
            stack.append(stack[len(stack) - 1])
        elif command[0] = "pop":
            stack = stack[:len(stack) - 1]
def run(filename):
    """
    This function runs an mdl script
    """

    global basename
    global num_frames
    global knoblist
    basename = ''
    num_frames = 0
    status = True

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    status = first_pass(commands)

    if (status == True):
        knoblist = second_pass(commands)
        #print knoblist
        third_pass(commands, symbols)

    make_animation(basename)
Example #5
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
        if 'background' not in symbols.keys():
            symbols['background'] = [0, 0, 0]
        color_bg = symbols['background']
        (frames, basename) = first_pass(commands)
        knobs = second_pass(commands, frames, linear_func)
        print commands
        print knobs
        # we have knobs for each frame, this is where the fun starts
        # commands that can have knobs are move,rotate,scale
        knobbed_cmds = ["move", "scale", "rotate"]
        no_send = ["vary", "set", "frames", "basename"]
        DIRNAME = "img/"
        ctr = 0
        for frame in knobs:
            print "clear"
            print "%d %d %d" % (color_bg[0], color_bg[1], color_bg[2])
            for command in commands:
                if command["op"] in knobbed_cmds:
                    print(command["op"])
                    output_line = []
                    knob_mod = 1.0
                    if command["knob"] is not None:
                        knob_mod = frame[command["knob"]]
                    for arg in command["args"]:
                        if isinstance(arg, float):
                            arg *= knob_mod
                        output_line.append("%s " % str(arg))
                    print ''.join(output_line)
                # non knobbed, just print it regularly with args on next line
                # do not print commands that have no meaning to go
                elif command["op"] not in no_send:
                    print(command["op"])
                    output_line = []
                    if command["args"] is not None:
                        for arg in command["args"]:
                            output_line.append("%s " % str(arg))
                    print ''.join(output_line)
            ### SAVE FILE
            if frames > 1:
                print "save"
                print DIRNAME + "%s%03d.png" % (basename, ctr)
                ctr += 1
        ## save everything
        if frames > 1:
            name_arg = DIRNAME + basename + "*"
            name = basename + ".gif"
            print "animate"
            print name_arg + " " + name
    else:
        print "Parsing failed."
        return
Example #6
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [tmp]
    screen = new_screen()

    for command in commands:
        print command
        operator = command[0]
        if operator == "push":
            stack.append(stack[0])
        elif operator == "pop":
            stack = stack[1:]
        elif operator == "move":
            M = make_translate(command[1], command[2], command[3])
            if command[4] != None:
                scalar_mult(M, command[4])
            matrix_mult(M, stack[0])
        elif operator == "rotate2":
            if command[1] == "x":
                M = make_rotX(command[2])
            elif command[1] == "y":
                M = make_rotY(command[2])
            elif command[1] == "z":
                M = make_rotZ(command[2])
            if command[3] != None:
                scalar_mult(M, command[3])
            matrix_mult(M, stack[0])
        elif operator == "scale":
            M = make_scale(command[1], command[2], command[3])
            if command[4] != None:
                scalar_mult(M, command[4])
            matrix_mult(M, stack[0])
        elif operator == "box":
            points = []
            add_prism(points, command[1], command[2], command[3], command[4],
                      command[5], command[6])
            matrix_mult(stack[0], points)
            draw_polygons(points, screen, color)
        elif operator == "sphere":
            points = []
            add_sphere(points, command[1], command[2], command[3], command[4])
            matrix_mult(stack[0], points)
            draw_polygons(points, screen, color)
        elif operator == "display":
            display(screen)
Example #7
0
def run(filename):

    r = mdl.parseFile(filename)
    if r:
        (commands, symbols) = r
        scan(commands)
    else:
        print "parsing failed"
        return
Example #8
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)
    p = mdl.parseFile(filename)
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()
        
    for command in commands:
        #print command
        matrix = []
        if command[0] == "push":
            stack.append(copy.deepcopy(stack[-1]))
        elif command[0] == "pop":
            stack.pop()
        elif command[0] == "line":
            add_edge(matrix, command[1],command[2],command[3],command[4], command[5],command[6])
            draw_lines( matrix, screen, color )
            matrix=[];
        elif command[0] in ["sphere","box","torus"]:
            if command[0] == "sphere":
                add_sphere(matrix,command[1],command[2],command[3],command[4],5)
            elif command[0] == "box":
                add_box(matrix, command[1],command[2],command[3],command[4], command[5],command[6])
            else:
                add_torus(matrix, command[1],command[2],command[3],command[4], command[5],5)
            matrix_mult(stack[-1],matrix)
            draw_polygons( matrix, screen, color )
            matrix=[];
        elif command[0] in ["move","scale","rotate"]:
            if command[0] == "move":
                t= make_translate(command[1],command[2],command[3])
            elif command[0] == "scale":
                t = make_scale(command[1],command[2],command[3])
            else:
                r = command[2]*(math.pi/180.0)
                if command[1] == "x":
                    t = make_rotX(r)
                elif command[1] == "y":
                    t = make_rotY(r)
                elif command[1] == "z":
                    t = make_rotZ(r)
            matrix_mult(stack[-1],t)
            stack[-1]=t
        elif command[0] == "display":
            display( screen )
        elif command[0] == "save":
            save_extension( screen, command[1] )
Example #9
0
def run(filename):

    r = mdl.parseFile(filename)
    if r:
        (commands, symbols) = r
        scan(commands)
    else:
        print "parsing failed"
        return
Example #10
0
def parseObj(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
Example #11
0
def run(filename):
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()
        
    for command in commands:
        points = []
        print command
        cmd = command[0]
        args = command[1:]
        top = stack[-1]
        if cmd == "push":
            stack.append(copy.deepcopy(top))
        elif cmd == "pop":
            stack.pop()
        elif cmd in ["move","rotate","scale"]:
            if cmd == "move":
                x = make_translate(args[0], args[1], args[2])
            elif cmd == "rotate":
                x = get_rot(args[0],args[1]*(math.pi/180))
            elif cmd == "scale":
                x = make_scale(args[0], args[1], args[2])
            matrix_mult(top, x)
            stack[-1] = x
        elif cmd in ["box","sphere","torus"]:
            if cmd == "box":
                add_box(points, args[0], args[1], args[2], args[3], args[4], args[5])
            elif cmd == "sphere":
                add_sphere(points, args[0], args[1], args[2], args[3], 5)
            elif cmd == "torus":
                add_torus(points, args[0], args[1], args[2], args[3], args[4], 5)
            matrix_mult(top, points)
            draw_polygons(points, screen, color)
        elif cmd == "line":
            add_edge(points, args[0], args[1], args[2], args[3], args[4], args[5])
            matrix_mult(top, points)
            draw_lines(points, screen, color)
        elif cmd == "save":
            save_extension(screen, args[0])
        elif cmd == "display":
            display(screen)
Example #12
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        f = open('__COMPYLED_CODE__', 'w')
        f.write(str(p))
        f.close()
    else:
        print "Parsing failed."
        return
Example #13
0
def run(filename):
    """
    This function runs an mdl script
    """

    p = mdl.parseFile(filename)
    
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    for command in commands:
        cmd = command[0]
        if cmd == "push":
            stack.append(copy.deepcopy(stack[len(stack)-1]))
        if cmd == "pop":
            stack.pop()
        if cmd == "move":
            mult(make_translate(command[1], command[2], command[3]))
        if cmd == "rotate":
            t = command[2]*math.pi/180
            axis = command[1]
            if axis == 'x':
                mult(make_rotX(t))
            if axis == 'y':
                mult(make_rotY(t))
            if axis == 'z':
                mult(make_rotZ(t))
        if cmd == "scale":
            mult(make_scale(command[1], command[2], command[3]))
        if cmd in ["box", "sphere", "torus"]:
            polygons = []
            if cmd == "box":
                add_box(polygons, command[1],command[2],command[3],command[4],command[5],command[6])
            if cmd == "sphere":
                add_sphere(polygons, command[1],command[2],command[3],command[4],5)
            if cmd == "torus":
                add_torus(polygons, command[1],command[2],command[3],command[4],command[5],5)
            matrix_mult(stack[len(stack)-1], polygons)
            draw_polygons(polygons, screen, color)
        if cmd == "line":
            points = []
            add_edge(points, command[1],command[2],command[3],command[4],command[5],command[6])
            matrix_mult(stack[len(stack)-1], points)
            draw_lines(polygons, screen, color)
        if cmd == "save":
            save_extension(screen, cmd[1])
        if cmd == "display":
            display(screen)
Example #14
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()

    for command in commands:
        print command
        points = []
        cmd = command[0]
        arg = command[1:]
        if cmd == "push":
            stack.append(copy.deepcopy(stack[-1]))
        elif cmd == "pop":
            stack.pop()
        elif cmd in ["move", "rotate", "scale"]:
            if cmd == "move":
                matrix = make_translate(arg[0], arg[1], arg[2])
            elif cmd == "rotate":
                matrix = make_rot(arg[0], arg[1]*(math.pi/180))
            else:
                matrix = make_scale(arg[0], arg[1], arg[2])
            matrix_mult(stack[-1], matrix)
            stack[-1] = matrix
        elif cmd in ["box", "sphere", "torus"]:
            if cmd == "box":
                add_box(points, arg[0], arg[1], arg[2], arg[3], arg[4], arg[5])
            elif cmd == "sphere":
                add_sphere(points, arg[0], arg[1], arg[2], arg[3], 5)
            else:
                add_torus(points, arg[0], arg[1], arg[2], arg[3], arg[4], 5)
            matrix_mult(stack[-1], points)
            draw_polygons(points, screen, color)
        elif cmd == "save":
            save_extension(screen, arg[0])
        elif cmd == "display":
            display(screen)
Example #15
0
def run(filename):
    """
    This function runs an mdl script
    """

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    first_pass(commands)
Example #16
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        commands, symbols = p
        if is_animated(commands):
            frames = num_frames(commands)

            #print symbols

            # Set knob values for each frame
            knobs = make_knobs(commands, frames)

            #print jsonfmt(knobs, sort_keys=True, indent=4)

            basename = get_basename(commands)
            # Construct format string using format string
            fmt_string = "%s-%%0%dd.gif" % (basename, int(1 + max(log10(frames), 0)) )
            #print fmt_string

            screen = new_screen()
            for i in range(frames):
                print "Drawing frame %d of %d ..." % (i, frames - 1)
                draw_frame(commands, symbols, screen, knobs, i)
                save_extension(screen, fmt_string % (i))

            print '''


Done making your animation.

To show it, run the following ImageMagick terminal commands:

$ convert %s-*.gif %s.gif && animate -loop 0 %s.gif

                        - or -

$ animate -loop 0 %s-*.gif

Have a nice day!
            ''' % (basename, basename, basename, basename)
        else:
            draw_frame(commands, symbols)
    else:
        print "Parsing failed."
        return
Example #17
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    view = [0,
            0,
            1];
    ambient = [50,
               50,
               50]
    light = [[0.5,
              0.75,
              1],
             [255,
              255,
              255]]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident( tmp )

    stack = [ [x[:] for x in tmp] ]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 100
    consts = ''
    coords = []
    coords1 = []
    symbols['.white'] = ['constants',
                         {'red': [0.2, 0.5, 0.5],
                          'green': [0.2, 0.5, 0.5],
                          'blue': [0.2, 0.5, 0.5]}]
    reflect = '.white'

    print(symbols)
    for command in commands:
        print(command)
Example #18
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)

    points = []

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [tmp]
    screen = new_screen()
    print "----------------------------------------"
    for symbol in symbols:
        print symbol
    print "----------------------------------------"
    for command in commands:
        print command

    for command in commands:
        if command[0] == "push":
            stack.append(stack[-1])
        elif command[0] == "move":
            matrix_mult(make_translate(command[1], command[2], command[3]),
                        stack[-1])
        elif command[0] == "box":
            m = []
            add_box(m, command[1], command[2], command[3], command[4],
                    command[5], command[6])
            for x in stack:
                matrix_mult(x, m)
            for x in m:
                points.append(x)
            draw_polygons(m, screen, color)
        elif command[0] == "rotate" and command[1] == "y":
            matrix_mult(make_rotY(command[2]), stack[1])
        elif command[0] == "display":
            display(screen)
Example #19
0
def run(filename):
    color = [255, 255, 255]
    tmp = identity_matrix()

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "parsing failed"
        return

    stack = [tmp]
    screen = new_screen()

    for command in commands:
        print command
Example #20
0
def run(filename):
    """
    This function runs an mdl script
    """

    p = mdl.parseFile(filename)
    #print str(p)
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."

    (num_frames, basename, knob_vals) = first_pass(commands)
    print str(num_frames)
    print basename
    i = 0
    while i < num_frames:
        parse(commands, basename, knob_vals[i], i)
        i += 1
Example #21
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ] #initializes origin stack with identity matrix pushed on
    screen = new_screen()
        
    for command in commands:
        if command[0] == 'push':
            push(stack)
        elif command[0] == 'pop':
            pop(stack)
        elif command[0] == 'move':
            stack[-1] = move(stack[-1], command[1], command[2], command[3])
        elif command[0] == 'rotate':
            stack[-1] = rotate(stack[-1], command[1], command[2])
        elif command[0] == 'scale':
            stack[-1] = scale(stack[-1], command[1], command[2], command[3])
        elif command[0] == 'box':
            box(stack[-1], command[1], command[2], command[3], command[4], command[5], command[6], screen, color)
        elif command[0] == 'torus':
            torus(stack[-1], command[1], command[2], command[3], command[4], screen, color)
        elif command[0] == 'sphere':
            sphere(stack[-1], command[1], command[2], command[3], command[4], screen, color)
        elif command[0] == 'line':
            line(stack[-1], command[1], command[2], command[3], command[4], command[5], command[6], screen, color)
        elif command[0] == 'save':
            save_extension(screen, command[1])
        elif command[0] == 'display':
            display(screen)
        else:
            pass
Example #22
0
def run(filename):
    """
    This function runs an mdl script
    """
    view = [0,
            0,
            1];
    ambient = [50,
               50,
               50]
    light = [[0.5,
              0.75,
              1],
             [0,
              255,
              255]]
    areflect = [0.1,
                0.1,
                0.1]
    dreflect = [0.5,
                0.5,
                0.5]
    sreflect = [0.5,
                0.5,
                0.5]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident( tmp )

    stack = [ [x[:] for x in tmp] ]

    p = mdl.parseFile(filename)

    if p:
        f = open('__COMPYLED_CODE__', 'w')
        f.write(str(p))
        f.close()
    else:
        print "Parsing failed."
        return
Example #23
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [tmp]
    screen = new_screen()

    for command in commands:
        print command
Example #24
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()
        
    for command in commands:
        print command
Example #25
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)
    
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    num_frames,basename = first_pass(commands)
    frames = second_pass(commands,num_frames)

    if num_frames > 1 and not os.path.exists("anim"):
        os.makedirs("anim")

    for frame,i in zip(frames,range(len(frames))):
        print i,frame
        screen = run_frame(commands,frame)
        if num_frames > 1:
            save_ppm(screen, "anim/"+basename+("%03d"%i)+".ppm")
Example #26
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = [[0.5, 0.75, 1], [255, 255, 255]]

    color = [0, 0, 0]
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.5],
            'green': [0.2, 0.5, 0.5],
            'blue': [0.2, 0.5, 0.5]
        }
    ]
    reflect = '.white'

    (name, num_frames) = first_pass(commands)
    frames = second_pass(commands, num_frames)

    if num_frames != 1:
        for i in range(num_frames):
            tmp = new_matrix()
            ident(tmp)

            stack = [[x[:] for x in tmp]]
            screen = new_screen()
            zbuffer = new_zbuffer()
            tmp = []
            step_3d = 100
            consts = ''
            coords = []
            coords1 = []
            print("Creating frame number: " + str(i))
            for q in frames[i]:
                symbols[q][1] = frames[i][q]
                #print(symbols)
            for command in commands:
                #print(command)
                c = command['op']
                args = command['args']
                knob_value = 1

                if c == 'box':
                    if command['constants']:
                        reflect = command['constants']
                    add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                            args[5])
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'sphere':
                    if command['constants']:
                        reflect = command['constants']
                    add_sphere(tmp, args[0], args[1], args[2], args[3],
                               step_3d)
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'torus':
                    if command['constants']:
                        reflect = command['constants']
                    add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                              step_3d)
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'line':
                    add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                             args[5])
                    matrix_mult(stack[-1], tmp)
                    draw_lines(tmp, screen, zbuffer, color)
                    tmp = []
                elif c == 'move':
                    if command['knob']:
                        knob_value = symbols[command['knob']][1]
                    tmp = make_translate(args[0] * knob_value,
                                         args[1] * knob_value,
                                         args[2] * knob_value)
                    matrix_mult(stack[-1], tmp)
                    stack[-1] = [x[:] for x in tmp]
                    tmp = []
                    knob_value = 1
                elif c == 'scale':
                    if command['knob']:
                        knob_value = symbols[command['knob']][1]
                    tmp = make_scale(args[0] * knob_value,
                                     args[1] * knob_value,
                                     args[2] * knob_value)
                    matrix_mult(stack[-1], tmp)
                    stack[-1] = [x[:] for x in tmp]
                    tmp = []
                    knob_value = 1
                elif c == 'rotate':
                    if command['knob']:
                        knob_value = symbols[command['knob']][1]
                    theta = args[1] * (math.pi / 180) * knob_value
                    if args[0] == 'x':
                        tmp = make_rotX(theta)
                    elif args[0] == 'y':
                        tmp = make_rotY(theta)
                    else:
                        tmp = make_rotZ(theta)
                    matrix_mult(stack[-1], tmp)
                    stack[-1] = [x[:] for x in tmp]
                    tmp = []
                    knob_value = 1
                elif c == 'push':
                    stack.append([x[:] for x in stack[-1]])
                elif c == 'pop':
                    stack.pop()
            save_extension(screen, 'anim/' + name + '%03d' % i)
        make_animation(name)
    if (num_frames == 1):
        for command in commands:
            tmp = new_matrix()
            ident(tmp)

            stack = [[x[:] for x in tmp]]
            screen = new_screen()
            zbuffer = new_zbuffer()
            tmp = []
            step_3d = 100
            consts = ''
            coords = []
            coords1 = []
            #print(command)
            c = command['op']
            args = command['args']
            knob_value = 1

            if c == 'box':
                if command['constants']:
                    reflect = command['constants']
                add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'sphere':
                if command['constants']:
                    reflect = command['constants']
                add_sphere(tmp, args[0], args[1], args[2], args[3], step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'torus':
                if command['constants']:
                    reflect = command['constants']
                add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                          step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'line':
                add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                         args[5])
                matrix_mult(stack[-1], tmp)
                draw_lines(tmp, screen, zbuffer, color)
                tmp = []
            elif c == 'move':
                tmp = make_translate(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':
                tmp = make_scale(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':
                theta = args[1] * (math.pi / 180)
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c == 'pop':
                stack.pop()
            elif c == 'display':
                display(screen)
            elif c == 'save':
                save_extension(screen, args[0])
Example #27
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
        
    stack = [ tmp ]
    screen = new_screen() 

    framesNum = first_pass(commands)
    knobs = second_pass(commands, framesNum)   
        
    for i in range(framesNum):
        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [ tmp ]

            if command[0] == "push":
                stack.append( stack[-1][:] )

            if command[0] == "save":
                save_extension(screen, command[1])

            if command[0] == "display":
                display(screen)

            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "box":                
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "bezier":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'bezier')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "hermite":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'hermite')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], .05)
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "move":                
                xval = command[1]
                yval = command[2]
                zval = command[3]
                        
                t = make_translate(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t

            if command[0] == "scale":
                xval = command[1]
                yval = command[2]
                zval = command[3]

                t = make_scale(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t
                
            if command[0] == "rotate":
                angle = command[2] * (math.pi / 180)

                if command[1] == 'x':
                    t = make_rotX( angle )
                elif command[1] == 'y':
                    t = make_rotY( angle )
                elif command[1] == 'z':
                    t = make_rotZ( angle )            
                    
                matrix_mult( stack[-1], t )
                stack[-1] = t

        if framesNum > 1:
                save_extension(screen,"frames/"+basename+str(i)+".png")
                screen = new_screen()
                stack = []
Example #28
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)

    stack = []
    stack += [tmp]

    p = mdl.parseFile(filename)
    p = p[0]

    c = 0

    # if p:
    #     (commands, symbols) = p
    #     print commands
    #     print symbols
    # else:
    #     print "Parsing failed."
    #     return

    stack = [tmp]
    screen = new_screen()

    while c < len(p):
        cmd = p[c]
        command = cmd[0]
        if cmd[0] in ARG_COMMANDS:
            args = cmd[1:]
            #print command
            #print args
            if command == 'line':
                edge = []
                add_edge(edge, args[0], args[1], args[2], args[3], args[4],
                         args[5])
                matrix_mult(stack[-1], edge)
                draw_lines(edge, screen, color)

            elif command == 'circle':
                edge = []
                add_circle(edge, args[0], args[1], 0, args[2], .01)
                matrix_mult(stack[-1], edge)
                draw_lines(edge, screen, color)

            elif command == 'bezier':
                edge = []
                add_curve(edge, args[0], args[1], args[2], args[3], args[4],
                          args[5], args[6], args[7], .01, 'bezier')
                matrix_mult(stack[-1], edge)
                draw_lines(edge, screen, color)

            elif command == 'hermite':
                edge = []
                add_curve(edge, args[0], args[1], args[2], args[3], args[4],
                          args[5], args[6], args[7], .01, 'hermite')
                matrix_mult(stack[-1], edge)
                draw_lines(edge, screen, color)

            elif command == 'sphere':
                polygon = []
                add_sphere(polygon, args[0], args[1], args[2], args[3], 5)
                matrix_mult(stack[-1], polygon)
                draw_polygons(polygon, screen, color)

            elif command == 'torus':
                polygon = []
                add_torus(polygon, args[0], args[1], 0, args[2], args[3], 5)
                matrix_mult(stack[-1], polygon)
                draw_polygons(polygon, screen, color)

            elif command == 'box':
                polygon = []
                add_box(polygon, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                matrix_mult(stack[-1], polygon)
                draw_polygons(polygon, screen, color)

            elif command == 'scale':
                s = make_scale(args[0], args[1], args[2])
                matrix_mult(stack[-1], s)
                stack[-1] = s

            elif command == 'move':
                t = make_translate(args[0], args[1], args[2])
                matrix_mult(stack[-1], t)
                stack[-1] = t

            elif command == 'rotate':
                angle = args[1] * (math.pi / 180)
                if args[0] == 'x':
                    r = make_rotX(angle)
                elif args[0] == 'y':
                    r = make_rotY(angle)
                elif args[0] == 'z':
                    r = make_rotZ(angle)
                matrix_mult(stack[-1], r)
                stack[-1] = r

        elif command == 'push':
            stack.append(stack[-1])

        elif command == 'pop':
            stack.pop()

        elif command == 'ident':
            ident(stack[-1])

        elif command == 'apply':
            matrix_mult(stack[-1], points)

        elif command == 'clear':
            points = []

        elif command in ['display', 'save']:
            #screen = new_screen()
            #draw_polygons( points, screen, color )

            if command == 'display':
                display(screen)

            elif command == 'save':
                c += 1
                save_extension(screen, commands[c].strip())
        elif command == 'quit':
            return
        c += 1
Example #29
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    view = [0, 0, 1]
    ambient = [150, 150, 150]
    light = [[0.5, 0.75, 1], [255, 255, 255]]

    color = [0, 0, 0]
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.7],
            'green': [0.2, 0.5, 0.7],
            'blue': [0.2, 0.5, 0.7]
        }
    ]
    reflect = '.white'

    (name, num_frames) = first_pass(commands)
    frames = second_pass(commands, num_frames)

    tmp = new_matrix()
    ident(tmp)

    stack = [[x[:] for x in tmp]]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 100
    consts = ''
    coords = []
    coords1 = []

    print(symbols)
    for command in commands:
        print(command)

    if num_frames == 1:
        for command in commands:
            # print(command)
            c = command['op']
            args = command['args']
            knob_value = 1

            if c == 'box':
                if command['constants']:
                    reflect = command['constants']
                add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'pyramid':
                if command['constants']:
                    reflect = command['constants']
                add_pyramid(tmp, args[0], args[1], args[2], args[3], args[4])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'cone':
                if command['constants']:
                    reflect = command['constants']
                add_cone(tmp, args[0], args[1], args[2], args[3], args[4],
                         step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'cylinder':
                if command['constants']:
                    reflect = command['constants']
                add_cylinder(tmp, args[0], args[1], args[2], args[3], args[4],
                             step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'sphere':
                if command['constants']:
                    reflect = command['constants']
                add_sphere(tmp, args[0], args[1], args[2], args[3], step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'torus':
                if command['constants']:
                    reflect = command['constants']
                add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                          step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'line':
                add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                         args[5])
                matrix_mult(stack[-1], tmp)
                draw_lines(tmp, screen, zbuffer, color)
                tmp = []
            elif c == 'move':
                tmp = make_translate(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':
                tmp = make_scale(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':
                theta = args[1] * (math.pi / 180)
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c == 'pop':
                stack.pop()
            elif c == 'display':

                display(screen)
            elif c == 'save':
                save_extension(screen, args[0])
                # end operation loop
    else:
        frame = 0
        while (frame < num_frames):
            tmp = new_matrix()
            ident(tmp)

            stack = [[x[:] for x in tmp]]
            screen = new_screen()
            zbuffer = new_zbuffer()
            tmp = []
            step_3d = 100
            consts = ''
            coords = []
            coords1 = []
            for command in commands:
                c = command['op']
                args = command['args']
                knob_value = 1

                if c == 'box':
                    if command['constants']:
                        reflect = command['constants']
                    add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                            args[5])
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'pyramid':
                    if command['constants']:
                        reflect = command['constants']
                    add_pyramid(tmp, args[0], args[1], args[2], args[3],
                                args[4])
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'cone':
                    if command['constants']:
                        reflect = command['constants']
                    add_cone(tmp, args[0], args[1], args[2], args[3], args[4],
                             step_3d)
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'cylinder':
                    if command['constants']:
                        reflect = command['constants']
                    add_cylinder(tmp, args[0], args[1], args[2], args[3],
                                 args[4], step_3d)
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'sphere':
                    if command['constants']:
                        reflect = command['constants']
                    add_sphere(tmp, args[0], args[1], args[2], args[3],
                               step_3d)
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'torus':
                    if command['constants']:
                        reflect = command['constants']
                    add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                              step_3d)
                    matrix_mult(stack[-1], tmp)
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                    tmp = []
                    reflect = '.white'
                elif c == 'line':
                    add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                             args[5])
                    matrix_mult(stack[-1], tmp)
                    draw_lines(tmp, screen, zbuffer, color)
                    tmp = []
                elif c == 'move':
                    if command['knob'] != None:
                        m_move = frames[frame][command['knob']]
                        tmp = make_translate(args[0] * m_move,
                                             args[1] * m_move,
                                             args[2] * m_move)
                    else:
                        tmp = make_translate(args[0], args[1], args[2])
                    matrix_mult(stack[-1], tmp)
                    stack[-1] = [x[:] for x in tmp]
                    tmp = []
                elif c == 'scale':
                    if command['knob'] != None:
                        m_scale = frames[frame][command['knob']]
                        # m_scale = 1
                        tmp = make_scale(args[0] * m_scale, args[1] * m_scale,
                                         args[2] * m_scale)
                    else:
                        tmp = make_scale(args[0], args[1], args[2])
                    # print_matrix (tmp)
                    matrix_mult(stack[-1], tmp)
                    stack[-1] = [x[:] for x in tmp]
                    tmp = []
                    # print (args[0], mul/tiplier, args[0] * multiplier)
                elif c == 'rotate':
                    if command['knob'] != None:
                        m_rot = frames[frame][command['knob']]
                        theta = args[1] * m_rot * (math.pi / 180)
                    else:
                        theta = args[1] * (math.pi / 180)
                    if args[0] == 'x':
                        tmp = make_rotX(theta)
                    elif args[0] == 'y':
                        tmp = make_rotY(theta)
                    else:
                        tmp = make_rotZ(theta)
                    # print_matrix (tmp)
                    matrix_mult(stack[-1], tmp)
                    stack[-1] = [x[:] for x in tmp]
                    tmp = []
                elif c == 'push':
                    stack.append([x[:] for x in stack[-1]])
                elif c == 'pop':
                    stack.pop()
                # end operation loop
            print("Generating frame " + str(frame) + "...")
            if (frame < 10):
                frame = "00" + str(frame)
            elif (frame < 100):
                frame = "0" + str(frame)
            else:
                frame = str(frame)

            save_extension(screen, "anim/" + name + frame + ".png")
            clear_screen(screen)
            clear_zbuffer(zbuffer)
            frame = int(frame) + 1
        print("Frame generation complete")
Example #30
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = [[0.5, 0.75, 1], [255, 255, 255]]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident(tmp)

    stack = [[x[:] for x in tmp]]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 100
    consts = ''
    coords = []
    coords1 = []
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.5],
            'green': [0.2, 0.5, 0.5],
            'blue': [0.2, 0.5, 0.5]
        }
    ]
    reflect = '.white'
    #I can iterate through this list later to find the reflective values
    colors = ['red', 'green', 'blue']
    vertices = []
    polygons = []

    #print(symbols)
    for command in commands:
        #pulling all necessary data from commands dictionary
        function = command['op']
        args = command['args']

        if function == "push":
            #just copying from old parser-should be no difference
            #renaming system to stack
            stack.append([x[:] for x in stack[-1]])
        elif function == "pop":
            #just copying from old parser-should be no difference
            #renaming system to stack
            stack.pop()
        elif function == "rotate":
            #float is no longer necessary
            theta = args[1] * (math.pi / 180)
            if args[0] == 'x':
                t = make_rotX(theta)
            elif args[0] == 'y':
                t = make_rotY(theta)
            else:
                t = make_rotZ(theta)
            matrix_mult(stack[-1], t)
            stack[-1] = [x[:] for x in t]
        elif function == "move":
            t = make_translate(args[0], args[1], args[2])
            matrix_mult(stack[-1], t)
            stack[-1] = [x[:] for x in t]
        elif function == "scale":
            t = make_scale(args[0], args[1], args[2])
            matrix_mult(stack[-1], t)
            stack[-1] = [x[:] for x in t]
        elif function == "box":
            #replacing tmp with temp
            #constants is only a a parameter for certain functions
            consts = command['constants']
            if consts is None:
                consts = reflect
            add_box(tmp, args[0], args[1], args[2], args[3], args[4], args[5])
            matrix_mult(stack[-1], tmp)
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols,
                          consts)
            tmp = []
        elif function == "torus":
            consts = command['constants']
            if consts is None:
                consts = reflect
            add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                      step_3d)
            matrix_mult(stack[-1], tmp)
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols,
                          consts)
            tmp = []
        elif function == "sphere":
            consts = command['constants']
            if consts is None:
                consts = reflect
            add_sphere(tmp, args[0], args[1], args[2], args[3], step_3d)
            matrix_mult(stack[-1], tmp)
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols,
                          consts)
            tmp = []
        elif function == "line":
            add_edge(tmp, args[0], args[1], args[2], args[3], args[4], args[5])
            matrix_mult(stack[-1], tmp)
            draw_lines(tmp, screen, zbuffer, color)
            tmp = []
        elif function == "vertex":
            vertices.append(args[0:3])
        elif function == "polygon":
            add_polygon(polygons, vertices[int(args[0])][0],
                        vertices[int(args[0])][1], vertices[int(args[0])][2],
                        vertices[int(args[1])][0], vertices[int(args[1])][1],
                        vertices[int(args[1])][2], vertices[int(args[2])][0],
                        vertices[int(args[2])][1], vertices[int(args[2])][2])
        elif function == "draw":
            consts = command['constants']
            if consts is None:
                consts = reflect
            matrix_mult(stack[-1], polygons)
            draw_polygons(polygons, screen, zbuffer, view, ambient, light,
                          symbols, consts)
            polygons = []
        elif function == "save":
            save_extension(screen, args[0])
        elif function == "display":
            display(screen)
Example #31
0
def run(filename):
    """
    This function runs an mdl script
    """
    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = [[0.5, 0.75, 1], [0, 255, 255]]
    areflect = [0.1, 0.1, 0.1]
    dreflect = [0.5, 0.5, 0.5]
    sreflect = [0.5, 0.5, 0.5]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident(tmp)

    systems = [[x[:] for x in tmp]]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 20
    polygons = []

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    for i in p[0]:
        print i
        if i[0] == 'sphere':
            #print 'SPHERE\t' + str(args)
            add_sphere(polygons, float(i[1]), float(i[2]), float(i[3]),
                       float(i[4]), step_3d)
            matrix_mult(systems[-1], polygons)
            draw_polygons(polygons, screen, zbuffer, view, ambient, light,
                          areflect, dreflect, sreflect)
            polygons = []

        elif i[0] == 'torus':
            #print 'TORUS\t' + str(args)
            add_torus(polygons, float(i[1]), float(i[2]), float(i[3]),
                      float(i[4]), float(i[5]), step_3d)
            matrix_mult(systems[-1], polygons)
            draw_polygons(polygons, screen, zbuffer, view, ambient, light,
                          areflect, dreflect, sreflect)
            polygons = []

        elif i[0] == 'box':
            #print 'BOX\t' + str(args)
            add_box(polygons, float(i[1]), float(i[2]), float(i[3]),
                    float(i[4]), float(i[5]), float(i[6]))
            matrix_mult(systems[-1], polygons)
            draw_polygons(polygons, screen, zbuffer, view, ambient, light,
                          areflect, dreflect, sreflect)
            polygons = []

        elif i[0] == 'circle':
            #print 'CIRCLE\t' + str(args)
            add_circle(edges, float(i[1]), float(i[2]), float(i[3]),
                       float(i[4]), step)
            matrix_mult(systems[-1], edges)
            draw_lines(edges, screen, zbuffer, color)
            edges = []

        elif i[0] == 'hermite' or i[0] == 'bezier':
            #print 'curve\t' + line + ": " + str(args)
            add_curve(edges, float(i[1]), float(i[2]), float(i[3]),
                      float(i[4]), float(i[5]), float(i[6]), float(i[7]),
                      float(i[8]), step, i[0])
            matrix_mult(systems[-1], edges)
            draw_lines(edges, screen, zbuffer, color)
            edges = []

        elif i[0] == 'line':
            #print 'LINE\t' + str(args)

            add_edge(edges, float(i[1]), float(i[2]), float(i[3]), float(i[4]),
                     float(i[5]), float(i[6]))
            matrix_mult(systems[-1], edges)
            draw_lines(eges, screen, zbuffer, color)
            edges = []

        elif i[0] == 'scale':
            #print 'SCALE\t' + str(args)
            t = make_scale(float(i[1]), float(i[2]), float(i[3]))
            matrix_mult(systems[-1], t)
            systems[-1] = [x[:] for x in t]

        elif i[0] == 'move':
            #print 'MOVE\t' + str(args)
            t = make_translate(float(i[1]), float(i[2]), float(i[3]))
            matrix_mult(systems[-1], t)
            systems[-1] = [x[:] for x in t]

        elif i[0] == 'rotate':
            #print 'ROTATE\t' + str(args)
            theta = float(i[2]) * (math.pi / 180)
            if i[1] == 'x':
                t = make_rotX(theta)
            elif i[1] == 'y':
                t = make_rotY(theta)
            else:
                t = make_rotZ(theta)
            matrix_mult(systems[-1], t)
            systems[-1] = [x[:] for x in t]

        elif i[0] == 'push':
            systems.append([x[:] for x in systems[-1]])

        elif i[0] == 'pop':
            systems.pop()

        elif i[0] == 'display' or i[0] == 'save':
            if i[0] == 'display':
                display(screen)
            else:
                save_extension(screen, i[1] + i[2])
Example #32
0
def run(filename):
    """
    This function runs an mdl script
    """
    view = [0,
            0,
            1];
    ambient = [50,
               50,
               50]
    light = [[0.5,
              0.75,
              1],
             [0,
              255,
              255]]
    areflect = [0.1,
                0.1,
                0.1]
    dreflect = [0.5,
                0.5,
                0.5]
    sreflect = [0.5,
                0.5,
                0.5]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident( tmp )

    stack = [ [x[:] for x in tmp] ]
    screen = new_screen()
    zbuffer = new_zbuffer()
    step_3d = 20
    edges = []
    polygons = []

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
    script = (commands,symbols)[0]
    for command in script:
        line = command[0]
        args = command[1:]
        print [line]+list(args)
        if line == 'push':
            stack.append([x[:] for x in stack[-1]])
        elif line == 'pop':
            stack.pop()
        elif line == 'move':
            t = make_translate(float(args[0]), float(args[1]), float(args[2]))
            matrix_mult( stack[-1], t )
            stack[-1] = [ x[:] for x in t]
        elif line == 'rotate':
            theta = float(args[1]) * (math.pi / 180)
            if args[0] == 'x':
                t = make_rotX(theta)
            elif args[0] == 'y':
                t = make_rotY(theta)
            else:
                t = make_rotZ(theta)
            matrix_mult( stack[-1], t )
            stack[-1] = [ x[:] for x in t]
        elif line == 'scale':
            t = make_scale(float(args[0]), float(args[1]), float(args[2]))
            matrix_mult( stack[-1], t )
            stack[-1] = [ x[:] for x in t]
        elif line == 'box':
            i = 0
            if not isinstance(args[0],float):
                i += 1
            add_box(polygons,
                    float(args[0+i]), float(args[1+i]), float(args[2+i]),
                    float(args[3+i]), float(args[4+i]), float(args[5+i]))
            matrix_mult( stack[-1], polygons )
            draw_polygons(polygons, screen, zbuffer, view, ambient, light, areflect, dreflect, sreflect)
            polygons = []
        elif line == 'sphere':
            i = 0
            if not isinstance(args[0],float):
                i += 1
            add_sphere(polygons,
                       float(args[0+i]), float(args[1+i]), float(args[2+i]),
                       float(args[3+i]), step_3d)
            matrix_mult( stack[-1], polygons )
            draw_polygons(polygons, screen, zbuffer, view, ambient, light, areflect, dreflect, sreflect)
            polygons = []
        elif line == 'torus':
            i = 0
            if not isinstance(args[0],float):
                i += 1
            add_torus(polygons,
                      float(args[0+i]), float(args[1+i]), float(args[2+i]),
                      float(args[3+i]), float(args[4+i]), step_3d)
            matrix_mult( stack[-1], polygons )
            draw_polygons(polygons, screen, zbuffer, view, ambient, light, areflect, dreflect, sreflect)
            polygons = []
        elif line == 'line':
            i = 0
            if not isinstance(args[0],float):
                i += 1
            j = 0
            if not isinstance(args[3+i],float):
                j += 1
            add_edge( edges,
                      float(args[0+i]), float(args[1+i]), float(args[2+i]),
                      float(args[3+i+j]), float(args[4+i+j]), float(args[5+i+j]))
            matrix_mult( stack[-1], edges )
            draw_lines(edges, screen, zbuffer, color)
            edges = []
        elif line == 'save':
            save_extension(screen, args[0]+args[1])
        elif line == 'display':
            display(screen)
Example #33
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    (name, num_frames) = first_pass( commands )
    knobs = second_pass( commands, num_frames )

        
    for f in range( num_frames ):

        stack = [ tmp ]
        screen = new_screen()    
        z_buffer = new_screen(XRES, YRES, [None])
        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [ tmp ]

            if command[0] == "push":
                stack.append( stack[-1][:] )

            if command[0] == "save":
                save_extension(screen, command[1])

            if command[0] == "display":
                display(screen)

            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, z_buffer, color )

            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen,  z_buffer,color )

            if command[0] == "box":                
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, z_buffer, color )

            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, z_buffer, color )

            if command[0] == "bezier":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'bezier')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, z_buffer, color )

            if command[0] == "hermite":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'hermite')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, z_buffer, color )

            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], .05)
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, z_buffer, color )

            if command[0] == "move":                
                xval = command[1]
                yval = command[2]
                zval = command[3]

                if command[4]:
                    knob = knobs[f][command[4]]
                    xval*= knob
                    yval*= knob
                    zval*= knob
                    
                t = make_translate(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t

            if command[0] == "scale":
                xval = command[1]
                yval = command[2]
                zval = command[3]

                if command[4]:
                    knob = knobs[f][command[4]]
                    xval*= knob
                    yval*= knob
                    zval*= knob

                t = make_scale(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t

            if command[0] == "rotate":
                angle = command[2] * (math.pi / 180)

                if command[3]:
                    angle*= knobs[f][command[3]]

                if command[1] == 'x':
                    t = make_rotX( angle )
                elif command[1] == 'y':
                    t = make_rotY( angle )
                elif command[1] == 'z':
                    t = make_rotZ( angle )            
                    
                matrix_mult( stack[-1], t )
                stack[-1] = t
                
        if num_frames > 1:
            fname = 'anim/%s%03d.png' % (name, f)
            print 'Drawing frame: ' + fname
            save_extension(screen, fname)
Example #34
0
def run(filename):    
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)
    stack = [tmp]
    screen = new_screen()

    
    def mult(m):
        matrix_mult(stack[len(stack)-1], m)
        stack[len(stack)-1] = m    

    p = mdl.parseFile(filename)
    
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
    
    first_pass(commands)
    knobs = second_pass(commands)

    global basename
    global frames
    
    for frame in range(frames):
        #frames = 1 means no animation aka static image
        for command in commands:
            if not stack:
                stack = [tmp]
            cmd = command[0]
            k = 1
            if cmd == "push":
                stack.append(copy.deepcopy(stack[len(stack)-1]))
            if cmd == "pop":
                stack.pop()
            if cmd == "move":
                if frames > 1 and command[4] in knobs[frame]:
                    k = knobs[frame][command[4]]
                mult(make_translate(command[1]*k, command[2]*k, command[3]*k))
            if cmd == "rotate":
                if frames > 1 and command[3] in knobs[frame]:
                    k = knobs[frame][command[3]]
                t = k*command[2]*math.pi/180
                #print t
                axis = command[1]
                if axis == 'x':
                    mult(make_rotX(t))
                if axis == 'y':
                    mult(make_rotY(t))
                if axis == 'z':
                    mult(make_rotZ(t))
            if cmd == "scale":
                if frames > 1 and command[4] in knobs[frame]:
                    k = knobs[frame][command[4]]
                mult(make_scale(command[1]*k, command[2]*k, command[3]*k))
            if cmd in ["box", "sphere", "torus"]:
                polygons = []
                if cmd == "box":
                    add_box(polygons, command[1],command[2],command[3],command[4],command[5],command[6])
                if cmd == "sphere":
                    add_sphere(polygons, command[1],command[2],command[3],command[4],5)
                if cmd == "torus":
                    add_torus(polygons, command[1],command[2],command[3],command[4],command[5],5)
                matrix_mult(stack[len(stack)-1], polygons)
                draw_polygons(polygons, screen, color)
            if cmd == "line":
                points = []
                add_edge(points, command[1],command[2],command[3],command[4],command[5],command[6])
                matrix_mult(stack[len(stack)-1], points)
                draw_lines(polygons, screen, color)
            if cmd == "save":
                save_ppm(screen, cmd[1])
            if cmd == "display":
                display(screen)
        if frames > 1:
            save_extension(screen, "animations/" + basename + "%03d"%frame + ".png")
            screen = new_screen()
            stack = []
Example #35
0
def run(filename):
    """
    This function runs an mdl script
    """
    
    
    p = mdl.parseFile(filename)
    
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
    
    basename, num_frames = first_pass(commands)
    frames = second_pass(commands, num_frames)
    lights, ambient = third_pass(symbols)
    
    for frame_num in range(num_frames):
        view = [0,
                0,
                1];
        ambient = [50,
                   50,
                   50]
        light = [[0.5,
                  0.75,
                  1],
                 [0,
                  255,
                  255]]
        areflect = [0.1,
                    0.1,
                    0.1]
        dreflect = [0.5,
                    0.5,
                    0.5]
        sreflect = [0.5,
                    0.5,
                    0.5]
        color = [0, 0, 0]
        knob_val = 1

        tmp = new_matrix()
        ident( tmp )

        stack = [ [x[:] for x in tmp] ]
        screen = new_screen()
        zbuffer = new_zbuffer()
        tmp = []
        step_3d = 20
        consts = ''
        coords = []
        coords1 = []
        knob_value = 1
        
        if num_frames > 1:
            for knob in frames[frame_num]:
                symbols[knob][1] = frames[frame_num][knob]
                
        for command in commands:
            c = command['op']
            args = command['args']
            
            if c in ['move', 'scale', 'rotate'] and command['knob']:
                knob_val = symbols[command['knob']][1]

            
            if c == 'box':
                if isinstance(args[0], str):
                    consts = args[0]
                    args = args[1:]
                if isinstance(args[-1], str):
                    coords = args[-1]
                add_box(tmp,
                        args[0], args[1], args[2],
                        args[3], args[4], args[5])
                matrix_mult( stack[-1], tmp )
                draw_polygons(tmp, screen, zbuffer, view, ambient, lights, symbols[command['constants']][1])
                tmp = []
            elif c == 'sphere':
                add_sphere(tmp,
                           args[0], args[1], args[2], args[3], step_3d)
                matrix_mult( stack[-1], tmp )
                draw_polygons(tmp, screen, zbuffer, view, ambient, lights, symbols[command['constants']][1])
                tmp = []
            elif c == 'torus':
                add_torus(tmp,
                          args[0], args[1], args[2], args[3], args[4], step_3d)
                matrix_mult( stack[-1], tmp )
                draw_polygons(tmp, screen, zbuffer, view, ambient, lights, symbols[command['constants']][1])
                tmp = []
            elif c == 'line':
                if isinstance(args[0], str):
                    consts = args[0]
                    args = args[1:]
                if isinstance(args[3], str):
                    coords = args[3]
                    args = args[:3] + args[4:]
                if isinstance(args[-1], str):
                    coords1 = args[-1]
                add_edge(tmp,
                         args[0], args[1], args[2], args[3], args[4], args[5])
                matrix_mult( stack[-1], tmp )
                draw_lines(tmp, screen, zbuffer, color)
                tmp = []
            elif c == 'move':
                tmp = make_translate(args[0] * knob_val, args[1] * knob_val, args[2] * knob_val)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':
                tmp = make_scale(args[0] * knob_val, args[1] * knob_val, args[2] * knob_val)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':
                theta = args[1] * (math.pi/180) * knob_val
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult( stack[-1], tmp )
                stack[-1] = [ x[:] for x in tmp]
                tmp = []
            elif c == 'push':
                stack.append([x[:] for x in stack[-1]] )
            elif c == 'pop':
                stack.pop()
            elif c == 'display':
                display(screen)
            elif c == 'save':
                save_extension(screen, args[0])
        if num_frames > 1:
            frame_name = 'anim/%s%03d.png' % (basename, frame_num)
            print 'Saving frame: ' + str(frame_num)
            save_extension( screen, frame_name )
    if num_frames > 1:
        make_animation(basename)
Example #36
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()
    #I made matrix_mult return m2, which is helpful 
    
    print" here "

    for command in commands:
    #Andreas is doing these 
        if command[0] == 'push':
            stack.append(copy.deepcopy(stack[-1]))
        elif command[0] == 'pop':
            stack.pop()
        elif command[0] == 'move':
            tmp = make_translate(command[1],command[2], command[3])
            stack[-1] = matrix_mult(stack[-1],tmp)
        elif command[0] == 'rotate':
            if command[1] == "x":
                stack[-1] = matrix_mult(stack[-1], make_rotX(command[2]))
            elif command[1] =="y":
                stack[-1] = matrix_mult(stack[-1], make_rotY(command[2]))
            elif command[1] =="z":
                stack[-1] = matrix_mult(stack[-1], make_rotZ(command[2]))
        elif command[0] == 'scale':
                stack[-1] = matrix_mult(stack[-1],make_scale(command[1],command[2], command[3]))
        elif command[0] == 'box':
                tmp =[]
                add_box(tmp, command[1], command[2], command[3], command[4],command[5], command[6])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, color)
                
    
    #cooper you do these 
        elif command[0] == 'torus':
                tmp = []
                step = command[6]
                if not step:
                    step = 5
                add_torus(tmp, command[1],command[2], command[3], command[4],command[5], step)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, color)
        elif command[0] == 'sphere':
                tmp = []
                step = command[5]
                if not step:
                    step = 5
                add_sphere(tmp, command[1],command[2], command[3], command[4], step )
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, color)
        elif command[0] == 'line':
                tmp = []
                add_edge( tmp, command[1], command[2], command[3], command[4], command[5], command[6])
                matrix_mult(stack[-1],temp)
                draw_lines(temp, screen, color)
        elif command[0] == 'save':
                fname = command[1]
                save_extension(screen, fname)
        elif command[0] == 'display':
                display(screen)
        print command
Example #37
0
def run(filename):
    """
    This function runs an mdl script
    """
    zbuff = z_buffer()
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [tmp]
    screen = new_screen()
    counter = 0
    first_pass(commands)
    if NUM_FRAMES == 1:
        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [tmp]

            if command[0] == "push":
                stack.append(stack[-1][:])

            if command[0] == "save":
                print command[0]
                save_extension(screen, command[1])

            if command[0] == "display":
                display(screen)

            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 2)
                matrix_mult(stack[-1], m)
                counter = (counter + 1) % 3
                color = coors[counter]
                draw_polygons(m, screen, color, zbuff)

            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                draw_polygons(m, screen, color, zbuff)

            if command[0] == "box":
                m = []
                counter = (counter + 1) % 3
                color = coors[counter]
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons(m, screen, color, zbuff)

            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)

            if command[0] == "bezier":
                m = []
                add_curve(
                    m,
                    command[1],
                    command[2],
                    command[3],
                    command[4],
                    command[5],
                    command[6],
                    command[7],
                    command[8],
                    0.05,
                    "bezier",
                )
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)

            if command[0] == "hermite":
                m = []
                add_curve(
                    m,
                    command[1],
                    command[2],
                    command[3],
                    command[4],
                    command[5],
                    command[6],
                    command[7],
                    command[8],
                    0.05,
                    "hermite",
                )
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)

            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], 0.05)
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)

            if command[0] == "move":
                k = 1
                xval = command[1] * k
                yval = command[2] * k
                zval = command[3] * k

                t = make_translate(xval, yval, zval)
                matrix_mult(stack[-1], t)
                stack[-1] = t

            if command[0] == "scale":
                xval = command[1]
                yval = command[2]
                zval = command[3]

                t = make_scale(xval, yval, zval)
                matrix_mult(stack[-1], t)
                stack[-1] = t

            if command[0] == "rotate":
                angle = command[2] * (math.pi / 180)

                if command[1] == "x":
                    t = make_rotX(angle)
                elif command[1] == "y":
                    t = make_rotY(angle)
                elif command[1] == "z":
                    t = make_rotZ(angle)

                matrix_mult(stack[-1], t)
                stack[-1] = t
        return

    knobs = second_pass(commands, NUM_FRAMES)
    # , zbuff

    if NUM_FRAMES <= 0:
        print "error"
        return

    for i in range(NUM_FRAMES):
        zbuff = z_buffer()

        stack = [tmp]
        screen = new_screen()

        commands[-1] = "save", "animations/" + BASENAME + str("%03d" % i) + ".png"
        curr_knobs = knobs[i]

        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [tmp]
            if command[0] == "push":
                stack.append(stack[-1][:])
            if command[0] == "save":
                print command[0], i
                save_extension(screen, command[1])
            if command[0] == "display":
                display(screen)
            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                draw_polygons(m, screen, color, zbuff)
            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                draw_polygons(m, screen, color, zbuff)
            if command[0] == "box":
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons(m, screen, color, zbuff)
            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)
            if command[0] == "bezier":
                m = []
                add_curve(
                    m,
                    command[1],
                    command[2],
                    command[3],
                    command[4],
                    command[5],
                    command[6],
                    command[7],
                    command[8],
                    0.05,
                    "bezier",
                )
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)
            if command[0] == "hermite":
                m = []
                add_curve(
                    m,
                    command[1],
                    command[2],
                    command[3],
                    command[4],
                    command[5],
                    command[6],
                    command[7],
                    command[8],
                    0.05,
                    "hermite",
                )
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)
            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], 0.05)
                matrix_mult(stack[-1], m)
                draw_lines(m, screen, color, zbuff)
            if command[0] == "move":
                k = 1
                if command[4] != None:
                    k = curr_knobs[command[4]]
                xval = command[1] * k
                yval = command[2] * k
                zval = command[3] * k

                t = make_translate(xval, yval, zval)
                matrix_mult(stack[-1], t)
                stack[-1] = t
            if command[0] == "scale":
                k = 1
                if command[4] != None:
                    k = curr_knobs[command[4]]

                xval = command[1] * k
                yval = command[2] * k
                zval = command[3] * k
                t = make_scale(xval, yval, zval)
                matrix_mult(stack[-1], t)
                stack[-1] = t

            if command[0] == "rotate":
                k = 1
                if command[3] != None:
                    k = curr_knobs[command[3]]

                angle = command[2] * (math.pi / 180) * k
                if command[1] == "x":
                    t = make_rotX(angle)
                elif command[1] == "y":
                    t = make_rotY(angle)
                elif command[1] == "z":
                    t = make_rotZ(angle)

                matrix_mult(stack[-1], t)
                stack[-1] = t
Example #38
0
def run(filename):
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)
    p = mdl.parseFile(filename)
    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
    
    animation_values = first_pass(commands)
    isAnimated = True
    frame_values = []
    if animation_values[0] is None:
        animation_values[0] = 1
        isAnimated = False
    else:
        frame_values = second_pass(commands, animation_values[0])
    for i in range(animation_values[0]):
        screen = new_screen()
        z_buffer = new_screen(XRES, YRES, [None])
        stack = [tmp]
        print "Frame %04d" % i
        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [tmp]
            if command[0] == "push":
                stack.append(stack[-1][:])
            if command[0] == "save":
                save_extension(screen, command[1])
            if command[0] == "display":
                display(screen)
            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                draw_flat_polygons(m, screen, z_buffer)
                #draw_polygons(m, screen, z_buffer, color)
            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                draw_flat_polygons(m, screen, z_buffer)
                #draw_polygons( m, screen, z_buffer, color )
            if command[0] == "box":
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_flat_polygons(m, screen, z_buffer)
                #draw_polygons( m, screen, z_buffer, color )
            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, z_buffer, color )
            if command[0] == "bezier":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'bezier')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, z_buffer, color )
            if command[0] == "hermite":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'hermite')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, z_buffer, color )
            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], .05)
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )
            if command[0] in ["move", "scale", "rotate"]:
                factor = 1
                if command[-1] and len(frame_values) > 0:
                    factor = frame_values[i][command[-1]]
                t = new_matrix()
                if command[0] == "move":                
                    xval = command[1] * factor
                    yval = command[2] * factor
                    zval = command[3] * factor
                    t = make_translate(xval, yval, zval)
                if command[0] == "scale":
                    xval = command[1] * factor
                    yval = command[2] * factor
                    zval = command[3] * factor
                    t = make_scale(xval, yval, zval)
                if command[0] == "rotate":
                    angle = command[2] * (math.pi / 180) * factor
                    if command[1] == 'x':
                        t = make_rotX( angle )
                    elif command[1] == 'y':
                        t = make_rotY( angle )
                    elif command[1] == 'z':
                        t = make_rotZ( angle )            
                matrix_mult( stack[-1], t )
                stack[-1] = t
        if isAnimated:
            save_extension(screen, "anim/" + animation_values[1] + "%04d.png" % i)
Example #39
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )
    polygons = []
    
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()
    top = 0
    step = 5
    print filename

    print stack[top]
    for command in commands:
        print command

        cmd = command[0]
        if cmd == "push":
            tmp = [row[:] for row in stack[-1]]
            stack.append(tmp)
            top+=1
            
        elif cmd == "pop":
            stack.pop()
            top-=1
            
        elif cmd == "move":
            tmp = make_translate( command[1], command[2], command[3])
            #print tmp
            matrix_mult(stack[top], tmp)
            stack[top] = tmp
            
        elif cmd == "rotate":
            theta = command[2] * math.pi / 180
            if command[1] == "x":
                tmp = make_rotX( theta )
            elif command[1] == "y":
                tmp = make_rotY( theta )
            elif command [1] == "z":
                tmp = make_rotZ( theta )
            else:
                print "invalid/incomplete call to ROTATE"
            matrix_mult(stack[top], tmp)
            stack[top] = tmp
            
        elif cmd == "scale":
            tmp = make_scale( command[1], command[2], command[3])
            matrix_mult(stack[top], tmp)
            stack[top] = tmp
            
        elif cmd == "box":
            add_box(polygons, command[1], command[2], command[3], command[4], command[5], command[6])
            matrix_mult(stack[top], polygons)
            draw_polygons(polygons, screen, color)
            polygons = []
            stack[top] = tmp
            
        elif cmd == "sphere":
            add_sphere(polygons, command[1], command[2], command[3], command[4], step)
            matrix_mult(stack[top], polygons)
            draw_polygons(polygons, screen, color)
            polygons = []
            stack[top] = tmp
            
        elif cmd == "torus":
            add_torus(polygons, command[1], command[2], command[3], command[4], command[5], step)
            matrix_mult(stack[top], polygons)
            draw_polygons(polygons, screen, color)
            polygons=[]
            stack[top] = tmp
            
        elif cmd == "clear":
            poly = []
            ident(tmp)
            clear_screen(screen)
        elif cmd == "display":
            display(screen)
        elif cmd == "save":
            try:
                save_extension(screen, command[1])
            except:
                save_extention(screen, "img.png")
        else:
            print "did not recognize the command: "+cmd
Example #40
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    view = [0,
            0,
            1];
    ambient = [50,
               50,
               50]
    light = [[0.5,
              0.75,
              1],
             [255,
              255,
              255]]

    color = [0, 0, 0]
    symbols['.white'] = ['constants',
                         {'red': [0.2, 0.5, 0.5],
                          'green': [0.2, 0.5, 0.5],
                          'blue': [0.2, 0.5, 0.5]}]
    reflect = '.white'

    (name, num_frames) = first_pass(commands)
    frames = second_pass(commands, num_frames)

    tmp = new_matrix()
    ident( tmp )

    stack = [ [x[:] for x in tmp] ]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 100
    consts = ''
    coords = []
    coords1 = []

    for command in commands:
        print command
        c = command['op']
        args = command['args']
        knob_value = 1

        if c == 'box':
            if command['constants']:
                reflect = command['constants']
            add_box(tmp,
                    args[0], args[1], args[2],
                    args[3], args[4], args[5])
            matrix_mult( stack[-1], tmp )
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
            reflect = '.white'
        elif c == 'sphere':
            if command['constants']:
                reflect = command['constants']
            add_sphere(tmp,
                       args[0], args[1], args[2], args[3], step_3d)
            matrix_mult( stack[-1], tmp )
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
            reflect = '.white'
        elif c == 'torus':
            if command['constants']:
                reflect = command['constants']
            add_torus(tmp,
                      args[0], args[1], args[2], args[3], args[4], step_3d)
            matrix_mult( stack[-1], tmp )
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
            reflect = '.white'
        elif c == 'line':
            add_edge(tmp,
                     args[0], args[1], args[2], args[3], args[4], args[5])
            matrix_mult( stack[-1], tmp )
            draw_lines(tmp, screen, zbuffer, color)
            tmp = []
        elif c == 'move':
            tmp = make_translate(args[0], args[1], args[2])
            matrix_mult(stack[-1], tmp)
            stack[-1] = [x[:] for x in tmp]
            tmp = []
        elif c == 'scale':
            tmp = make_scale(args[0], args[1], args[2])
            matrix_mult(stack[-1], tmp)
            stack[-1] = [x[:] for x in tmp]
            tmp = []
        elif c == 'rotate':
            theta = args[1] * (math.pi/180)
            if args[0] == 'x':
                tmp = make_rotX(theta)
            elif args[0] == 'y':
                tmp = make_rotY(theta)
            else:
                tmp = make_rotZ(theta)
            matrix_mult( stack[-1], tmp )
            stack[-1] = [ x[:] for x in tmp]
            tmp = []
        elif c=='mesh':
            meshFaces(tmp,args[0]+".obj")
            matrix_mult(stack[-1],tmp)
            draw_polygons(tmp,screen,zbuffer,view,ambient,light,symbols,reflect)
            tmp=[]
        elif c == 'cylinder':
            if command['constants']:
                reflect = command['constants']
            add_cylinder(tmp,
                        args[0], args[1], args[2], args[3], args[4], step_3d)
            matrix_mult( stack[-1], tmp )
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
            reflect = '.white'
        elif c == 'cone':
            if command['constants']:
                reflect = command['constants']
            add_cone(tmp,
                             args[0], args[1], args[2], args[3], args[4], step_3d)
            matrix_mult( stack[-1], tmp )
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
            reflect = '.white'
        elif c == 'push':
            stack.append([x[:] for x in stack[-1]] )
        elif c == 'pop':
            stack.pop()
        elif c == 'display':
            display(screen)
        elif c == 'save':
            save_extension(screen, args[0])
Example #41
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
    
    stack = [ tmp ]
    screen = new_screen()    

    first_pass(commands)
    second_pass(commands, num_frames)
    print knobs

    for i in xrange(num_frames):
        
        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [ tmp ]

            if command[0] == "push":
                stack.append( stack[-1][:] )

            if command[0] == "save":
                save_extension(screen, command[1])

            if command[0] == "display":
                display(screen)

            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "box":                
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "bezier":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'bezier')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "hermite":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'hermite')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], .05)
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "move":                
                xval = command[1]
                yval = command[2]
                zval = command[3]
                if len(command) > 4: #knob present
                    knob = command[4]

                if knob:
                    print "current knob:" + knob
                    print "value of " + knob + ":" + str(knobs[i][knob])
                    xval = xval * knobs[i][knob]
                    yval = yval * knobs[i][knob]
                    zval = zval * knobs[i][knob]
                t = make_translate(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t


            if command[0] == "scale":
                xval = command[1]
                yval = command[2]
                zval = command[3]
                if len(command) > 4: #knob present
                    knob = command[4]

                if knob:
                    print "current knob:" + knob
                    print "value of " + knob + ":" + str(knobs[i][knob])
                    xval = xval * knobs[i][knob]
                    yval = yval * knobs[i][knob]
                    zval = zval * knobs[i][knob]
                t = make_scale(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t
                

            if command[0] == "rotate":
                angle = command[2] * (math.pi / 180)
                if len(command) > 3: #knob present
                    knob = command[3]

                if knob:
                    print "current knob:" + knob
                    print "value of " + knob + ":" + str(knobs[i][knob])                   
                    angle = angle * knobs[i][knob]
                if command[1] == 'x':
                    t = make_rotX( angle )
                elif command[1] == 'y':
                    t = make_rotY( angle )
                elif command[1] == 'z':
                    t = make_rotZ( angle )            

                matrix_mult( stack[-1], t )
                stack[-1] = t
        pic_location = "{0}/{1:03}{0}.png".format(basename, i)
        print pic_location
        save_extension(screen, pic_location)
        knob = ''
        screen = new_screen()
        stack = [ tmp ]
Example #42
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)
    screen = new_screen()
    step = 0.01

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    BASENAME, NUM_FRAMES = first_pass(commands)
    knobs = second_pass(commands, NUM_FRAMES)

    if NUM_FRAMES == -1:
        NUM_FRAMES = 1

    for frame_num in xrange(NUM_FRAMES):
        print 'Pass {0}...    '.format(frame_num),
        tmp = new_matrix()
        ident(tmp)
        stack = [[x[:] for x in tmp]]

        tmp = []
        step = 0.1

        for command in commands:
            c = command[0]
            args = command[1:]

            if c == 'set':
                symbols[args[0]][1] = float(args[1])

            elif c == 'setknobs':
                for s in symbols:
                    if symbols[s][0] == 'knob':
                        symbols[s][1] = float(args[0])

            elif c == 'box':
                add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, color)
                tmp = []

            elif c == 'sphere':
                add_sphere(tmp, args[0], args[1], args[2], args[3], step)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, color)
                tmp = []
            elif c == 'torus':
                add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                          step)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, color)
                tmp = []
            elif c == 'move':

                if args[3] != None:
                    a = knobs[frame_num][args[3]] * args[0]
                    b = knobs[frame_num][args[3]] * args[1]
                    c = knobs[frame_num][args[3]] * args[2]
                    args = (a, b, c, args[3])

                tmp = make_translate(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':

                if args[3] != None:
                    a = knobs[frame_num][args[3]] * args[0]
                    b = knobs[frame_num][args[3]] * args[1]
                    c = knobs[frame_num][args[3]] * args[2]
                    args = (a, b, c, args[3])

                tmp = make_scale(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':

                if args[2] != None:
                    a = knobs[frame_num][args[2]] * args[1]
                    args = (args[0], a, args[2])

                theta = args[1] * (math.pi / 180)
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []

            elif c == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c == 'pop':
                stack.pop()
            elif c == 'display':
                display(screen)
            elif c == 'save':
                save_extension(screen, args[0])
        print 'Done!'

        name = 'anim/' + BASENAME + (3 - len(str(frame_num))) * '0' + str(
            frame_num) + '.ppm'

        if not os.path.exists('anim'):
            os.makedirs('anim')

        save_ppm(screen, name)
        clear_screen(screen)

    make_animation(BASENAME)
Example #43
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()

    for command in commands:
        
        print command
        print stack
        cmd = command[0]
        args = command[1:]
        
        if cmd == "push":
            stack.append(copy.deepcopy(stack[-1]))
        elif cmd == "pop":
            stack.pop()
        elif cmd == "move":
            t = make_translate( args[0], args[1], args[2] )
            matrix_mult( stack[-1], t )
            stack[-1] = t
        elif cmd == "rotate":
            angle = args[1] * pi / 180.0
            if args[0] == 'x':
                r = make_rotX( angle )
            elif args[0] == 'y':
                r = make_rotY( angle )
            elif args[0] == 'z':
                r = make_rotZ( angle )
            matrix_mult( stack[-1], r )
            stack[-1] = r
        elif cmd == "scale":
            s = make_scale( args[0], args[1], args[2] )
            matrix_mult( stack[-1], s )
            stack[-1] = s
        elif cmd == "box":
            temp = []
            add_box( temp, args[0], args[1], args[2], args[3], args[4], args[5] )
            matrix_mult( stack[-1], temp )
            draw_polygons( temp, screen, color)
        elif cmd == "sphere":
            temp = []
            add_sphere( temp, args[0], args[1], args[2], args[3], 5 )
            matrix_mult( stack[-1], temp )
            draw_polygons( temp, screen, color)
        elif cmd == "torus":
            temp = []
            add_torus( temp, args[0], args[1], args[2], args[3], args[4], 5 )
            matrix_mult( stack[-1], temp )
            draw_polygons( temp, screen, color)
        elif cmd == "line":
            temp = []
            add_edge( temp, args[0], args[1], args[2], args[3], args[4], args[5] )
            matrix_mult( stack[-1], temp )
            draw_lines( temp, screen, color)
        elif cmd == "save":
            save_extension( screen, args[0] )
        elif cmd == "display":
            display( screen )
            pass
Example #44
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = [[0.5, 0.75, 1], [255, 255, 255]]

    color = [0, 0, 0]
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.5],
            'green': [0.2, 0.5, 0.5],
            'blue': [0.2, 0.5, 0.5]
        }
    ]
    reflect = '.white'

    (name, num_frames) = first_pass(commands)
    frames = second_pass(commands, num_frames)
    for frame in range(int(num_frames)):
        if num_frames > 1:
            for knob in frames[frame]:
                symbols[knob] = ['knob', frames[frame][knob]]
        tmp = new_matrix()
        ident(tmp)

        stack = [[x[:] for x in tmp]]
        screen = new_screen()
        zbuffer = new_zbuffer()
        tmp = []
        step_3d = 100
        consts = ''
        coords = []
        coords1 = []

        for command in commands:
            print(command)
            c = command['op']
            args = command['args']
            knob_value = 1

            if c == 'box':
                if command['constants']:
                    reflect = command['constants']
                add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'sphere':
                if command['constants']:
                    reflect = command['constants']
                add_sphere(tmp, args[0], args[1], args[2], args[3], step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'torus':
                if command['constants']:
                    reflect = command['constants']
                add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                          step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'cone':
                if command['constants']:
                    reflect = command['constants']
                add_cone(tmp, args[0], args[1], args[2], args[3], args[4],
                         step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'cylinder':
                if command['constants']:
                    reflect = command['constants']
                add_cylinder(tmp, args[0], args[1], args[2], args[3], args[4],
                             step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'pyramid':
                if command['constants']:
                    reflect = command['constants']
                add_pyramid(tmp, args[0], args[1], args[2], args[3], args[4])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'line':
                add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                         args[5])
                matrix_mult(stack[-1], tmp)
                draw_lines(tmp, screen, zbuffer, color)
                tmp = []
            elif c == 'move':
                if command['knob']:
                    tmp = make_translate(args[0] * symbols[command['knob']][1],
                                         args[1] * symbols[command['knob']][1],
                                         args[2] * symbols[command['knob']][1])
                else:
                    tmp = make_translate(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':
                if command['knob']:
                    tmp = make_scale(args[0] * symbols[command['knob']][1],
                                     args[1] * symbols[command['knob']][1],
                                     args[2] * symbols[command['knob']][1])
                else:
                    tmp = make_scale(args[0], args[1], args[2])
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':
                if command['knob']:
                    theta = args[1] * (math.pi /
                                       180) * symbols[command['knob']][1]
                else:
                    theta = args[1] * (math.pi / 180)
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'mesh':
                if command['constants']:
                    reflect = command['constants'] if command[
                        'constants'] != ':' else '.white'
                add_mesh(tmp, command['cs'])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
                tmp = []
            elif c == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c == 'pop':
                stack.pop()
            elif c == 'display':
                display(screen)
            elif c == 'save':
                save_extension(screen, args[0] + '.png')
            # end operation loop
        if num_frames > 1:
            save_extension(screen, "anim/" + name + "%03d" % frame + '.png')
    if num_frames > 1:
        make_animation(name)
Example #45
0
def run(filename):
    global basename
    global num_frames
    global varies
    global has_anim
    global constantd
    global ls
    global a
    """
    This function runs an mdl script
    """
    color = [100, 100, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
        
    stack = [new_matrix()]
    ident(stack[-1])

    screen = new_screen()
    zbuff = [[-9223372036854775807 for x in range(500)] for x in range(500)] 
    first_pass(commands)
    print num_frames
    print basename


    second_pass(commands,num_frames)
    print "STACK: " + str(stack)
    for frame in range(0,num_frames):
        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [ tmp ]

            if command[0] == "push":
                stack.append(stack[-1][:] )

            if command[0] == "save":
                save_extension(screen, command[1])

            if command[0] == "display":
                display(screen)

            if command[0] == "sphere":
                m = []
                #constan = constantd[command[1]]
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color,zbuff,[.3,.3,.3],[.3,.3,.3],[.8,.8,.8],[255,255,255],[[[0,0,255],[command[2]-10, command[3]-10, command[4]-100]]] )

            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 15)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color,zbuff,[.5,.5,.5],[.4,.4,.4],[.1,.1,.1],[0,0,0],[[[150,0,50],[command[1]-10, command[2]-10, command[3]-100]]] )

            if command[0] == "box":
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color,zbuff,[.5,.5,.5],[.4,.4,.4],[.1,.1,.1],[0,0,0],[[[0,0,255],[command[2]-10, command[3]-10, command[4]-100]]] )

            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color,zbuff )

            if command[0] == "bezier":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'bezier')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color,zbuff )

            if command[0] == "hermite":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'hermite')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color,zbuff )

            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], .05)
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )
          

            if command[0] in ["move","scale","rotate"]:
                if command[0] == "move":
                    xval = command[1]
                    yval = command[2]
                    zval = command[3]
                    if command[len(command)-1]:
                        print "knobing the move"
                        xval*=knobs[frame][command[len(command)-1]]
                        yval*=knobs[frame][command[len(command)-1]]
                        zval*=knobs[frame][command[len(command)-1]]
                    t = make_translate(xval, yval, zval)

                elif command[0] == "scale":
                    xval = command[1]
                    yval = command[2]
                    zval = command[3]
                    if command[len(command)-1]:
                        xval*=knobs[frame][command[len(command)-1]]
                        yval*=knobs[frame][command[len(command)-1]]
                        zval*=knobs[frame][command[len(command)-1]]
                    t = make_scale(xval, yval, zval)
                
                elif command[0] == "rotate":
                    angle = command[2] * (math.pi / 180)
                    print angle
                    if command[len(command)-1]:
                        angle*=knobs[frame][command[len(command)-1]]
                    if command[1] == 'x':
                        t = make_rotX( angle )
                    elif command[1] == 'y':
                        t = make_rotY( angle )
                    elif command[1] == 'z':
                        t = make_rotZ( angle )
                
                #print "TRANSFORMATION MATRIX: " +str(t)
        

                #print command[0]
                #print t
                #print "STACK PRE TRANSFORM: " + str(stack[-1])
                matrix_mult( stack[-1], t )
                stack[-1] = t
                #print "STACK POST TRANSFORM: " + str(stack[-1])

        if has_anim:
            print "Saving frame "+ str(frame)
            #print screen
            save_extension(screen, "animation/"+basename+str(frame).zfill(3)+".png" )
            screen = new_screen()
            stack = [new_matrix()]
            ident(stack[-1])
Example #46
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    print("======")
    print(p)

    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = [[0.5, 0.75, 1], [255, 255, 255]]

    color = [0, 0, 0]
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.5],
            'green': [0.2, 0.5, 0.5],
            'blue': [0.2, 0.5, 0.5]
        }
    ]
    reflect = '.white'

    (name, num_frames) = first_pass(commands)
    frames = second_pass(commands, num_frames)

    fr = 0
    for frame in frames:
        tmp = new_matrix()
        ident(tmp)

        stack = [[x[:] for x in tmp]]
        screen = new_screen()
        zbuffer = new_zbuffer()
        tmp = []
        step_3d = 100
        consts = ''
        coords = []
        coords1 = []

        for knob in frame.keys():
            symbols[knob] = frame[knob]

        for command in commands:
            print(command)
            c = command['op']
            args = command['args']
            knob_value = 1

            if c == 'box':
                if command['constants']:
                    reflect = command['constants']
                add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                if command["cs"] == None:
                    matrix_mult(stack[-1], tmp)
                else:
                    cs = command["cs"]
                    matrix_mult(symbols[cs], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'sphere':
                if command['constants']:
                    reflect = command['constants']
                add_sphere(tmp, args[0], args[1], args[2], args[3], step_3d)
                if command["cs"] == None:
                    matrix_mult(stack[-1], tmp)
                else:
                    cs = command["cs"]
                    matrix_mult(symbols[cs], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'torus':
                if command['constants']:
                    reflect = command['constants']
                add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                          step_3d)
                if command["cs"] == None:
                    matrix_mult(stack[-1], tmp)
                else:
                    cs = command["cs"]
                    matrix_mult(symbols[cs], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'

            elif c == 'pyramid':
                if command['constants']:
                    reflect = command['constants']
                    #x y z height s
                add_pyramid(tmp, args[0], args[1], args[2], args[3], args[4])
                if command["cs"] == None:
                    matrix_mult(stack[-1], tmp)
                else:
                    cs = command["cs"]
                    matrix_mult(symbols[cs], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'triangle':
                if command['constants']:
                    reflect = command['constants']
                    #x y z height s
                add_triangle(tmp, args[0], args[1], args[2], args[3], args[4],
                             args[5])
                if command["cs"] == None:
                    matrix_mult(stack[-1], tmp)
                else:
                    cs = command["cs"]
                    matrix_mult(symbols[cs], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'mesh':
                if command['constants']:
                    reflect = command['constants']
                if command['cs']:
                    tmp = []
                #print(args)
                add_mesh(tmp, args[0])
                if command["cs"] == None:
                    matrix_mult(stack[-1], tmp)
                else:
                    cs = command["cs"]
                    matrix_mult(symbols[cs], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'save_coord_system':
                cs = command['cs']
                symbols[cs] = stack[-1]

            elif c == 'line':
                add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                         args[5])
                matrix_mult(stack[-1], tmp)
                draw_lines(tmp, screen, zbuffer, color)
                tmp = []
            elif c == 'move':
                if command["knob"]:
                    knob_value = symbols[command["knob"]]
                tmp = make_translate(args[0] * knob_value,
                                     args[1] * knob_value,
                                     args[2] * knob_value)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':
                if command["knob"]:
                    knob_value = symbols[command["knob"]]
                print(args)
                tmp = make_scale(args[0] * knob_value, args[1] * knob_value,
                                 args[2] * knob_value)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':
                if command["knob"]:
                    knob_value = symbols[command["knob"]]
                theta = args[1] * (math.pi / 180) * knob_value
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c == 'pop':
                stack.pop()
            elif c == 'display':
                display(screen)
            elif c == 'save':
                save_extension(screen, args[0])

        save_extension(screen, 'anim/' + name + "%03d" % fr)
        fr += 1
        # end operation loop

    make_animation(name)
Example #47
0
        if command[0] == "scale":
            xval = command[1]
            yval = command[2]
            zval = command[3]

            t = make_scale(xval, yval, zval)
            matrix_mult( stack[-1], t )
            stack[-1] = t
            
        if command[0] == "rotate":
            angle = command[2] * (math.pi / 180)

            if command[1] == 'x':
                t = make_rotX( angle )
            elif command[1] == 'y':
                t = make_rotY( angle )
            elif command[1] == 'z':
                t = make_rotZ( angle )            
                
            matrix_mult( stack[-1], t )
            stack[-1] = t
            
if __name__ == "__main__":
    p = mdl.parseFile(raw_input(">"))

    if p:
        (commands, symbols) = p

    first_pass(commands)
    print second_pass(commands)
Example #48
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = [[0.5, 0.75, 1], [255, 255, 255]]

    color = [0, 0, 0]
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.5],
            'green': [0.2, 0.5, 0.5],
            'blue': [0.2, 0.5, 0.5]
        }
    ]
    reflect = '.white'

    (name, num_frames) = first_pass(commands)
    frames = second_pass(commands, num_frames)
    print(frames)

    cur_frame = 0
    while (cur_frame < num_frames):
        print('Making frame ' + str(cur_frame))

        tmp = new_matrix()
        ident(tmp)

        stack = [[x[:] for x in tmp]]
        screen = new_screen()
        zbuffer = new_zbuffer()
        tmp = []
        step_3d = 100
        consts = ''
        coords = []
        coords1 = []

        for command in commands:
            c = command['op']
            args = command['args']
            knob_value = 1

            if c == 'box':
                if command['constants']:
                    reflect = command['constants']
                add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'sphere':
                if command['constants']:
                    reflect = command['constants']
                add_sphere(tmp, args[0], args[1], args[2], args[3], step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'torus':
                if command['constants']:
                    reflect = command['constants']
                add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                          step_3d)
                matrix_mult(stack[-1], tmp)
                draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                              symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'line':
                add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                         args[5])
                matrix_mult(stack[-1], tmp)
                draw_lines(tmp, screen, zbuffer, color)
                tmp = []
            elif c == 'move':
                if (command['knob']):
                    knob = frames[cur_frame][command['knob']]
                else:
                    knob = 1
                tmp = make_translate(args[0] * knob, args[1] * knob,
                                     args[2] * knob)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':
                if (command['knob']):
                    knob = frames[cur_frame][command['knob']]
                    print(knob)
                else:
                    knob = 1
                tmp = make_scale(args[0] * knob, args[1] * knob,
                                 args[2] * knob)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':
                if (command['knob']):
                    knob = frames[cur_frame][command['knob']]
                else:
                    knob = 1
                theta = args[1] * (math.pi / 180) * knob
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c == 'pop':
                stack.pop()
            elif c == 'display':
                display(screen)
            elif c == 'save':
                save_extension(screen, args[0])
            # end operation loop

        cur_frame += 1
        if (num_frames < 100):
            save_extension(screen, 'anim/' + name + '%02d' % cur_frame)
        else:
            save_extension(screen, 'anim/' + name + '%03d' % cur_frame)

    if (num_frames > 1):
        make_animation(name)
Example #49
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print("Parsing failed.")
        return

    view = [0,
            0,
            1];
    ambient = [50,
               50,
               50]
    light = [[0.5,
              0.75,
              1],
             [255,
              255,
              255]]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident( tmp )

    stack = [ [x[:] for x in tmp] ]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 100
    consts = ''
    coords = []
    coords1 = []
    symbols['.white'] = ['constants',
                         {'red': [0.2, 0.5, 0.5],
                          'green': [0.2, 0.5, 0.5],
                          'blue': [0.2, 0.5, 0.5]}]
    reflect = '.white'

    print(symbols)
    for command in commands:
        if command['op'] == 'push':
            stack.append( [x[:] for x in stack[-1]] )

        elif command['op'] == 'pop':
            stack.pop()

        elif command['op'] == 'move':
            t = make_translate(float(command['args'][0]), float(command['args'][1]), float(command['args'][2]))
            matrix_mult(stack[-1], t)
            stack[-1] = [x[:] for x in t]

        elif command['op'] == 'rotate':
            theta = float(command['args'][1]) * (math.pi / 180)

            t = new_matrix()
            if command['args'][0] == 'x':
                t = make_rotX(theta)

            if command['args'][0] == 'y':
                t = make_rotY(theta)

            if command['args'][0] == 'z':
                t = make_rotZ(theta)

            matrix_mult(stack[-1], t)
            stack[-1] = [ x[:] for x in t ]


        elif command['op'] == 'scale':
            t = make_scale(command['args'][0],command['args'][1],command['args'][2])
            matrix_mult(stack[-1], t)
            stack[-1] = [ x[:] for x in t ]

        elif command['op'] == 'box':
            add_box(tmp, float(command['args'][0]), float(command['args'][1]), float(command['args'][2]), float(command['args'][3]), float(command['args'][4]), float(command['args'][5]))

            matrix_mult(stack[-1], tmp)

            if(command['constants']):
                draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, command['constants'])
            else:
                draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)

            tmp = []

        elif command['op'] == 'sphere':
            add_sphere(tmp, float(command['args'][0]), float(command['args'][1]), float(command['args'][2]), float(command['args'][3]), step_3d)

            matrix_mult(stack[-1], tmp)

            if(command['constants']):
                draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, command['constants'])
            else:
                draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)

            tmp = []

        elif command['op'] == 'torus':
            add_torus(tmp, float(command['args'][0]), float(command['args'][1]), float(command['args'][2]), float(command['args'][3]), float(command['args'][4]), step_3d)

            matrix_mult(stack[-1], tmp)

            if(command['constants']):
                draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, command['constants'])
            else:
                draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)

            tmp = []

        elif command['op'] == 'constants':
            pass

        elif command['op'] == 'line':
            add_edge(tmp, float(command['args'][0]), float(command['args'][1]), float(command['args'][2]), float(command['args'][3]), float(command['args'][4]), float(command['args'][5]))
            matrix_mult(stack[-1], edges)
            draw_lines(edges, screen, zbuffer, color)
            tmp = []

        elif command['op'] == 'save':
            save_extension(screen, command['args'][0] + ".png")

        elif command['op'] == 'display':
            print(command['op'])

        print(command)
Example #50
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    color_list = [ [255,255,255] , [255,255,0], [255,0,255], [0,255,255],
                   [255,0,0], [0,255,0], [0,0,255]]
    color_index = 0
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
    tmp = new_matrix()
    ident( tmp )

    num_frames = first_pass(commands)
    knobs = second_pass(commands, num_frames)
    print len(knobs)
    for i in range(num_frames):
        stack = [ tmp ]
        screen = new_screen()    



        for command in commands:
            if command[0] == "pop":
                stack.pop()
                if not stack:
                    stack = [ tmp ]
                    
            if command[0] == "push":
                stack.append( stack[-1][:] )

            if command[0] == "save":
                save_extension(screen, command[1])
            
            if command[0] == "display":
                display(screen)
            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                color = [0,0,225]
                draw_polygons( m, screen, color )
                

            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                color = [0,225,0]
                draw_polygons( m, screen, color )

            if command[0] == "box":                
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )
                
            if command[0] == "bezier":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'bezier')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "hermite":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'hermite')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], .05)
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "move":
                knob = 1
                if knobs != [] and command[4] in knobs[i]:
                    knob = knobs[i][command[4]]
                if knob > 0:
                    xval = command[1] * knob
                    yval = command[2] * knob
                    zval = command[3] * knob
                    
                    t = make_translate(xval, yval, zval)
                    matrix_mult( stack[-1], t )
                    stack[-1] = t
                
            if command[0] == "scale":
                knob = 1
                if knobs != [] and command[4] in knobs[i]:
                    knob = knobs[i][command[4]]
                if knob > 0:
                    xval = command[1] * knob
                    yval = command[2] * knob
                    zval = command[3] * knob
                    
                    t = make_scale(xval, yval, zval)
                    matrix_mult( stack[-1], t )
                    stack[-1] = t
                
            if command[0] == "rotate":
                knob = 1
                if knobs != [] and command[3] in knobs[i]:
                    print knobs[i]
                    knob = knobs[i][command[3]]
                if knob > 0:
                    angle = command[2] * (math.pi / 180) * knob
                    
                    if command[1] == 'x':
                        t = make_rotX( angle )
                    elif command[1] == 'y':
                        t = make_rotY( angle )
                    elif command[1] == 'z':
                        t = make_rotZ( angle )            
                        
                        matrix_mult( stack[-1], t )
                        stack[-1] = t
            
        if (num_frames > 1):
            save_extension(screen, "animations/" + basename + "%03d"%i + ".ppm")
            save_ppm(screen, "animations/" + basename + "%03d"%i + ".ppm")
Example #51
0
def run(filename):
    """
    This function runs an mdl script
    This is the only file you need to modify in order
    to get a working mdl project (for now).

    my_main.c will serve as the interpreter for mdl.
    When an mdl script goes through a lexer and parser,
    the resulting operations will be in the array op[].

    Your job is to go through each entry in op and perform
    the required action from the list below:

    push: push a new origin matrix onto the origin stack

    pop: remove the top matrix on the origin stack

    move/scale/rotate: create a transformation matrix
                     based on the provided values, then
                     multiply the current top of the
                     origins stack by it.

    box/sphere/torus: create a solid object based on the
                    provided values. Store that in a
                    temporary matrix, multiply it by the
                    current top of the origins stack, then
                    call draw_polygons.

    line: create a line based on the provided values. Store
        that in a temporary matrix, multiply it by the
        current top of the origins stack, then call draw_lines.

    save: call save_extension with the provided filename

    display: view the screen
    """
    view = [0,
            0,
            1];
    ambient = [50,
               50,
               50]
    light = [[0.5,
              0.75,
              1],
             [0,
              255,
              255]]
    areflect = [0.1,
                0.1,
                0.1]
    dreflect = [0.5,
                0.5,
                0.5]
    sreflect = [0.5,
                0.5,
                0.5]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident( tmp )

    stack = [ [x[:] for x in tmp] ]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    polygons = []
    edges = []
    step_3d = 20

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
        for c in commands:
            
            if c[0] == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c[0] == 'pop':
                stack.pop()

            elif c[0] == 'move':
                t = make_translate(float(c[1]), float(c[2]), float(c[3]))
                matrix_mult(stack[-1], t)
                stack[-1] = [x[:] for x in t]
            elif c[0] == 'scale':
                s = make_scale(float(c[1]), float(c[2]), float(c[3]))
                matrix_mult(stack[-1], s)
                stack[-1] = [x[:] for x in s]
            elif c[0] == 'rotate':
                theta = float(c[2]) * (math.pi / 180)
                if c[1] == 'x':
                    r = make_rotX(theta)
                elif c[1] == 'y':
                    r = make_rotY(theta)
                else:
                    r = make_rotZ(theta)
                matrix_mult(stack[-1], r)
                stack[-1] = [x[:] for x in r]

            elif c[0] == 'box':
                add_box(polygons,
                        float(c[1]), float(c[2]), float(c[3]),
                        float(c[4]), float(c[5]), float(c[6]))
                matrix_mult(stack[-1], polygons)
                draw_polygons(polygons, screen, zbuffer, view, ambient, light, areflect, dreflect, sreflect)
                polygons = []
            elif c[0] == 'sphere':
                add_sphere(polygons,
                           float(c[1]), float(c[2]), float(c[3]),
                           float(c[4]), step_3d)
                matrix_mult(stack[-1], polygons)
                draw_polygons(polygons, screen, zbuffer, view, ambient, light, areflect, dreflect, sreflect)
                polygons = []
            elif c[0] == 'torus':
                add_torus(polygons,
                          float(c[1]), float(c[2]), float(c[3]),
                          float(c[4]), float(c[5]), step_3d)
                matrix_mult(stack[-1], polygons)
                draw_polygons(polygons, screen, zbuffer, view, ambient, light, areflect, dreflect, sreflect)
                polygons = []
            elif c[0] == 'line':
                add_edge(edges,
                         float(c[1]), float(c[2]), float(c[3]),
                         float(c[4]), float(c[5]), float(c[6]))
                matrix_mult(stack[-1], edges)
                draw_lines(edges, screen, zbuffer, color)
                edges = []
                
            elif c[0] == 'save':
                save_extension(screen, c[1] + c[2])
            elif c[0] == 'display':
                display(screen)
            
    else:
        print "Parsing failed."
        return
Example #52
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = [[0.5, 0.75, 1], [255, 255, 255]]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident(tmp)

    stack = [[x[:] for x in tmp]]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 100
    consts = ''
    coords = []
    coords1 = []
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.5],
            'green': [0.2, 0.5, 0.5],
            'blue': [0.2, 0.5, 0.5]
        }
    ]
    reflect = '.white'

    for command in commands:
        if command["op"] == "push":
            stack.append([x[:] for x in stack[-1]])
        elif command["op"] == "pop":
            stack.pop()
        elif command["op"] == "move":
            t = make_translate(command["args"][0],\
                           command["args"][1],\
                           command["args"][2],)
            matrix_mult(stack[-1], t)
            stack[-1] = [x[:] for x in t]
        elif command["op"] == "rotate":
            theta = command["args"][1] * (math.pi / 180)
            if command["args"][0] == 'x':
                t = make_rotX(theta)
            elif command["args"][0] == 'y':
                t = make_rotY(theta)
            else:
                t = make_rotZ(theta)
            matrix_mult(stack[-1], t)
            stack[-1] = [x[:] for x in t]
        elif command["op"] == "scale":
            t = make_scale(command["args"][0],\
                           command["args"][1],\
                           command["args"][2],)
            matrix_mult(stack[-1], t)
            stack[-1] = [x[:] for x in t]
        elif command["op"] == "box":
            add_box(tmp,\
                        command["args"][0],\
                        command["args"][1],\
                        command["args"][2],\
                        command["args"][3],\
                        command["args"][4],\
                        command["args"][5])
            matrix_mult(stack[-1], tmp)
            if command["constants"] != None:
                draw_polygons(tmp, screen,\
                            zbuffer, view,\
                            ambient, light,\
                            symbols, command["constants"])
            else:
                draw_polygons(tmp, screen,\
                            zbuffer, view,\
                            ambient, light,\
                            symbols, reflect)
            tmp = []
        elif command["op"] == "sphere":
            add_sphere(tmp,\
                        command["args"][0],\
                        command["args"][1],\
                        command["args"][2],\
                        command["args"][3],\
                        step_3d)
            matrix_mult(stack[-1], tmp)
            if command["constants"] != None:
                draw_polygons(tmp, screen,\
                            zbuffer, view,\
                            ambient, light,\
                            symbols, command["constants"])
            else:
                draw_polygons(tmp, screen,\
                            zbuffer, view,\
                            ambient, light,\
                            symbols, reflect)
            tmp = []

        elif command["op"] == "torus":
            add_torus(tmp,\
                        command["args"][0],\
                        command["args"][1],\
                        command["args"][2],\
                        command["args"][3],\
                        command["args"][4],\
                        step_3d)
            matrix_mult(stack[-1], tmp)
            if command["constants"] != None:
                draw_polygons(tmp, screen,\
                            zbuffer, view,\
                            ambient, light,\
                            symbols, command["constants"])
            else:
                draw_polygons(tmp, screen,\
                            zbuffer, view,\
                            ambient, light,\
                            symbols, reflect)
            tmp = []
        elif command["op"] == "line":
            add_edge( edges,\
                      command["args"][0],\
                      command["args"][1],\
                      command["args"][2],\
                      command["args"][3],\
                      command["args"][4],\
                      command["args"][5])
            matrix_mult(systems[-1], edges)
            draw_lines(eges, screen, zbuffer, color)
            edges = []

        elif command["op"] == "constants":
            pass
        elif command["op"] == "save":
            save_extension(screen, command["args"][0] + ".png")
        elif command["op"] == "display":
            display(screen)
        else:
            print command
Example #53
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident(tmp)

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [tmp]
    screen = new_screen()
    points = []

    for command in commands:
        print command
        if command[0] in ARG_COMMANDS:
            if command[0] == "line":
                add_edge(points, command[1], command[2], command[3], command[4], command[5], command[6])
                matrix_mult(stack[-1], points)
                draw_lines(points, screen, color)
                points = []
            elif command[0] == "scale":
                t = make_scale(command[1], command[2], command[3])
                matrix_mult(stack[-1], t)
                stack[-1] = t
            elif command[0] == "move":
                t = make_translate(command[1], command[2], command[3])
                matrix_mult(stack[-1], t)
                stack[-1] = t
            elif command[0] == "rotate":
                if command[1] == "x":
                    t = make_rotX(command[2] * math.pi / 180)
                elif command[1] == "y":
                    t = make_rotY(command[2] * math.pi / 180)
                elif command[1] == "z":
                    t = make_rotZ(command[2] * math.pi / 180)
                matrix_mult(stack[-1], t)
                stack[-1] = t
            elif command[0] == "circle":
                add_cricle(points, command[1], command[2], 0, command[3], 0.01)
                matrix_mult(stack[-1], points)
                draw_lines(points, screen, color)
                points = []
            elif command[0] == "bezier":
                add_curve(
                    points,
                    command[1],
                    command[2],
                    command[3],
                    command[4],
                    command[5],
                    command[6],
                    command[7],
                    command[8],
                    0.01,
                    "bezier",
                )
                matrix_mult(stack[-1], points)
                draw_lines(points, screen, color)
                points = []
            elif command[0] == "hermite":
                add_curve(
                    points,
                    command[1],
                    command[2],
                    command[3],
                    command[4],
                    command[5],
                    command[6],
                    command[7],
                    command[8],
                    0.01,
                    "hermite",
                )
                matrix_mult(stack[-1], points)
                draw_lines(points, screen, color)
                points = []
            elif command[0] == "sphere":
                add_sphere(points, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], points)
                draw_polygons(points, screen, color)
                points = []
            elif command[0] == "torus":
                add_torus(points, command[1], command[2], 0, command[3], command[4], 5)
                matrix_mult(stack[-1], points)
                draw_polygons(points, screen, color)
                points = []
            elif command[0] == "box":
                add_box(points, command[1], command[2], command[3], command[4], command[5], command[6])
                matrix_mult(stack[-1], points)
                draw_polygons(points, screen, color)
                points = []
        # elif command[0] == "ident":
        #    ident(stack)
        # elif command[0] == "apply":
        #    matrix_mult(stack, points)
        elif command[0] == "push":
            stack.append(stack[-1])
        elif command[0] == "pop":
            stack.pop()
        elif command[0] == "clear":
            points = []
        if command[0] in ["display", "save"]:
            # screen = new_screen()
            # draw_polygons( points, screen, color )

            if command[0] == "display":
                display(screen)
                screen = new_screen()
            elif command[0] == "save":
                save_extension(screen, commands[1].strip())
        elif command[0] == "quit":
            return
        elif command[0] == "#":
            print "invalid command: " + command[0]
Example #54
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print ("Parsing failed.")
        return

    view = [0,
            0,
            1];
    ambient = [50,
               50,
               50]
    light = [[0.5,
              0.75,
              1],
             [255,
              255,
              255]]

    color = [0, 0, 0]
    tmp = new_matrix()
    ident( tmp )

    stack = [ [x[:] for x in tmp] ]
    screen = new_screen()
    zbuffer = new_zbuffer()
    tmp = []
    step_3d = 100
    consts = ''
    coords = []
    coords1 = []
    symbols['.white'] = ['constants',
                         {'red': [0.2, 0.5, 0.5],
                          'green': [0.2, 0.5, 0.5],
                          'blue': [0.2, 0.5, 0.5]}]
    reflect = '.white'

    # print symbols
    for command in commands:
        if command["op"] == "push":
            stack.append([x[:] for x in stack[-1]])
        if command["op"] == "pop":
            stack.pop()
        if command["op"] == "move":
            args = command["args"]
            tmp = make_translate(args[0], args[1], args[2])
            matrix_mult(stack[-1], tmp)
            stack[-1] = [x[:] for x in tmp]
            tmp = []
        if command["op"] == "rotate":
            args = command["args"]
            theta = args[1] * (math.pi/180)
            if args[0] == 'x':
                tmp = make_rotX(theta)
            elif args[0] == 'y':
                tmp = make_rotY(theta)
            else:
                tmp = make_rotZ(theta)
            matrix_mult( stack[-1], tmp )
            stack[-1] = [ x[:] for x in tmp]
            tmp = []
        if command["op"] == "scale":
            args = command["args"]
            tmp = make_scale(args[0], args[1], args[2])
            matrix_mult(stack[-1], tmp)
            stack[-1] = [x[:] for x in tmp]
            tmp = []
        if command["op"] == "box":
            args = command["args"]
            add_box(tmp,
                    args[0], args[1], args[2],
                    args[3], args[4], args[5])
            matrix_mult( stack[-1], tmp )
            if (command['constants']):
                reflect = command['constants']
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
        if command["op"] == "sphere":
            args = command["args"]
            add_sphere(tmp,
                       args[0], args[1], args[2], args[3], step_3d)
            matrix_mult( stack[-1], tmp )
            if (command['constants']):
                reflect = command['constants']
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
        if command["op"] == "torus":
            args = command["args"]
            add_torus(tmp,
                      args[0], args[1], args[2], args[3], args[4], step_3d)
            matrix_mult( stack[-1], tmp )
            if (command['constants']):
                reflect = command['constants']
            draw_polygons(tmp, screen, zbuffer, view, ambient, light, symbols, reflect)
            tmp = []
        if command["op"] == "line":
            args = command["args"]
            add_edge(tmp,
                     args[0], args[1], args[2], args[3], args[4], args[5])
            matrix_mult( stack[-1], tmp )
            draw_lines(tmp, screen, zbuffer, color)
            tmp = []
        if command["op"] == "save":
            args = command["args"]
            save_extension(screen, args[0])
            screen = new_screen()
            zbuffer = new_zbuffer()
        if command["op"] == "display":
            display(screen)
Example #55
0
def run(filename):
    """
    This function runs an mdl script
    """
    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    view = [0, 0, 1]
    ambient = [50, 50, 50]
    light = []

    color = [0, 0, 0]
    symbols['.white'] = [
        'constants', {
            'red': [0.2, 0.5, 0.5],
            'green': [0.2, 0.5, 0.5],
            'blue': [0.2, 0.5, 0.5]
        }
    ]
    reflect = '.white'

    (name, num_frames) = first_pass(commands)
    frames = second_pass(commands, num_frames)
    if num_frames > 1:
        if not path.exists('anim/' + name):
            mkdir('anim/' + name)
    print("frames")
    print(num_frames)
    shade = 0
    for i in range(num_frames):
        tmp = new_matrix()
        ident(tmp)

        stack = [[x[:] for x in tmp]]
        screen = new_screen()
        zbuffer = new_zbuffer()
        tmp = []
        step_3d = 100
        consts = ''
        coords = []
        coords1 = []
        shade = 0
        if num_frames > 1:
            for knob in frames[i]:
                symbols[knob][1] = frames[i][knob]
        for command in commands:
            print command
            c = command['op']
            args = command['args']
            knob_value = 1
            if c == 'shading':
                if command["shade_type"] == "gouraud":
                    shade = 1
            if c == 'box':
                if command['constants']:
                    reflect = command['constants']
                add_box(tmp, args[0], args[1], args[2], args[3], args[4],
                        args[5])
                matrix_mult(stack[-1], tmp)
                if shade == 1:
                    draw_polygonsG(tmp, screen, zbuffer, view, ambient, light,
                                   symbols, reflect)
                else:
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'sphere':
                if command['constants']:
                    reflect = command['constants']
                add_sphere(tmp, args[0], args[1], args[2], args[3], step_3d)
                matrix_mult(stack[-1], tmp)
                if shade == 1:
                    draw_polygonsG(tmp, screen, zbuffer, view, ambient, light,
                                   symbols, reflect)
                else:
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'torus':
                if command['constants']:
                    reflect = command['constants']
                add_torus(tmp, args[0], args[1], args[2], args[3], args[4],
                          step_3d)
                matrix_mult(stack[-1], tmp)
                if shade == 1:
                    draw_polygonsG(tmp, screen, zbuffer, view, ambient, light,
                                   symbols, reflect)
                else:
                    draw_polygons(tmp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                tmp = []
                reflect = '.white'
            elif c == 'line':
                add_edge(tmp, args[0], args[1], args[2], args[3], args[4],
                         args[5])
                matrix_mult(stack[-1], tmp)
                draw_lines(tmp, screen, zbuffer, color)
                tmp = []
            elif c == 'move':
                step = 1
                if command['knob']:
                    step = symbols[command["knob"]][1]
                tmp = make_translate(args[0] * step, args[1] * step,
                                     args[2] * step)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'scale':
                step = 1
                if command['knob']:
                    step = symbols[command["knob"]][1]
                tmp = make_scale(args[0] * step, args[1] * step,
                                 args[2] * step)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'rotate':
                step = 1
                if command['knob']:
                    step = symbols[command["knob"]][1]
                theta = args[1] * (math.pi / 180) * step
                if args[0] == 'x':
                    tmp = make_rotX(theta)
                elif args[0] == 'y':
                    tmp = make_rotY(theta)
                else:
                    tmp = make_rotZ(theta)
                matrix_mult(stack[-1], tmp)
                stack[-1] = [x[:] for x in tmp]
                tmp = []
            elif c == 'push':
                stack.append([x[:] for x in stack[-1]])
            elif c == 'pop':
                stack.pop()
            elif c == 'display':
                display(screen)
            elif c == 'save':
                save_extension(screen, args[0])
            elif c == 'mesh':
                f = open(args[0] + ".obj", 'r')
                vertex = []
                temp = []
                for line in f.readlines():
                    words = line.split(" ")
                    if words[0] == "v":
                        vertex.append([
                            20 * float(words[1]), 20 * float(words[2]),
                            20 * float(words[3])
                        ])
                    if words[0] == 'f':
                        f = []
                        for j in range(1, len(words)):
                            f.append(vertex[int(words[j]) - 1])
                        for i in range(2, len(f)):
                            add_polygon(temp, f[i - 2][0], f[i - 2][1],
                                        f[i - 2][2], f[i - 1][0], f[i - 1][1],
                                        f[i - 1][2], f[i][0], f[i][1], f[i][2])
                matrix_mult(stack[-1], temp)
                if shade == 1:
                    draw_polygonsG(temp, screen, zbuffer, view, ambient, light,
                                   symbols, reflect)
                else:
                    draw_polygons(temp, screen, zbuffer, view, ambient, light,
                                  symbols, reflect)
                temp = []
            elif c == 'light':
                print(command['light'])
                light.append([args[0], args[1], args[2]])
                light.append([args[3], args[4], args[5]])
        if num_frames > 1:
            print(i)
            filename = 'anim/' + name + ('%03d' % i)
            save_extension(screen, filename)
    if num_frames > 1:
        make_animation(name)
Example #56
0
def run(filename):    
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return
        
    screen = new_screen()    
    first_pass(commands)
    second_pass(commands, nframes)
    
    print nframes

    try:
        os.mkdir(basename)
    except:
        pass
        
    for j in range(nframes):
        stack = [ tmp ]
        for command in commands:
            if command[0] == "pop":
                stack.pop()
            if not stack:
                stack = [ tmp ]
            if command[0] == "ambient":                
                color[0] = command[1]
                color[1] = command[2]
                color[2] = command[3]

            if command[0] == "push":
                stack.append( stack[-1][:] )

            if command[0] == "save":
                save_extension(screen, command[1])

            if command[0] == "display":
                display(screen)

            if command[0] == "sphere":
                m = []
                add_sphere(m, command[1], command[2], command[3], command[4], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "torus":
                m = []
                add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "box":                
                m = []
                add_box(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_polygons( m, screen, color )

            if command[0] == "line":
                m = []
                add_edge(m, *command[1:])
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "bezier":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'bezier')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "hermite":
                m = []
                add_curve(m, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], .05, 'hermite')
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "circle":
                m = []
                add_circle(m, command[1], command[2], command[3], command[4], .05)
                matrix_mult(stack[-1], m)
                draw_lines( m, screen, color )

            if command[0] == "move":                
                if command[-1] == "mover":
                    xval = command[1]*knob[j]["mover"]
                    yval = command[2]*knob[j]["mover"]
                    zval = command[3]*knob[j]["mover"]
                else:
                    xval = command[1]
                    yval = command[2]
                    zval = command[3]  
                t = make_translate(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t
    
            if command[0] == "scale":
                if command[-1] == "bigenator":
                    xval = command[1]*knob[j]["bigenator"]
                    yval = command[2]*knob[j]["bigenator"]
                    zval = command[3]*knob[j]["bigenator"]
                else:
                    xval = command[1]
                    yval = command[2]
                    zval = command[3]
                t = make_scale(xval, yval, zval)
                matrix_mult( stack[-1], t )
                stack[-1] = t
            
            if command[0] == "rotate":     
                if command[-1] == "spinny":
                    angle = command[2] * (math.pi / 180) * knob[j]["spinny"]
                 
                    if command[1] == 'x':
                        t = make_rotX( angle )
                    elif command[1] == 'y':
                        t = make_rotY( angle )
                    elif command[1] == 'z':
                        t = make_rotZ( angle )            
                else:
                    angle = command[2] * (math.pi / 180)
                 
                    if command[1] == 'x':
                        t = make_rotX( angle )
                    elif command[1] == 'y':
                        t = make_rotY( angle )
                    elif command[1] == 'z':
                        t = make_rotZ( angle )            
                matrix_mult( stack[-1], t )
                stack[-1] = t
        
        if j==0:
             save_ppm(screen,basename+"/"+basename+'00'+str(j)+".png")
        else:
             z = 2-int(math.log10(j))              
             save_ppm(screen,basename+"/"+basename+'0'*z+str(j)+".png")        
        clear_screen(screen)     
        print j
Example #57
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [255, 255, 255]
    tmp = new_matrix()
    points = []
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()
    
    for command in commands:
        cmd = command[0]
        print command
        
        if cmd == "push":
            new = copy.deepcopy( stack[ len(stack) - 1 ] )
            stack.append(new)
        elif cmd == "pop":
            stack.pop()
        elif cmd == "line": # Line
            add_edge( points, command[1], command[2], command[3], command[4], command[5], command[6] )
            draw_lines( points, screen, color )
            points = []            
        elif cmd in ["sphere","box","torus"]: # 3d Stuff
            if cmd == "sphere":
                add_sphere( points, command[1], command[2], command[3], command[4], STEPS )
            elif cmd == "box":
                add_box( points, command[1], command[2], command[3], command[4], command[5], command[6] )
            elif cmd == "torus":
                add_torus( points, command[1], command[2], command[3], command[4], command[5], STEPS )

            matrix_mult( stack[-1], points )
            draw_polygons( points, screen, color )
            points = []
        elif cmd in ["move","scale","rotate"]: # Translation STuff
            if cmd == "move":
                t = make_translate( command[1], command[2], command[3] )
            elif cmd == "scale":
                t = make_scale( command[1], command[2], command[3] )
            elif cmd == "rotate":
                rad = command[2] * ( math.pi / 180.0 )

                if command[1] == "x":
                    t = make_rotX(rad)
                elif command[1] == "y":
                    t = make_rotY(rad)
                elif command[1] == "z":
                    t = make_rotZ(rad)
    
            matrix_mult( stack[-1], t )
            stack[-1] = t

        elif cmd == "display":
            display( screen )
Example #58
0
def run(filename):
    """
    This function runs an mdl script
    """
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    num_frames = 1
    animation_frames = []
    basename = "img%03d"
    color = [0, 0, 0]
    ambient_light = [10, 10, 10]
    diffuse_light = [[[100, 100, 100],[-1,0,0]]]
    spec_light = [[[120, 120, 120],[-1,-1,0]]]

    #first pass
    for command in commands:
        operator = command[0]
        if operator == "frames":
            num_frames = command[1]
        elif operator == "basename":
            basename = command[1] + "%03d"
    if basename == "img%03d":
        print "No animation basename specified, using 'img*'"

    #second pass
    for command in commands:
        operator = command[0]
        for i in range(num_frames):
            animation_frames.append({})
        if operator == "vary":
            if num_frames <= 1:
                print "Frames not set, but vary used"
                sys.exit()
            if command[1] not in animation_frames[0]:
                for i in range(num_frames):
                    animation_frames[i][command[1]] = 0
            start = command[2]
            end = command[3]
            startVal = command[4]
            endVal = command[5]
            for i in range(start, end + 1):
                animation_frames[i][command[1]] = startVal + 1. * (endVal - startVal) / (end - start) * (i - start)
    
    #third pass
    for i in range(num_frames):
        stack = [ tmp ]
        screen = new_screen()
        zbuffer = new_zbuffer()

        for command in commands:
            #print command
            operator = command[0]
            if operator == "push":
                stack.append(copy.deepcopy(stack[-1])) #deepcopy required to copy an independent matrix
            elif operator == "pop":
                stack.pop()
            elif operator == "move":
                v = 1
                if command[4] != None:
                    v = animation_frames[i][command[4]]
                M = make_translate(command[1] * v, command[2] * v, command[3] * v)
                matrix_mult(stack[-1], M)
                stack[-1] = M
            elif operator == "rotate":
                v = 1
                if command[3] != None:
                    v = animation_frames[i][command[3]]
                if command[1] == "x":
                    M = make_rotX(command[2] * v)
                elif command[1] == "y":
                    M = make_rotY(command[2] * v)
                elif command[1] == "z":
                    M = make_rotZ(command[2] * v)
                matrix_mult(stack[-1], M)
                stack[-1] = M
            elif operator == "scale":
                v = 1
                if command[4] != None:
                    v = animation_frames[i][command[4]]
                M = make_scale(command[1] * v, command[2] * v, command[3] * v)
                matrix_mult(stack[-1], M)
                stack[-1] = M
            elif operator == "box":
                points = []
                add_prism(points, command[1], command[2], command[3], command[4], command[5], command[6])
                matrix_mult(stack[-1], points)
                draw_polygons(points, screen, color, zbuffer,
                              ambient_light, diffuse_light, spec_light)
            elif operator == "sphere":
                points = []
                add_sphere(points, command[1], command[2], command[3], command[4])
                matrix_mult(stack[-1], points)
                draw_polygons(points, screen, color, zbuffer,
                              ambient_light, diffuse_light, spec_light)
            elif operator == "torus":
                points = []
                add_torus(points, command[1], command[2], command[3], command[4], command[5])
                matrix_mult(stack[-1], points)
                draw_polygons(points, screen, color, zbuffer,
                              ambient_light, diffuse_light, spec_light)
            elif operator == "mesh":
                points = []
                add_custom_shape(points, command[1])
                matrix_mult(stack[-1], points)
                draw_polygons(points, screen, color, zbuffer,
                              ambient_light, diffuse_light, spec_light)
            elif operator == "line":
                points = []
                add_edge(points, command[1], command[2], command[3], command[4], command[5], command[6])
                matrix_mult(stack[-1], points)
                draw_lines(points, screen, color, zbuffer)
            elif operator == "hermite":
                points = []
                add_curve(points, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], command[9], command[10], command[11], command[12], 0.03, "hermite")
                matrix_mult(stack[-1], points)
                draw_lines(points, screen, color, zbuffer)
            elif operator == "bezier":
                points = []
                add_curve(points, command[1], command[2], command[3], command[4], command[5], command[6], command[7], command[8], command[9], command[10], command[11], command[12], 0.03, "bezier")
                matrix_mult(stack[-1], points)
                draw_lines(points, screen, color, zbuffer)
            elif operator == "display":
                display(screen)
            elif operator == "save":
                save_ppm(screen, command[1])
        save_ppm(screen, ("animations/" + basename + ".ppm")%i)
Example #59
0
def run(filename):
    """
    This function runs an mdl script
    """
    color = [240, 240, 120]
    tmp = new_matrix()
    ident( tmp )

    p = mdl.parseFile(filename)

    if p:
        (commands, symbols) = p
    else:
        print "Parsing failed."
        return

    stack = [ tmp ]
    screen = new_screen()

    for command in commands:
        print command
        print stack

        if command[0] == 'push':
            m = copy.deepcopy(stack[-1])
            stack.append(m)
        elif command[0] == 'pop':
            stack.pop()
        elif command[0] == 'move':
            m = make_translate(command[1],command[2],command[3])
            matrix_mult(stack[-1], m)
            stack[-1] = m

        elif command[0] == 'rotate':
            a = command[2] * (math.pi / 180)
            if command[1] == "x":
                m = make_rotX(a)
            elif command[1] == "y":
                m = make_rotY(a)
            elif command[1] == "z":
                m = make_rotZ(a)
            matrix_mult(stack[-1],m)
            stack[-1] = m

        elif command[0] == 'scale':
            m = make_scale(command[1],command[2],command[3])
            matrix_mult(m, stack[-1])
            stack[-1] = m

        elif command[0] == 'box':
            m = []
            add_box(m, command[1], command[2], command[3], command[4], command[5], command[6])
            matrix_mult(stack[-1], m)
            draw_polygons(m, screen, color)
        elif command[0] == 'sphere':
            m = []
            add_sphere(m, command[1], command[2], command[3], command[4], 5)
            matrix_mult(stack[-1], m)
            draw_polygons(m, screen, color)
        elif command[0] == 'torus':
            m = []
            add_torus(m, command[1], command[2], command[3], command[4], command[5], 5)
            matrix_mult(stack[-1], m)
            draw_polygons( m, screen, color)
        elif command[0] == 'line':
            m = []
            add_edge(m, command[1], command[2], command[3], command[4], command[5], command[6])
            matrix_mult(stack[-1], m)
            draw_lines(m, screen, color)
        elif command[0] == 'save':
            save_extension( screen, command[1] )
        elif command[0] == 'display':
            display(screen)

        print ""