You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
9.9 KiB

5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef RENDER_3D_OPENGL_H
  25. #define RENDER_3D_OPENGL_H
  26. #include "../render_3d_base.h"
  27. #include "layer_triangles.h"
  28. #include "3d_spheres_gizmo.h"
  29. #include "../raytracing/shapes2D/polygon_2d.h"
  30. #include "../raytracing/shapes2D/triangle_2d.h"
  31. #include "../raytracing/shapes2D/4pt_polygon_2d.h"
  32. #include "../raytracing/shapes2D/filled_circle_2d.h"
  33. #include "../raytracing/shapes2D/ring_2d.h"
  34. #include "../raytracing/shapes2D/round_segment_2d.h"
  35. #include "3d_model.h"
  36. #include "3d_cache/3d_info.h"
  37. #include <map>
  38. typedef std::map< PCB_LAYER_ID, OPENGL_RENDER_LIST* > MAP_OGL_DISP_LISTS;
  39. typedef std::list<TRIANGLE_DISPLAY_LIST* > LIST_TRIANGLES;
  40. #define SIZE_OF_CIRCLE_TEXTURE 1024
  41. /**
  42. * Object to render the board using openGL.
  43. */
  44. class RENDER_3D_OPENGL : public RENDER_3D_BASE
  45. {
  46. public:
  47. explicit RENDER_3D_OPENGL( EDA_3D_CANVAS* aCanvas, BOARD_ADAPTER& aAdapter, CAMERA& aCamera );
  48. ~RENDER_3D_OPENGL();
  49. void SetCurWindowSize( const wxSize& aSize ) override;
  50. bool Redraw( bool aIsMoving, REPORTER* aStatusReporter, REPORTER* aWarningReporter ) override;
  51. int GetWaitForEditingTimeOut() override;
  52. void SetCurrentRollOverItem( BOARD_ITEM* aRollOverItem )
  53. {
  54. m_currentRollOverItem = aRollOverItem;
  55. }
  56. /**
  57. * Load footprint models if they are not already loaded, i.e. if m_3dModelMap is empty
  58. */
  59. void Load3dModelsIfNeeded();
  60. void handleGizmoMouseInput( int mouseX, int mouseY );
  61. void setGizmoViewport( int x, int y, int width, int height );
  62. std::tuple<int, int, int, int> getGizmoViewport() const;
  63. SPHERES_GIZMO::GizmoSphereSelection getSelectedGizmoSphere() const;
  64. void resetSelectedGizmoSphere();
  65. private:
  66. OPENGL_RENDER_LIST* generateHoles( const LIST_OBJECT2D& aListHolesObject2d,
  67. const SHAPE_POLY_SET& aPoly, float aZtop, float aZbot,
  68. bool aInvertFaces,
  69. const BVH_CONTAINER_2D* aThroughHoles = nullptr );
  70. OPENGL_RENDER_LIST* generateLayerList( const BVH_CONTAINER_2D* aContainer,
  71. const SHAPE_POLY_SET* aPolyList, PCB_LAYER_ID aLayer,
  72. const BVH_CONTAINER_2D* aThroughHoles = nullptr );
  73. OPENGL_RENDER_LIST* generateEmptyLayerList( PCB_LAYER_ID aLayer );
  74. void addTopAndBottomTriangles( TRIANGLE_DISPLAY_LIST* aDst, const SFVEC2F& v0,
  75. const SFVEC2F& v1, const SFVEC2F& v2, float top, float bot );
  76. void addObjectTriangles( const RING_2D* aRing, TRIANGLE_DISPLAY_LIST* aDstLayer,
  77. float aZtop, float aZbot );
  78. void addObjectTriangles( const POLYGON_4PT_2D* aPoly, TRIANGLE_DISPLAY_LIST* aDstLayer,
  79. float aZtop, float aZbot );
  80. void addObjectTriangles( const FILLED_CIRCLE_2D* aCircle, TRIANGLE_DISPLAY_LIST* aDstLayer,
  81. float aZtop, float aZbot );
  82. void addObjectTriangles( const TRIANGLE_2D* aTri, TRIANGLE_DISPLAY_LIST* aDstLayer,
  83. float aZtop, float aZbot );
  84. void addObjectTriangles( const ROUND_SEGMENT_2D* aSeg, TRIANGLE_DISPLAY_LIST* aDstLayer,
  85. float aZtop, float aZbot );
  86. void renderSolderMaskLayer( PCB_LAYER_ID aLayerID, float aZPos, bool aShowThickness,
  87. bool aSkipRenderHoles );
  88. void renderBoardBody( bool aSkipRenderHoles );
  89. void getLayerZPos( PCB_LAYER_ID aLayerID, float& aOutZtop, float& aOutZbot ) const;
  90. void generateRing( const SFVEC2F& aCenter, float aInnerRadius, float aOuterRadius,
  91. unsigned int aNr_sides_per_circle,
  92. std::vector< SFVEC2F >& aInnerContourResult,
  93. std::vector< SFVEC2F >& aOuterContourResult, bool aInvertOrder );
  94. void generateCylinder( const SFVEC2F& aCenter, float aInnerRadius, float aOuterRadius,
  95. float aZtop, float aZbot, unsigned int aNr_sides_per_circle,
  96. TRIANGLE_DISPLAY_LIST* aDstLayer );
  97. void generateDisk( const SFVEC2F& aCenter, float aRadius, float aZ,
  98. unsigned int aNr_sides_per_circle, TRIANGLE_DISPLAY_LIST* aDstLayer,
  99. bool aTop );
  100. void generateDimple( const SFVEC2F& aCenter, float aRadius, float aZ, float aDepth,
  101. unsigned int aNr_sides_per_circle, TRIANGLE_DISPLAY_LIST* aDstLayer,
  102. bool aTop );
  103. void generateViasAndPads();
  104. /**
  105. * Load footprint models from the cache and load it to openGL lists in the form of
  106. * #MODEL_3D objects.
  107. *
  108. * This map of models will work as a local cache for this render. (cache based on
  109. * MODEL_3D with associated openGL lists in GPU memory)
  110. */
  111. void load3dModels( REPORTER* aStatusReporter );
  112. struct MODELTORENDER
  113. {
  114. glm::mat4 m_modelWorldMat;
  115. const MODEL_3D* m_model;
  116. float m_opacity;
  117. bool m_isTransparent;
  118. bool m_isSelected;
  119. MODELTORENDER( glm::mat4 aModelWorldMat,
  120. const MODEL_3D* aNodel,
  121. float aOpacity,
  122. bool aIsTransparent,
  123. bool aIsSelected ) :
  124. m_modelWorldMat( std::move( aModelWorldMat ) ),
  125. m_model( aNodel ),
  126. m_opacity( aOpacity ),
  127. m_isTransparent( aIsTransparent ),
  128. m_isSelected( aIsSelected )
  129. {
  130. }
  131. };
  132. void renderOpaqueModels( const glm::mat4 &aCameraViewMatrix );
  133. void renderTransparentModels( const glm::mat4 &aCameraViewMatrix );
  134. void renderModel( const glm::mat4 &aCameraViewMatrix, const MODELTORENDER &aModelToRender,
  135. const SFVEC3F &aSelColor, const SFVEC3F *aCameraWorldPos );
  136. void get3dModelsSelected( std::list<MODELTORENDER> &aDstRenderList, bool aGetTop, bool aGetBot,
  137. bool aRenderTransparentOnly, bool aRenderSelectedOnly );
  138. void get3dModelsFromFootprint( std::list<MODELTORENDER> &aDstRenderList,
  139. const FOOTPRINT* aFootprint, bool aRenderTransparentOnly,
  140. bool aIsSelected );
  141. void setLightFront( bool enabled );
  142. void setLightTop( bool enabled );
  143. void setLightBottom( bool enabled );
  144. /**
  145. * Create a 3D grid to an OpenGL display list.
  146. *
  147. * A horizontal grid (XY plane and Z = 0, and a vertical grid (XZ plane and Y = 0).
  148. */
  149. void generate3dGrid( GRID3D_TYPE aGridType );
  150. // Materials
  151. void setupMaterials();
  152. void setCopperMaterial();
  153. void setPlatedCopperAndDepthOffset( PCB_LAYER_ID aLayer_id );
  154. void unsetDepthOffset();
  155. void setLayerMaterial( PCB_LAYER_ID aLayerID );
  156. bool initializeOpenGL();
  157. OPENGL_RENDER_LIST* createBoard( const SHAPE_POLY_SET& aBoardPoly,
  158. const BVH_CONTAINER_2D* aThroughHoles = nullptr );
  159. void reload( REPORTER* aStatusReporter, REPORTER* aWarningReporter );
  160. void setArrowMaterial();
  161. void freeAllLists();
  162. struct
  163. {
  164. SMATERIAL m_Paste;
  165. SMATERIAL m_SilkSBot;
  166. SMATERIAL m_SilkSTop;
  167. SMATERIAL m_SolderMask;
  168. SMATERIAL m_EpoxyBoard;
  169. SMATERIAL m_NonPlatedCopper; // raw copper
  170. SMATERIAL m_Copper;
  171. SMATERIAL m_Plastic;
  172. SMATERIAL m_GrayMaterial;
  173. } m_materials;
  174. EDA_3D_CANVAS* m_canvas;
  175. MAP_OGL_DISP_LISTS m_layers;
  176. OPENGL_RENDER_LIST* m_platedPadsFront;
  177. OPENGL_RENDER_LIST* m_platedPadsBack;
  178. OPENGL_RENDER_LIST* m_offboardPadsFront;
  179. OPENGL_RENDER_LIST* m_offboardPadsBack;
  180. MAP_OGL_DISP_LISTS m_outerLayerHoles;
  181. MAP_OGL_DISP_LISTS m_innerLayerHoles;
  182. OPENGL_RENDER_LIST* m_board;
  183. OPENGL_RENDER_LIST* m_boardWithHoles;
  184. OPENGL_RENDER_LIST* m_antiBoard;
  185. OPENGL_RENDER_LIST* m_outerThroughHoles;
  186. OPENGL_RENDER_LIST* m_outerViaThroughHoles;
  187. OPENGL_RENDER_LIST* m_outerThroughHoleRings;
  188. LIST_TRIANGLES m_triangles; ///< store pointers so can be deleted latter
  189. GLuint m_circleTexture;
  190. GLuint m_grid; ///< oGL list that stores current grid
  191. GRID3D_TYPE m_lastGridType; ///< Stores the last grid type.
  192. OPENGL_RENDER_LIST* m_microviaHoles;
  193. OPENGL_RENDER_LIST* m_padHoles;
  194. OPENGL_RENDER_LIST* m_viaFrontCover;
  195. OPENGL_RENDER_LIST* m_viaBackCover;
  196. // Caches
  197. std::map<wxString, MODEL_3D*> m_3dModelMap;
  198. std::map<std::vector<float>, glm::mat4> m_3dModelMatrixMap;
  199. BOARD_ITEM* m_currentRollOverItem;
  200. SHAPE_POLY_SET m_antiBoardPolys; ///< The negative polygon representation of the board
  201. ///< outline.
  202. SPHERES_GIZMO* m_spheres_gizmo;
  203. };
  204. #endif // RENDER_3D_OPENGL_H