OpenGL 1.4.当我使用鼠标拾取更改旋转中心时,场景会发生变化 [英] OpenGL 1.4. The scene shifts when I change the center of rotation using mouse picking

查看:56
本文介绍了OpenGL 1.4.当我使用鼠标拾取更改旋转中心时,场景会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来绘制场景,但是当旋转中心改变时,场景上的物体向下移动(沿轴移动场景),请帮助找出如何避免这种效果.

glTranslated(m_rotationCenter.x, 0, 0);glTranslated(0, m_rotationCenter.y, 0);glTranslated(0, 0, m_rotationCenter.z);glScalef(规模,规模,规模);glTranslated(-m_rotationCenter.x, 0, 0);glTranslated(0, -m_rotationCenter.y, 0);glTranslated(0, 0, -m_rotationCenter.z);glTranslated(m_rotationCenter.x-xTrans, 0, 0);glTranslated(0, m_rotationCenter.y+yTrans, 0);glTranslated(0, 0, m_rotationCenter.z+zTrans);glRotated( xRot, 1.0, 0.0, 0.0 );glRotated( yRot, 0.0, 1.0, 0.0 );glRotated( zRot, 0.0, 0.0, 1.0 );glTranslated(-m_rotationCenter.x+xTrans, 0, 0);glTranslated(0, -m_rotationCenter.y-yTrans, 0);glTranslated(0, 0, -m_rotationCenter.z-zTrans);glTranslated( -xTrans, 0.0, 0.0 );glTranslated( 0.0, yTrans, 0.0 );glTranslated(0.0, 0.0, zTrans);

使用鼠标拾取更改旋转中心.

例如,在原始中心

然后我通过鼠标点击改变中心

解决方案

首先,这段代码:

<块引用>

glTranslated(m_rotationCenter.x, 0, 0);glTranslated(0, m_rotationCenter.y, 0);glTranslated(0, 0, m_rotationCenter.z);glScalef(规模,规模,规模);glTranslated(-m_rotationCenter.x, 0, 0);glTranslated(0, -m_rotationCenter.y, 0);glTranslated(0, 0, -m_rotationCenter.z);glTranslated(m_rotationCenter.x-xTrans, 0, 0);glTranslated(0, m_rotationCenter.y+yTrans, 0);glTranslated(0, 0, m_rotationCenter.z+zTrans);glRotated( xRot, 1.0, 0.0, 0.0 );glRotated( yRot, 0.0, 1.0, 0.0 );glRotated( zRot, 0.0, 0.0, 1.0 );glTranslated(-m_rotationCenter.x+xTrans, 0, 0);glTranslated(0, -m_rotationCenter.y-yTrans, 0);glTranslated(0, 0, -m_rotationCenter.z-zTrans);glTranslated( -xTrans, 0.0, 0.0 );glTranslated( 0.0, yTrans, 0.0 );glTranslated(0.0, 0.0, zTrans);

可以这样写,并且会做完全一样的事情:

glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);glScalef(规模,规模,规模);glTranslated(-xTrans, yTrans, zTrans);glRotated( xRot, 1.0, 0.0, 0.0 );glRotated( yRot, 0.0, 1.0, 0.0 );glRotated( zRot, 0.0, 0.0, 1.0 );glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z);

但是如果你想围绕一个点(m_rotationCenter)做一个旋转,那么你必须改变指令的顺序.

请注意,要围绕枢轴旋转,您必须以这种方式平移对象,即旋转点位于原点 (0, 0, 0):

glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z);

旋转对象:

glRotated( xRot, 1.0, 0.0, 0.0 );glRotated( yRot, 0.0, 1.0, 0.0 );glRotated( zRot, 0.0, 0.0, 1.0 );

最后将旋转后的物体移回原来的位置:

glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);

当然这个指令必须以相反的顺序完成,因为像 glTranslatedglRotated 定义一个新矩阵并将其与当前矩阵相乘.

最终代码必须是:

glScalef( scale, scale, scale );glTranslated(-xTrans, yTrans, zTrans);glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);glRotated( xRot, 1.0, 0.0, 0.0 );glRotated( yRot, 0.0, 1.0, 0.0 );glRotated( zRot, 0.0, 0.0, 1.0 );glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z);

<小时>

此外,我建议先进行缩放(代码中的最后一个),这样定义像 m_rotationCenterxTrans 这样的坐标会更容易,因为缩放不是应用于此坐标.仅应用于模型:

//将模型移动到它的位置glTranslated(-xTrans, yTrans, zTrans);//围绕 `m_rotationCenter` 旋转模型glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);glRotated( xRot, 1.0, 0.0, 0.0 );glRotated( yRot, 0.0, 1.0, 0.0 );glRotated( zRot, 0.0, 0.0, 1.0 );glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z);//缩放模型glScalef(规模,规模,规模);

