示例#1
0
文件: cuboids.py 项目: studiovc/pg
 def setup(self):
     self.wasd = pg.WASD(self, speed=8)
     self.wasd.look_at((-10, 0, 0), (0, 0, 0))
     # cuboids
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.use_color = True
     self.context.ambient_color = (0.5, 0.5, 0.5)
     self.context.light_color = (0.5, 0.5, 0.5)
     self.context.light_direction = pg.normalize((-1, 1, 1))
     data = []
     n = 16
     for x in range(256):
         z = random.randint(-n, n)
         y = random.randint(-n, n)
         cuboid = pg.Cuboid(x, x + 1, y - 0.5, y + 0.5, z - 0.5, z + 0.5)
         color = pg.hex_color(random.randint(0, 0xffffff))
         colors = [color] * len(cuboid.positions)
         data.extend(pg.interleave(
             cuboid.positions, cuboid.normals, colors))
     self.context.position, self.context.normal, self.context.color = (
         pg.VertexBuffer(data).slices(3, 3, 3))
     # bullets
     self.bullet = pg.Context(pg.DirectionalLightProgram())
     self.bullet.ambient_color = (0.5, 0.5, 0.5)
     self.bullet.light_color = (0.5, 0.5, 0.5)
     sphere = pg.Sphere(3, 0.05, (0, 0, 0))
     self.bullet.position = pg.VertexBuffer(sphere.positions)
     self.bullet.normal = pg.VertexBuffer(sphere.normals)
     self.bullets = []
     # crosshairs
     self.crosshairs = pg.Context(pg.SolidColorProgram())
     self.crosshairs.position = pg.VertexBuffer(pg.Crosshairs().positions)
示例#2
0
 def setup(self):
     self.wasd = pg.WASD(self)
     self.wasd.look_at((0, 0, 4), (0, 0, 0))
     self.program = pg.DirectionalLightProgram()
     self.context1 = pg.Context(self.program)
     self.context2 = pg.Context(self.program)
     self.sphere1 = pg.Sphere(4, 0.5, (2, 0, 0))
     self.sphere2 = pg.Sphere(4, 0.5, (-2, 0, 0))
