CoreOSG.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************************************************************************
  2. 版本所有(C), 2020-2022, 北京普达迪泰
  3. ***************************************************************************************************
  4. 文 件 名: CoreOSG.h
  5. 功能描述: OSG封装类
  6. 版 本 号: 1.00初版
  7. 作 者: HYF
  8. 生成日期; 2021-12-20
  9. 最近修改:
  10. 函数列表:
  11. 修改历史:
  12. 1.日 期 :2021-12-20
  13. 作 者 : HYF
  14. 修改内容 : 创建文件
  15. **************************************************************************************************/
  16. #pragma once
  17. #include <osgViewer/Viewer>
  18. #include <osgViewer/ViewerEventHandlers>
  19. #include <osgViewer/api/win32/GraphicsWindowWin32>
  20. #include <osgGA/TrackballManipulator>
  21. #include <osgGA/KeySwitchMatrixManipulator>
  22. #include <osgGA/StateSetManipulator>
  23. #include <osgDB/DatabasePager>
  24. #include <osgDB/Registry>
  25. #include <osgDB/ReadFile>
  26. #include <osgUtil/Optimizer>
  27. #include <osg/MatrixTransform>
  28. #include <osg/BlendColor>
  29. #include <osg/BlendFunc>
  30. #include <osg/Geode>
  31. #include <osg/Geometry>
  32. #include <osg/LineWidth>
  33. #include <osg/ComputeBoundsVisitor>
  34. #include <osg/Node>
  35. #include <osg/Group>
  36. #include <osg/Matrix>
  37. #include <osg/FrameStamp>
  38. #include <osg/Camera>
  39. #include <string>
  40. class ModelRotateCallback : public osg::NodeCallback
  41. {
  42. public:
  43. ModelRotateCallback();
  44. virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
  45. void setRotateAnglexyz(const double fx, const double fy, const double fz);
  46. private:
  47. double m_fAxisX;
  48. double m_fAxisY;
  49. double m_fAxisZ;
  50. };
  51. class CoreOSG
  52. {
  53. public:
  54. CoreOSG(HWND hWnd);
  55. ~CoreOSG();
  56. BOOL InitOSG();
  57. void InitManipulators();
  58. // 加载模型文件
  59. BOOL LoadModel(std::vector<string> &vecModel, std::string axesPath);
  60. //BOOL ReLoadModel(std::string modelname);
  61. BOOL InitCameraConfig(void);
  62. BOOL ReInitCameraConfig(void);
  63. void setColor(osg::Node* pNode, osg::Vec4 vecColor);
  64. //void SetupWindow(void);
  65. //void SetupCamera(void);
  66. void PreFrameUpdate(void);
  67. void PostFrameUpdate(void);
  68. void Done(bool value) { m_bDone = value; }
  69. bool Done(void) { return m_bDone; }
  70. //static void Render(void* ptr);
  71. BOOL Release();
  72. BOOL ReleaseViewer();
  73. osgViewer::Viewer* getViewer() { return m_Viewer; };
  74. osg::Vec3d DegreesToRadians(const osg::Vec3& vecRotate);
  75. osg::Vec3d RadiansToDegrees(const osg::Vec3& vecRotate);
  76. void Quat2RadiansRotate(const osg::Quat& quat, osg::Vec3& vecRotate);
  77. void Quat2DegreesRotate(const osg::Quat& quat, osg::Vec3& vecRotate);
  78. osg::Matrix RadiansRotate2Matrix(const osg::Vec3d& vecRotate);
  79. osg::Matrix DegreesRotate2Matrix(const osg::Vec3d& vecRotate);
  80. osg::ref_ptr<osg::Node> CreateLine(osg::Vec3 firstpoit, osg::Vec3 endpoint);
  81. void Export(const std::string& strPaths, const std::string& strPathd);
  82. public:
  83. bool AddLine();
  84. std::vector<osg::ref_ptr<osg::MatrixTransform>> m_vecEngine;
  85. void VibrateEngine(int iMearObj);
  86. osg::Camera* createHudCamera();
  87. void RotateCurModel(const double fx, const double fy, const double fz);
  88. osg::ref_ptr<ModelRotateCallback> m_pModelRotateCallback;
  89. //class HudCallback;
  90. private:
  91. HWND m_hWnd;
  92. int m_giCurMearObj = 2;//当前测试对象 0表示左发动机 1表示右发动机
  93. bool m_bDone;
  94. std::string m_szModelName;
  95. //osgViewer::Viewer* m_Viewer;
  96. osg::ref_ptr<osgViewer::Viewer> m_Viewer;
  97. osg::ref_ptr<osg::Group> m_Root;
  98. osg::ref_ptr<osg::MatrixTransform> m_gEngineR = nullptr;
  99. osg::ref_ptr<osg::MatrixTransform> m_gEngineL = nullptr;
  100. osg::ref_ptr<osgGA::TrackballManipulator> m_Trackball;
  101. osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> m_KeyswitchManipulator;
  102. //
  103. };
  104. class RenderingThread : public OpenThreads::Thread
  105. {
  106. public:
  107. RenderingThread(CoreOSG* ptr);
  108. virtual ~RenderingThread();
  109. virtual void run();
  110. void Done(bool value) { _done = value; }
  111. bool Done(void) { return _done; }
  112. protected:
  113. CoreOSG* _ptr;
  114. bool _done;
  115. };
  116. class HudCallback : public osg::NodeCallback
  117. {
  118. public:
  119. HudCallback(osgViewer::Viewer* viewer);
  120. ~HudCallback();
  121. void operator()(osg::Node* node, osg::NodeVisitor* nv);
  122. private:
  123. osg::ref_ptr<osgViewer::Viewer> m_xyzviewer;
  124. };
  125. class HUDAxis :public osg::Camera
  126. {
  127. public:
  128. HUDAxis();
  129. //HUDAxis(HUDAxis const& copy, CopyOp copyOp = CopyOp::SHALLOW_COPY);
  130. //META_Node(osg, HUDAxis);
  131. inline void setMainCamera(osg::Camera* camera) { _mainCamera = camera; }
  132. virtual void traverse(osg::NodeVisitor& nv);
  133. protected:
  134. //virtual ~HUDAxis();
  135. osg::ref_ptr<osg::Camera> _mainCamera;
  136. };
  137. //node访问器,继承自osg::NodeVisitor类
  138. class VertexVisitor : public osg::NodeVisitor
  139. {
  140. public:
  141. //构造函数,初始化一下并设置为遍历所有子节点
  142. VertexVisitor();
  143. // ~VertexVisitor();
  144. //设置推力线终点坐标
  145. void setThrustlineEndPoit(osg::Vec3 vThrustlineEndPoit);
  146. //设置要查找的推力线几何体名称
  147. void setFindThrustlineName(const std::string strThrustlineName);
  148. void setRotatexyz(const double fx, const double fy, const double fz);
  149. void apply(osg::MatrixTransform& geode);
  150. void apply(osg::Node& node);
  151. void apply(osg::Geode& genode);
  152. void apply(osg::Transform &genode);
  153. private:
  154. //保存顶点数据
  155. //osg::ref_ptr<osg::Vec3Array> m_extractedverts;
  156. //推力线终点坐标
  157. osg::Vec3 m_vThrustlineEndPoit;
  158. std::string m_strThrustlineName;
  159. double m_fAxisX;
  160. double m_fAxisY;
  161. double m_fAxisZ;
  162. };