Share Pastebin
Guest
Public paste!

phlyingpenguin

By: a guest | Sep 29th, 2008 | Syntax: Python | Size: 1.57 KB | Hits: 295 | Expires: Never
Copy text to clipboard
  1. from OpenGL.GL import *
  2. from OpenGL.GLU import *
  3. from OpenGL.GLUT import *
  4.  
  5. """
  6. This is a converted version of the Homework 4 program from C to Python.
  7. """
  8.  
  9. class HW4:
  10.         year = 0
  11.         day = 0
  12.  
  13.         def display(self):
  14.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  15.                 glPushMatrix()
  16.                 glColor3f(1.0, 1.0, 0.0)
  17.                 glutSolidSphere(1.0, 20, 16)
  18.                 glRotatef(self.year, 0.0, 1.0, 0.0)
  19.                 glTranslatef(2.0, 0.0, 0.0)
  20.                 glRotatef(self.day, 0.0, 1.0, 0.0)
  21.                 glColor3f(0.0, 0.0, 1.0)
  22.                 glutWireSphere(0.2, 10, 8)
  23.                 glPopMatrix()
  24.                 glutSwapBuffers()
  25.  
  26.         def reshape(self, w, h):
  27.                 glViewport(0,0,w,h)
  28.                 glMatrixMode(GL_PROJECTION)
  29.                 glLoadIdentity()
  30.                 if w <= h:
  31.                         glOrtho(-4.0, 4.0, -4.0 *  h /  w, 4.0 *  h /  w, -10.0, 10.0)
  32.                 else:
  33.                         glOrtho(-4.0 *  w /  h, 4.0 *  w /  h, -4.0, 4.0, -10.0, 10.0)
  34.                 glMatrixMode(GL_MODELVIEW)
  35.                 glLoadIdentity()
  36.  
  37.         def keyboard(self, key, x, y):
  38.                 if key == 'd': self.day=(self.day+10)%360
  39.                 if key == 'D' : self.day=(self.day-10)%360
  40.                 if key == 'y': self.year=(self.year+5)%360
  41.                 if key == 'Y': self.year=(self.year-5)%360
  42.                 if key == 'Q' or key == 'q': exit(0)
  43.                 glutPostRedisplay()
  44.  
  45.         def __init__(self):
  46.                 glutInit()
  47.                 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
  48.                 glutInitWindowSize(500, 500)
  49.                 glutInitWindowPosition(100,100)
  50.                 glutCreateWindow("HW4-2")
  51.                 glClearColor(0.0, 0.0, 0.0, 1.0)
  52.                 glutDisplayFunc(self.display)
  53.  
  54.                 glMatrixMode(GL_PROJECTION)
  55.                 glutReshapeFunc(self.reshape)
  56.                 glutKeyboardFunc(self.keyboard)
  57.                 glEnable(GL_DEPTH_TEST) #       /* Enable hidden-surface removal */
  58.  
  59.                 glutMainLoop()
  60.  
  61. HW4()