Created
June 23, 2014 23:18
-
-
Save jzrake/63a51aa725d7666cfecf to your computer and use it in GitHub Desktop.
Minimal template to run GLUT from Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from OpenGL.GLUT import * | |
from OpenGL.GLU import * | |
from OpenGL.GL import * | |
rotationX = 0.0 | |
rotationY = 0.0 | |
last_x = 0 | |
last_y = 0 | |
def draw(): | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glDisable(GL_DEPTH_TEST) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
glTranslatef(0.0, 0.0, -8.0) | |
glRotatef(rotationX, 1.0, 0.0, 0.0) | |
glRotatef(rotationY, 0.0, 1.0, 0.0) | |
glColor3f(1.0, 1.0, 0.0) | |
glBegin(GL_POLYGON) | |
glVertex3f(-1.0, -1.0, 0.0) | |
glVertex3f(-1.0, +1.0, 0.0) | |
glVertex3f(+1.0, +1.0, 0.0) | |
glVertex3f(+1.0, -1.0, 0.0) | |
glVertex3f(-1.0, -1.0, 0.0) | |
glEnd() | |
glutSwapBuffers() | |
def motion(x, y): | |
global rotationX, rotationY, last_x, last_y | |
rotationX += y - last_y | |
rotationY += x - last_x | |
last_x = x | |
last_y = y | |
glutPostRedisplay() | |
def keyboard(c, x, y): | |
if c == chr(27): | |
exit() | |
def reshape(x, y): | |
xy_aspect = float(x) / y | |
glViewport(0, 0, x, y) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(60, xy_aspect, 1.0, 100.0) | |
glMatrixMode(GL_MODELVIEW) | |
if __name__ == "__main__": | |
glutInit() | |
glutInitWindowPosition(112, 84) | |
glutInitWindowSize(800, 800) | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE) | |
glutCreateWindow("minimal") | |
glutDisplayFunc(draw) | |
glutReshapeFunc(reshape) | |
glutMotionFunc(motion) | |
glutKeyboardFunc(keyboard) | |
glClearDepth(1.0) | |
glClearColor(0.0, 0.0, 0.0, 0.0) | |
glutMainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment