Demo: texture

Web VPython 3.2
#from vpython import *
#---------------------------------------------------------------------------------------- COSTANTI
scene.width  = 600
scene.height = 600

NAMES    = [ 'flower', 'granite' , 'gravel',
             'metal' , 'rock'    , 'rough' ,
             'rug'   , 'stones'  , 'stucco',
             'wood'  , 'wood_old', 'earth' ]

TEXTURES = [ textures.flower, textures.granite , textures.gravel,
             textures.metal , textures.rock    , textures.rough ,
             textures.rug   , textures.stones  , textures.stucco,
             textures.wood  , textures.wood_old, textures.earth ]

BUMPMAPS = [ None, None             , bumpmaps.gravel,
             None, bumpmaps.rock    , None           ,
             None, bumpmaps.stones  , bumpmaps.stucco,
             None, bumpmaps.wood_old, None           ]

#----------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------- FUNZIONI
def erase():
    for obj in scene.objects:
        obj.visible = False

def show_object(index, x, y):
    D = 0.7  # size of box
    t = TEXTURES[index]
    b = BUMPMAPS[index]

    if(show == 'box')       : c = box(     pos = vec(x, y, 0),     size = D*vec(1, 1, 1))
    elif(show == 'cone')    : c = cone(    pos = vec(x-D/2, y, 0), size = D*vec(1, 1, 1))
    elif(show == 'cylinder'): c = cylinder(pos = vec(x-D/2, y, 0), size = D*vec(1, 1, 1))
    elif(show == 'pyramid') : c = pyramid( pos = vec(x-D/2, y, 0), size = D*vec(1, 1, 1))
    elif(show == 'sphere')  : c = sphere(  pos = vec(x, y, 0),     size = D*vec(1, 1, 1))

    c.index     = index
    c.shininess = 0
    c.texture   = {'file':t, 'bumpmap':b}
    
    label(pos  = vec(x, y-0.5, 0),
          box  = 0,
          text = 'textures.' + NAMES[index])

def setup():
    scene.range   = 2.2
    scene.fov     = 0.2
    scene.center  = vec(1.5, 2, 0)
    scene.forward = vec(0, 0, -1)
    erase()
    scene.visible = False
    
    index = 0
    y = 3.3
    while(y > 0):
        for x in range(4):
            if(index >= len(NAMES)):
                break; 
            show_object(index, x, y)
            index += 1
        y -= 1.3

    scene.visible = True

def single_object(index):
    R = 0.4  # radius of sphere
    scene.center = vec(0, -0.1*R, 0)
    scene.range  = 1.5*R
    erase()
    show_object(index, 0, 0)
#--------------------------------------------------------------------
#-------------------------------------------------------------------- EVENTI
show      = 'box'  # oggetto scelto con il menu
last_show = show   # ...
hit       = None   # oggetto scelto con il mouse
clicked   = False
picked    = None  

def bind_choose(c):
    global show
    show = c.selected

def bind_handle_click(ev):
    global hit, clicked
    hit     = scene.mouse.pick
    clicked = True
#--------------------------------------------------------------------
#-------------------------------------------------------------------- INTERFACCIA
setup()
scene.visible = False
scene.caption = "Loading textures..."
scene.waitfor("textures")
scene.visible = True

scene.caption = "\nChoose the type of object: "
menu(choices  = ['box', 'cone', 'cylinder', 'pyramid', 'sphere'],
     selected = 'box',
     bind     = bind_choose)
scene.append_to_caption('\n\nClick an object to enlarge it.')
scene.append_to_caption('\nClick anywhere to show all objects again.')
scene.bind('click', bind_handle_click)
#--------------------------------------------------------------------
#-------------------------------------------------------------------- MAIN
while True:
    rate(30)
    if(show != last_show):
        last_show = show
        if(picked != None):
            single_object(picked.index)
        else:
            setup()
    if(clicked):
        clicked = False
        if(picked != None):
            picked = None
            setup()
        elif(picked == None) and (hit != None):
            picked = hit
            hit = None
            single_object(picked.index)

Lascia un commento