def __init__(self): self._position = Vector4f(0.0, 0.0, 0.0, 0.0) self._direction = Vector3f(0.0, 0.0, 0.0) self._ambient = ColorRGB(1.0, 1.0, 1.0) self._diffuse = ColorRGB(1.0, 1.0, 1.0) self._specular = ColorRGB(1.0, 1.0, 1.0) self._attn = Vector3f(1.0, 0.0, 0.0)
def move_target(self, direction): """ Move the camera's target position. Parameters ---------- direction :obj:`int` from :class:`DIRECTION` Combination of directions the user wants the target to move. """ d = self._speed * get_dt() if direction & DIRECTION.FORWARD: self._target += Vector3f(d*sin(self._yaw), 0 ,d*cos(self._yaw)) if direction & DIRECTION.BACKWARD: self._target -= [d*sin(self._yaw), 0, d*cos(self._yaw)] if direction & DIRECTION.LEFT: self._target -= d*self._right if direction & DIRECTION.RIGHT: self._target += d*self._right if direction & DIRECTION.UP: self._target += [0, d, 0] if direction & DIRECTION.DOWN: self._target -= [0, d, 0] self._dirty = True
def generate(self, xoffset, zoffset, xsize, zsize, xscale, zscale): size = xsize * zsize elevs = _zeros(xsize * zsize) positions = empty(size * 6, dtype=Vector3f) normals = _zeros(size * 6, dtype=Vector3f._UNIT) colors = empty(size, dtype=Vector3f) index = 0 for iz in xrange(zsize): for ix in xrange(xsize): # Determine the positions of the quad p1 = Vector3f(xoffset + ix, 0, zoffset + iz) p2 = Vector3f(xoffset + ix, 0, zoffset + iz + 1) p3 = Vector3f(xoffset + ix + 1, 0, zoffset + iz + 1) p4 = Vector3f(xoffset + ix + 1, 0, zoffset + iz) positions[index:index + 6] = [p1, p2, p4, p4, p2, p3]
def get_next_vector3f(self): """ Retrieve the next three values and place them into a Vector3f. """ try: x = self.get_next_float() y = self.get_next_float() z = self.get_next_float() except Exception as e: print e return None if x is None or y is None or z is None: return None return Vector3f(x, y, z)
def __init__(self, world): self.world = world self._position = Vector3f(5, 2, 5) self._target = Vector3f(0, 0, 0) self._matrix = None # View direction coordinates self._front = Vector3f(0, 0, 0) # Camera Z-Axis self._right = Vector3f(0, 0, 0) # Camera X-Axis self._up = Vector3f(0, 1, 0) # Local vertical # Third person camera parameters self._range = 10.0 self._zoom = 1.0 self._elevation = 10.0 * D2R self._yaw = PI#PI_2 self._speed = 5 self._rotspeed = 2 self._speed_zoom = 2 self._dirty = True self.update()
def generate(self, xoffset, zoffset, xsize, zsize, xscale, zscale): size = xsize * zsize elevs = self.__generate_elevations(xoffset, zoffset, xsize, zsize, xscale, zscale) positions = empty(size * 6, dtype=Vector3f) normals = empty(size * 6, dtype=Vector3f) colors = empty(size * 6, dtype=Vector3f) index = 0 for iz in xrange(zsize): for ix in xrange(xsize): # Determine the positions of the quad p1 = Vector3f(xoffset + ix, elevs[iz << 5 | ix], zoffset + iz) p2 = Vector3f(xoffset + ix, elevs[(iz + 1) << 5 | ix], zoffset + iz + 1) p3 = Vector3f(xoffset + ix + 1, elevs[(iz + 1) << 5 | (ix + 1)], zoffset + iz + 1) p4 = Vector3f(xoffset + ix + 1, elevs[iz << 5 | (ix + 1)], zoffset + iz) # Calculate the normals of the triangles n1 = calc_surface_normal(p1, p2, p4) n2 = calc_surface_normal(p4, p2, p3) # Calculate the colors c1 = (1 - p1.y / elevs.max()) * Vector3f(0.0, 1.0, 0.0) c2 = (1 - p2.y / elevs.max()) * Vector3f(0.0, 1.0, 0.0) c3 = (1 - p3.y / elevs.max()) * Vector3f(0.0, 1.0, 0.0) c4 = (1 - p4.y / elevs.max()) * Vector3f(0.0, 1.0, 0.0) positions[index:index + 6] = [p1, p2, p4, p4, p2, p3] normals[index:index + 6] = [n1, n1, n1, n2, n2, n2] colors[index:index + 6] = [c1, c2, c4, c4, c2, c3] index += 6 return MeshBundle(("TERRAIN", MeshData(positions, colors, normals)))
def load_mesh_from_obj(self, filename, flip_uv=True, keep_subs=False): """ Load the mesh data contained in the input Wavefront file. Paramters --------- filename : :obj:`str` Path and name of the wavefront file. flip_uv : :obj:`bool`, default `True` Flag to flip the uv.y coordinates. keep_subs : :obj:`bool`, default `True` Flag to keep each subsection as seperate meshes. Returns ------- :class:`MeshBundle` """ pos = [] apos = [] uvs = [] auvs = [] norm = [] anorm = [] subs = [] faces = [] meshes = MeshBundle() with open(filename, "rb") as stream: for line in stream: tokens = line.strip().split(" ") if len(tokens) == 0: continue # empty line if len(tokens[0]) == 0: continue # blank line first = tokens[0].lower() if first[0] == "#": continue # Comment line if first[0] == "v": # Vertex Data if len(first) == 1: # position pos.append(Vector3f(tokens[1], tokens[2], tokens[3])) elif first[1] == "n": # normal norm.append(Vector3f(tokens[1], tokens[2], tokens[3])) elif first[1] == "t": # texture coordinates if flip_uv: uvs.append( Vector2f(float(tokens[1]), 1 - float(tokens[2]))) else: uvs.append(Vector2f(tokens[1], tokens[2])) elif first == "f": # Face index data face = [] for token in tokens[1:]: indexes = token.split("/") v = [] v.append(int(indexes[0])) # Some obj files don't have textures assigned to them if len(indexes[1]) > 0: v.append(int(indexes[1])) v.append(int(indexes[2])) face.append(v) if len(face) == 4: # Convert the quad to two tris i1, i2, i3, i4 = face faces.append([i1, i2, i4]) faces.append([i4, i2, i3]) else: faces.append(face) elif first == "o" or first == "g": # Section marks if len(subs) > 0: subs[-1][2] = len(faces) subs.append([tokens[1], len(faces), -1]) else: continue if len(subs) > 0: subs[-1][2] = len(faces) else: subs.append([None, 0, len(faces)]) for sub_name, start, stop in subs: if len(faces[start][0]) > 2: for face in faces[start:stop]: for pi, ui, ni in face: apos.append(pos[pi - 1]) auvs.append(uvs[ui - 1]) anorm.append(norm[ni - 1]) else: for face in faces[start:stop]: for pi, ni in face: apos.append(pos[pi - 1]) anorm.append(norm[ni - 1]) if keep_subs: if sub_name in meshes: print "WARNING: SubMesh <{:s}> already exist in the meshes." meshes[sub_name] = MeshData(apos, auvs, anorm) apos = [] auvs = [] anorm = [] if not keep_subs: meshes["MASTER"] = MeshData(apos, auvs, anorm) return meshes
test2 = ColorRGBA(0.2, 48, .01, 1.3) test2.r = 0x3F print test2 mat_test = Material("test") mat_test.set_ka(4) print mat_test.ka mat_test.set_ka(test) print mat_test.ka loader = MaterialLoader() loader.from_mtl_file("../res/Birch1.mtl") for m in loader.materials: print m.get_name() print "ka", m.ka print "kd", m.kd test_light = Light() test_light.position.x = 4 print test_light.position new_position = Vector3f(1, 2, 3) test_light.set_position(new_position) print test_light.position print test_light.to_array test_light.set_direction(new_position) print test_light.direction test_light.coeffecient *= 4 print test_light.attn Light()