OSG 模型随鼠标移动

该博客介绍了一个目标:通过使视角反向移动来模拟模型被拖拽的效果。当鼠标在屏幕上从m_1移动到m_0时,计算相机在x-z平面上的移动,以及沿y轴的旋转。相机的位移基于dx和dy的值,以及相机的视角角度theta和theta_y。最终,更新相机位置以达到预期的移动效果。示例代码展示了如何实现这一功能。

实现目标:当鼠标向某个方向移动时,视角向其反方向移动,相当于对模型整体进行拖拽的感觉。
示意图:

在这里插入图片描述

如图,在屏幕平面上,鼠标从m_1位置移动到m_0位置,两个点坐标的x方向差值为dx,y方向差值为dy
在过相机位置(eye)且垂直于相机于y轴夹角方向向量(图中x-o-y平面紫色的向量)的平面中,要将屏幕上鼠标的移动对应到相机上,在图中鼠标是向屏幕左上方移动的,相机应该向右下方移动到目标位置,为了方便理解,我们先计算相机移动到左上方的目标反方向位置(点p):
假设相机移动步长为move_len, 那么相机z轴方向位移z_len为:move_len·sin(theta)
相机在平行于x-o-y面的移动距离xy_len为move_len·cos(theta)

在这里插入图片描述

如上图,在x-o-y面,相当于相机向左平移move_len · cos(theta)
则相机x轴方向平移距离:|move_len · cos(theta) · cos(theta_y)| theta_y为相机视角与y轴夹角
则相机y轴方向平移距离:|move_len·cos(theta)·sin(theta_y)|
那么若将相机移动到图1 p点 其移动可表示为:
eye += osg::Vec3f(-|move_len·cos(theta)·cos(theta_y)|,|move_len·cos(theta)·sin(theta_y)| ,move_len·sin(theta))
将相机移动到目标位置就是移动到点p的反方向:
eye += -osg::Vec3f(-|move_len·cos(theta)·cos(theta_y)|,|move_len·cos(theta)·sin(theta_y)| ,move_len*sin(theta))

附部分代码:

.h中定义两个临时事件指针
osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t1;//旧事件
osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t0;//新事件
//保存事件函数:
void addMouseEvent(const osgGA::GUIEventAdapter& ea)
{
	_ga_t1 = _ga_t0;//上次事件
	_ga_t0 = &ea;//最新事件
}
//handle处理按键事件
case osgGA::GUIEventAdapter::PUSH:
	{
		if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)
		{
			addMouseEvent(ea);
		}
		return false;
	}
//handle处理拖拽事件
case osgGA::GUIEventAdapter::DRAG:
	{
		if (_ga_t0 == NULL)
		{
			return false;
		}
		unsigned int buttonMask = _ga_t0->getButtonMask();
		if(buttonMask == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)
		{		
			addMouseEvent(ea);//添加事件
			//计算x y偏移
			int delX = _ga_t0->getX() - _ga_t1->getX();
			int delY = _ga_t0->getY() - _ga_t1->getY();
			//计算sin theta 及 cos theta
			int abs_x = abs(delX);
			int abs_y = abs(delY);
			float sin_theta = abs_y / sqrt(abs_x*abs_x + abs_y * abs_y);
			float cos_theta = abs_x / sqrt(abs_x*abs_x + abs_y * abs_y);
			int range = 3;灵敏度
			osg::Vec3f move(0, 0, node_radius * 0.05);//相机对应移动长度 此处设置为模型包围球半径*0.05
			if (delY > 0 && delX >0 && abs_x > range && abs_y > range)
			{//鼠标右上移动 视角左下移动
				move.x() = -node_radius * 0.05*cos_theta*cosf(m_rotate[2]);//m_rotate[2]是相机与y轴夹角 正负号与该角度相关
				move.y() = -node_radius * 0.05*cos_theta*sinf(m_rotate[2]);
				move.z() = -move.z()*sin_theta;
				m_Position += move;//m_Position为相机位置
			}
			else if (delY < 0 && delX < 0 && abs_x > range && abs_y > range)
			{//鼠标 左下 视角 右上
				move.x() = node_radius * 0.05*cos_theta*cosf(m_rotate[2]);
				move.y() = node_radius * 0.05*cos_theta*sinf(m_rotate[2]);
				move.z() = move.z()*sin_theta;
				m_Position += move;
			}
			else if (delY < 0 && delX > 0 && abs_x > range && abs_y > range)
			{//鼠标右下 视角 左上
				move.x() = -node_radius * 0.05*cosf(m_rotate[2]);
				move.y() = -node_radius * 0.05*sinf(m_rotate[2]);
				move.z() = move.z()*sin_theta;
				m_Position += move;
			}
			else if (delY > 0 && delX < 0 && abs_x > range && abs_y > range)
			{//鼠标 左上 视角 右下
				move.x() = node_radius * 0.05 * cosf(m_rotate[2]);
				move.y() = node_radius * 0.05 * sinf(m_rotate[2]);
				move.z() = -move.z()*sin_theta;
				m_Position += move;
			}
			else if (delY > 0 && delX == 0)//abs_x < range)// && abs_y >= range)
			{//鼠标向上 视角向下
				//move.x() = move_step * 0.3*cosf(m_rotate[2]);
				//move.y() = -move_step * 0.3*sinf(m_rotate[2]);
				move.z() = -move.z();
				m_Position += move;
			}
			else if (delY < 0 && delX == 0)// && abs_y >= range)
			{//鼠标向下 视角向上
				//move.x() = move_step * 0.3*cosf(m_rotate[2]);
				//move.y() = -move_step * 0.3*sinf(m_rotate[2]);
				//move.z() = -move.z();
				m_Position += move;
			}
			else if (delX < 0 && delY == 0)// abs_y < range && abs_x >= range)
			{//鼠标向左 视角向右移动
				move.x() = node_radius * 0.05*cosf(m_rotate[2]);
				move.y() = node_radius * 0.05*sinf(m_rotate[2]);
				move.z() = 0;
				m_Position += move;
			}
			else if (delX > 0 && delY == 0) //abs_y < range && abs_x >= range)
			{//鼠标向右 视角向左移动
				move.x() = -node_radius * 0.05*cosf(m_rotate[2]);
				move.y() = -node_radius * 0.05*sinf(m_rotate[2]);
				move.z() = 0;
				m_Position += move;
			}
		//}
	}
	break;
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值