I use the following code to draw the scene, but when the center of rotation changes, the objects on the scene shift down (displacing the scene along the axes), please help find out how to avoid this effect.

glTranslated(m_rotationCenter.x, 0, 0);
glTranslated(0, m_rotationCenter.y, 0);
glTranslated(0, 0, m_rotationCenter.z);
glScalef( scale, scale, scale );
glTranslated(-m_rotationCenter.x, 0, 0);
glTranslated(0, -m_rotationCenter.y, 0);
glTranslated(0, 0, -m_rotationCenter.z);
glTranslated(m_rotationCenter.x-xTrans, 0, 0);
glTranslated(0, m_rotationCenter.y+yTrans, 0);
glTranslated(0, 0, m_rotationCenter.z+zTrans);
glRotated( xRot, 1.0, 0.0, 0.0 );
glRotated( yRot, 0.0, 1.0, 0.0 );
glRotated( zRot, 0.0, 0.0, 1.0 );
glTranslated(-m_rotationCenter.x+xTrans, 0, 0);
glTranslated(0, -m_rotationCenter.y-yTrans, 0);
glTranslated(0, 0, -m_rotationCenter.z-zTrans);
glTranslated( -xTrans, 0.0, 0.0 );
glTranslated( 0.0, yTrans, 0.0 );
glTranslated( 0.0, 0.0, zTrans);

The center of rotation is changed using mouse picking.

For example, in original center

Then i change center by mouse click

解决方案

First of all, this code:

glTranslated(m_rotationCenter.x, 0, 0);
glTranslated(0, m_rotationCenter.y, 0);
glTranslated(0, 0, m_rotationCenter.z);
glScalef( scale, scale, scale );
glTranslated(-m_rotationCenter.x, 0, 0);
glTranslated(0, -m_rotationCenter.y, 0);
glTranslated(0, 0, -m_rotationCenter.z);
glTranslated(m_rotationCenter.x-xTrans, 0, 0);
glTranslated(0, m_rotationCenter.y+yTrans, 0);
glTranslated(0, 0, m_rotationCenter.z+zTrans);
glRotated( xRot, 1.0, 0.0, 0.0 );
glRotated( yRot, 0.0, 1.0, 0.0 );
glRotated( zRot, 0.0, 0.0, 1.0 );
glTranslated(-m_rotationCenter.x+xTrans, 0, 0);
glTranslated(0, -m_rotationCenter.y-yTrans, 0);
glTranslated(0, 0, -m_rotationCenter.z-zTrans);
glTranslated( -xTrans, 0.0, 0.0 );
glTranslated( 0.0, yTrans, 0.0 );
glTranslated( 0.0, 0.0, zTrans);

Can be written like this and will do exactly the same:

glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);
glScalef( scale, scale, scale );
glTranslated(-xTrans, yTrans, zTrans);
glRotated( xRot, 1.0, 0.0, 0.0 );
glRotated( yRot, 0.0, 1.0, 0.0 );
glRotated( zRot, 0.0, 0.0, 1.0 );
glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z);

But if you want to do a rotation around a point (m_rotationCenter), then you have to change the order of the instructions.

Note, to rotate around a pivot, you have to translate the object in that way, that the point of rotation is at the origin (0, 0, 0):

glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z); 

The rotate the object:

glRotated( xRot, 1.0, 0.0, 0.0 );
glRotated( yRot, 0.0, 1.0, 0.0 );
glRotated( zRot, 0.0, 0.0, 1.0 );

Finally move the rotated object back on its original place:

glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);

Of course this instructions have to be done in the reverse order, because operations like glTranslated and glRotated define a new matrix and multiply it to the current matrix.

The final code has to be:

glScalef( scale, scale, scale );
glTranslated(-xTrans, yTrans, zTrans);

glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);

glRotated( xRot, 1.0, 0.0, 0.0 );
glRotated( yRot, 0.0, 1.0, 0.0 );
glRotated( zRot, 0.0, 0.0, 1.0 );

glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z);


Further I recommend to do the scaling first (last in the code), so it is easier to define the coordinates like m_rotationCenter and xTrans, because the scale is not applied to this coordinates.It is applied to the model only:

// move the model to its location
glTranslated(-xTrans, yTrans, zTrans);

// rotate the model around `m_rotationCenter`
glTranslated(m_rotationCenter.x, m_rotationCenter.y, m_rotationCenter.z);
glRotated( xRot, 1.0, 0.0, 0.0 );
glRotated( yRot, 0.0, 1.0, 0.0 );
glRotated( zRot, 0.0, 0.0, 1.0 );
glTranslated(-m_rotationCenter.x, -m_rotationCenter.y, -m_rotationCenter.z);

// scale the model
glScalef( scale, scale, scale );

这篇关于OpenGL 1.4.当我使用鼠标拾取更改旋转中心时,场景会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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