CoreOSG.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /**************************************************************************************************
  2. 版本所有(C), 2020-2022, 北京普达迪泰
  3. ***************************************************************************************************
  4. 文 件 名: CoreOSG.cpp
  5. 功能描述: OSG封装类
  6. 版 本 号: 1.00初版
  7. 作 者: HYF
  8. 生成日期; 2021-12-20
  9. 最近修改:
  10. 函数列表:
  11. 修改历史:
  12. 1.日 期 : 2021-12-20
  13. 作 者 : HYF
  14. 修改内容 : 创建文件
  15. **************************************************************************************************/
  16. #include "pch.h"
  17. #include "CoreOSG.h"
  18. CoreOSG::CoreOSG(HWND hWnd) :
  19. m_hWnd(hWnd)
  20. {
  21. }
  22. CoreOSG::~CoreOSG()
  23. {
  24. // 析构操作
  25. // m_Viewer->setDone(true);
  26. // Sleep(1000);
  27. // m_Viewer->stopThreading();
  28. //delete m_Viewer;
  29. }
  30. BOOL CoreOSG::InitOSG()
  31. {
  32. // 初始化OSG的不同部分
  33. InitManipulators();
  34. //InitCameraConfig();
  35. return TRUE;
  36. }
  37. BOOL CoreOSG::InitCameraConfig(void)
  38. {
  39. // 局部变量存放窗口矩形
  40. RECT rect;
  41. // 创建一个viewer
  42. //m_Viewer = new osgViewer::Viewer();
  43. // 加入一个statshandler
  44. m_Viewer->addEventHandler(new osgViewer::StatsHandler);
  45. // 得到当前窗口矩形
  46. ::GetWindowRect(m_hWnd, &rect);
  47. // 初始化图形描述什么东西,反正就是那么回事
  48. osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
  49. // 初始化窗口变量,为OSG所用
  50. osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
  51. // 设置一些个参数
  52. traits->x = 0;
  53. traits->y = 0;
  54. traits->width = rect.right - rect.left;
  55. traits->height = rect.bottom - rect.top;
  56. traits->windowDecoration = false;
  57. traits->doubleBuffer = true;
  58. traits->sharedContext = 0;
  59. traits->setInheritedWindowPixelFormat = true;
  60. traits->inheritedWindowData = windata;
  61. // 创建图形上下文
  62. osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
  63. // 初始化一个相机
  64. //osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  65. osg::ref_ptr<osg::Camera> camera = m_Viewer->getCamera();
  66. // 绑定
  67. camera->setGraphicsContext(gc);
  68. //根据分辨率来确定合适的投影来保证显示的图形不变形
  69. double fovy, aspectRatio, zNear, zFar;
  70. camera->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
  71. double newAspectRatio = double(traits->width) / double(traits->height);
  72. double aspectRatioChange = newAspectRatio / aspectRatio;
  73. if (aspectRatioChange != 1.0)
  74. {
  75. camera->getProjectionMatrix() *= osg::Matrix::scale(1.0 / aspectRatioChange, 1.0, 1.0);
  76. }
  77. // 相机视口设置
  78. camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
  79. // set the draw and read buffers up for a double buffered window with rendering going to back buffer
  80. camera->setDrawBuffer(GL_BACK);
  81. camera->setReadBuffer(GL_BACK);
  82. // Set projection matrix and camera attribtues
  83. camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  84. camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f));
  85. //camera->setClearColor(osg::Vec4f(1.0f, 1.0f, 1.0f, 1.0f));
  86. camera->setProjectionMatrixAsPerspective(
  87. 30.0f, static_cast<double>(traits->width) / static_cast<double>(traits->height), 1.0, 1000.0);
  88. // 添加相机到VIEWER
  89. //m_Viewer->addSlave(camera.get());
  90. return TRUE;
  91. }
  92. BOOL CoreOSG::ReInitCameraConfig(void)
  93. {
  94. // 局部变量存放窗口矩形
  95. RECT rect;
  96. // 得到当前窗口矩形
  97. ::GetWindowRect(m_hWnd, &rect);
  98. // 初始化一个相机
  99. //osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  100. osg::ref_ptr<osg::Camera> camera = m_Viewer->getCamera();
  101. // 绑定
  102. //camera->setGraphicsContext(gc);
  103. //根据分辨率来确定合适的投影来保证显示的图形不变形
  104. double fovy, aspectRatio, zNear, zFar;
  105. camera->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
  106. double newAspectRatio = double(rect.right - rect.left) / double(rect.bottom - rect.top);
  107. double aspectRatioChange = newAspectRatio / aspectRatio;
  108. if (aspectRatioChange != 1.0)
  109. {
  110. camera->getProjectionMatrix() *= osg::Matrix::scale(1.0 / aspectRatioChange, 1.0, 1.0);
  111. }
  112. // 相机视口设置
  113. camera->setViewport(new osg::Viewport(0, 0, rect.right - rect.left, rect.bottom - rect.top));
  114. // 添加相机到VIEWER
  115. //m_Viewer->addSlave(camera.get());
  116. // 添加操作器到VIEWER
  117. //m_Viewer->setCameraManipulator(m_KeyswitchManipulator.get());
  118. //m_Viewer->setSceneData(m_Root.get());
  119. // 实现VIEWER
  120. m_Viewer->realize();
  121. return TRUE;
  122. }
  123. void CoreOSG::InitManipulators()
  124. {
  125. // Create a trackball manipulator
  126. osg::ref_ptr<osgGA::TrackballManipulator> trackball = new osgGA::TrackballManipulator();
  127. // Create a Manipulator Switcher
  128. m_KeyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
  129. // Add our trackball manipulator to the switcher
  130. m_KeyswitchManipulator->addMatrixManipulator('1', "Trackball", trackball.get());
  131. // Init the switcher to the first manipulator (in this case the only manipulator)
  132. m_KeyswitchManipulator->selectMatrixManipulator(0); // Zero based index Value
  133. }
  134. osg::Camera* CoreOSG::createHudCamera()
  135. {
  136. osg::ref_ptr<osg::Camera> hudCamera = new osg::Camera;
  137. // 局部变量存放窗口矩形
  138. RECT rect;
  139. // 得到当前窗口矩形
  140. ::GetWindowRect(m_hWnd, &rect);
  141. double width = double(rect.right - rect.left);
  142. double height = (rect.bottom - rect.top);
  143. hudCamera->setProjectionMatrixAsOrtho(0, width, 0, height, 1, 100);
  144. hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
  145. hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  146. hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
  147. osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile("D:/osg/OpenSceneGraph-Data-3.4.0/OpenSceneGraph-Data/axes.osgt");
  148. //osg::MatrixTransform* pTM = new osg::MatrixTransform;
  149. osg::ref_ptr<osg::MatrixTransform> pTM = new osg::MatrixTransform;
  150. pTM->addChild(axes);
  151. axes->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
  152. pTM->setMatrix(osg::Matrix::scale(osg::Vec3(width / 22, width / 22, width / 22))*osg::Matrix::translate(osg::Vec3(width / 16, width / 16, 3)));
  153. pTM->setUpdateCallback(new HudCallback(m_Viewer));
  154. hudCamera->addChild(pTM);
  155. //hudCamera->setViewMatrixAsLookAt(osg::Vec3(0,-1,0),osg::Vec3(0, 0, 0),osg::Vec3(0,0,1)); // osg 默认相机位置
  156. //hudCamera->setViewMatrixAsLookAt(osg::Vec3(0, 0, 1), osg::Vec3(0, 0, 0), osg::Vec3(0, 1, 0)); // opengl 默认相机位置
  157. return hudCamera.release();
  158. }
  159. void cretateBoundingBox(osg::Node * node)
  160. {
  161. osg::ComputeBoundsVisitor boundVisitor;
  162. osg::ref_ptr<osg::MatrixTransform> cowMt = new osg::MatrixTransform(osg::Matrix::translate(0, 0, 0));
  163. cowMt->addChild(node);
  164. cowMt->accept(boundVisitor);
  165. osg::BoundingBox boundingBox = boundVisitor.getBoundingBox();
  166. float length = boundingBox.xMax() - boundingBox.xMin();
  167. float width = boundingBox.yMax() - boundingBox.yMin();
  168. float height = boundingBox.zMax() - boundingBox.zMin();
  169. TRACE("length:%f", length);
  170. TRACE("width:%f", width);
  171. TRACE("height:%f", height);
  172. }
  173. BOOL CoreOSG::LoadModel(std::vector<string> &vecPath, std::string axesPath)
  174. {
  175. osg::ref_ptr<osg::Node> mdLoadModel;
  176. // 创建一个viewer
  177. m_Viewer = new osgViewer::Viewer();
  178. InitOSG();
  179. // 初始化组
  180. m_Root = new osg::Group();
  181. // 初始化模型
  182. m_vecEngine.clear();
  183. for (int i=0; i < vecPath.size(); i++)
  184. {
  185. //读取发动机模型
  186. std::string strdevr = vecPath[i];
  187. osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strdevr);
  188. if (node == NULL) {
  189. LOG::D("read node file error");
  190. return FALSE;
  191. }
  192. //创建矩阵变换节点
  193. osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform();
  194. mt->addChild(node.get());
  195. m_pModelRotateCallback = new ModelRotateCallback();
  196. mt->setUpdateCallback(m_pModelRotateCallback);
  197. m_Root->addChild(mt.get());
  198. // 添加到MatricTransform向量中
  199. m_vecEngine.push_back(mt);
  200. }
  201. // 优化场景数据
  202. osgUtil::Optimizer optimizer;
  203. optimizer.optimize(m_Root.get());
  204. optimizer.reset();
  205. //绘制坐标轴
  206. //m_Root->addChild(createHudCamera());
  207. // 使用hudAxes类绘制的坐标系
  208. osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile(axesPath);
  209. 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));
  210. osg::ref_ptr<osg::MatrixTransform> mtaxes = new osg::MatrixTransform();
  211. mtaxes->addChild(axes.get());
  212. mtaxes->setMatrix(mraxes);
  213. osg::ref_ptr<HUDAxis> hudAxes = new HUDAxis;
  214. hudAxes->addChild(mtaxes.get());
  215. hudAxes->setMainCamera(m_Viewer->getCamera());
  216. hudAxes->setRenderOrder(osg::Camera::POST_RENDER);
  217. hudAxes->setClearMask(GL_DEPTH_BUFFER_BIT);
  218. hudAxes->setAllowEventFocus(false);
  219. hudAxes->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  220. m_Root->addChild(hudAxes);
  221. //m_Root->addChild(createHudCamera(&viewer));
  222. InitCameraConfig();
  223. // 添加操作器到VIEWER
  224. m_Viewer->setCameraManipulator(m_KeyswitchManipulator.get());
  225. //m_Viewer->setCameraManipulator(new osgGA::TrackballManipulator());
  226. m_Viewer->setSceneData(m_Root.get());
  227. // 实现VIEWER
  228. m_Viewer->realize();
  229. //m_Viewer->run();
  230. /*osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
  231. viewer->setSceneData(m_Root.get());
  232. viewer->realize();
  233. viewer->run();*/
  234. return TRUE;
  235. }
  236. /**************************************************************************************************
  237. 函 数 名 :SwiftEngine
  238. 功能描述 :切换测试发动机
  239. 输入参数 :iMearObj 表示当前要测试的发动机 1表示左发动机 2表示右发动机
  240. 输出参数 :略
  241. 返 回 值 :略
  242. **************************************************************************************************/
  243. void CoreOSG::VibrateEngine(int iMearObj)
  244. {
  245. // 如果大于分机下标,则返回
  246. if (iMearObj >= m_vecEngine.size()) return;
  247. // 获取当前分机MatricTransform
  248. osg::ref_ptr<osg::MatrixTransform> mt = m_vecEngine[iMearObj];
  249. mt->removeUpdateCallback(m_pModelRotateCallback);
  250. // 设置更新回调
  251. m_pModelRotateCallback = new ModelRotateCallback();
  252. mt->setUpdateCallback(m_pModelRotateCallback);
  253. m_giCurMearObj = iMearObj;
  254. return;
  255. }
  256. void CoreOSG::setColor(osg::Node* pNode, osg::Vec4 vecColor)
  257. {
  258. // 获取节点状态设置
  259. osg::StateSet* StateSet = pNode->getOrCreateStateSet();
  260. // 混合颜色
  261. osg::ref_ptr<osg::BlendColor> rpBlendColor = new osg::BlendColor();
  262. // 混合功能
  263. osg::ref_ptr<osg::BlendFunc> rpBlendFunc = new osg::BlendFunc();
  264. // 添加混合功能属性
  265. StateSet->setAttributeAndModes(rpBlendFunc.get(), osg::StateAttribute::PROTECTED | osg::StateAttribute::ON);
  266. // 添加混合颜色属性
  267. StateSet->setAttributeAndModes(rpBlendColor.get(), osg::StateAttribute::PROTECTED | osg::StateAttribute::ON);
  268. // 一减常量颜色
  269. rpBlendFunc->setSource(osg::BlendFunc::ONE_MINUS_CONSTANT_COLOR);
  270. // 一减常量透明度
  271. rpBlendFunc->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
  272. osg::Vec4 vecColorNew = osg::Vec4(1.0, 1.0, 1.0, 1.0) - vecColor;
  273. vecColorNew.a() = vecColor.a();
  274. // 设置颜色
  275. rpBlendColor->setConstantColor(vecColorNew);
  276. }
  277. void CoreOSG::PreFrameUpdate()
  278. {
  279. // 帧前操作
  280. }
  281. void CoreOSG::PostFrameUpdate()
  282. {
  283. // 帧后操作
  284. }
  285. osg::Vec3d CoreOSG::DegreesToRadians(const osg::Vec3& vecRotate)
  286. {
  287. osg::Vec3 vecRotateNew;
  288. vecRotateNew.x() = osg::DegreesToRadians(vecRotate.x());
  289. vecRotateNew.y() = osg::DegreesToRadians(vecRotate.y());
  290. vecRotateNew.z() = osg::DegreesToRadians(vecRotate.z());
  291. return vecRotateNew;
  292. }
  293. osg::Vec3d CoreOSG::RadiansToDegrees(const osg::Vec3& vecRotate)
  294. {
  295. osg::Vec3 vecRotateNew;
  296. vecRotateNew.x() = osg::RadiansToDegrees(vecRotate.x());
  297. vecRotateNew.y() = osg::RadiansToDegrees(vecRotate.y());
  298. vecRotateNew.z() = osg::RadiansToDegrees(vecRotate.z());
  299. return vecRotateNew;
  300. }
  301. void CoreOSG::Quat2RadiansRotate(const osg::Quat& quat, osg::Vec3& vecRotate)
  302. {
  303. double q0 = quat.w();
  304. double q1 = quat.x();
  305. double q2 = quat.y();
  306. double q3 = quat.z();
  307. vecRotate.x() = float(atan2(2 * (q2*q3 + q0 * q1), (q0*q0 - q1 * q1 - q2 * q2 + q3 * q3)));
  308. vecRotate.y() = float(asin(-2 * (q1*q3 - q0 * q2)));
  309. vecRotate.z() = float(atan2(2 * (q1*q2 + q0 * q3), (q0*q0 + q1 * q1 - q2 * q2 - q3 * q3)));
  310. }
  311. void CoreOSG::Quat2DegreesRotate(const osg::Quat& quat, osg::Vec3& vecRotate)
  312. {
  313. Quat2RadiansRotate(quat, vecRotate);
  314. vecRotate = RadiansToDegrees(vecRotate);
  315. }
  316. osg::Matrix CoreOSG::RadiansRotate2Matrix(const osg::Vec3d& vecRotate)
  317. {
  318. return osg::Matrix::rotate(vecRotate.x(), osg::X_AXIS, vecRotate.y(), osg::Y_AXIS, vecRotate.z(), osg::Z_AXIS);
  319. }
  320. osg::Matrix CoreOSG::DegreesRotate2Matrix(const osg::Vec3d& vecRotate)
  321. {
  322. return RadiansRotate2Matrix(DegreesToRadians(vecRotate));
  323. }
  324. osg::ref_ptr<osg::Node> CoreOSG::CreateLine(osg::Vec3 firstpoit, osg::Vec3 endpoint)
  325. {
  326. osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
  327. osg::ref_ptr <osg::LineWidth> LineSize = new osg::LineWidth;
  328. LineSize->setWidth(2.0);
  329. geom->getOrCreateStateSet()->setAttributeAndModes(LineSize.get(), osg::StateAttribute::ON);
  330. // 首先定义2个点
  331. osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
  332. geom->setVertexArray(v.get());
  333. //v->push_back(osg::Vec3(-0.15f, 0.f, 0.056f));
  334. //v->push_back(osg::Vec3(0.2f, 0.f, 0.056f));
  335. //v->push_back(osg::Vec3(-0.15f, 0.f, 0.1f));
  336. //v->push_back(osg::Vec3(0.2f, 0.f, 0.1f));
  337. v->push_back(firstpoit);
  338. v->push_back(endpoint);
  339. //v->push_back(osg::Vec3(0.f, 0.f, 0.f));
  340. //v->push_back(osg::Vec3(1.f, 0.f, 0.f));
  341. // 定义颜色数组
  342. osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
  343. geom->setColorArray(c.get());
  344. geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
  345. c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));
  346. c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));
  347. //c->push_back(osg::Vec4(0.f, 0.f, 1.f, 1.f));
  348. //c->push_back(osg::Vec4(1.f, 1.f, 1.f, 1.f));
  349. // 定义法线
  350. osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
  351. geom->setNormalArray(n.get());
  352. geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  353. //n->push_back(osg::Vec3(0.f, -1.f, 0.f));
  354. n->push_back(osg::Vec3(0.f, 0.f, 1.f));
  355. // 设置顶点关联方式
  356. geom->addPrimitiveSet(
  357. new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2));
  358. // 几何组结点
  359. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  360. geode->addDrawable(geom.get());
  361. return geode.get();
  362. }
  363. // 导出新模型
  364. void CoreOSG::Export(const std::string& strPaths, const std::string& strPathd)
  365. {
  366. osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strPaths);
  367. osg::ref_ptr<osg::MatrixTransform> max = new osg::MatrixTransform;
  368. max->addChild(node.get());
  369. max->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(45.0), osg::Vec3(1, 0, 0)));
  370. osgDB::ReaderWriter::WriteResult result = osgDB::Registry::instance()->writeNode(*max.get(), strPathd, osgDB::Registry::instance()->getOptions());
  371. if (result.success())
  372. {
  373. osg::notify(osg::NOTICE) << "Write Node Success" << std::endl;
  374. }
  375. }
  376. // 导出新模型
  377. bool CoreOSG::AddLine()
  378. {
  379. osg::ref_ptr<osg::MatrixTransform> rpMTRT1 = new osg::MatrixTransform;
  380. rpMTRT1->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(45.0), osg::X_AXIS));
  381. // 画线
  382. rpMTRT1->addChild(CreateLine(osg::Vec3(-0.15f, 0.f, 0.056f), osg::Vec3(0.2f, 0.f, 0.056f)).get());
  383. m_Root->addChild(rpMTRT1.get());
  384. return true;
  385. }
  386. BOOL CoreOSG::Release()
  387. {
  388. //m_Viewer->setCamera(NULL);
  389. //
  390. m_Viewer->frame();
  391. m_Viewer->frame();
  392. Sleep(10);
  393. m_Viewer->setCamera(NULL);
  394. m_Viewer->setSceneData(NULL);
  395. m_Viewer->setDone(true);
  396. m_Viewer->stopThreading();
  397. return TRUE;
  398. }
  399. BOOL CoreOSG::ReleaseViewer()
  400. {
  401. //m_Viewer->setDone(true);
  402. //m_Viewer->stopThreading();
  403. //delete m_Viewer;
  404. //m_Viewer = nullptr;
  405. //m_Root = nullptr;
  406. //m_KeyswitchManipulator = nullptr;
  407. return TRUE;
  408. }
  409. void CoreOSG::RotateCurModel(const double fx, const double fy, const double fz)
  410. {
  411. if ( NULL == m_Root )
  412. {
  413. return;
  414. }
  415. VertexVisitor vtea;
  416. vtea.setRotatexyz(fx, fy, fz);
  417. m_Root->accept(vtea);
  418. }
  419. // 渲染线程
  420. RenderingThread::RenderingThread(CoreOSG* ptr)
  421. : OpenThreads::Thread(), _ptr(ptr), _done(false)
  422. {
  423. }
  424. RenderingThread::~RenderingThread()
  425. {
  426. _done = true;
  427. if (isRunning())
  428. {
  429. cancel();
  430. join();
  431. }
  432. }
  433. void RenderingThread::run()
  434. {
  435. if (!_ptr)
  436. {
  437. _done = true;
  438. return;
  439. }
  440. osg::ref_ptr<osgViewer::Viewer> viewer = _ptr->getViewer();
  441. //viewer->run();
  442. #if 1
  443. do
  444. {
  445. _ptr->PreFrameUpdate();
  446. viewer->frame();
  447. _ptr->PostFrameUpdate();
  448. } while (!testCancel() && !viewer->done() && !_done);
  449. #endif
  450. return;
  451. }
  452. HudCallback::HudCallback(osgViewer::Viewer* viewer) :m_xyzviewer(viewer)
  453. {
  454. m_xyzviewer = viewer;
  455. }
  456. HudCallback:: ~HudCallback()
  457. {
  458. }
  459. void HudCallback:: operator()(osg::Node* node, osg::NodeVisitor* nv)
  460. {
  461. //osg::MatrixTransform* pTM = dynamic_cast<osg::MatrixTransform*>(node);
  462. osg::ref_ptr<osg::MatrixTransform> pTM = dynamic_cast<osg::MatrixTransform*>(node);
  463. if (pTM)
  464. {
  465. //osg::Camera* camera = m_xyzviewer->getCamera();
  466. osg::ref_ptr<osg::Camera> camera = m_xyzviewer->getCamera();
  467. osg::Vec3 translate = pTM->getMatrix().getTrans();
  468. osg::Vec3 scale = pTM->getMatrix().getScale();
  469. osg::Matrix mv = camera->getViewMatrix();
  470. osg::Matrix inv_mv = camera->getInverseViewMatrix(); // 将视图矩阵转换为正常的变换矩阵
  471. osg::Quat inv_q = inv_mv.getRotate();
  472. osg::Quat q = mv.getRotate();
  473. // 模型当前所处的旋转坐标系,和相机所处的坐标系存在绕x轴逆时顺时针转90度
  474. osg::Quat dq(osg::DegreesToRadians(90.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
  475. // pTM->setMatrix(osg::Matrix::scale(scale)* osg::Matrix::rotate( dq * q ) * osg::Matrix::translate(translate));
  476. static osg::Matrix mm = osg::Matrix::rotate(dq);
  477. mv.setTrans(translate);
  478. pTM->setMatrix(osg::Matrix::scale(scale) * mv /** mm*/);
  479. }
  480. }
  481. HUDAxis::HUDAxis()
  482. {
  483. // 可以在这直接读取axes.osgt;
  484. // this->addChild(osgDB::readNodeFile("axes.osgt"));
  485. }
  486. void HUDAxis::traverse(osg::NodeVisitor& nv)
  487. {
  488. double fovy, aspectRatio, vNear, vFar;
  489. _mainCamera->getProjectionMatrixAsPerspective(fovy, aspectRatio, vNear, vFar);
  490. //this->setProjectionMatrixAsOrtho(-10.0*aspectRatio, 10.0*aspectRatio, -10.0, 10.0, 2.0, -2.0); //设置投影矩阵,使缩放不起效果
  491. this->setProjectionMatrixAsOrtho2D(-10.0*aspectRatio, -1.0*aspectRatio, -10.0, -1.0);//可以调整坐标大小
  492. osg::Vec3 trans(-9.3*aspectRatio, -8.5, -8.0);//第一个参数调整左右位置,正值表示在右边,负值表示在左边
  493. if (_mainCamera.valid() && nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR)
  494. {
  495. osg::Matrix matrix = _mainCamera->getViewMatrix();//改变视图矩阵,让移动位置固定
  496. matrix.setTrans(trans);
  497. this->setViewMatrix(matrix);
  498. }//if
  499. osg::Camera::traverse(nv);
  500. }
  501. //构造函数,初始化一下并设置为遍历所有子节点
  502. VertexVisitor::VertexVisitor() :osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
  503. {
  504. //m_extractedverts = new osg::Vec3Array();
  505. m_fAxisX = 0.0f;
  506. m_fAxisY = 0.0f;
  507. m_fAxisZ = 0.0f;
  508. }
  509. void VertexVisitor::setThrustlineEndPoit(osg::Vec3 vThrustlineEndPoit)
  510. {
  511. m_vThrustlineEndPoit = vThrustlineEndPoit;
  512. }
  513. void VertexVisitor::setFindThrustlineName(const std::string strThrustlineName)
  514. {
  515. m_strThrustlineName = strThrustlineName;
  516. }
  517. //设置的是角度,用的时候需要转成弧度
  518. void VertexVisitor::setRotatexyz(const double fx, const double fy, const double fz)
  519. {
  520. m_fAxisX = fx;
  521. m_fAxisY = fy;
  522. m_fAxisZ = fz;
  523. }
  524. void VertexVisitor::apply(osg::MatrixTransform& geode)
  525. {
  526. string strname = geode.getName();
  527. TRACE("MatrixTransform名称:%s:\n", strname);
  528. //旋转
  529. osg::Matrix mr = osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisX), osg::Vec3(0.1f, 0.0f, 0.0f))) *
  530. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisY), osg::Vec3(0.0f, 0.1f, 0.0f))) *
  531. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisZ), osg::Vec3(0.0f, 0.0f, 0.1f)));
  532. //mr.makeRotate(m_fAxisX, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
  533. //mr.makeRotate(m_fAxisY, osg::Vec3(0.0f, 0.1f, 0.0f));//沿x轴旋转
  534. //mr.makeRotate(m_fAxisZ, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
  535. //设置矩阵
  536. geode.setMatrix(mr);
  537. traverse(geode);
  538. }
  539. void VertexVisitor::apply(osg::Node& node)
  540. {
  541. string strname = node.getName();
  542. TRACE("节点名称:%s:\n", strname);
  543. //osg::Node* tmpnode = &node;
  544. osg::ref_ptr<osg::MatrixTransform> mtCow = dynamic_cast<osg::MatrixTransform*>(&node);
  545. if (NULL == mtCow)
  546. {
  547. traverse(node);
  548. return;
  549. }
  550. traverse(node);
  551. }
  552. void VertexVisitor:: apply(osg::Geode& genode)
  553. {
  554. string strname = genode.getName();
  555. TRACE("Geode名称:%s:\n", strname);
  556. traverse(genode);
  557. }
  558. void VertexVisitor::apply(osg::Transform &genode1)
  559. {
  560. string strname = genode1.getName();
  561. TRACE("Geode名称:%s:\n", strname);
  562. osg::Transform* tem = genode1.asTransform();
  563. TRACE("Geode名称:%s:\n", strname);
  564. osg::ref_ptr<osg::MatrixTransform> mtCow = dynamic_cast<osg::MatrixTransform*>(tem);
  565. if (NULL == mtCow)
  566. {
  567. traverse(genode1);
  568. return;
  569. }
  570. //旋转
  571. osg::Matrix mr = osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisX), osg::Vec3(0.1f, 0.0f, 0.0f))) *
  572. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisY), osg::Vec3(0.0f, 0.1f, 0.0f))) *
  573. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisZ), osg::Vec3(0.0f, 0.0f, 0.1f)));
  574. //mr.makeRotate(m_fAxisX, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
  575. //mr.makeRotate(m_fAxisY, osg::Vec3(0.0f, 0.1f, 0.0f));//沿x轴旋转
  576. //mr.makeRotate(m_fAxisZ, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
  577. //设置矩阵
  578. //geode1.setMatrix(mr);
  579. traverse(genode1);
  580. }
  581. ModelRotateCallback::ModelRotateCallback()
  582. {
  583. m_fAxisX = 0.0f;
  584. m_fAxisY = 0.0f;
  585. m_fAxisZ = 0.0f;
  586. }
  587. void ModelRotateCallback::setRotateAnglexyz(const double fx, const double fy, const double fz)
  588. {
  589. m_fAxisX = fx;
  590. m_fAxisY = fy;
  591. m_fAxisZ = fz;
  592. }
  593. void ModelRotateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
  594. {
  595. //创建矩阵变换节点
  596. osg::ref_ptr<osg::MatrixTransform> rotatennode = dynamic_cast<osg::MatrixTransform*>(node);
  597. //旋转
  598. osg::Matrix mr = osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(90.0), osg::Vec3(0.0f, 0.1f, 0.0f))) *
  599. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(90.0), osg::Vec3(0.0f, 0.0f, 0.1f)))*
  600. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisX), osg::Vec3(0.1f, 0.0f, 0.0f))) *
  601. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisY), osg::Vec3(0.0f, 0.1f, 0.0f))) *
  602. osg::Matrixd::rotate(osg::Quat(osg::DegreesToRadians(m_fAxisZ), osg::Vec3(0.0f, 0.0f, 0.1f)));
  603. //mr.makeRotate(m_fAxisX, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
  604. //mr.makeRotate(m_fAxisY, osg::Vec3(0.0f, 0.1f, 0.0f));//沿x轴旋转
  605. //mr.makeRotate(m_fAxisZ, osg::Vec3(0.1f, 0.0f, 0.0f));//沿x轴旋转
  606. //设置矩阵
  607. rotatennode->setMatrix(mr);
  608. //m_fAxisX *= -1.0f;
  609. //m_fAxisY *= -1.0f;
  610. //m_fAxisZ *= -1.0f;
  611. //Sleep(100);
  612. //继续传递参数,执行其他设置回调的节点
  613. //traverse(node, nv);
  614. }