Python 언어로 작성하여 실행해 본 OpenGL 예제: Redbook 의 Cube
아래의 파이썬용 소스를 실행시키자면 PyOpenGL 을 먼저 설치해야 한다.
소스의 구조는 C 언어 용으로 작성된 cube,c 의 것과 거의 유사하다.
# Filename: cube.py
#
# See Wiki: http://www.de-brauwer.be/wiki/wikka.php?wakka=PyOpenGL
# See Source: http://www.de-brauwer.be/wiki/wikka.php?wakka=PyOpenGLHelloWorld
# See C Source: http://www.glprogramming.com/red/chapter03.html
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
def initFun():
glClearColor (0.0, 0.0, 0.0, 0.0)
glShadeModel (GL_FLAT)
def displayFun():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0, 1.0, 1.0)
glLoadIdentity() # clear the matrix
# viewing transformation */
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glScalef(1.0, 2.0, 1.0) # modeling transformation
glutWireCube(1.0)
glFlush()
def reshapeFun(w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
glMatrixMode(GL_MODELVIEW)
if __name__ == '__main__':
glutInit()
glutInitWindowSize(500, 500)
glutInitWindowPosition(100, 100)
glutCreateWindow("Draw a cube")
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutDisplayFunc(displayFun)
glutReshapeFunc(reshapeFun)
initFun()
glutMainLoop()
실행하기: python cube.py
실행 결과: