KiCad PCB EDA Suite
c3d_render_raytracing.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) 1992-2020 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 
30 #ifndef C3D_RENDER_RAYTRACING_H
31 #define C3D_RENDER_RAYTRACING_H
32 
33 #include "../../common_ogl/openGL_includes.h"
36 #include "../c3d_render_base.h"
37 #include "clight.h"
38 #include "../cpostshader_ssao.h"
39 #include "cmaterial.h"
40 #include <plugins/3dapi/c3dmodel.h>
41 
42 #include <map>
43 
45 typedef std::vector< CBLINN_PHONG_MATERIAL > MODEL_MATERIALS;
46 
48 typedef std::map< const S3DMODEL * , MODEL_MATERIALS > MAP_MODEL_MATERIALS;
49 
50 typedef enum
51 {
58 
60 {
61 public:
62  explicit C3D_RENDER_RAYTRACING( BOARD_ADAPTER& aAdapter, CCAMERA& aCamera );
63 
65 
66  // Imported from C3D_RENDER_BASE
67  void SetCurWindowSize( const wxSize &aSize ) override;
68  bool Redraw( bool aIsMoving, REPORTER* aStatusReporter, REPORTER* aWarningReporter ) override;
69 
70  int GetWaitForEditingTimeOut() override;
71 
72  void Reload( REPORTER* aStatusReporter, REPORTER* aWarningReporter, bool aOnlyLoadCopperAndShapes );
73 
74  BOARD_ITEM *IntersectBoardItem( const RAY &aRay );
75 
76 private:
77  bool initializeOpenGL();
79  void opengl_init_pbo();
80  void opengl_delete_pbo();
81  void createItemsFromContainer( const CBVHCONTAINER2D *aContainer2d,
82  PCB_LAYER_ID aLayer_id,
83  const CMATERIAL *aMaterialLayer,
84  const SFVEC3F &aLayerColor,
85  float aLayerZOffset );
86 
87  void restart_render_state();
88  void rt_render_tracing( GLubyte* ptrPBO, REPORTER* aStatusReporter );
89  void rt_render_post_process_shade( GLubyte* ptrPBO, REPORTER* aStatusReporter );
90  void rt_render_post_process_blur_finish( GLubyte* ptrPBO, REPORTER* aStatusReporter );
91  void rt_render_trace_block( GLubyte *ptrPBO , signed int iBlock );
92  void rt_final_color( GLubyte *ptrPBO, const SFVEC3F &rgbColor, bool applyColorSpaceConversion );
93 
94  void rt_shades_packet( const SFVEC3F *bgColorY,
95  const RAY *aRayPkt,
96  HITINFO_PACKET *aHitPacket,
97  bool is_testShadow,
98  SFVEC3F *aOutHitColor );
99 
100  void rt_trace_AA_packet( const SFVEC3F *aBgColorY,
101  const HITINFO_PACKET *aHitPck_X0Y0,
102  const HITINFO_PACKET *aHitPck_AA_X1Y1,
103  const RAY *aRayPck,
104  SFVEC3F *aOutHitColor );
105 
106  // Materials
107  void setupMaterials();
108 
109  struct
110  {
118  }m_materials;
119 
127 
129 
130  SFVEC3F shadeHit( const SFVEC3F &aBgColor,
131  const RAY &aRay,
132  HITINFO &aHitInfo,
133  bool aIsInsideObject,
134  unsigned int aRecursiveLevel,
135  bool is_testShadow ) const;
136 
139 
141  unsigned long int m_stats_start_rendering_time;
142 
145 
147 
149 
151 
153  GLuint m_pboId;
155 
157 
161 
163 
165 
168 
169  // Morton codes
170 
173 
175  std::vector< SFVEC2UI > m_blockPositions;
176 
178  std::vector< int > m_blockPositionsWasProcessed;
179 
181  std::vector< SFVEC2UI > m_blockPositionsFast;
182 
185 
187 
189 
190  // Display Offset
191  unsigned int m_xoffset;
192  unsigned int m_yoffset;
193 
194  // Statistics
197 
198  void create_3d_object_from( CCONTAINER &aDstContainer,
199  const COBJECT2D *aObject2D,
200  float aZMin, float aZMax,
201  const CMATERIAL *aMaterial,
202  const SFVEC3F &aObjColor );
203 
205  void insert3DViaHole( const VIA* aVia );
206  void insert3DPadHole( const D_PAD* aPad );
207  void load_3D_models( CCONTAINER &aDstContainer, bool aSkipMaterialInformation );
208  void add_3D_models( CCONTAINER &aDstContainer,
209  const S3DMODEL *a3DModel,
210  const glm::mat4 &aModelMatrix,
211  float aModuleOpacity,
212  bool aSkipMaterialInformation,
213  BOARD_ITEM *aBoardItem );
214 
215  MODEL_MATERIALS *get_3D_model_material( const S3DMODEL *a3DModel );
216 
219 
221 
222  void render( GLubyte* ptrPBO, REPORTER* aStatusReporter );
223  void render_preview( GLubyte *ptrPBO );
224 };
225 
226 #define USE_SRGB_SPACE
227 
228 #ifdef USE_SRGB_SPACE
229 extern SFVEC3F ConvertSRGBToLinear( const SFVEC3F &aSRGBcolor );
230 #else
231 #define ConvertSRGBToLinear(v) (v)
232 #endif
233 
234 #endif // C3D_RENDER_RAYTRACING_H
define an internal structure to be used by the 3D renders
void rt_shades_packet(const SFVEC3F *bgColorY, const RAY *aRayPkt, HITINFO_PACKET *aHitPacket, bool is_testShadow, SFVEC3F *aOutHitColor)
void rt_render_post_process_shade(GLubyte *ptrPBO, REPORTER *aStatusReporter)
CPLASTICNORMAL m_plastic_normal_perturbator
void rt_final_color(GLubyte *ptrPBO, const SFVEC3F &rgbColor, bool applyColorSpaceConversion)
CBOARDNORMAL m_board_normal_perturbator
Directional light - a light based only on a direction vector.
Definition: clight.h:114
CPOSTSHADER_SSAO m_postshader_ssao
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
std::vector< int > m_blockPositionsWasProcessed
this flags if a position was already processed (cleared each new render)
A base material class that can be used to derive a material implementation.
Definition: cmaterial.h:216
wxSize m_oldWindowsSize
used to see if the windows size changed
SFVEC3F ConvertSRGBToLinear(const SFVEC3F &aSRGBcolor)
CCOPPERNORMAL m_copper_normal_perturbator
MODEL_MATERIALS * get_3D_model_material(const S3DMODEL *a3DModel)
void create_3d_object_from(CCONTAINER &aDstContainer, const COBJECT2D *aObject2D, float aZMin, float aZMax, const CMATERIAL *aMaterial, const SFVEC3F &aObjColor)
Function create_3d_object_from.
Class CCAMERA is a virtual class used to derive CCAMERA objects from.
Definition: ccamera.h:79
CBLINN_PHONG_MATERIAL m_Floor
bool Redraw(bool aIsMoving, REPORTER *aStatusReporter, REPORTER *aWarningReporter) override
Redraw - Ask to redraw the view.
CBLINN_PHONG_MATERIAL m_NonPlatedCopper
void insert3DViaHole(const VIA *aVia)
CPLASTICSHINENORMAL m_plastic_shine_normal_perturbator
Definition: ray.h:67
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
CMETALBRUSHEDNORMAL m_brushed_metal_normal_perturbator
HITINFO_PACKET * m_firstHitinfo
unsigned int m_stats_converted_dummy_to_plane
CBLINN_PHONG_MATERIAL m_Paste
std::vector< CBLINN_PHONG_MATERIAL > MODEL_MATERIALS
Vector of materials.
glm::uvec2 SFVEC2UI
Definition: xv3d_types.h:41
CGENERICACCELERATOR * m_accelerator
CBLINN_PHONG_MATERIAL m_SilkS
declare and implement light types classes
size_t m_nrBlocksRenderProgress
Save the number of blocks progress of the render.
PCB_LAYER_ID
A quick note on layer IDs:
void add_3D_models(CCONTAINER &aDstContainer, const S3DMODEL *a3DModel, const glm::mat4 &aModelMatrix, float aModuleOpacity, bool aSkipMaterialInformation, BOARD_ITEM *aBoardItem)
void createItemsFromContainer(const CBVHCONTAINER2D *aContainer2d, PCB_LAYER_ID aLayer_id, const CMATERIAL *aMaterialLayer, const SFVEC3F &aLayerColor, float aLayerZOffset)
SFVEC3F shadeHit(const SFVEC3F &aBgColor, const RAY &aRay, HITINFO &aHitInfo, bool aIsInsideObject, unsigned int aRecursiveLevel, bool is_testShadow) const
void load_3D_models(CCONTAINER &aDstContainer, bool aSkipMaterialInformation)
Blinn Phong based material https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model.
Definition: cmaterial.h:318
RT_RENDER_STATE m_rt_render_state
State used on quality render.
CBLINN_PHONG_MATERIAL m_SolderMask
CCONTAINER2D m_containerWithObjectsToDelete
This will store the list of created objects special for RT, that will be clear in the end.
unsigned int m_stats_converted_roundsegment2d_to_roundsegment
int GetWaitForEditingTimeOut() override
GetWaitForEditingTimeOut - Give the interface the time (in ms) that it should wait for editing or mov...
void rt_trace_AA_packet(const SFVEC3F *aBgColorY, const HITINFO_PACKET *aHitPck_X0Y0, const HITINFO_PACKET *aHitPck_AA_X1Y1, const RAY *aRayPck, SFVEC3F *aOutHitColor)
void Reload(REPORTER *aStatusReporter, REPORTER *aWarningReporter, bool aOnlyLoadCopperAndShapes)
CPLATEDCOPPERNORMAL m_platedcopper_normal_perturbator
CDIRECTIONALLIGHT * m_camera_light
void render(GLubyte *ptrPBO, REPORTER *aStatusReporter)
BOARD_ITEM * IntersectBoardItem(const RAY &aRay)
std::vector< SFVEC2UI > m_blockPositionsFast
this encodes the Morton code positions (on fast preview mode)
A light contariner. It will add lights and remove it in the end.
Definition: clight.h:155
CSOLDERMASKNORMAL m_solder_mask_normal_perturbator
void rt_render_tracing(GLubyte *ptrPBO, REPORTER *aStatusReporter)
CBLINN_PHONG_MATERIAL m_Copper
struct C3D_RENDER_RAYTRACING::@3 m_materials
Stores the hit information of a ray with a point on the surface of a object.
Definition: hitinfo.h:40
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
std::vector< SFVEC2UI > m_blockPositions
this encodes the Morton code positions
unsigned long int m_stats_start_rendering_time
Time that the render starts.
CBLINN_PHONG_MATERIAL m_EpoxyBoard
std::map< const S3DMODEL *, MODEL_MATERIALS > MAP_MODEL_MATERIALS
Maps a S3DMODEL pointer with a created CBLINN_PHONG_MATERIAL vector.
void SetCurWindowSize(const wxSize &aSize) override
SetCurWindowSize - Before each render, the canvas will tell the render what is the size of its window...
CCONTAINER2D * m_outlineBoard2dObjects
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
void rt_render_post_process_blur_finish(GLubyte *ptrPBO, REPORTER *aStatusReporter)
C3D_RENDER_RAYTRACING(BOARD_ADAPTER &aAdapter, CCAMERA &aCamera)
void insert3DPadHole(const D_PAD *aPad)
Class BOARD_ADAPTER Helper class to handle information needed to display 3D board.
Definition: board_adapter.h:68
MAP_MODEL_MATERIALS m_model_materials
Stores materials of the 3D models.
void render_preview(GLubyte *ptrPBO)
void rt_render_trace_block(GLubyte *ptrPBO, signed int iBlock)
This is a base class to hold data and functions for render targets.