我不明白这个GluLookAt是如何在OpenGl中工作的。
我想知道如何转换这两条线:
gluLookAt(5.0, 15.0, 2.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0);
gluLookAt(5.0, 0.0, 5.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0);
使用glRotatef和glTranslatef。
经过一些搜索,似乎存在一种制造这种东西的方法:
glRotatef();
glRotatef();
glTranslatef(5.0,15.0,2.0);
glRotatef();
glRotatef();
glTranslatef(5.0,0.0,5.0);
只需要使用两个旋转和一个平移。但是我不明白怎样才能找到这些旋转的角度和轴。
我试图在下面解释函数是如何工作的。希望它能让你理解这个概念。对于旋转和平移,您可以查看此链接以了解如何处理它。
struct Triple
{
float x,y,z;
}
//CameraPosition
Triple Cp(a,b,c); //initialise your camera position
//LookatPosition
Triple Lp(e,f,g); //initialise your lookat position
//Up vector
Triple Up(k,l,m); //initialise your up vector
UpdateCamera()
{
//Update Cp, Lp here
//if you move your camera use translatef to update camera position
//if you want to change looking direction use correct rotation and translation to update your lookat position
//if you need to change up vector simply change it to
Up = Triple(knew,lnew,mnew);
}
display()
{
gluLookAt(Cp.x,Cp.y,Cp.z,Lp.x,Lp.y,Lp.z,Up.x,Up.y,Up.z);
//Your object drawings Here
}
我想避开glRotate和glTranslate,改为使用glLoadMatrix(如果要相乘,glLoadMatrix将替换堆栈上的当前矩阵,请使用glMultMatrix):然后使用一个浮点数组,其中包含按列主顺序排列的矩阵:
xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
-dot(xaxis, camP) -dot(yaxis, camP) -dot(zaxis, camP) 1
其中
zaxis = normal(At - camP)
xaxis = normal(cross(Up, zaxis))
yaxis = cross(zaxis, xaxis)
camP是相机的位置,在相机正在观察的点上,向上是上向量。