示例#3
0
 def setup(self):
     self.wasd = pg.WASD(self)
     self.wasd.look_at((0, 0, 3), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     mesh1 = pg.OBJ('examples/suzanne.obj').center()
     mesh2 = mesh1.smooth_normals()
     self.meshes = [mesh1, mesh2]
示例#4
0
 def setup(self):
     self.wasd = pg.WASD(self, speed=10)
     self.wasd.look_at((0, 3, 12), (0, 0, 7))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.points = pg.poisson_disc(-10, -10, 10, 10, 1.5, 32)
     self.mats = [pg.Matrix().translate((x, 0, z)) for x, z in self.points]
     self.sphere = pg.Sphere(4, 0.7)
示例#5
0
 def run(self):
     context = pg.Context(Program())
     font = pg.Font(self, 2, FONT, 24, (0, 0, 0, 1))
     self.message = 'loading triangle mesh'
     mesh = pg.STL('examples/%s.stl' % NAME).center()
     self.triangles = '%d triangles' % (len(mesh.positions) / 3)
     self.message = 'computing bounding box'
     (x0, y0, z0), (x1, y1, z1) = pg.bounding_box(mesh.positions)
     context.uv0 = (x0, z0)
     context.uv1 = (x1, z1)
     self.message = 'generating vertex buffer'
     context.position = pg.VertexBuffer(mesh.positions)
     self.message = 'generating height map'
     p = mesh.positions
     lookup = defaultdict(list)
     for v1, v2, v3 in zip(p[::3], p[1::3], p[2::3]):
         x, y, z = v1
         x, z = int(round(x / STEP)), int(round(z / STEP))
         lookup[(x, z)].append((v1, v2, v3))
     self.message = 'loading brightness texture'
     context.sampler = pg.Texture(0, 'examples/%s.jpg' % NAME)
     self.message = 'loading normal texture'
     context.normal_sampler = pg.Texture(1, 'examples/%s.png' % NAME)
     self.result = {
         'font': font,
         'context': context,
         'lookup': lookup,
     }
示例#6
0
 def __init__(self, occupied):
     self.occupied = occupied
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.object_color = pg.hex_color(random.choice(COLORS))
     self.context.position = self.positions = pg.VertexBuffer()
     self.context.normal = self.normals = pg.VertexBuffer()
     self.restart()
示例#7
0
 def setup(self):
     self.font = pg.Font(self, 0, '/Library/Fonts/Arial.ttf', 24)
     self.wasd = pg.WASD(self, speed=5)
     self.wasd.look_at((14, 0, 0), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.sphere = pg.Sphere(5, 0.4, (0, 0, 0))
     self.context.ambient_color = (0.4, 0.4, 0.4)
     self.context.light_color = (0.6, 0.6, 0.6)
示例#8
0
 def setup(self):
     self.wasd = pg.WASD(self)
     self.wasd.look_at((0, 0, 2), (0, 0, 0))
     self.program = pg.DirectionalLightProgram()
     self.context = pg.Context(self.program)
     sphere = pg.Sphere(3, 0.5, (0, 0, 0))
     self.context.position = pg.VertexBuffer(sphere.positions)
     self.context.normal = pg.VertexBuffer(sphere.normals)
示例#9
0
 def setup(self):
     self.wasd = pg.WASD(self, speed=4)
     self.wasd.look_at((-3, 1, -6), (0, 0, -2))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.ambient_color = (0.7, 0.7, 0.7)
     self.context.light_color = (0.3, 0.3, 0.3)
     self.context.use_texture = True
     self.context.sampler = pg.Texture(0, 'examples/bus.jpg')
     self.mesh = pg.OBJ('examples/bus.obj').center().smooth_normals()
示例#10
0
文件: starfield.py 项目: ssebs/pg
 def setup(self):
     data = []
     shape = pg.Plane((0, 0, 0), (0, 0, 1), 0.5, False)
     for _ in xrange(COUNT):
         x = (random.random() - 0.5) * FIELD_SIZE
         y = (random.random() - 0.5) * FIELD_SIZE
         z = random.random() * FIELD_DEPTH
         mesh = pg.Matrix().translate((x, y, z)) * shape
         data.extend(mesh.positions)
     self.context = pg.Context(pg.SolidColorProgram())
     self.context.position = pg.VertexBuffer(data)
示例#11
0
 def setup(self):
     self.font = pg.Font(self, 1, '/Library/Fonts/Arial.ttf', 24)
     self.wasd = pg.WASD(self)
     self.wasd.look_at(pg.normalize((1, 0, 1)), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.sampler = pg.Texture(0, 'examples/earth.png')
     self.context.use_texture = True
     sphere = pg.Sphere(4, 0.5, (0, 0, 0))
     self.context.position = pg.VertexBuffer(sphere.positions)
     self.context.normal = pg.VertexBuffer(sphere.normals)
     self.context.uv = pg.VertexBuffer(sphere.uvs)
示例#12
0
文件: avoid.py 项目: studiovc/pg
 def setup(self):
     self.wasd = pg.WASD(self, speed=100)
     self.wasd.look_at((300, 15, 300), (0, -50, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.target = pg.Sphere(3, 2)
     a = pg.Solid(pg.Sphere(3, 4))
     b = pg.Solid(pg.Sphere(2, 2, (3, 0, 0)))
     c = pg.Solid(pg.Cylinder((-6, 0, 0), (0, 0, 0), 2, 16))
     d = pg.Solid(pg.Sphere(2, 2, (-6, 0, 0)))
     solid = (a - b) | c | d
     self.mesh = solid.mesh()
     self.model = Model(400, 400, 100)
示例#13
0
 def setup(self):
     self.wasd = pg.WASD(self, speed=5)
     self.wasd.look_at((-2, 2, 2), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.sampler = pg.Texture(0, 'examples/bronze.jpg')
     self.context.use_texture = True
     a = pg.Solid(pg.Cuboid(-1, 1, -1, 1, -1, 1))
     b = pg.Solid(pg.Sphere(2, 1.35))
     c = pg.Solid(pg.Cylinder((-1, 0, 0), (1, 0, 0), 0.5, 18))
     d = pg.Solid(pg.Cylinder((0, -1, 0), (0, 1, 0), 0.5, 18))
     e = pg.Solid(pg.Cylinder((0, 0, -1), (0, 0, 1), 0.5, 18))
     solid = (a & b) - (c | d | e)
     self.mesh = solid.mesh()
示例#14
0
文件: main.py 项目: oscarxgit/GPS
 def setup(self):
     self.device = gps.Device()
     pg. async (self.device.run)
     self.fix = False
     self.font = pg.Font(self, 3, FONT, 18, bg=(0, 0, 0))
     self.wasd = pg.WASD(self, speed=SPEED)
     self.wasd.look_at((0, 0, EARTH_RADIUS + ALTITUDE * 2), (0, 0, 0))
     # stars
     self.stars = pg.Context(StarsProgram())
     self.stars.sampler = pg.Texture(2, 'resources/stars.png')
     self.stars_sphere = pg.Sphere(4).reverse_winding()
     # earth
     self.earth = pg.Context(EarthProgram())
     self.earth.day = pg.Texture(0, 'resources/earth_day.jpg')
     self.earth.night = pg.Texture(1, 'resources/earth_night.jpg')
     self.earth.ambient_color = (0.4, 0.4, 0.4)
     self.earth.light_color = (1.25, 1.25, 1.25)
     self.earth.specular_power = 20.0
     self.earth.specular_multiplier = 0.3
     self.earth_sphere = pg.Sphere(5, EARTH_RADIUS)
     # moon
     self.moon = pg.Context(pg.DirectionalLightProgram())
     self.moon.use_texture = True
     self.moon.sampler = pg.Texture(4, 'resources/moon.jpg')
     self.moon.ambient_color = (0.1, 0.1, 0.1)
     self.moon.light_color = (1.3, 1.3, 1.3)
     self.moon.specular_power = 20.0
     self.moon.specular_multiplier = 0.3
     self.moon_sphere = pg.Sphere(4, MOON_RADIUS)
     # satellites
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.object_color = (1, 1, 1)
     m = SATELLITE_SCALE
     self.satellite = pg.STL('resources/dawn.stl').center()
     self.satellite = pg.Matrix().scale((m, m, m)) * self.satellite
     # lines
     self.lines = pg.Context(pg.SolidColorProgram())
     self.lines.color = (1, 1, 1, 0.25)
示例#15
0
 def setup(self):
     self.wasd = pg.WASD(self, speed=3)
     self.wasd.look_at((-5, 4, 5), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     data = []
     points = pg.poisson_disc(-4, -4, 4, 4, 1, 32)
     for x, z in points:
         noise = pg.simplex2(10 + x * 0.25, 10 + z * 0.25, 4)
         y = (noise + 1) / 1
         shape = pg.Cone((x, 0, z), (x, y, z), 0.4, 36)
         data.extend(pg.interleave(shape.positions, shape.normals))
         shape = pg.Sphere(3, 0.3, (x, y, z))
         data.extend(pg.interleave(shape.positions, shape.normals))
     self.context.position, self.context.normal = (
         pg.VertexBuffer(data).slices(3, 3))
     self.plane = pg.Context(pg.DirectionalLightProgram())
     self.plane.object_color = (1, 1, 1)
     shape = pg.Plane((0, -0.1, 0), (0, 1, 0), 5)
     data = pg.interleave(shape.positions, shape.normals)
     self.plane.position, self.plane.normal = (pg.VertexBuffer(data).slices(
         3, 3))
     self.axes = pg.Context(pg.SolidColorProgram())
     self.axes.color = (0.3, 0.3, 0.3)
     self.axes.position = pg.VertexBuffer(pg.Axes(100).positions)
示例#16
0
文件: circles.py 项目: studiovc/pg
 def setup(self):
     plane = pg.Plane((0, 0, 0), (0, 0, 1), 1)
     self.context = pg.Context(Program())
     self.context.position = pg.VertexBuffer(plane.positions)
     self.context.matrix = pg.Matrix().orthographic(-1, 1, -1, 1, -1, 1)
     self.positions = []
     n = GRID_SIZE
     m = CIRCLE_SPACING
     for i in range(n):
         for j in range(n):
             x = (i - (n - 1) / 2.0) * m
             y = (j - (n - 1) / 2.0) * m
             d = (x * x + y * y)**0.5
             self.positions.append((x, y, d))
     self.max_distance = max(x[-1] for x in self.positions)
示例#17
0
文件: terrain.py 项目: ssebs/pg
 def setup(self):
     self.wasd = pg.WASD(self, speed=30)
     self.wasd.look_at((-20, 20, -8), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.use_color = True
     self.context.specular_power = 8.0
     self.context.specular_multiplier = 0.3
     normals = defaultdict(list)
     position = []
     normal = []
     color = []
     size = 50
     # generate height map
     height = {}
     colors = {}
     for x in xrange(-size, size + 1):
         for z in xrange(-size, size + 1):
             height[(x, z)] = noise(x, z)
             colors[(x, z)] = generate_color(x, z)
     # generate triangles and track normals for all vertices
     for x in xrange(-size, size):
         for z in xrange(-size, size):
             t1 = [x + 0, z + 0, x + 1, z + 0, x + 0, z + 1]
             t2 = [x + 0, z + 1, x + 1, z + 0, x + 1, z + 1]
             for t in [t1, t2]:
                 x1, z1, x2, z2, x3, z3 = t
                 p1 = (x1, height[(x1, z1)], z1)
                 p2 = (x2, height[(x2, z2)], z2)
                 p3 = (x3, height[(x3, z3)], z3)
                 c1 = colors[(x1, z1)]
                 c2 = colors[(x2, z2)]
                 c3 = colors[(x3, z3)]
                 position.extend([p3, p2, p1])
                 color.extend([c3, c2, c1])
                 n = pg.normalize(pg.cross(pg.sub(p3, p1), pg.sub(p2, p1)))
                 normals[(x1, z1)].append(n)
                 normals[(x2, z2)].append(n)
                 normals[(x3, z3)].append(n)
     # compute average normal for all vertices
     for key, value in normals.items():
         normals[key] = pg.normalize(reduce(pg.add, value))
     for x, y, z in position:
         normal.append(normals[(x, z)])
     # generate vertex buffer
     vb = pg.VertexBuffer(pg.interleave(position, normal, color))
     self.context.position, self.context.normal, self.context.color = (
         vb.slices(3, 3, 3))
示例#18
0
 def setup(self):
     self.set_clear_color(0.87, 0.81, 0.70)
     self.wasd = pg.WASD(self, speed=10)
     self.wasd.look_at((0, 0, 0), (30, -5, 30))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.context.specular_multiplier = 0.25
     self.context.ambient_color = (0.5, 0.5, 0.5)
     self.context.light_color = (0.5, 0.5, 0.5)
     self.context.use_texture = True
     self.context.sampler = pg.Texture(0, 'examples/gusev.jpg')
     mesh = pg.STL('examples/gusev.stl').smooth_normals()
     (x0, y0, z0), (x1, y1, z1) = pg.bounding_box(mesh.positions)
     for x, y, z in mesh.positions:
         u = 1 - (z - z0) / (z1 - z0)
         v = 1 - (x - x0) / (x1 - x0)
         mesh.uvs.append((u, v))
     self.mesh = mesh
     p = self.mesh.positions
     self.lookup = defaultdict(list)
     for v1, v2, v3 in zip(p[::3], p[1::3], p[2::3]):
         x, y, z = v1
         x, z = int(round(x)), int(round(z))
         self.lookup[(x, z)].append((v1, v2, v3))
示例#19
0
 def setup(self):
     self.wasd = pg.WASD(self, speed=10)
     self.wasd.look_at((0, 8, 30), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.mesh = pg.OBJ('examples/lego.obj').center().smooth_normals()
     self.men = [LegoMan() for _ in xrange(50)]
示例#20
0
 def setup(self):
     self.wasd = pg.WASD(self)
     self.wasd.look_at((-1, 1, 1), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.sphere = pg.Sphere(4, 0.5, (0, 0, 0))
示例#21
0
文件: field.py 项目: ssebs/pg
 def setup(self):
     plane = pg.Plane((0, 0, 0), (0, 0, 1), 1)
     self.context = pg.Context(Program())
     self.context.position = pg.VertexBuffer(plane.positions)
     self.context.matrix = pg.Matrix().orthographic(-1, 1, -1, 1, -1, 1)
示例#22
0
 def setup(self):
     print 'CylinderScene.setup()'
     self.wasd = pg.WASD(self)
     self.wasd.look_at((-1, 1, 1), (0, 0, 0))
     self.context = pg.Context(pg.DirectionalLightProgram())
     self.cylinder = pg.Cylinder((0, -0.5, 0), (0, 0.5, 0), 0.5, 18)