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.
"""
year = 0
day = 0
def display():
global day,year
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glColor3f(1.0, 1.0, 0.0)
glutSolidSphere(1.0, 20, 16)
glRotatef(year, 0.0, 1.0, 0.0)
glTranslatef(2.0, 0.0, 0.0)
glRotatef(day, 0.0, 1.0, 0.0)
glColor3f(0.0, 0.0, 1.0)
glutWireSphere(0.2, 10, 8)
glPopMatrix()
glutSwapBuffers()
def reshape(w, h):
global day,year
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(key, x, y):
global day,year
if key == 'd': day=(day+10)%360
if key == 'D' : day=(day-10)%360
if key == 'y': year=(year+5)%360
if key == 'Y': year=(year-5)%360
if key in ['Q','q']: exit(0)
glutPostRedisplay()
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(display)
glMatrixMode(GL_PROJECTION)
glutReshapeFunc(reshape)
glutKeyboardFunc(keyboard)
glEnable(GL_DEPTH_TEST) # /* Enable hidden-surface removal */
glutMainLoop()