示例#1
0
def main(infile, outfile):
    lights, radius, region, resolution, strands = parse_file(infile)
    x0,x1,y0,y1 = region
    imagex,imagey = resolution
    
    if not bool(lights):
        print "No lights found in the input file."
        quit()
    
    ims = [] #one image for each light
    
    t1 = time()
    
    for light in lights:
        im = Image.new('L', (imagex, imagey), "white")
        pix = im.load()
        for strand in strands:
            for point in strand:
                cast_shadow_sphere(pix, light, point, radius, region, resolution)
            for i in xrange(len(strand)-1):
                point1 = strand[i]
                point2 = strand[i+1]
                cast_shadow_cylinder(pix, light, point1, point2, radius, region, resolution)
        ims.append(im)
    
    t2 = time()
    
    im = Image.new('L', (imagex, imagey), "white")
    pix = im.load()
    pixs = [i.load() for i in ims]
    for x in xrange(0,imagex):
        for y in xrange(0,imagey):
            acc = 0
            for p in pixs:
                acc += p[x,y]
            pix[x,y] = int( float(acc)/float(len(ims)) )
            
    t3 = time()
    
    im.save(outfile, 'JPEG', quality=95)
    
    t4 = time()
    
    print 'It took',t2-t1,'seconds to draw the shadows for individual lights.'
    print 'It took',t3-t2,'seconds to merge the shadows.'
    print 'It took',t4-t3,'seconds to save the image.'
def main(strandFile, shadowFile):
  lights, radius, region, resolution, strands = parse_file(strandFile)
  vertices,edges = build_v_and_e(strands)
  
  x0,x1,y0,y1 = region
  verticesW = (
    (x0, y0, 0),
    (x1, y0, 0),
    (x1, y1, 0),
    (x0, y1, 0)
    )
  
  edgesW = (
    (0,1),
    (1,2),
    (2,3),
    (0,3)
    )
  
  # Start pygame
  pygame.init()
  display = (1024,720)
  pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

  # Set background color
  glClearColor (.8, .8, .8, 1.0)
  #
  glEnable(GL_COLOR_MATERIAL)
  glEnable(GL_TEXTURE_2D)

  # Set shader properties of objects
  glMaterialfv(GL_FRONT, GL_DIFFUSE, (0.7, 0.7, 0.7, 1.0))
  glMaterialfv(GL_FRONT, GL_SPECULAR, (1.0, 1.0, 1.0, 1.0))
  glMaterialfv(GL_FRONT, GL_SHININESS, (100.0))
  glShadeModel(GL_SMOOTH)

  # Set light position and intensity
  glLightfv( GL_LIGHT0, GL_POSITION, ( -20, 100, -0.5, 0.2 ) )
  glLightfv( GL_LIGHT0, GL_DIFFUSE, ( 1, 1, 1, .2 ) )
  glLightfv( GL_LIGHT0, GL_SPECULAR, ( 1, 1, 1, .2 ) )
  glLightfv( GL_LIGHT0, GL_AMBIENT, ( .8, .8, .8, 1 ) )

  # Allow GL to figure out how to render objects with depth
  glEnable(GL_DEPTH_TEST)
  glEnable(GL_AUTO_NORMAL)
  #glEnable(GL_NORMALIZE)

  quadric=gluNewQuadric()
  gluQuadricNormals(quadric, GLU_SMOOTH)
  gluQuadricTexture(quadric, GL_TRUE)

  # get texture from filename
  texData, width, height = loadImage(shadowFile)

  gluPerspective(20, (1.0*display[0]/display[1]), 0.1, 50.0)
  glTranslatef(-1.5, 0.0, -10)
  while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        quit()
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
      x = 1
    elif pressed[pygame.K_DOWN]:
      x = -1
    else:
      x = 0
    if pressed[pygame.K_RIGHT]:
      y = -1
    elif pressed[pygame.K_LEFT]:
      y = 1
    else:
      y = 0
    if x != 0 or y != 0:
      glRotatef(2, x, y, 0)
    
    # Set object color
    glColor3fv((.1, .2, .3))
    pipes(quadric, edges, vertices, radius)
    pipes(quadric, edgesW, verticesW, 0.02)
    glPushMatrix()
    glTranslatef(0,0,verticesW[0][2])
    # Set object color
    glColor3fv((1, 1, 1))
    texture = bindImage(texData, width, height)
    glBindTexture(GL_TEXTURE_2D, texture)
    glCallList(createTexDL(width, height, verticesW))
    glPopMatrix()
    glDeleteTextures([texture])
    glDisable(GL_LIGHTING)
    pygame.display.flip()
    pygame.time.wait(10)