缩放,平移和旋转如何工作? [英] How does zooming, panning and rotating work?

查看:107
本文介绍了缩放,平移和旋转如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenGL绘制校园的原始地图.

Using OpenGL I'm attempting to draw a primitive map of my campus.

任何人都可以向我解释平移,缩放和旋转通常是如何实现的吗?

Can anyone explain to me how panning, zooming and rotating is usually implemented?

例如,在进行平移和缩放时,仅仅是调整视口吗?因此,我绘制并绘制了构成地图的所有线条,然后随着用户单击并拖动它来调整我的视口?

For example, with panning and zooming, is that simply me adjusting my viewport? So I plot and draw all my lines that compose my map, and then as the user clicks and drags it adjusts my viewport?

对于平移,它会移动视口的x/y值吗?对于缩放,是否会在一定程度上增加/减少视口?轮换呢?

For panning, does it shift the x/y values of my viewport and for zooming does it increase/decrease my viewport by some amount? What about for rotation?

对于旋转,我是否必须对代表我的校园地图的每条折线进行仿射变换?在大小适中的地图上即时进行操作会不会很昂贵?

For rotation, do I have to do affine transforms for each polyline that represents my campus map? Won't this be expensive to do on the fly on a decent sized map?

或者,视口是否保持不变,并且以其他方式进行了平移/缩放/旋转?

Or, is the viewport left the same and panning/zooming/rotation is done in some otherway?

例如,如果您转到此链接您会看到他通过修改视口来准确地描述我上面的平移和缩放.

For example, if you go to this link you'll see him describe panning and zooming exactly how I have above, by modifying the viewport.

这不正确吗?

推荐答案

它们是通过在绘制场景之前应用一系列glTranslate,glRotate命令(代表相机的位置和方向)来实现的. (从技术上讲,您正在旋转整个场景!)

They're achieved by applying a series of glTranslate, glRotate commands (that represent camera position and orientation) before drawing the scene. (technically, you're rotating the whole scene!)

有诸如gluLookAt之类的实用程序功能,它可以抽象一些有关此的详细信息.

There are utility functions like gluLookAt which sorta abstract some details about this.

为简单起见,假设您有两个代表相机的向量:位置和方向.

To simplyify things, assume you have two vectors representing your camera: position and direction.

gluLookAt获取位置,目的地和上向量.

gluLookAt takes the position, destination, and up vector.

如果实现矢量类,则destinaion = position + direction应该为您提供一个目的地.

If you implement a vector class, destinaion = position + direction should give you a destination point.

为使事情变得简单,您可以假设up向量始终为(0,1,0)

Again to make things simple, you can assume the up vector to always be (0,1,0)

然后,在渲染场景中的任何内容之前,加载身份矩阵并调用gluLookAt

Then, before rendering anything in your scene, load the identity matrix and call gluLookAt

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( source.x, source.y, source.z, destination.x, destination.y, destination.z, 0, 1, 0 );

然后开始绘制对象

您可以通过稍微向右或向左更改位置来让用户跨越.旋转有点复杂,因为您必须旋转方向向量.假设您旋转的是相机,而不是场景中的某些物体.

You can let the user span by changing the position slightly to the right or to the left. Rotation is a bit more complicated as you have to rotate the direction vector. Assuming that what you're rotating is the camera, not some object in the scene.

一个问题是,如果只有方向矢量前进",如何移动它?右边和左边在哪里?

One problem is, if you only have a direction vector "forward" how do you move it? where is the right and left?

在这种情况下,我的方法是仅取方向"与(0,1,0)的叉积.

My approach in this case is to just take the cross product of "direction" and (0,1,0).

现在,您可以使用以下方式将摄像机左右移动:

Now you can move the camera to the left and to the right using something like:

position = position + right * amount; //amount < 0 moves to the left

您可以使用方向矢量"向前移动,但是IMO最好将运动限制在水平面内,因此以与获得正确矢量相同的方式获取前向矢量:

You can move forward using the "direction vector", but IMO it's better to restrict movement to a horizontal plane, so get the forward vector the same way we got the right vector:

forward = cross( up, right )

说实话,这有点像是骇人听闻的方法.

To be honest, this is somewhat of a hackish approach.

正确的方法是使用更复杂"的数据结构来表示相机的方向",而不仅仅是向前.但是,由于您才刚刚起步,因此最好一次完成一个步骤.

The proper approach is to use a more "sophisticated" data structure to represent the "orientation" of the camera, not just the forward direction. However, since you're just starting out, it's good to take things one step at a time.

这篇关于缩放,平移和旋转如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