概要:gluLookAt

$ cd ../redbook/Chapter3,Viewing/cube/
$ python2.5 -i cube.py -ss
['cube.py', '-ss']
>>> print gluLookAt.__doc__
gluLookAt(
    GLdouble(eyeX), GLdouble(eyeY), GLdouble(eyeZ),
    GLdouble(centerX), GLdouble(centerY), GLdouble(centerZ),
    GLdouble(upX), GLdouble(upY), GLdouble(upZ)
    ) -> None

Example 3-1:Transformed Cube: cube.c

$ python2.5 cube.py
視点 (0,0,5) から目標 (0,0,0) を観察すると、ウィンドウの中心に立方体(原点)が位置します。Y 軸 (0,1,0) が上向きになります。
#! /usr/bin/python
"""
OpenGL Programming Guide
Chapter 3 : Viewing
A Simple Example: Drawing a Cube
Example 3-1 : Transformed Cube: cube.c
"""
from OpenGL.GL   import *
from OpenGL.GLU  import *
from OpenGL.GLUT import *
from sys import argv

def init(): 
   glClearColor(0.0, 0.0, 0.0, 0.0)
   glShadeModel(GL_FLAT)

def display():
   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 reshape(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)

## ----------------------------------------
def ex_():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
    glutInitWindowSize(200, 100)
    glutInitWindowPosition(100, 100)
    glutCreateWindow(title())             # modify
    init()
    glutDisplayFunc(display)
    glutReshapeFunc(reshape)
    glutKeyboardFunc(keyboard)         # append
    glutMainLoop()

misc

def title():
    return argv[0][:-3]

def keyboard(*args):
    print args
    key, x, y = args
    if key == '\x1b':   # escape
        exit()

def ex_s(): print argv

EX = ""
if __name__ == '__main__':
    for e in argv:
        if e.startswith("-s"): EX = e[2:]
    eval("ex_%s()"%EX)

事例:コマンドライン引数

gluLookAt の引数を、コマンドラインから指定します。

$ python2.5 cube.py -sp 0 0 4 0.5 -1.0 0 0 1 0
eye(0,0,4) center(0.5,-1.0,0) up(0,1,0)
目標を x 軸右方向に 0.5、y 軸下方向に 1.0 移動します。すると、立方体の角(右下隅)がウィンドウの中央になります。
$ python2.5 cube.py -sp 0 0 4 0 0 0 1 1 0
eye(0,0,4) center(0,0,0) up(1,1,0)
法線ベクトルを xy 平面の斜め方向に傾けます。
 
 
□□□□□□□□□□□□□□□□□□□□□□□□ □□□□□□□□□□□□□□□□□□□□□□□□
def ex_p():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
    glutInitWindowSize(200, 200)
    glutInitWindowPosition(100, 100)
    glutCreateWindow(title())
    init()
    glutDisplayFunc(display1)
    glutReshapeFunc(reshape)
    glutKeyboardFunc(keyboard)
    glutMainLoop()

def display1():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0, 1.0, 1.0)
    glLoadIdentity()                 # clear the matrix 
    # viewing transformation
    _gluLookAt()
    glScalef(1.0, 2.0, 1.0)          # modeling transformation 
    glutWireCube(1.0)
    glFlush()

args = ([0]*9, argv[2:])[argv[1] == "-sp"]
eyeX,eyeY,eyeZ, centerX,centerY,centerZ, upX,upY,upZ = args

def _gluLookAt():
    global eyeX,eyeY,eyeZ, centerX,centerY,centerZ, upX,upY,upZ
    print "eye(%s,%s,%s) center(%s,%s,%s) up(%s,%s,%s)"%(
        eyeX,eyeY,eyeZ, centerX,centerY,centerZ, upX,upY,upZ
        )
    gluLookAt(
        float(eyeX),    float(eyeY),    float(eyeZ),
        float(centerX), float(centerY), float(centerZ),
        float(upX),     float(upY),     float(upZ),
        )