| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813 |
- /**************************************************************************************************
- 版本所有(C), 2020-2022, 北京普达迪泰
- ***************************************************************************************************
- 文 件 名: CoreOSG.cpp
- 功能描述: OSG封装类
- 版 本 号: 1.00初版
- 作 者: HYF
- 生成日期; 2021-12-20
- 最近修改:
- 函数列表:
- 修改历史:
- 1.日 期 : 2021-12-20
- 作 者 : HYF
- 修改内容 : 创建文件
- **************************************************************************************************/
- #include "pch.h"
- #include "CoreOSG.h"
- CoreOSG::CoreOSG(HWND hWnd) :
- m_hWnd(hWnd)
- {
- }
- CoreOSG::~CoreOSG()
- {
- // 析构操作
- // m_Viewer->setDone(true);
- // Sleep(1000);
- // m_Viewer->stopThreading();
- //delete m_Viewer;
- }
- BOOL CoreOSG::InitOSG()
- {
- // 初始化OSG的不同部分
- InitManipulators();
- //InitCameraConfig();
- return TRUE;
- }
- BOOL CoreOSG::InitCameraConfig(void)
- {
- // 局部变量存放窗口矩形
- RECT rect;
- // 创建一个viewer
- //m_Viewer = new osgViewer::Viewer();
- // 加入一个statshandler
- m_Viewer->addEventHandler(new osgViewer::StatsHandler);
- // 得到当前窗口矩形
- ::GetWindowRect(m_hWnd, &rect);
- // 初始化图形描述什么东西,反正就是那么回事
- osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
- // 初始化窗口变量,为OSG所用
- osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
- // 设置一些个参数
- traits->x = 0;
- traits->y = 0;
- traits->width = rect.right - rect.left;
- traits->height = rect.bottom - rect.top;
- traits->windowDecoration = false;
- traits->doubleBuffer = true;
- traits->sharedContext = 0;
- traits->setInheritedWindowPixelFormat = true;
- traits->inheritedWindowData = windata;
- // 创建图形上下文
- osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
- // 初始化一个相机
- //osg::ref_ptr<osg::Camera> camera = new osg::Camera;
- osg::ref_ptr<osg::Camera> camera = m_Viewer->getCamera();
- // 绑定
- camera->setGraphicsContext(gc);
- //根据分辨率来确定合适的投影来保证显示的图形不变形
- double fovy, aspectRatio, zNear, zFar;
- camera->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
- double newAspectRatio = double(traits->width) / double(traits->height);
- double aspectRatioChange = newAspectRatio / aspectRatio;
- if (aspectRatioChange != 1.0)
- {
- camera->getProjectionMatrix() *= osg::Matrix::scale(1.0 / aspectRatioChange, 1.0, 1.0);
- }
- // 相机视口设置
- camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
- // set the draw and read buffers up for a double buffered window with rendering going to back buffer
- camera->setDrawBuffer(GL_BACK);
- camera->setReadBuffer(GL_BACK);
- // Set projection matrix and camera attribtues
- camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
- camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f));
- //camera->setClearColor(osg::Vec4f(1.0f, 1.0f, 1.0f, 1.0f));
- camera->setProjectionMatrixAsPerspective(
- 30.0f, static_cast<double>(traits->width) / static_cast<double>(traits->height), 1.0, 1000.0);
- // 添加相机到VIEWER
- //m_Viewer->addSlave(camera.get());
- return TRUE;
- }
- BOOL CoreOSG::ReInitCameraConfig(void)
- {
- // 局部变量存放窗口矩形
- RECT rect;
- // 得到当前窗口矩形
- ::GetWindowRect(m_hWnd, &rect);
- // 初始化一个相机
- //osg::ref_ptr<osg::Camera> camera = new osg::Camera;
- osg::ref_ptr<osg::Camera> camera = m_Viewer->getCamera();
- // 绑定
- //camera->setGraphicsContext(gc);
- //根据分辨率来确定合适的投影来保证显示的图形不变形
- double fovy, aspectRatio, zNear, zFar;
- camera->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
- double newAspectRatio = double(rect.right - rect.left) / double(rect.bottom - rect.top);
- double aspectRatioChange = newAspectRatio / aspectRatio;
- if (aspectRatioChange != 1.0)
- {
- camera->getProjectionMatrix() *= osg::Matrix::scale(1.0 / aspectRatioChange, 1.0, 1.0);
- }
- // 相机视口设置
- camera->setViewport(new osg::Viewport(0, 0, rect.right - rect.left, rect.bottom - rect.top));
- // 添加相机到VIEWER
- //m_Viewer->addSlave(camera.get());
- // 添加操作器到VIEWER
- //m_Viewer->setCameraManipulator(m_KeyswitchManipulator.get());
- //m_Viewer->setSceneData(m_Root.get());
- // 实现VIEWER
- m_Viewer->realize();
- return TRUE;
- }
- void CoreOSG::InitManipulators()
- {
- // Create a trackball manipulator
- osg::ref_ptr<osgGA::TrackballManipulator> trackball = new osgGA::TrackballManipulator();
- // Create a Manipulator Switcher
- m_KeyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
- // Add our trackball manipulator to the switcher
- m_KeyswitchManipulator->addMatrixManipulator('1', "Trackball", trackball.get());
- // Init the switcher to the first manipulator (in this case the only manipulator)
- m_KeyswitchManipulator->selectMatrixManipulator(0); // Zero based index Value
- }
- osg::Camera* CoreOSG::createHudCamera()
- {
- osg::ref_ptr<osg::Camera> hudCamera = new osg::Camera;
- // 局部变量存放窗口矩形
- RECT rect;
- // 得到当前窗口矩形
- ::GetWindowRect(m_hWnd, &rect);
- double width = double(rect.right - rect.left);
- double height = (rect.bottom - rect.top);
- hudCamera->setProjectionMatrixAsOrtho(0, width, 0, height, 1, 100);
- hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
- hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
- osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile("D:/osg/OpenSceneGraph-Data-3.4.0/OpenSceneGraph-Data/axes.osgt");
- //osg::MatrixTransform* pTM = new osg::MatrixTransform;
- osg::ref_ptr<osg::MatrixTransform> pTM = new osg::MatrixTransform;
- pTM->addChild(axes);
- axes->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
- pTM->setMatrix(osg::Matrix::scale(osg::Vec3(width / 22, width / 22, width / 22))*osg::Matrix::translate(osg::Vec3(width / 16, width / 16, 3)));
- pTM->setUpdateCallback(new HudCallback(m_Viewer));
- hudCamera->addChild(pTM);
- //hudCamera->setViewMatrixAsLookAt(osg::Vec3(0,-1,0),osg::Vec3(0, 0, 0),osg::Vec3(0,0,1)); // osg 默认相机位置
- //hudCamera->setViewMatrixAsLookAt(osg::Vec3(0, 0, 1), osg::Vec3(0, 0, 0), osg::Vec3(0, 1, 0)); // opengl 默认相机位置
- return hudCamera.release();
- }
- void cretateBoundingBox(osg::Node * node)
- {
- osg::ComputeBoundsVisitor boundVisitor;
- osg::ref_ptr<osg::MatrixTransform> cowMt = new osg::MatrixTransform(osg::Matrix::translate(0, 0, 0));
- cowMt->addChild(node);
- cowMt->accept(boundVisitor);
- osg::BoundingBox boundingBox = boundVisitor.getBoundingBox();
- float length = boundingBox.xMax() - boundingBox.xMin();
- float width = boundingBox.yMax() - boundingBox.yMin();
- float height = boundingBox.zMax() - boundingBox.zMin();
- TRACE("length:%f", length);
- TRACE("width:%f", width);
- TRACE("height:%f", height);
- }
- BOOL CoreOSG::LoadModel(std::vector<string> &vecPath, std::string axesPath)
- {
- osg::ref_ptr<osg::Node> mdLoadModel;
- // 创建一个viewer
- m_Viewer = new osgViewer::Viewer();
- InitOSG();
- // 初始化组
- m_Root = new osg::Group();
- // 初始化模型
- m_vecEngine.clear();
- for (int i=0; i < vecPath.size(); i++)
- {
- //读取发动机模型
- std::string strdevr = vecPath[i];
- osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strdevr);
- if (node == NULL) {
- LOG::D("read node file error");
- return FALSE;
- }
- //创建矩阵变换节点
- osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform();
- mt->addChild(node.get());
- m_pModelRotateCallback = new ModelRotateCallback();
- mt->setUpdateCallback(m_pModelRotateCallback);
- m_Root->addChild(mt.get());
- // 添加到MatricTransform向量中
- m_vecEngine.push_back(mt);
- }
- // 优化场景数据
- osgUtil::Optimizer optimizer;
- optimizer.optimize(m_Root.get());
- optimizer.reset();
- //绘制坐标轴
- //m_Root->addChild(createHudCamera());
- // 使用hudAxes类绘制的坐标系
- osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile(axesPath);
- osg::Matrix mraxes = osg::Matrixd::rotate(osg::DegreesToRadians(90.0), osg::Vec3(0, 1, 0)) * osg::Matrixd::rotate(osg::DegreesToRadians(90.0), osg::Vec3(0, 0, 1));
- osg::ref_ptr<osg::MatrixTransform> mtaxes = new osg::MatrixTransform();
- mtaxes->addChild(axes.get());
- mtaxes->setMatrix(mraxes);
- osg::ref_ptr<HUDAxis> hudAxes = new HUDAxis;
- hudAxes->addChild(mtaxes.get());
- hudAxes->setMainCamera(m_Viewer->getCamera());
- hudAxes->setRenderOrder(osg::Camera::POST_RENDER);
- hudAxes->setClearMask(GL_DEPTH_BUFFER_BIT);
- hudAxes->setAllowEventFocus(false);
- hudAxes->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- m_Root->addChild(hudAxes);
- //m_Root->addChild(createHudCamera(&viewer));
- InitCameraConfig();
- // 添加操作器到VIEWER
- m_Viewer->setCameraManipulator(m_KeyswitchManipulator.get());
- //m_Viewer->setCameraManipulator(new osgGA::TrackballManipulator());
- m_Viewer->setSceneData(m_Root.get());
- // 实现VIEWER
- m_Viewer->realize();
- //m_Viewer->run();
- /*osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
- viewer->setSceneData(m_Root.get());
- viewer->realize();
- viewer->run();*/
- return TRUE;
- }
- /**************************************************************************************************
- 函 数 名 :SwiftEngine
- 功能描述 :切换测试发动机
- 输入参数 :iMearObj 表示当前要测试的发动机 1表示左发动机 2表示右发动机
- 输出参数 :略
- 返 回 值 :略
- **************************************************************************************************/
- void CoreOSG::VibrateEngine(int iMearObj)
- {
- // 如果大于分机下标,则返回
- if (iMearObj >= m_vecEngine.size()) return;
- // 获取当前分机MatricTransform
- osg::ref_ptr<osg::MatrixTransform> mt = m_vecEngine[iMearObj];
- mt->removeUpdateCallback(m_pModelRotateCallback);
- // 设置更新回调
- m_pModelRotateCallback = new ModelRotateCallback();
- mt->setUpdateCallback(m_pModelRotateCallback);
- m_giCurMearObj = iMearObj;
- return;
- }
- void CoreOSG::setColor(osg::Node* pNode, osg::Vec4 vecColor)
- {
- // 获取节点状态设置
- osg::StateSet* StateSet = pNode->getOrCreateStateSet();
- // 混合颜色
- osg::ref_ptr<osg::BlendColor> rpBlendColor = new osg::BlendColor();
- // 混合功能
- osg::ref_ptr<osg::BlendFunc> rpBlendFunc = new osg::BlendFunc();
- // 添加混合功能属性
- StateSet->setAttributeAndModes(rpBlendFunc.get(), osg::StateAttribute::PROTECTED | osg::StateAttribute::ON);
- // 添加混合颜色属性
- StateSet->setAttributeAndModes(rpBlendColor.get(), osg::StateAttribute::PROTECTED | osg::StateAttribute::ON);
- // 一减常量颜色
- rpBlendFunc->setSource(osg::BlendFunc::ONE_MINUS_CONSTANT_COLOR);
- // 一减常量透明度
- rpBlendFunc->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
- osg::Vec4 vecColorNew = osg::Vec4(1.0, 1.0, 1.0, 1.0) - vecColor;
- vecColorNew.a() = vecColor.a();
- // 设置颜色
- rpBlendColor->setConstantColor(vecColorNew);
- }
- void CoreOSG::PreFrameUpdate()
- {
- // 帧前操作
- }
- void CoreOSG::PostFrameUpdate()
- {
- // 帧后操作
- }
- osg::Vec3d CoreOSG::DegreesToRadians(const osg::Vec3& vecRotate)
- {
- osg::Vec3 vecRotateNew;
- vecRotateNew.x() = osg::DegreesToRadians(vecRotate.x());
- vecRotateNew.y() = osg::DegreesToRadians(vecRotate.y());
- vecRotateNew.z() = osg::DegreesToRadians(vecRotate.z());
- return vecRotateNew;
- }
- osg::Vec3d CoreOSG::RadiansToDegrees(const osg::Vec3& vecRotate)
- {
- osg::Vec3 vecRotateNew;
- vecRotateNew.x() = osg::RadiansToDegrees(vecRotate.x());
- vecRotateNew.y() = osg::RadiansToDegrees(vecRotate.y());
- vecRotateNew.z() = osg::RadiansToDegrees(vecRotate.z());
- return vecRotateNew;
- }
- void CoreOSG::Quat2RadiansRotate(const osg::Quat& quat, osg::Vec3& vecRotate)
- {
- double q0 = quat.w();
- double q1 = quat.x();
- double q2 = quat.y();
- double q3 = quat.z();
- vecRotate.x() = float(atan2(2 * (q2*q3 + q0 * q1), (q0*q0 - q1 * q1 - q2 * q2 + q3 * q3)));
- vecRotate.y() = float(asin(-2 * (q1*q3 - q0 * q2)));
- vecRotate.z() = float(atan2(2 * (q1*q2 + q0 * q3), (q0*q0 + q1 * q1 - q2 * q2 - q3 * q3)));
- }
- void CoreOSG::Quat2DegreesRotate(const osg::Quat& quat, osg::Vec3& vecRotate)
- {
- Quat2RadiansRotate(quat, vecRotate);
- vecRotate = RadiansToDegrees(vecRotate);
- }
- osg::Matrix CoreOSG::RadiansRotate2Matrix(const osg::Vec3d& vecRotate)
- {
- return osg::Matrix::rotate(vecRotate.x(), osg::X_AXIS, vecRotate.y(), osg::Y_AXIS, vecRotate.z(), osg::Z_AXIS);
- }
- osg::Matrix CoreOSG::DegreesRotate2Matrix(const osg::Vec3d& vecRotate)
- {
- return RadiansRotate2Matrix(DegreesToRadians(vecRotate));
- }
- osg::ref_ptr<osg::Node> CoreOSG::CreateLine(osg::Vec3 firstpoit, osg::Vec3 endpoint)
- {
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
- osg::ref_ptr <osg::LineWidth> LineSize = new osg::LineWidth;
- LineSize->setWidth(2.0);
- geom->getOrCreateStateSet()->setAttributeAndModes(LineSize.get(), osg::StateAttribute::ON);
- // 首先定义2个点
- osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
- geom->setVertexArray(v.get());
- //v->push_back(osg::Vec3(-0.15f, 0.f, 0.056f));
- //v->push_back(osg::Vec3(0.2f, 0.f, 0.056f));
- //v->push_back(osg::Vec3(-0.15f, 0.f, 0.1f));
- //v->push_back(osg::Vec3(0.2f, 0.f, 0.1f));
- v->push_back(firstpoit);
- v->push_back(endpoint);
- //v->push_back(osg::Vec3(0.f, 0.f, 0.f));
- //v->push_back(osg::Vec3(1.f, 0.f, 0.f));
- // 定义颜色数组
- osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
- geom->setColorArray(c.get());
- geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
- c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));
- c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));
- //c->push_back(osg::Vec4(0.f, 0.f, 1.f, 1.f));
- //c->push_back(osg::Vec4(1.f, 1.f, 1.f, 1.f));
- // 定义法线
- osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
- geom->setNormalArray(n.get());
- geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- //n->push_back(osg::Vec3(0.f, -1.f, 0.f));
- n->push_back(osg::Vec3(0.f, 0.f, 1.f));
- // 设置顶点关联方式
- geom->addPrimitiveSet(
- new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2));
- // 几何组结点
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable(geom.get());
- return geode.get();
- }
- // 导出新模型
- void CoreOSG::Export(const std::string& strPaths, const std::string& strPathd)
- {
- osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strPaths);
- osg::ref_ptr<osg::MatrixTransform> max = new osg::MatrixTransform;
- max->addChild(node.get());
- max->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(45.0), osg::Vec3(1, 0, 0)));
- osgDB::ReaderWriter::WriteResult result = osgDB::Registry::instance()->writeNode(*max.get(), strPathd, osgDB::Registry::instance()->getOptions());
- if (result.success())
- {
- osg::notify(osg::NOTICE) << "Write Node Success" << std::endl;
- }
- }
- // 导出新模型
- bool CoreOSG::AddLine()
- {
- osg::ref_ptr<osg::MatrixTransform> rpMTRT1 = new osg::MatrixTransform;
- rpMTRT1->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(45.0), osg::X_AXIS));
- // 画线
- rpMTRT1->addChild(CreateLine(osg::Vec3(-0.15f, 0.f, 0.056f), osg::Vec3(0.2f, 0.f, 0.056f)).get());
- m_Root->addChild(rpMTRT1.get());
- return true;
- }
- BOOL CoreOSG::Release()
- {
- //m_Viewer->setCamera(NULL);
- //
- m_Viewer->frame();
- m_Viewer->frame();
- Sleep(10);
- m_Viewer->setCamera(NULL);
- m_Viewer->setSceneData(NULL);
- m_Viewer->setDone(true);
- m_Viewer->stopThreading();
- return TRUE;
- }
- BOOL CoreOSG::ReleaseViewer()
- {
- //m_Viewer->setDone(true);
- //m_Viewer->stopThreading();
- //delete m_Viewer;
- //m_Viewer = nullptr;
- //m_Root = nullptr;
- //m_KeyswitchManipulator = nullptr;
- return TRUE;
- }
- void CoreOSG::RotateCurModel(const double fx, const double fy, const double fz)
- {
- if ( NULL == m_Root )
- {
- return;
- }
- VertexVisitor vtea;
- vtea.setRotatexyz(fx, fy, fz);
- m_Root->accept(vtea);
- }
- // 渲染线程
- RenderingThread::RenderingThread(CoreOSG* ptr)
- : OpenThreads::Thread(), _ptr(ptr), _done(false)
- {
- }
- RenderingThread::~RenderingThread()
- {
- _done = true;
- if (isRunning())
- {
- cancel();
- join();
- }
- }
- void RenderingThread::run()
- {
- if (!_ptr)
- {
- _done = true;
- return;
- }
- osg::ref_ptr<osgViewer::Viewer> viewer = _ptr->getViewer();
- //viewer->run();
- #if 1
- do
- {
- _ptr->PreFrameUpdate();
- viewer->frame();
- _ptr->PostFrameUpdate();
- } while (!testCancel() && !viewer->done() && !_done);
- #endif
- return;
- }
- HudCallback::HudCallback(osgViewer::Viewer* viewer) :m_xyzviewer(viewer)
- {
- m_xyzviewer = viewer;
- }
- HudCallback:: ~HudCallback()
- {
- }
- void HudCallback:: operator()(osg::Node* node, osg::NodeVisitor* nv)
- {
- //osg::MatrixTransform* pTM = dynamic_cast<osg::MatrixTransform*>(node);
- osg::ref_ptr<osg::MatrixTransform> pTM = dynamic_cast<osg::MatrixTransform*>(node);
- if (pTM)
- {
- //osg::Camera* camera = m_xyzviewer->getCamera();
- osg::ref_ptr<osg::Camera> camera = m_xyzviewer->getCamera();
- osg::Vec3 translate = pTM->getMatrix().getTrans();
- osg::Vec3 scale = pTM->getMatrix().getScale();
- osg::Matrix mv = camera->getViewMatrix();
- osg::Matrix inv_mv = camera->getInverseViewMatrix(); // 将视图矩阵转换为正常的变换矩阵
- osg::Quat inv_q = inv_mv.getRotate();
- osg::Quat q = mv.getRotate();
- // 模型当前所处的旋转坐标系,和相机所处的坐标系存在绕x轴逆时顺时针转90度
- osg::Quat dq(osg::DegreesToRadians(90.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
- // pTM->setMatrix(osg::Matrix::scale(scale)* osg::Matrix::rotate( dq * q ) * osg::Matrix::translate(translate));
- static osg::Matrix mm = osg::Matrix::rotate(dq);
- mv.setTrans(translate);
- pTM->setMatrix(osg::Matrix::scale(scale) * mv /** mm*/);
- }
- }
- HUDAxis::HUDAxis()
- {
- // 可以在这直接读取axes.osgt;
- // this->addChild(osgDB::readNodeFile("axes.osgt"));
- }
- void HUDAxis::traverse(osg::NodeVisitor& nv)
- {
- double fovy, aspectRatio, vNear, vFar;
- _mainCamera->getProjectionMatrixAsPerspective(fovy, aspectRatio, vNear, vFar);
- //this->setProjectionMatrixAsOrtho(-10.0*aspectRatio, 10.0*aspectRatio, -10.0, 10.0, 2.0, -2.0); //设置投影矩阵,使缩放不起效果
- this->setProjectionMatrixAsOrtho2D(-10.0*aspectRatio, -1.0*aspectRatio, -10.0, -1.0);//可以调整坐标大小
- osg::Vec3 trans(-9.3*aspectRatio, -8.5, -8.0);//第一个参数调整左右位置,正值表示在右边,负值表示在左边
- if (_mainCamera.valid() && nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR)
- {
- osg::Matrix matrix = _mainCamera->getViewMatrix();//改变视图矩阵,让移动位置固定
- matrix.setTrans(trans);
- this->setViewMatrix(matrix);
- }//if
- osg::Camera::traverse(nv);
- }
- //构造函数,初始化一下并设置为遍历所有子节点
- VertexVisitor::VertexVisitor() :osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
- {
- //m_extractedverts = new osg::Vec3Array();
- m_fAxisX = 0.0f;
- m_fAxisY = 0.0f;
- m_fAxisZ = 0.0f;
- }
- void VertexVisitor::setThrustlineEndPoit(osg::Vec3 vThrustlineEndPoit)
- {
- m_vThrustlineEndPoit = vThrustlineEndPoit;
- }
- void VertexVisitor::setFindThrustlineName(const std::string strThrustlineName)
- {
- m_strThrustlineName = strThrustlineName;
- }
- //设置的是角度,用的时候需要转成弧度
- void VertexVisitor::setRotatexyz(const double fx, const double fy, const double fz)
- {
- m_fAxisX = fx;
- m_fAxisY = fy;
- m_fAxisZ = fz;
- }
- void VertexVisitor::apply(osg::MatrixTransform& geode)
- {
- string strname = geode.getName();
- TRACE("MatrixTransform名称:%s:\n", strname);
- //旋转
- osg::Matrix mr = osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisX), osg::Vec3(0.1f, 0.0f, 0.0f))) *
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisY), osg::Vec3(0.0f, 0.1f, 0.0f))) *
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisZ), osg::Vec3(0.0f, 0.0f, 0.1f)));
- //mr.makeRotate(m_fAxisX, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
- //mr.makeRotate(m_fAxisY, osg::Vec3(0.0f, 0.1f, 0.0f));//沿x轴旋转
- //mr.makeRotate(m_fAxisZ, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
-
- //设置矩阵
- geode.setMatrix(mr);
- traverse(geode);
- }
- void VertexVisitor::apply(osg::Node& node)
- {
- string strname = node.getName();
- TRACE("节点名称:%s:\n", strname);
- //osg::Node* tmpnode = &node;
- osg::ref_ptr<osg::MatrixTransform> mtCow = dynamic_cast<osg::MatrixTransform*>(&node);
- if (NULL == mtCow)
- {
- traverse(node);
- return;
- }
-
- traverse(node);
- }
- void VertexVisitor:: apply(osg::Geode& genode)
- {
- string strname = genode.getName();
-
- TRACE("Geode名称:%s:\n", strname);
- traverse(genode);
- }
- void VertexVisitor::apply(osg::Transform &genode1)
- {
- string strname = genode1.getName();
- TRACE("Geode名称:%s:\n", strname);
- osg::Transform* tem = genode1.asTransform();
- TRACE("Geode名称:%s:\n", strname);
- osg::ref_ptr<osg::MatrixTransform> mtCow = dynamic_cast<osg::MatrixTransform*>(tem);
- if (NULL == mtCow)
- {
- traverse(genode1);
- return;
- }
-
- //旋转
- osg::Matrix mr = osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisX), osg::Vec3(0.1f, 0.0f, 0.0f))) *
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisY), osg::Vec3(0.0f, 0.1f, 0.0f))) *
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisZ), osg::Vec3(0.0f, 0.0f, 0.1f)));
- //mr.makeRotate(m_fAxisX, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
- //mr.makeRotate(m_fAxisY, osg::Vec3(0.0f, 0.1f, 0.0f));//沿x轴旋转
- //mr.makeRotate(m_fAxisZ, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
- //设置矩阵
- //geode1.setMatrix(mr);
-
-
- traverse(genode1);
- }
- ModelRotateCallback::ModelRotateCallback()
- {
- m_fAxisX = 0.0f;
- m_fAxisY = 0.0f;
- m_fAxisZ = 0.0f;
- }
- void ModelRotateCallback::setRotateAnglexyz(const double fx, const double fy, const double fz)
- {
- m_fAxisX = fx;
- m_fAxisY = fy;
- m_fAxisZ = fz;
- }
- void ModelRotateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
- {
- //创建矩阵变换节点
- osg::ref_ptr<osg::MatrixTransform> rotatennode = dynamic_cast<osg::MatrixTransform*>(node);
- //旋转
- osg::Matrix mr = osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(90.0), osg::Vec3(0.0f, 0.1f, 0.0f))) *
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(90.0), osg::Vec3(0.0f, 0.0f, 0.1f)))*
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisX), osg::Vec3(0.1f, 0.0f, 0.0f))) *
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisY), osg::Vec3(0.0f, 0.1f, 0.0f))) *
- osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisZ), osg::Vec3(0.0f, 0.0f, 0.1f)));
- //mr.makeRotate(m_fAxisX, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
- //mr.makeRotate(m_fAxisY, osg::Vec3(0.0f, 0.1f, 0.0f));//沿x轴旋转
- //mr.makeRotate(m_fAxisZ, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
- //设置矩阵
- rotatennode->setMatrix(mr);
- //m_fAxisX *= -1.0f;
- //m_fAxisY *= -1.0f;
- //m_fAxisZ *= -1.0f;
- //Sleep(100);
- //继续传递参数,执行其他设置回调的节点
- //traverse(node, nv);
- }
|