OpenGL在场景中心周围旋转相机(OpenGL Rotate camera around center

编程入门 行业动态 更新时间:2024-10-28 08:16:58
OpenGL在场景中心周围旋转相机(OpenGL Rotate camera around center-of-scene)

我有一个场景基本上是一个15x15的方形楼层(四边形坐标(0,0,0)(0,0,15)(15,0,15)(15,0,0))。

我将场景中心设置为(7.5,0,7.5)。 问题是我无法弄清楚如何围绕该场景中心水平旋转相机(也就是让相机在场景中心周围做360度水平圆)。 我知道你需要用罪和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);

I have a scene which is basically a square floor measuring 15x15 (a quad with coordinates (0,0,0) (0,0,15) (15,0,15) (15,0,0) ).

I 've set the center-of-scene to be at (7.5,0,7.5). Problem is I can't figure out how to rotate the camera horizontally around that center of scene (aka make the camera do a 360 horizontal circle around center-of-scene). I know you need to do something with sin and cos, but don't know what exactly.

Here is the code (plain 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);

最满意答案

您需要修改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

You need to modify the camx and camz variables.

The points you want to walk through lie on the circle and their coordinates are determined by x = r*sin(alpha) + 7.5, z = r*cos(alpha) + 7,5, where r is the radius of the circle and alpha is the angle between xy plane and the current position of your camera.

Of course the angle depends on the rotation speed and also on the time from the beginning of the animation. Basically, the only thing you need to do is to set the right angle and then calculate the coordinates from the expressions above.

For more info about the circle coordinates, see Wiki : http://en.wikipedia.org/wiki/Unit_circle

更多推荐

本文发布于:2023-07-22 18:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:场景   相机   中心   OpenGL   camera

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!