KiCad PCB EDA Suite
render_3d_raytrace.h
Go to the documentation of this file.
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 
29 #ifndef RENDER_3D_RAYTRACE_H
30 #define RENDER_3D_RAYTRACE_H
31 
32 #include "../../common_ogl/openGL_includes.h"
35 #include "../render_3d_base.h"
36 #include "light.h"
37 #include "../post_shader_ssao.h"
38 #include "material.h"
39 #include <plugins/3dapi/c3dmodel.h>
40 
41 #include <map>
42 
44 typedef std::vector< BLINN_PHONG_MATERIAL > MODEL_MATERIALS;
45 
47 typedef std::map< const S3DMODEL* , MODEL_MATERIALS > MAP_MODEL_MATERIALS;
48 
49 typedef enum
50 {
57 
58 
60 {
61 public:
62  explicit RENDER_3D_RAYTRACE( EDA_3D_CANVAS* aCanvas, BOARD_ADAPTER& aAdapter, CAMERA& aCamera );
63 
65 
66  void SetCurWindowSize( const wxSize& aSize ) override;
67  bool Redraw( bool aIsMoving, REPORTER* aStatusReporter, REPORTER* aWarningReporter ) override;
68 
69  int GetWaitForEditingTimeOut() override;
70 
71  void Reload( REPORTER* aStatusReporter, REPORTER* aWarningReporter,
72  bool aOnlyLoadCopperAndShapes );
73 
74  BOARD_ITEM *IntersectBoardItem( const RAY& aRay );
75 
76 private:
77  bool initializeOpenGL();
79  void initPbo();
80  void deletePbo();
81  void createItemsFromContainer( const BVH_CONTAINER_2D* aContainer2d, PCB_LAYER_ID aLayer_id,
82  const MATERIAL* aMaterialLayer, const SFVEC3F& aLayerColor,
83  float aLayerZOffset );
84 
85  void restartRenderState();
86  void renderTracing( GLubyte* ptrPBO, REPORTER* aStatusReporter );
87  void postProcessShading( GLubyte* ptrPBO, REPORTER* aStatusReporter );
88  void postProcessBlurFinish( GLubyte* ptrPBO, REPORTER* aStatusReporter );
89  void renderBlockTracing( GLubyte* ptrPBO , signed int iBlock );
90  void renderFinalColor( GLubyte* ptrPBO, const SFVEC3F& rgbColor,
91  bool applyColorSpaceConversion );
92 
93  void renderRayPackets( const SFVEC3F* bgColorY, const RAY* aRayPkt, HITINFO_PACKET* aHitPacket,
94  bool is_testShadow, SFVEC3F* aOutHitColor );
95 
96  void renderAntiAliasPackets( const SFVEC3F* aBgColorY, const HITINFO_PACKET* aHitPck_X0Y0,
97  const HITINFO_PACKET* aHitPck_AA_X1Y1, const RAY* aRayPck,
98  SFVEC3F* aOutHitColor );
99 
100  // Materials
101  void setupMaterials();
102 
103  SFVEC3F shadeHit( const SFVEC3F& aBgColor, const RAY& aRay, HITINFO& aHitInfo,
104  bool aIsInsideObject, unsigned int aRecursiveLevel,
105  bool is_testShadow ) const;
106 
113  void createObject( CONTAINER_3D& aDstContainer, const OBJECT_2D* aObject2D, float aZMin,
114  float aZMax, const MATERIAL* aMaterial, const SFVEC3F& aObjColor );
115 
116  void addPadsAndVias();
117  void insertHole( const PCB_VIA* aVia );
118  void insertHole( const PAD* aPad );
119  void load3DModels( CONTAINER_3D& aDstContainer, bool aSkipMaterialInformation );
120  void addModels( CONTAINER_3D& aDstContainer, const S3DMODEL* a3DModel,
121  const glm::mat4& aModelMatrix, float aFPOpacity,
122  bool aSkipMaterialInformation, BOARD_ITEM* aBoardItem );
123 
124  MODEL_MATERIALS* getModelMaterial( const S3DMODEL* a3DModel );
125 
127 
128  void render( GLubyte* ptrPBO, REPORTER* aStatusReporter );
129  void renderPreview( GLubyte* ptrPBO );
130 
131  struct
132  {
140  } m_materials;
141 
150 
152 
155 
157  unsigned long int m_renderStartTime;
158 
161 
163 
164  std::list<LIGHT*> m_lights;
165 
167 
169 
170  GLuint m_pboId;
172 
174 
177 
180 
182 
185 
188 
190  std::vector< SFVEC2UI > m_blockPositions;
191 
193  std::vector< int > m_blockPositionsWasProcessed;
194 
196  std::vector< SFVEC2UI > m_blockPositionsFast;
197 
200 
202 
204 
205  // Display Offset
206  unsigned int m_xoffset;
207  unsigned int m_yoffset;
208 
211 
212  // Statistics
215 };
216 
217 #define USE_SRGB_SPACE
218 
219 #ifdef USE_SRGB_SPACE
220 extern SFVEC3F ConvertSRGBToLinear( const SFVEC3F& aSRGBcolor );
221 #else
222 #define ConvertSRGBToLinear( v ) ( v )
223 #endif
224 
225 #endif // RENDER_3D_RAYTRACE_H
define an internal structure to be used by the 3D renders
std::vector< int > m_blockPositionsWasProcessed
Encode the Morton code positions (on fast preview mode).
Procedural generation of the copper normals.
Definition: material.h:79
SOLDER_MASK_NORMAL m_solderMaskMaterial
SFVEC3F shadeHit(const SFVEC3F &aBgColor, const RAY &aRay, HITINFO &aHitInfo, bool aIsInsideObject, unsigned int aRecursiveLevel, bool is_testShadow) const
MAP_MODEL_MATERIALS m_modelMaterialMap
Stores materials of the 3D models.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
unsigned int m_convertedDummyBlockCount
MODEL_MATERIALS * getModelMaterial(const S3DMODEL *a3DModel)
COPPER_NORMAL m_copperMaterial
PLATED_COPPER_NORMAL m_platedCopperMaterial
unsigned long int m_renderStartTime
Time that the render starts.
void renderRayPackets(const SFVEC3F *bgColorY, const RAY *aRayPkt, HITINFO_PACKET *aHitPacket, bool is_testShadow, SFVEC3F *aOutHitColor)
Implement a canvas based on a wxGLCanvas.
Definition: eda_3d_canvas.h:48
Procedural generation of the plastic normals.
Definition: material.h:149
BLINN_PHONG_MATERIAL m_Paste
std::vector< BLINN_PHONG_MATERIAL > MODEL_MATERIALS
Vector of materials.
RT_RENDER_STATE m_renderState
State used on quality render.
Definition: ray.h:67
BLINN_PHONG_MATERIAL m_Floor
Blinn Phong based material https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model.
Definition: material.h:382
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
std::list< LIGHT * > m_lights
std::map< const S3DMODEL *, MODEL_MATERIALS > MAP_MODEL_MATERIALS
Maps a S3DMODEL pointer with a created BLINN_PHONG_MATERIAL vector.
void renderPreview(GLubyte *ptrPBO)
std::vector< SFVEC2UI > m_blockPositions
Flag if a position was already processed (cleared each new render).
BLINN_PHONG_MATERIAL m_SolderMask
void renderBlockTracing(GLubyte *ptrPBO, signed int iBlock)
Declare and implement light sources.
CONTAINER_2D m_containerWithObjectsToDelete
size_t m_blockRenderProgressCount
Save the number of blocks progress of the render.
Base material class that can be used to derive other material implementations.
Definition: material.h:243
std::vector< SFVEC2UI > m_blockPositionsFast
A class used to derive camera objects from.
Definition: camera.h:77
glm::uvec2 SFVEC2UI
Definition: xv3d_types.h:38
void renderTracing(GLubyte *ptrPBO, REPORTER *aStatusReporter)
void addModels(CONTAINER_3D &aDstContainer, const S3DMODEL *a3DModel, const glm::mat4 &aModelMatrix, float aFPOpacity, bool aSkipMaterialInformation, BOARD_ITEM *aBoardItem)
unsigned int m_converted2dRoundSegmentCount
BLINN_PHONG_MATERIAL m_NonPlatedCopper
void load3DModels(CONTAINER_3D &aDstContainer, bool aSkipMaterialInformation)
bool Redraw(bool aIsMoving, REPORTER *aStatusReporter, REPORTER *aWarningReporter) override
Redraw the view.
void Reload(REPORTER *aStatusReporter, REPORTER *aWarningReporter, bool aOnlyLoadCopperAndShapes)
void renderFinalColor(GLubyte *ptrPBO, const SFVEC3F &rgbColor, bool applyColorSpaceConversion)
BLINN_PHONG_MATERIAL m_SilkS
RENDER_3D_RAYTRACE(EDA_3D_CANVAS *aCanvas, BOARD_ADAPTER &aAdapter, CAMERA &aCamera)
This is a base class to hold data and functions for render targets.
BLINN_PHONG_MATERIAL m_EpoxyBoard
void createObject(CONTAINER_3D &aDstContainer, const OBJECT_2D *aObject2D, float aZMin, float aZMax, const MATERIAL *aMaterial, const SFVEC3F &aObjColor)
Create one or more 3D objects form a 2D object and Z positions.
BRUSHED_METAL_NORMAL m_brushedMetalMaterial
POST_SHADER_SSAO m_postShaderSsao
BOARD_ITEM * IntersectBoardItem(const RAY &aRay)
PLASTIC_SHINE_NORMAL m_shinyPlasticMaterial
void SetCurWindowSize(const wxSize &aSize) override
Before each render, the canvas will tell the render what is the size of its windows,...
SFVEC3F m_backgroundColorBottom
Used to see if the windows size changed.
Procedural generation of the shiny brushed metal.
Definition: material.h:198
void renderAntiAliasPackets(const SFVEC3F *aBgColorY, const HITINFO_PACKET *aHitPck_X0Y0, const HITINFO_PACKET *aHitPck_AA_X1Y1, const RAY *aRayPck, SFVEC3F *aOutHitColor)
DIRECTIONAL_LIGHT * m_cameraLight
A light source based only on a directional vector.
Definition: light.h:115
Stores the hit information of a ray with a point on the surface of a object.
Definition: hitinfo.h:40
BOARD_NORMAL m_boardMaterial
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:64
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
void createItemsFromContainer(const BVH_CONTAINER_2D *aContainer2d, PCB_LAYER_ID aLayer_id, const MATERIAL *aMaterialLayer, const SFVEC3F &aLayerColor, float aLayerZOffset)
void insertHole(const PCB_VIA *aVia)
Procedural generation of the shiny plastic normals.
Definition: material.h:173
void postProcessShading(GLubyte *ptrPBO, REPORTER *aStatusReporter)
BLINN_PHONG_MATERIAL m_Copper
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
RT_RENDER_STATE
void postProcessBlurFinish(GLubyte *ptrPBO, REPORTER *aStatusReporter)
Definition: pad.h:57
void render(GLubyte *ptrPBO, REPORTER *aStatusReporter)
Procedural generation of the solder mask.
Definition: material.h:129
PLASTIC_NORMAL m_plasticMaterial
struct RENDER_3D_RAYTRACE::@2 m_materials
ACCELERATOR_3D * m_accelerator
Helper class to handle information needed to display 3D board.
Definition: board_adapter.h:68
CONTAINER_2D * m_outlineBoard2dObjects
HITINFO_PACKET * m_firstHitinfo
BVH_CONTAINER_2D * m_antioutlineBoard2dObjects
int GetWaitForEditingTimeOut() override
Give the interface the time (in ms) that it should wait for editing or movements before (this works f...
CONTAINER_3D m_objectContainer
Store the list of created objects special for RT that will be clear in the end.
SFVEC3F ConvertSRGBToLinear(const SFVEC3F &aSRGBcolor)
wxSize m_oldWindowsSize
Encode Morton code positions.
SILK_SCREEN_NORMAL m_silkScreenMaterial