OSG下鼠标点击实现对模型的移动,旋转(漫游器)

更新时间:2023-06-04 13:31:55 阅读: 评论:0

OSG下⿏标点击实现对模型的移动,旋转(漫游器)
点击⿏标实现对模型的移动,旋转功能,需要⽤到漫游器。在实现的过程中的需要⽤到osgGA模块的知识,
重写osgGA下的该函数virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us);可以实现对模型的操作。
1、模型的移动:
static Matrixd  translate (const Vec3f &dv)
2、模型的缩放
static Matrixd  scale (const Vec3f &sv)
3、模型的旋转
static Matrixd  rotate (const Vec3f &from, const Vec3f &to)事业单位抚恤金
4、可以直接⽤MatrixTransform操作矩阵,来控制模型的平移、缩放和旋转
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
osg::ref_ptr<osg::Node> cow= osgDB::readNodeFile("cow.osg");
中国十大名酒是哪十种mt->addChild(cow);
//先向x轴移动30, 绕x轴旋转90度, 再缩放到原来的0.2倍
mt->tMatrix(osg::Matrix::translate(osg::Vec3(30, 0, 0)) *
预备党员必须面向什么进行入党宣誓osg::Matrix::rotate(90, osg::Vec3d(1, 0, 0)) *
osg::Matrix::scale(osg::Vec3(0.2, 0.2, 0.2)) );
如何实现?
可以通过按W、A、S 、D去实现模型的移动,可以按⿏标左键进⾏物体位置的拖移,按⿏标右键拖移可以实现物体的旋转。
在实现的过程有⼀个屏幕坐标向世界坐标转换的过程。以下部分代码的函数和变量并未有⽤到。
#include "stdafx.h"
#include <iostream>
#include <osg/Node>
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Light>
#include <osg/Group>
#include <osg/LightSource>
#include <osg/BoundingSphere>
#include <osg/BoundingBox>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osg/MatrixTransform>
#include <osg/StateSet>
#include <osgGA/CameraManipulator>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIActionAdapter>
#include <osgGA/GUIEventHandler>
#include <osgViewer/ViewerEventHandlers>
class TravelManipulator :public osgGA::CameraManipulator
{
public:
TravelManipulator();
~TravelManipulator();
public:
//设置当前视⼝
virtual void tByMatrix(const osg::Matrixd&matrix);
//设置当前视⼝
virtual void tByInverMatrix(const osg::Matrixd&matrix);
virtual void tByInverMatrix(const osg::Matrixd&matrix);
//得到当前矩阵
virtual osg::Matrixd getMatrix() const;
/
/得到当前矩阵
virtual osg::Matrixd getInverMatrix() const;
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us);
osg::Vec3 screen2World(float x, float y);
private:
//视点
osg::Vec3 mPosition;
//朝向
osg::Vec3 mRotation;
osg::ref_ptr<osgViewer::View> view;
bool lbuttonDown;
bool m_rButtonDown;
float m_leftX;
float m_leftY;
float m_leftZ;
float m_rightX;
float m_rightY;
float m_rightZ;
};
TravelManipulator::TravelManipulator()
{
mPosition = osg::Vec3(0, 0, 50);
mRotation = osg::Vec3(0, 0, 0);
m_rButtonDown = fal;
lbuttonDown = fal;
舞龙怎么画m_leftX = 0;
m_leftY = 0;
m_rightX = 0;
m_rightY = 0;
}
TravelManipulator::~TravelManipulator()
{
}
void TravelManipulator::tByMatrix(const osg::Matrixd&matrix)
{
}
儿童简易窗花void TravelManipulator::tByInverMatrix(const osg::Matrixd&matrix)
{
}
osg::Matrixd TravelManipulator::getMatrix() const
{
osg::Matrixd mat;
mat.makeTranslate(mPosition);
return mat*osg::Matrixd::rotate(mRotation[0], osg::X_AXIS, mRotation[1], osg::Y_AXIS, mRotation[2], osg::Z_AXIS);
return mat*osg::Matrixd::rotate(mRotation[0], osg::X_AXIS, mRotation[1], osg::Y_AXIS, mRotation[2], osg::Z_AXIS);
}
osg::Matrixd TravelManipulator::getInverMatrix() const
{
osg::Matrixd mat;
mat.makeTranslate(mPosition);
return osg::Matrixd::inver(mat*osg::Matrixd::rotate(mRotation[0], osg::X_AXIS, mRotation[1], osg::Y_AXIS, mRotation[2], osg::Z_AXIS)); }
bool TravelManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
{
view = dynamic_cast<osgViewer::View*>(&us);
if (!view)return fal;
switch (ea.getEventType()) {
热爱生命ca osgGA::GUIEventAdapter::KEYDOWN:
if (ea.getKey() == 'w') {
mPosition[2] += 2;
}
el if (ea.getKey() == 's') {
mPosition[2] -= 2;
}
el if (ea.getKey() == 'a') {
mPosition[0] -= 2;
}
el if (ea.getKey() == 'd') {
mPosition[0] += 2;
}
break;
ca osgGA::GUIEventAdapter::PUSH:
{
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
{
osg::Vec3 vec1 = X(), ea.getY());
/
*m_leftX = ea.getX();
m_leftY = ea.getY();*/
m_leftZ = vec1.z();
鼻子上有横纹
m_leftX = vec1.x();
m_leftY = vec1.y();
lbuttonDown = true;
}
if (ea.getButton()==osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)
{
m_rightX = ea.getX();
m_rightY = ea.getY();
m_rButtonDown = true;
}
return fal;
}
ca osgGA::GUIEventAdapter::DRAG:
{
if (lbuttonDown)
{
osg::Vec3 vec1 = X(), ea.getY());
float ix = vec1.x() - m_leftX;
float iy = vec1.y() - m_leftY;
float iz = vec1.z() - m_leftZ;
mPosition[2] += 0.1*iz;
mPosition[1] -= 0.1*iy;
mPosition[1] -= 0.1*iy;
mPosition[0] -= 0.1*ix;
}
if (m_rButtonDown)
{
float ix = ea.getX() - m_rightX;
float iy = ea.getY() - m_rightY;
mRotation[2] += osg::DegreesToRadians(0.1*ix);
mRotation[0] -= osg::DegreesToRadians(0.1*iy);
}
return fal;
}
ca osgGA::GUIEventAdapter::RELEASE:
{
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
{
lbuttonDown = fal;
}
if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)
{
m_rButtonDown = fal;
}
}
default:
break;
}
return fal;
}
osg::Vec3 TravelManipulator::screen2World(float x, float y)
{
osg::Vec3 vec3;
osg::ref_ptr<osg::Camera> camera = view->getCamera();
osg::Vec3 vScreen(x, y, 0);
osg::Matrix mVPW = camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix(); osg::Matrix invertVPW;
时光旅行者的妻子
invertVPW.invert(mVPW);
vec3 = vScreen * invertVPW;
return vec3;
}
int main(int argc, char** argv)
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->addEventHandler(new osgViewer::WindowSizeHandler);
viewer->addEventHandler(new osgViewer::StatsHandler);
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(osgDB::readNodeFile("cow.osg"));
viewer->tSceneData(root);
viewer->tCameraManipulator(new TravelManipulator());
viewer->realize();
viewer->run();
return 0;
}
注意:mat 放置前后位置不同,会出现不同的现象
mat*osg::Matrixd::rotate(mRotation[0], osg::X_AXIS, mRotation[1], osg::Y_AXIS, mRotation[2], osg::Z_AXIS);

本文发布于:2023-06-04 13:31:55,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/860556.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:实现   移动   旋转   过程   对模型   模型   物体   位置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图