def get_object_pos_rot(self, oid): if oid in self.objects: axis = v3.GLKVector3Make(*self.objects[oid][0][0:3]) quaternion = quat.GLKQuaternionMakeWithAngleAndVector3Axis( self.objects[oid][0][0], axis) pos = v3.GLKVector3Make(*self.objects[oid][0]) return pos, quaternion return None, None
def __init__(self, position=v3.GLKVector3Make(0, 0, 0), up=v3.GLKVector3Make(0, 1, 0), yaw=0.0, pitch=0.0): self.camera_id = None self.debug_model = None LookObject.__init__(self, position, up, yaw, pitch) self.camera_id = _PhysicsWorld.add_camera(self) print "Camera ID:", self.camera_id self.debug_model = XMLModel( __file__.replace('LightsCameras.py', '../test_model.xml')) self.update(0)
def update(self, dt): """ Called before every scene is rendered Args: dt (int): The time since the last update """ self.front = v3.GLKVector3MultiplyScalar( v3.GLKVector3Make( math.cos(math.radians(self.yaw)) * math.cos(math.radians(self.pitch)), math.sin(math.radians(self.pitch)), math.sin(math.radians(self.yaw)) * math.cos(math.radians(self.pitch))), 1) self.right = v3.GLKVector3CrossProduct(self.front, self.worldup) # self.right = self.front.cross(self.worldup) self.up = v3.GLKVector3CrossProduct(self.right, self.front) # self.up = self.right.cross(self.front) speed = dt * self.speed if self.strafe[0] != 0: self.position = v3.GLKVector3Add( self.position, v3.GLKVector3MultiplyScalar(self.front, speed * self.strafe[0])) # self.position += self.front * speed * self.strafe[0] if self.strafe[1] != 0: self.position = v3.GLKVector3Add( self.position, v3.GLKVector3MultiplyScalar(self.right, speed * self.strafe[1]))
def __init__(self, obj_xml, pos=v3.GLKVector3Make(0, 0, 0)): """ Subclass of OpenGLES.Util.RenderObject Args: obj_xml (str): The path to the xml file storing the object data pos (Optional[euclid.Vector3]): The initial position of the object Attributes: file (str): The filename of the object frames (dict): To be implimented later to support animations bframes (dict): To be implimented later to support animations frame (int): The current frame to render data (dict): Dictionary representation of the XML file """ OpenGLES.Util.RenderObject.__init__(self, pos) self.file = os.path.basename(obj_xml) self.frames = {} self.bframes = {} self.frame = 0 if self.file in _loaded_files: self.frames = _loaded_files[self.file] return None self.data = {} with open(obj_xml, "rb") as f: self.data = xmltodict.parse(f.read())["model"] for frame in self.data['anim']: frame = self.data['anim'][frame] self._parse_frame(frame) _loaded_files[self.file] = self.frames
def __init__(self): Util.RenderCycle.__init__(self) self.objects = [] for x in range(-10, 10, 4): for y in range(10, 14, 4): for z in range(-10, 10, 4): o1 = Util.Model.PhysicsObject("test_model.xml", v3.GLKVector3Make(x, y, z)) self.objects.append(o1) for _ in range(0, 25): o1 = Util.Model.PhysicsObject("test_model.xml", v3.GLKVector3Make(0, 0, 0)) self.objects.append(o1) o1 = Util.Model.XMLModel("plane.xml", v3.GLKVector3Make(-20, 0, -20)) o1.model = m4.GLKMatrix4Scale(o1.model, 100, 100, 100) self.objects.append(o1) self.v = Util.Shader.ShaderSource(VERTEX_SHADER_SOURCE, GL_VERTEX_SHADER) self.f = Util.Shader.ShaderSource(FRAGMENT_SHADER_SOURCE, GL_FRAGMENT_SHADER) self.sp = Util.Shader.ShaderProgram(self.v, self.f) self.eye = PhysicsCamera(v3.GLKVector3Make(-20, 10, -20), yaw=0, pitch=0) self.projection = m4.GLKMatrix4MakePerspective(45.0, 800.0 / 600.0, 0.1, 1000.0) self.view = self.eye.view self.model = m4.GLKMatrix4Identity() self.rt = 0 self.ut = 0 self.texture = None glviewv.add_subview(setting_view) setting_view.send_to_back()
def view(self): """ Returns: (euclid.Matrix4): The view matrix to be applied to a MVP """ pos = self.position p = v3.GLKVector3Make(pos.x, pos.y + 1, pos.z) e = v3.GLKVector3Add(p, self.front) u = self.up return m4.GLKMatrix4MakeLookAt(p.x, p.y, p.z, e.x, e.y, e.z, u.x, u.y, u.z)
def __init__(self, position=v3.GLKVector3Make(0, 0, 0), up=v3.GLKVector3Make(0, 1, 0), yaw=0.0, pitch=0.0): """ A simple camera class, for a basic fly camera. Uses similar math to gluLookAt Args: position (Optional[euclid.Vector3]): The initial position for the camera up (Optional[euclid.Vector3]): World up yaw (Optional[float]): Initial yaw of the camera pitch (Optional[float]): Initial pitch of the camera Attributes: camera_id (int): The physics object camera id (see OpenGLES.Util.LightsCameras.PhysicsCamera) position (euclid.Vector3): The position of the camera worldup (euclid.Vector3): The world up direction (default euclid.Vector(0,1,0)) up (euclid.Vector3): Up direction relative to the world and the camera front (euclid.Vector3): Front facing direction of the camera right (euclid.Vector3): right facing direction of the camera yaw (float): camera yaw (default 0.0) pitch (float): camera pitch (default 0.0) strafe (list[int]): Strafe direction in the format [x,y] speed (float): The speed the camera moves at """ self.camera_id = None self.position = position self.worldup = up self.up = v3.GLKVector3Make(0, 0, 0) self.front = v3.GLKVector3Make(0, 0, -1) self.right = v3.GLKVector3Make(0, 0, 0) self.yaw = yaw self.pitch = pitch self.strafe = [0, 0] self.speed = 10.0 self.update(0)
def get_object_mat(self, oid): """ A way of getting the matrix data of an object Args: oid (int): The id of the object Returns: (euclid.Matrix4): The model matrix of an MVP Raises: AttributeError: if the object cannot be found """ if oid is None: return None if oid in self.objects: axis = v3.GLKVector3Make(*self.objects[oid][1][1:]) quaternion = quat.GLKQuaternionMakeWithAngleAndVector3Axis( self.objects[oid][1][0], axis) mat = m4.GLKMatrix4MakeTranslation(*self.objects[oid][0]) mat = m4.GLKMatrix4Multiply( mat, m4.GLKMatrix4MakeWithQuaternion(quaternion)) return mat else: raise AttributeError, "Object with id '%s' does not exist" % oid
def update(self, dt): self.front = v3.GLKVector3MultiplyScalar( v3.GLKVector3Make( math.cos(math.radians(self.yaw)) * math.cos(math.radians(self.pitch)), math.sin(math.radians(self.pitch)), math.sin(math.radians(self.yaw)) * math.cos(math.radians(self.pitch))), 1) self.right = v3.GLKVector3CrossProduct(self.front, self.worldup) self.up = v3.GLKVector3CrossProduct(self.right, self.front) if self.camera_id: (npos, nrot) = _PhysicsWorld.get_object_pos_rot(self.camera_id) position = npos speed = dt * self.speed if self.strafe[0] != 0: position = v3.GLKVector3Add( position, v3.GLKVector3MultiplyScalar(self.front, speed * self.strafe[0])) if self.strafe[1] != 0: position = v3.GLKVector3Add( position, v3.GLKVector3MultiplyScalar(self.right, speed * self.strafe[1])) (npos, nrot) = _PhysicsWorld.get_object_pos_rot(self.camera_id) _PhysicsWorld.js.eval_js( 'set_object_pos(%i, %f, %f, %f);' % (self.camera_id, position.x, npos.y, position.z)) # I don't like how rotations are handled. # They should pay more attention to what is happening in the physics environment as well. rotation_matrix = m3.GLKMatrix3MakeWithQuaternion(nrot) r = math.radians(-self.yaw) q = quat.GLKQuaternionMakeWithMatrix3( m3.GLKMatrix3MakeZRotation(r)) _PhysicsWorld.js.eval_js('set_object_rot(%i, %f, %f, %f, %f);' % (self.camera_id, q.w, q.x, q.y, q.z)) (npos, nrot) = _PhysicsWorld.get_object_pos_rot(self.camera_id) position = npos self.position = position if self.debug_model: nmodel = m4.GLKMatrix4MakeTranslation(*position.v) nmodel = m4.GLKMatrix4Multiply( nmodel, m4.GLKMatrix4MakeWithQuaternion(q)) self.debug_model.model = nmodel else: speed = dt * self.speed if self.strafe[0] != 0: self.position = v3.GLKVector3Add( self.position, v3.GLKVector3MultiplyScalar(self.front, speed * self.strafe[0])) # self.position += self.front * speed * self.strafe[0] if self.strafe[1] != 0: self.position = v3.GLKVector3Add( self.position, v3.GLKVector3MultiplyScalar(self.right, speed * self.strafe[1])) # self.position += self.right * speed * self.strafe[1] if self.debug_model: self.debug_model.update(dt)
if self.debug_model: nmodel = m4.GLKMatrix4MakeTranslation(*position.v) nmodel = m4.GLKMatrix4Multiply( nmodel, m4.GLKMatrix4MakeWithQuaternion(q)) self.debug_model.model = nmodel else: speed = dt * self.speed if self.strafe[0] != 0: self.position = v3.GLKVector3Add( self.position, v3.GLKVector3MultiplyScalar(self.front, speed * self.strafe[0])) # self.position += self.front * speed * self.strafe[0] if self.strafe[1] != 0: self.position = v3.GLKVector3Add( self.position, v3.GLKVector3MultiplyScalar(self.right, speed * self.strafe[1])) # self.position += self.right * speed * self.strafe[1] if self.debug_model: self.debug_model.update(dt) if __name__ == '__main__': import OpenGLES.Util.Physics reload(OpenGLES.Util.Physics) _PhysicsWorld = OpenGLES.Util.Physics.getPhysicsWorld() c = PhysicsCamera(v3.GLKVector3Make(10, 10, 10)) c.update(123) print c.view
mat (euclid.Matrix4): the model matrix of the object """ start = time.clock() mat = _PhysicsWorld.get_object_mat(self.i) end = time.clock() # print self, '.get_mat', end - start return mat def update(self, dt): """ Called every frame before rendering Args: dt (int): The time since last update """ XMLModel.update(self, dt) start = time.clock() self.model = self.get_mat() end = time.clock() #print end - start if __name__ == "__main__": from OpenGLES.Util.Physics import getPhysicsWorld setPhysicsWorld(getPhysicsWorld()) m = XMLModel("../test_model.xml") m.setup_object() m2 = PhysicsObject("../test_model.xml", pos=v3.GLKVector3Make(10, 10, 10)) m2.setup_object() print '_loaded_files', _loaded_files print '_loaded_vbos', _loaded_vbos