class Basketball: def __init__(self, radius=40, image_file="bb.jpg"): self.radius = radius self.image_file = image_file label = "Basketball\n----------\n- Rotation via X, Y, Z axes\n- Scaling\n- On/Off Texture" self.shelf = Shelf(label = label) self.scale = 1 self.texture_status = True self.load_textures() # rotational values self.xrot = self.yrot = self.zrot = True self.rot = 0.0 def load_textures(self): texture_file = os.getcwd() + os.path.join('/images', self.image_file) texture_surface= image.load(texture_file) texture = texture_surface.image_data.create_texture(image.Texture) glBindTexture(GL_TEXTURE_2D, texture.id) # glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) # glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) self.texture = texture def draw(self): # draw the shelf glPushMatrix() glRotatef(90, 1, 0, 0) self.shelf.draw() glPopMatrix() # Enable/Disabling texture if self.texture_status: glBindTexture(GL_TEXTURE_2D, self.texture.id) else: glDisable(GL_TEXTURE_2D) # draw the bird glPushMatrix() glScalef(self.scale, self.scale, self.scale) glTranslatef(0, 2 * self.radius, 0) glRotatef(self.rot, int(self.xrot), int(self.yrot), int(self.zrot)) quad = gluNewQuadric() gluQuadricTexture(quad, GL_TRUE) gluSphere(quad, self.radius, 100, 100) glPopMatrix() # resetting texture status for drawing other objects glEnable(GL_TEXTURE_2D) def update(self): # a bit of a hack, I guess .. weird if(self.xrot == False and self.yrot == False and self.zrot == False): self.rot += 0.0 else: self.rot += 0.5 self.shelf.update()
class Teapot: def __init__(self, size=40, image_file='silverware.jpg'): self.size = size self.scale = 1 self.texture_status = True self.image_file = image_file self.load_textures() label = "Teapot\n----------\n- Rotation via X, Y, Z axes\n- Scaling\n- On/Off Texture" self.shelf = Shelf(label = label) # rotational values self.xrot = self.yrot = self.zrot = True self.rot = 0.0 def load_textures(self): texture_file = os.getcwd() + os.path.join('/images', self.image_file) texture_surface = image.load(texture_file) texture = texture_surface.image_data.create_texture(image.Texture) glBindTexture(GL_TEXTURE_2D, texture.id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) self.texture = texture def draw(self): # draw the shelf glPushMatrix() glRotatef(90, 1, 0, 0) self.shelf.draw() glPopMatrix() if self.texture_status: glBindTexture(GL_TEXTURE_2D, self.texture.id) else: glDisable(GL_TEXTURE_2D) glColor3f(0, 0, 0) # draw the teapot glPushMatrix() glScalef(self.scale, self.scale, self.scale) glTranslatef(0, 2 * self.size, 0) glRotatef(self.rot, int(self.xrot), int(self.yrot), int(self.zrot)) glutSolidTeapot(self.size) glPopMatrix() glColor3f(1, 1, 1) # resetting texture glEnable(GL_TEXTURE_2D) def update(self): # a bit of a hack, I guess .. weird if(self.xrot == False and self.yrot == False and self.zrot == False): self.rot += 0.0 else: self.rot += 0.5 self.shelf.update()
class Snowman: def __init__(self, body_radius=50 ): self.body_radius = body_radius self.scale = 1 label = "Snowman\n----------\n - Lethal\n- Scaling" self.shelf = Shelf(label=label) # rotational values self.xrot = self.yrot = self.zrot = False self.rot = 0.0 def draw(self): # draw the shelf glPushMatrix() glRotatef(90, 1, 0, 0) self.shelf.draw() glPopMatrix() glDisable(GL_TEXTURE_2D) # draw the snowman glPushMatrix() glTranslatef(0, (self.body_radius * self.scale)+20, 0) quad = gluNewQuadric() gluQuadricNormals(quad, GLU_SMOOTH) # Main body glPushMatrix() glColor3f(1, 1, 1) gluSphere(quad, self.body_radius * self.scale, 100, 100) glTranslatef(0, self.body_radius * 1.5 * self.scale, 0) gluSphere(quad, self.body_radius * 0.75 * self.scale, 100, 100) glTranslatef(0, self.body_radius * self.scale, 0) gluSphere(quad, self.body_radius * 0.5 * self.scale, 100, 100) # Nose glColor3f(1, 0, 0) glTranslatef(0, 0, self.body_radius*0.5 * self.scale) gluCylinder(quad, self.body_radius * 0.1 * self.scale, 0.0, 20 * self.scale, 100, 100) # Two Eyes glColor3f(0, 0, 0) glTranslatef(-10 * self.scale, 10 * self.scale, 0) gluSphere(quad, self.body_radius * 0.03 * self.scale, 100, 100) glTranslatef(20 * self.scale, 0, 0) gluSphere(quad, self.body_radius * 0.03 * self.scale, 100, 100) glColor3f(1,1, 1) glPopMatrix() # Hat glPushMatrix() glColor3f(0, 0, 0) glTranslatef(0, self.body_radius * 2.85 * self.scale, 0) glRotatef(-90, 1, 0, 0) gluCylinder(quad, self.body_radius * 0.4 * self.scale, self.body_radius * 0.4 * self.scale, 45 * self.scale, 100, 100) # Hat brim glColor3f(0,0,0) gluDisk(quad, 17 * self.scale, 38 * self.scale, 100, 100) glPopMatrix() glPopMatrix() glColor3f(1, 1, 1) glEnable(GL_TEXTURE_2D) def update(self): self.shelf.update()