PyOpenGL は、Python で OpenGL を使用するためのモジュールです。
ここでは、OpenGLを使用して2Dの描画をしてみます。
#!env python from OpenGL.GL import * from OpenGL.GLUT import * def draw(): glClearColor(1.0, 0.0, 0.0, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glFlush() glutSwapBuffers() glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(320, 240) glutCreateWindow("PyOpenGL 1") glutDisplayFunc(draw) glutMainLoop()
from OpenGL.GL import *
from OpenGL.GLUT import *
glClearColor(1.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glFlush()
glutSwapBuffers()
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(320, 240)
glutCreateWindow("PyOpenGL 1")
glutDisplayFunc(draw)
glutMainLoop()
#!env python from OpenGL.GL import * from OpenGL.GLUT import * def draw(): glClearColor(0.0, 0.0, 1.0, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glBegin(GL_TRIANGLES) glVertex(-1, -1) glVertex(1, -1) glVertex(0, 1) glEnd() glFlush() glutSwapBuffers() glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(320, 240) glutCreateWindow("PyOpenGL 2") glutDisplayFunc(draw) glutMainLoop()
glBegin(GL_TRIANGLES)
glVertex(-1, -1)
glEnd()
#!env python from OpenGL.GL import * from OpenGL.GLUT import * def resize(w, h): glViewport(30, 50, w/2, h/2) def draw(): glClearColor(0.0, 1.0, 0.0, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glBegin(GL_TRIANGLES) glVertex(-1, -1) glVertex(1, -1) glVertex(0, 1) glEnd() glBegin(GL_LINES) glVertex(-1, -1) glVertex(1, -1) glVertex(1, -1) glVertex(1, 1) glVertex(1, 1) glVertex(-1, 1) glVertex(-1, 1) glVertex(-1, -1) glEnd() glFlush() glutSwapBuffers() glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(320, 240) glutCreateWindow("PyOpenGL 3") glutReshapeFunc(resize) glutDisplayFunc(draw) glutMainLoop()
glViewport(30, 50, w/2, h/2)
glutReshapeFunc(resize)
#!env python from OpenGL.GL import * from OpenGL.GLUT import * def draw(): glClearColor(0.0, 0.5, 0.5, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glBegin(GL_TRIANGLES) glVertex(-1, -1) glVertex(1, -1) glVertex(0, 1) glEnd() glFlush() glutSwapBuffers() def resize(w, h): glViewport(0, 0, w, h) def mouse(button, state, x, y): if button == GLUT_LEFT_BUTTON: print "left button", elif button == GLUT_MIDDLE_BUTTON: print "middle button", elif button == GLUT_RIGHT_BUTTON: print "right button", else: print "unknown button:", button, if state == GLUT_DOWN: print "down mouse button", elif state == GLUT_UP: print "up mouse button", else: print "unknown state:", state, print(x, y) def motion(x, y): print "drag:", x, y glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(320, 240) glutCreateWindow("PyOpenGL 4") glutReshapeFunc(resize) glutDisplayFunc(draw) glutMouseFunc(mouse) glutMotionFunc(motion) glutMainLoop()
def mouse(button, state, x, y):
GLUT_LEFT_BUTTON
GLUT_MIDDLE_BUTTON
GLUT_RIGHT_BUTTON
GLUT_DOWN
GLUT_UP
def motion(x, y):
glutMouseFunc(mouse)
glutMotionFunc(motion)
#!env python from OpenGL.GL import * from OpenGL.GLUT import * def draw(): glClearColor(0.0, 0.5, 0.5, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glBegin(GL_TRIANGLES) glVertex(-1, -1) glVertex(1, -1) glVertex(0, 1) glEnd() glFlush() glutSwapBuffers() def resize(w, h): glViewport(0, 0, w, h) def mouse(button, state, x, y): if button == GLUT_LEFT_BUTTON: print "left button", elif button == GLUT_MIDDLE_BUTTON: print "middle button", elif button == GLUT_RIGHT_BUTTON: print "right button", else: print "unknown button:", button, if state == GLUT_DOWN: print "down mouse button", elif state == GLUT_UP: print "up mouse button", else: print "unknown state:", state, print(x, y) def motion(x, y): print "drag:", x, y def keyboard(key, x, y): if key == '\033': sys.exit() elif key == 'q': sys.exit() else: print(key) glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(320, 240) glutCreateWindow("PyOpenGL 5") glutReshapeFunc(resize) glutDisplayFunc(draw) glutMouseFunc(mouse) glutMotionFunc(motion) glutKeyboardFunc(keyboard) glutMainLoop()
def keyboard(key, x, y):
glutKeyboardFunc(keyboard)
以下の内容を~/.vimrcに保存します。
autocmd FileType python setl autoindent autocmd FileType python setl smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class autocmd FileType python setl tabstop=8 expandtab shiftwidth=4 softtabstop=4
glDrawBuffer(GL_FRONT)
glEnable(GL_COLOR_LOGIC_OP) glLogicOp(GL_INVERT) 直前の四角を描く → これで直前の四角が消える 現在の四角を描く glLogicOp(GL_COPY) glDisable(GL_COLOR_LOGIC_OP)
glEnable(GL_COLOR_LOGIC_OP) glLogicOp(GL_INVERT) 直前の四角を描く glLogicOp(GL_COPY) glDisable(GL_COLOR_LOGIC_OP) glDrawBuffer(GL_BACK)