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* aStatusTextReporter,
69  REPORTER* aWarningTextReporter ) override;
70 
71  int GetWaitForEditingTimeOut() override;
72 
73 private:
74  bool initializeOpenGL();
76  void opengl_init_pbo();
77  void opengl_delete_pbo();
78  void reload( REPORTER* aStatusTextReporter, REPORTER* aWarningTextReporter );
79 
80  void restart_render_state();
81  void rt_render_tracing( GLubyte *ptrPBO , REPORTER *aStatusTextReporter );
82  void rt_render_post_process_shade( GLubyte *ptrPBO , REPORTER *aStatusTextReporter );
83  void rt_render_post_process_blur_finish( GLubyte *ptrPBO , REPORTER *aStatusTextReporter );
84  void rt_render_trace_block( GLubyte *ptrPBO , signed int iBlock );
85  void rt_final_color( GLubyte *ptrPBO, const SFVEC3F &rgbColor, bool applyColorSpaceConversion );
86 
87  void rt_shades_packet( const SFVEC3F *bgColorY,
88  const RAY *aRayPkt,
89  HITINFO_PACKET *aHitPacket,
90  bool is_testShadow,
91  SFVEC3F *aOutHitColor );
92 
93  void rt_trace_AA_packet( const SFVEC3F *aBgColorY,
94  const HITINFO_PACKET *aHitPck_X0Y0,
95  const HITINFO_PACKET *aHitPck_AA_X1Y1,
96  const RAY *aRayPck,
97  SFVEC3F *aOutHitColor );
98 
99  // Materials
100  void setupMaterials();
101 
102  struct
103  {
110  }m_materials;
111 
118 
120 
121  SFVEC3F shadeHit( const SFVEC3F &aBgColor,
122  const RAY &aRay,
123  HITINFO &aHitInfo,
124  bool aIsInsideObject,
125  unsigned int aRecursiveLevel,
126  bool is_testShadow ) const;
127 
130 
132  unsigned long int m_stats_start_rendering_time;
133 
136 
138 
140 
142 
144  GLuint m_pboId;
146 
148 
152 
154 
156 
159 
160  // Morton codes
161 
164 
166  std::vector< SFVEC2UI > m_blockPositions;
167 
169  std::vector< int > m_blockPositionsWasProcessed;
170 
172  std::vector< SFVEC2UI > m_blockPositionsFast;
173 
176 
178 
180 
181  // Display Offset
182  unsigned int m_xoffset;
183  unsigned int m_yoffset;
184 
185  // Statistics
188 
189  void create_3d_object_from( CCONTAINER &aDstContainer,
190  const COBJECT2D *aObject2D,
191  float aZMin, float aZMax,
192  const CMATERIAL *aMaterial,
193  const SFVEC3F &aObjColor );
194 
196  void insert3DViaHole( const VIA* aVia );
197  void insert3DPadHole( const D_PAD* aPad );
198  void load_3D_models();
199  void add_3D_models( const S3DMODEL *a3DModel,
200  const glm::mat4 &aModelMatrix,
201  float aModuleOpacity );
202 
205 
207 
208  void render( GLubyte *ptrPBO, REPORTER *aStatusTextReporter );
209  void render_preview( GLubyte *ptrPBO );
210 };
211 
212 #define USE_SRGB_SPACE
213 
214 #ifdef USE_SRGB_SPACE
215 extern SFVEC3F ConvertSRGBToLinear( const SFVEC3F &aSRGBcolor );
216 #else
217 #define ConvertSRGBToLinear(v) (v)
218 #endif
219 
220 #endif // C3D_RENDER_RAYTRACING_H
define an internal structure to be used by the 3D renders
void reload(REPORTER *aStatusTextReporter, REPORTER *aWarningTextReporter)
void rt_shades_packet(const SFVEC3F *bgColorY, const RAY *aRayPkt, HITINFO_PACKET *aHitPacket, bool is_testShadow, SFVEC3F *aOutHitColor)
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
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:195
wxSize m_oldWindowsSize
used to see if the windows size changed
SFVEC3F ConvertSRGBToLinear(const SFVEC3F &aSRGBcolor)
CCOPPERNORMAL m_copper_normal_perturbator
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
void rt_render_post_process_shade(GLubyte *ptrPBO, REPORTER *aStatusTextReporter)
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:62
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.
bool Redraw(bool aIsMoving, REPORTER *aStatusTextReporter, REPORTER *aWarningTextReporter) override
Redraw - Ask to redraw the view.
SFVEC3F shadeHit(const SFVEC3F &aBgColor, const RAY &aRay, HITINFO &aHitInfo, bool aIsInsideObject, unsigned int aRecursiveLevel, bool is_testShadow) const
Blinn Phong based material https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model.
Definition: cmaterial.h:278
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
void rt_render_tracing(GLubyte *ptrPBO, REPORTER *aStatusTextReporter)
int GetWaitForEditingTimeOut() override
GetWaitForEditingTimeOut - Give the interface the time (in ms) that it should wait for editing or mov...
void rt_render_post_process_blur_finish(GLubyte *ptrPBO, REPORTER *aStatusTextReporter)
void rt_trace_AA_packet(const SFVEC3F *aBgColorY, const HITINFO_PACKET *aHitPck_X0Y0, const HITINFO_PACKET *aHitPck_AA_X1Y1, const RAY *aRayPck, SFVEC3F *aOutHitColor)
CDIRECTIONALLIGHT * m_camera_light
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
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
void add_3D_models(const S3DMODEL *a3DModel, const glm::mat4 &aModelMatrix, float aModuleOpacity)
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
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 render(GLubyte *ptrPBO, REPORTER *aStatusTextReporter)
void rt_render_trace_block(GLubyte *ptrPBO, signed int iBlock)
This is a base class to hold data and functions for render targets.