use*_*541 4 opengl camera center rotation
我有一个场景,它基本上是一个方形地板,尺寸为 15x15(坐标为 (0,0,0) (0,0,15) (15,0,15) (15,0,0) 的四边形)。
我已将场景中心设置为 (7.5,0,7.5)。问题是我不知道如何围绕场景中心水平旋转相机(也就是让相机围绕场景中心做一个 360 度水平的圆圈)。我知道你需要对 sin 和 cos 做一些事情,但不知道到底是什么。
这是代码(纯C):
//set camera position
//camera height is 17
GLfloat camx=0, camy=17, camz=0;
//set center of scene
GLfloat xref=7.5, yref=0, zref=7.5;
gluLookAt(camx, camy, camz, xref, yref, zref, 0, 1, 0);
//projection is standard gluPerspective, nothing special
gluPerspective(45, (GLdouble)width/(GLdouble)height, 1, 1000);
Run Code Online (Sandbox Code Playgroud)
您需要修改 camx 和 camz 变量。
您要穿过的点位于圆上,它们的坐标由 确定x = r*sin(alpha) + 7.5, z = r*cos(alpha) + 7,5
,其中 r 是圆的半径, alpha 是 xy 平面与相机当前位置之间的角度。
当然,角度取决于旋转速度以及从动画开始的时间。基本上,您唯一需要做的就是设置直角,然后根据上述表达式计算坐标。
有关圆坐标的更多信息,请参阅 Wiki:http : //en.wikipedia.org/wiki/Unit_circle