def draw(self): self.wasd.x = -10 self.clear() # cuboids self.context.camera_position = self.wasd.position model_matrix = pg.Matrix().translate((self.t * -4, 0, 0)) matrix = self.wasd.get_matrix() matrix = matrix.perspective(65, self.aspect, 0.1, 500) self.context.matrix = matrix * model_matrix self.context.model_matrix = model_matrix self.context.draw() # bullets self.bullet.camera_position = self.wasd.position for bullet in list(self.bullets): dt = self.time - bullet.time x, y, z = pg.add(bullet.position, pg.mul(bullet.vector, dt * 16)) model_matrix = pg.Matrix().translate((x, y, z)) self.bullet.model_matrix = model_matrix self.bullet.matrix = matrix * model_matrix self.bullet.draw() if dt > 10: self.bullets.remove(bullet) # crosshairs width, height = self.size matrix = pg.Matrix().translate((width / 2, height / 2, 0)) matrix = matrix.orthographic(0, width, 0, height, -1, 1) self.crosshairs.matrix = matrix glEnable(GL_COLOR_LOGIC_OP) glLogicOp(GL_INVERT) glLineWidth(3) self.crosshairs.draw(pg.gl.GL_LINES) glDisable(GL_COLOR_LOGIC_OP)
def set_matrix(self, x, y, z, a): matrix = pg.Matrix().rotate((0, 1, 0), a).translate((x, y, z)) inverse = pg.Matrix().rotate((0, 1, 0), -a) self.context.light_direction = inverse * pg.normalize((1, 1, 1)) self.context.camera_position = matrix.inverse() * self.wasd.position matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.1, 1000) self.context.matrix = matrix
def set_defaults(self, context): context.model_matrix = pg.Matrix() context.normal_matrix = pg.Matrix().inverse().transpose() context.specular_power = 32.0 context.specular_multiplier = 0.2 context.ambient_color = (0.4, 0.4, 0.4) context.light_color = (0.8, 0.8, 0.8) context.fog_color = (0.74, 0.70, 0.64) context.fog_distance = 10000 context.light_direction = pg.normalize((1, 0.5, 1))
def update(self, t, dt): self.wasd.y = 1.5 self.clear() for man in self.men: man.update(t, dt) a = man.a + pi / 2 matrix = pg.Matrix().rotate((0, 1, 0), a).translate((-man.x, 0, -man.z)) inverse = pg.Matrix().rotate((0, 1, 0), -a) self.context.light_direction = inverse * pg.normalize((1, 1, 1)) self.context.camera_position = matrix.inverse() * self.wasd.position matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.1, 1000) self.context.matrix = matrix self.mesh.draw(self.context)
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)
def rotate_satellite(self, position): dx, dy, dz = pg.normalize(position) rx = atan2(dz, dx) + pi / 2 ry = asin(dy) - pi / 2 matrix = pg.Matrix() matrix = matrix.rotate((0, 1, 0), rx) matrix = matrix.rotate((cos(rx), 0, sin(rx)), -ry) return matrix
def draw(self): self.clear() for m in xrange(-1, 2): z = m * FIELD_DEPTH + (-self.t * SPEED) % FIELD_DEPTH matrix = pg.Matrix().translate((0, 0, -z)) matrix = matrix.perspective(65, self.aspect, 1, 1000) self.context.matrix = matrix self.context.camera_position = (0, 0, z) self.context.draw()
def update(self, t, dt): matrix = pg.Matrix() #.rotate((0, 1, 0), t * 2 * pi / 60) matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.01, 100) self.context.matrix = matrix self.plane.matrix = matrix self.axes.matrix = matrix self.context.camera_position = self.wasd.position self.plane.camera_position = self.wasd.position
def update(self, t, dt): matrix = pg.Matrix() self.context.model_matrix = matrix normal_matrix = matrix.inverse().transpose() matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.01, 100) self.context.matrix = matrix self.context.normal_matrix = normal_matrix self.context.camera_position = self.wasd.position
def update(self, t, dt): matrix = pg.Matrix() matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.01, 100) self.context1.matrix = matrix self.context1.camera_position = self.wasd.position self.context1.object_color = (1.0, 0.2, 0.0) self.context2.matrix = matrix self.context2.camera_position = self.wasd.position
def rotate_moon(self, position): # TODO: account for libration dx, dy, dz = pg.normalize(position) rx = atan2(dz, dx) + pi / 2 ry = asin(dy) matrix = pg.Matrix() matrix = matrix.rotate((0, 1, 0), rx) matrix = matrix.rotate((cos(rx), 0, sin(rx)), -ry) return matrix
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)
def to_xyz(lat, lng, elevation, azimuth, altitude=ALTITUDE): r1 = EARTH_RADIUS r2 = r1 + altitude aa = radians(elevation) + pi / 2 ar = asin(r1 * sin(aa) / r2) ad = pi - aa - ar angle = pi / 2 - ad x = cos(angle) * r2 z = sin(angle) * r2 matrix = pg.Matrix() matrix = matrix.rotate((0, 0, -1), pi / 2 - radians(azimuth)) matrix = matrix.rotate((-1, 0, 0), -radians(lat)) matrix = matrix.rotate((0, -1, 0), radians(lng)) return matrix * (x, 0, z)
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)
def draw(self): self.clear() self.context.camera_position = self.wasd.position matrix = self.wasd.get_matrix() matrix = matrix.perspective(65, self.aspect, 0.01, 100) for z in range(-2, 3): for x in range(-10, 11): y = sin(self.t * pi / 4 + x * 0.5 + z * pi) * 3 model_matrix = pg.Matrix().translate((x, y, z * 3)) self.context.model_matrix = model_matrix self.context.matrix = matrix * model_matrix self.context.object_color = COLORS[(z + x) % len(COLORS)] self.sphere.draw(self.context) w, h = self.size self.font.render('%.1f fps' % self.fps, (w - 5, 0), (1, 0)) text = 'x=%.2f, y=%.2f, z=%.2f' % self.wasd.position self.font.render(text, (5, 0))
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)
def add_sphere(self, position): mesh = pg.Matrix().translate(position) * SPHERE self.positions.extend(mesh.positions) self.normals.extend(mesh.normals)
def add_cylinder(self, position, axis): mesh = pg.Matrix().translate(position) * CYLINDERS[axis] self.positions.extend(mesh.positions) self.normals.extend(mesh.normals)
def update(self, t, dt): matrix = pg.Matrix() matrix = self.wasd.get_matrix(matrix) matrix = matrix.perspective(65, self.aspect, 0.01, 100) self.context.matrix = matrix self.context.camera_position = self.wasd.position
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)
def draw(self): w, h = self.size w, h = w / 2, h / 2 matrix = pg.Matrix().orthographic(-w, w, -h, h, -1, 1) self.clear() self.batch.draw(matrix)