Posted by phlyingpenguin on Mon 29 Sep 20:37
report abuse | View followups from phlyingpenguin | download | new post
- from OpenGL.GL import *
- from OpenGL.GLU import *
- from OpenGL.GLUT import *
- """
- This is a converted version of the Homework 4 program from C to Python.
- """
- class HW4:
- year = 0
- day = 0
- def display(self):
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glPushMatrix()
- glColor3f(1.0, 1.0, 0.0)
- glutSolidSphere(1.0, 20, 16)
- glRotatef(self.year, 0.0, 1.0, 0.0)
- glTranslatef(2.0, 0.0, 0.0)
- glRotatef(self.day, 0.0, 1.0, 0.0)
- glColor3f(0.0, 0.0, 1.0)
- glutWireSphere(0.2, 10, 8)
- glPopMatrix()
- glutSwapBuffers()
- def reshape(self, w, h):
- glViewport(0,0,w,h)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- if w <= h:
- glOrtho(-4.0, 4.0, -4.0 * h / w, 4.0 * h / w, -10.0, 10.0)
- else:
- glOrtho(-4.0 * w / h, 4.0 * w / h, -4.0, 4.0, -10.0, 10.0)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- def keyboard(self, key, x, y):
- if key == 'd': self.day=(self.day+10)%360
- if key == 'D' : self.day=(self.day-10)%360
- if key == 'y': self.year=(self.year+5)%360
- if key == 'Y': self.year=(self.year-5)%360
- if key == 'Q' or key == 'q': exit(0)
- glutPostRedisplay()
- def __init__(self):
- glutInit()
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
- glutInitWindowSize(500, 500)
- glutInitWindowPosition(100,100)
- glutCreateWindow("HW4-2")
- glClearColor(0.0, 0.0, 0.0, 1.0)
- glutDisplayFunc(self.display)
- glMatrixMode(GL_PROJECTION)
- glutReshapeFunc(self.reshape)
- glutKeyboardFunc(self.keyboard)
- glEnable(GL_DEPTH_TEST) # /* Enable hidden-surface removal */
- glutMainLoop()
- HW4()
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.