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.

225 lines
7.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2020 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright (C) 2015-2021 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_RAYTRACE_H
  25. #define RENDER_3D_RAYTRACE_H
  26. #include "../../common_ogl/openGL_includes.h"
  27. #include "accelerators/container_3d.h"
  28. #include "accelerators/accelerator_3d.h"
  29. #include "../render_3d_base.h"
  30. #include "light.h"
  31. #include "../post_shader_ssao.h"
  32. #include "material.h"
  33. #include <plugins/3dapi/c3dmodel.h>
  34. #include <map>
  35. /// Vector of materials
  36. typedef std::vector< BLINN_PHONG_MATERIAL > MODEL_MATERIALS;
  37. /// Maps a S3DMODEL pointer with a created BLINN_PHONG_MATERIAL vector
  38. typedef std::map< const S3DMODEL* , MODEL_MATERIALS > MAP_MODEL_MATERIALS;
  39. typedef enum
  40. {
  41. RT_RENDER_STATE_TRACING = 0,
  42. RT_RENDER_STATE_POST_PROCESS_SHADE,
  43. RT_RENDER_STATE_POST_PROCESS_BLUR_AND_FINISH,
  44. RT_RENDER_STATE_FINISH,
  45. RT_RENDER_STATE_MAX
  46. } RT_RENDER_STATE;
  47. class RENDER_3D_RAYTRACE : public RENDER_3D_BASE
  48. {
  49. public:
  50. // TODO: Take into account board thickness so that the camera won't move inside of the board
  51. // when facing it perpendicularly.
  52. static constexpr float MIN_DISTANCE_IU = 4 * PCB_IU_PER_MM;
  53. explicit RENDER_3D_RAYTRACE( EDA_3D_CANVAS* aCanvas, BOARD_ADAPTER& aAdapter, CAMERA& aCamera );
  54. ~RENDER_3D_RAYTRACE();
  55. void SetCurWindowSize( const wxSize& aSize ) override;
  56. bool Redraw( bool aIsMoving, REPORTER* aStatusReporter, REPORTER* aWarningReporter ) override;
  57. int GetWaitForEditingTimeOut() override;
  58. void Reload( REPORTER* aStatusReporter, REPORTER* aWarningReporter,
  59. bool aOnlyLoadCopperAndShapes );
  60. BOARD_ITEM *IntersectBoardItem( const RAY& aRay );
  61. private:
  62. bool initializeOpenGL();
  63. void initializeNewWindowSize();
  64. void initPbo();
  65. void deletePbo();
  66. void createItemsFromContainer( const BVH_CONTAINER_2D* aContainer2d, PCB_LAYER_ID aLayer_id,
  67. const MATERIAL* aMaterialLayer, const SFVEC3F& aLayerColor,
  68. float aLayerZOffset );
  69. void restartRenderState();
  70. void renderTracing( GLubyte* ptrPBO, REPORTER* aStatusReporter );
  71. void postProcessShading( GLubyte* ptrPBO, REPORTER* aStatusReporter );
  72. void postProcessBlurFinish( GLubyte* ptrPBO, REPORTER* aStatusReporter );
  73. void renderBlockTracing( GLubyte* ptrPBO , signed int iBlock );
  74. void renderFinalColor( GLubyte* ptrPBO, const SFVEC3F& rgbColor,
  75. bool applyColorSpaceConversion );
  76. void renderRayPackets( const SFVEC3F* bgColorY, const RAY* aRayPkt, HITINFO_PACKET* aHitPacket,
  77. bool is_testShadow, SFVEC3F* aOutHitColor );
  78. void renderAntiAliasPackets( const SFVEC3F* aBgColorY, const HITINFO_PACKET* aHitPck_X0Y0,
  79. const HITINFO_PACKET* aHitPck_AA_X1Y1, const RAY* aRayPck,
  80. SFVEC3F* aOutHitColor );
  81. // Materials
  82. void setupMaterials();
  83. SFVEC3F shadeHit( const SFVEC3F& aBgColor, const RAY& aRay, HITINFO& aHitInfo,
  84. bool aIsInsideObject, unsigned int aRecursiveLevel,
  85. bool is_testShadow ) const;
  86. /**
  87. * Create one or more 3D objects form a 2D object and Z positions.
  88. *
  89. * It tries to optimize some types of objects that will be faster to trace than the
  90. * LAYER_ITEM object.
  91. */
  92. void createObject( CONTAINER_3D& aDstContainer, const OBJECT_2D* aObject2D, float aZMin,
  93. float aZMax, const MATERIAL* aMaterial, const SFVEC3F& aObjColor );
  94. void addPadsAndVias();
  95. void insertHole( const PCB_VIA* aVia );
  96. void insertHole( const PAD* aPad );
  97. void load3DModels( CONTAINER_3D& aDstContainer, bool aSkipMaterialInformation );
  98. void addModels( CONTAINER_3D& aDstContainer, const S3DMODEL* a3DModel,
  99. const glm::mat4& aModelMatrix, float aFPOpacity,
  100. bool aSkipMaterialInformation, BOARD_ITEM* aBoardItem );
  101. MODEL_MATERIALS* getModelMaterial( const S3DMODEL* a3DModel );
  102. void initializeBlockPositions();
  103. void render( GLubyte* ptrPBO, REPORTER* aStatusReporter );
  104. void renderPreview( GLubyte* ptrPBO );
  105. struct
  106. {
  107. BLINN_PHONG_MATERIAL m_Paste;
  108. BLINN_PHONG_MATERIAL m_SilkS;
  109. BLINN_PHONG_MATERIAL m_SolderMask;
  110. BLINN_PHONG_MATERIAL m_EpoxyBoard;
  111. BLINN_PHONG_MATERIAL m_Copper;
  112. BLINN_PHONG_MATERIAL m_NonPlatedCopper;
  113. BLINN_PHONG_MATERIAL m_Floor;
  114. } m_materials;
  115. BOARD_NORMAL m_boardMaterial;
  116. COPPER_NORMAL m_copperMaterial;
  117. PLATED_COPPER_NORMAL m_platedCopperMaterial;
  118. SOLDER_MASK_NORMAL m_solderMaskMaterial;
  119. PLASTIC_NORMAL m_plasticMaterial;
  120. PLASTIC_SHINE_NORMAL m_shinyPlasticMaterial;
  121. BRUSHED_METAL_NORMAL m_brushedMetalMaterial;
  122. SILK_SCREEN_NORMAL m_silkScreenMaterial;
  123. bool m_isPreview;
  124. /// State used on quality render
  125. RT_RENDER_STATE m_renderState;
  126. /// Time that the render starts
  127. unsigned long int m_renderStartTime;
  128. /// Save the number of blocks progress of the render
  129. size_t m_blockRenderProgressCount;
  130. POST_SHADER_SSAO m_postShaderSsao;
  131. std::list<LIGHT*> m_lights;
  132. DIRECTIONAL_LIGHT* m_cameraLight;
  133. bool m_openglSupportsVertexBufferObjects;
  134. GLuint m_pboId;
  135. GLuint m_pboDataSize;
  136. CONTAINER_3D m_objectContainer;
  137. ///< Store the list of created objects special for RT that will be clear in the end.
  138. CONTAINER_2D m_containerWithObjectsToDelete;
  139. CONTAINER_2D* m_outlineBoard2dObjects;
  140. BVH_CONTAINER_2D* m_antioutlineBoard2dObjects;
  141. ACCELERATOR_3D* m_accelerator;
  142. SFVEC3F m_backgroundColorTop;
  143. SFVEC3F m_backgroundColorBottom;
  144. ///< Used to see if the windows size changed.
  145. wxSize m_oldWindowsSize;
  146. ///< Encode Morton code positions.
  147. std::vector< SFVEC2UI > m_blockPositions;
  148. ///< Flag if a position was already processed (cleared each new render).
  149. std::vector< int > m_blockPositionsWasProcessed;
  150. ///< Encode the Morton code positions (on fast preview mode).
  151. std::vector< SFVEC2UI > m_blockPositionsFast;
  152. SFVEC2UI m_realBufferSize;
  153. SFVEC2UI m_fastPreviewModeSize;
  154. HITINFO_PACKET* m_firstHitinfo;
  155. SFVEC3F* m_shaderBuffer;
  156. // Display Offset
  157. unsigned int m_xoffset;
  158. unsigned int m_yoffset;
  159. /// Stores materials of the 3D models
  160. MAP_MODEL_MATERIALS m_modelMaterialMap;
  161. // Statistics
  162. unsigned int m_convertedDummyBlockCount;
  163. unsigned int m_converted2dRoundSegmentCount;
  164. };
  165. #define USE_SRGB_SPACE
  166. #ifdef USE_SRGB_SPACE
  167. extern SFVEC3F ConvertSRGBToLinear( const SFVEC3F& aSRGBcolor );
  168. #else
  169. #define ConvertSRGBToLinear( v ) ( v )
  170. #endif
  171. #endif // RENDER_3D_RAYTRACE_H