class Model(object): ''' A simple model class. The model is loaded from Wavefront OBJ file. ''' def __init__(self, filename, view): self.model = OBJ_vbo(filename) self.view = view self.morient = Matrix4() self.pivot = eVector3() self.position = eVector3() self.do_transformation_matrix() def do_transformation_matrix(self): if self.view.up.y == 1.: # Obj's are exported with z axis up (0,0,1), so we need to # rotate x for -90 degrees to align with view's y up axis (0,1,0) self.orient.rotatex(pi / -2.) ''' Apply transformations in following order (translation -> -pivot -> orientation -> pivot) in order to rotate around arbitrary pivot point and properly position the model. TODO: Optimize matrix concatenation (rewrite in compact form) ''' self.mtransform = Matrix4.new_translate(*self.position) * Matrix4.new_translate(*(self.pivot * -1.)) * \ self.morient * Matrix4.new_translate(*self.pivot) # Create ctype array of the matrix self.m = (GLfloat * 16)(*self.mtransform) def orient_from_direction(self, direction=None): ''' If direction is None the orientation will be determined from view's direction vector which means that model will align with camera's direction. ''' self.morient = self.view.get_parent2local(direction) self.do_transformation_matrix() def orient_from_axis_angle(self, angle, axis): self.morient.identity() self.morient.rotate_axis(angle, axis) self.do_transformation_matrix() def set_position(self, position): self.position = eVector3(*position) def set_pivot(self, pivot): self.pivot = eVector3(*pivot) def render(self): glPushMatrix() glMultMatrixf(self.m) self.model.render() glPopMatrix()
def __init__(self, filename, view): self.model = OBJ_vbo(filename) self.view = view self.morient = Matrix4() self.pivot = eVector3() self.position = eVector3() self.do_transformation_matrix()