ラバーバンドの表示

あとで書く。

from OpenGL.GL import *
from OpenGL.GLUT import *

MAXPOINTS = 100
point = [[0, 0]]*MAXPOINTS
pointnum = 0
rubberband = 0
savepoint = [0, 0]

def display():
    glClear(GL_COLOR_BUFFER_BIT)
    if pointnum > 1:
        glColor3d(0.0, 0.0, 0.0)
        glBegin(GL_LINES)
        for i in range(pointnum):
            glVertex2iv(point[i])
        glEnd()
    glFlush()

def resize(w, h):
    glViewport(0, 0, w, h)
    glLoadIdentity()
    glOrtho(-0.5, float(w)-0.5, float(h)-0.5, -0.5, -1.0, 1.0)

def mouse(button, state, x, y):
    global point
    global pointnum
    global rubberband
    if button == GLUT_LEFT_BUTTON:
        point[pointnum] = [x, y]
        if state == GLUT_UP:
            glColor3d(0.0, 0.0, 0.0)
            glBegin(GL_LINES)
            glVertex2iv(point[pointnum-1])
            glVertex2iv(point[pointnum])
            glEnd()
            glFlush()
            rubberband = 0
        if pointnum < MAXPOINTS -1:
            pointnum += 1
        else:
            pass

def motion(x, y):
    global rubberband
    global savepoint

    glEnable(GL_COLOR_LOGIC_OP)
    glLogicOp(GL_INVERT)

    glBegin(GL_LINES)
    if rubberband:
        glVertex2iv(point[pointnum - 1])
        glVertex2iv(savepoint)
    glVertex2iv(point[pointnum - 1])
    glVertex2i(x, y)
    glEnd()
    glFlush()
    glLogicOp(GL_COPY)
    glDisable(GL_COLOR_LOGIC_OP)

    savepoint = [x, y]
    rubberband = 1

def init():
    glClearColor(1.0, 1.0, 1.0, 1.0)

glutInitWindowPosition(100, 100)
glutInitWindowSize(320, 240)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA)
glutCreateWindow("sample2")
glutDisplayFunc(display)
glutReshapeFunc(resize)
glutMouseFunc(mouse)
glutMotionFunc(motion)
init()
glutMainLoop()