GLUT によってメニューを作成するには以下の関数を使用します。
glutCreateMenu(func)
glutAddMenuEntry(label, num)
glutAddSubMenu(lable, submenu)
glutAttachMenu(mouse)
#!env python
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
vertex = [
[ 0.0, 0.0, 0.0 ],
[ 1.0, 0.0, 0.0 ],
[ 1.0, 1.0, 0.0 ],
[ 0.0, 1.0, 0.0 ],
[ 0.0, 0.0, 1.0 ],
[ 1.0, 0.0, 1.0 ],
[ 1.0, 1.0, 1.0 ],
[ 0.0, 1.0, 1.0 ]]
edge = [
[ 0, 1 ],
[ 1, 2 ],
[ 2, 3 ],
[ 3, 0 ],
[ 4, 5 ],
[ 5, 6 ],
[ 6, 7 ],
[ 7, 4 ],
[ 0, 4 ],
[ 1, 5 ],
[ 2, 6 ],
[ 3, 7 ]]
direction = 0
angle = [0.0, 0.0, 0.0]
mousex = 0
rstart = False
def resize(w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(30.0, w/h, 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
def draw():
global angle
glClearColor(0.0, 0.0, 1.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glRotated(angle[0], 1.0, 0.0, 0.0);
glRotated(angle[1], 0.0, 1.0, 0.0);
glRotated(angle[2], 0.0, 0.0, 1.0);
glBegin(GL_LINES)
for i in range(0, 12):
glVertex(vertex[edge[i][0]])
glVertex(vertex[edge[i][1]])
glEnd()
glFlush()
glutSwapBuffers()
def mouse(button, state, x, y):
global mousex
global rstart
if button != GLUT_LEFT_BUTTON:
return
if state == GLUT_DOWN:
mousex = x
rstart = True
elif state == GLUT_UP:
rstart = False
def motion(x, y):
global mousex
global angle
global rstart
if rstart == False:
return
angle[direction] += mousex - x
mousex = x
glutPostRedisplay()
def menu(item):
global direction
if item == 3:
sys.exit()
else:
direction = item
return 0
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(320, 320)
glutCreateWindow("PyOpenGL 19")
glutDisplayFunc(draw)
glutReshapeFunc(resize)
glutMouseFunc(mouse)
glutMotionFunc(motion)
glutCreateMenu(menu);
glutAddMenuEntry('rotateX', 0)
glutAddMenuEntry('rotateY', 1)
glutAddMenuEntry('rotateZ', 2)
glutAddMenuEntry('quit', 3)
glutAttachMenu(GLUT_RIGHT_BUTTON)
glutMainLoop()
direction = 0
angle = [0.0, 0.0, 0.0]
mousex = 0
rstart = False
direction = item
glutCreateMenu(menu);
glutAddMenuEntry('rotateX', 0)
glutAddMenuEntry('rotateY', 1)
glutAddMenuEntry('rotateZ', 2)
glutAddMenuEntry('quit', 3)
glutAttachMenu(GLUT_RIGHT_BUTTON)
サブメニューを作成するには、glutAddSubMenu() を使用します。
上記のスクリプトを、サブメニュー化するにはmenu()とメニュー作成部分を以下のように変更します。
def menu1(item):
global direction
direction = item
def menu2(item):
if item == 0:
sys.exit()
submenu = glutCreateMenu(menu1);
glutAddMenuEntry('rotateX', 0)
glutAddMenuEntry('rotateY', 1)
glutAddMenuEntry('rotateZ', 2)
glutCreateMenu(menu2);
glutAddSubMenu('rotate', submenu)
glutAddMenuEntry('quit', 0)
glutAttachMenu(GLUT_RIGHT_BUTTON)