我试图在 OpenGL 窗口上获得不同的 3D 视角。
如果您运行代码(与注释掉的 gluLookAt 有任何关系)并左键单击您单击的位置的红线点,右键单击,绿线与您单击的位置对齐。一切都发生在 z=0.0 的 xy 平面上,它也不会让你选择任何 -ve y 值(这是故意的)。
我正在尝试使用“x”、“y”和“z”键来平移场景。我尝试的第一件事是更改“眼睛”所在位置的值。这不过是一场彻头彻尾的灾难!!
无论我读了多少次 gluLookAt 应该做的事情,我都无法让它做到!
在下面的代码中,我试图将视图设置为在 z 方向上远离原点 10 个单位,沿着 z 轴看向原点。
我只是得到一个黑屏(如果我遗漏了任何与 gluLookAt 相关的内容,它看起来像http://i.imgur.com/IXlcKdo.png)
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import numpy as np
import sys
global lp, lq, rp, rq, z, eyeX, eyeY, eyeZ
lp = 0.0
lq = 2.5
rp = 0.0
rq = 2.5
z = 0.0
# gluLookAt 'eye' coordinates
eyeX = 0.0
eyeY = 0.0
eyeZ = 10.0
def init():
global eyeX, eyeY, eyeZ
glClearColor(0.0, 0.0, 0.0, 1.0)
glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0)
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
def plotdial():
global lp, lq, rp, rq, eyeX, eyeY, eyeZ
#lOrigin = np.array((0.0, 0.0, 0.0))
rOrigin = np.array((5.0, 0.0, 0.0))
radius = 2.5
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
# Left arm unit vector calculation
lVec = np.array((lp, lq, z))
lMag = np.linalg.norm(lVec)
lVec = (lVec/lMag)*radius
glColor3f(1.0, 0.0, 0.0)
glLineWidth(3.0)
glBegin(GL_LINES)
glVertex3f(0.0, 0.0, z)
glVertex3f(lVec[0], lVec[1], z)
glEnd()
glLineWidth(1.0)
glColor3f(0.0, 0.0, 1.0)
glutWireSphere(2.5, 25, 25)
# Right
glPushMatrix()
glTranslatef(rOrigin[0], rOrigin[1], z)
glColor3f(0.0, 1.0, 0.0)
glLineWidth(3.0)
glBegin(GL_LINES)
glVertex3f(0.0, 0.0, z)
rVec = np.array((rp, rq, z))
rMag = np.linalg.norm(rVec)
rVec = (rVec/rMag)*radius
glVertex3f(rVec[0], rVec[1], z)
glEnd()
glLineWidth(1.0)
glColor3f(0.0, 0.0, 1.0)
glutWireSphere(2.5, 25, 25)
glPopMatrix()
glFlush()
def keyboard(key, x, y):
global glutshape, solid, eyeX, eyeY, eyeZ
step = 0.01
if key == chr(27) or key == "q":
sys.exit()
if key == "x":
eyeX -= step
if key == "X":
eyeX += step
if key == "y":
eyeY -= step
if key == "Y":
eyeY += step
if key == "z":
eyeZ -= step
if key == "Z":
eyeZ -= step
glutPostRedisplay()
def mouse(button, state, x, y):
global lp, lq, rp, rq
o_lp = lp
o_lq = lq
o_rp = rp
o_rq = rq
if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
lp = -10.0 + x/500.0 * 20
lq = 10 - y/500.0 * 20
if lq < 0.0:
lp = o_lp
lq = o_lq
if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
rp = -10.0 + x/500.0 * 20 - 5 # -5 is 2*radius
rq = 10 - y/500.0 * 20
if rq < 0.0:
rp = o_rp
rq = o_rq
glutPostRedisplay()
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(50,50)
glutCreateWindow('Dial Trial')
glutDisplayFunc(plotdial)
glutKeyboardFunc(keyboard)
glutMouseFunc(mouse)
init()
glutMainLoop()
main()
那么这里有什么故事呢?是不是 glLookAt 不使用 glOrtho 中设置的轴?