KiCad PCB EDA Suite
Loading...
Searching...
No Matches
render_3d_raytrace_base.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 <[email protected]>
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, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef RENDER_3D_RAYTRACE_BASE_H
22#define RENDER_3D_RAYTRACE_BASE_H
23
26#include "../render_3d_base.h"
27#include "light.h"
28#include "../post_shader_ssao.h"
29#include "material.h"
31
32#include <map>
33#include <memory>
34#include <mutex>
35#include <stop_token>
36
37class PAD;
38class PCB_VIA;
39
41typedef std::vector< BLINN_PHONG_MATERIAL > MODEL_MATERIALS;
42
44typedef std::map< const S3DMODEL* , MODEL_MATERIALS > MAP_MODEL_MATERIALS;
45
54
55
57{
58public:
59 // TODO: Take into account board thickness so that the camera won't move inside of the board
60 // when facing it perpendicularly.
61 static constexpr float MIN_DISTANCE_IU = 4 * PCB_IU_PER_MM;
62
63 explicit RENDER_3D_RAYTRACE_BASE( BOARD_ADAPTER& aAdapter, CAMERA& aCamera );
64
66
67 int GetWaitForEditingTimeOut() override;
68
69 void Reload( bool aOnlyLoadCopperAndShapes, std::stop_token aStop = {} );
70
78
79 BOARD_ITEM *IntersectBoardItem( const RAY& aRay );
80
81protected:
82 virtual void initPbo() = 0;
83 virtual void deletePbo() = 0;
84 void createItemsFromContainer( const BVH_CONTAINER_2D* aContainer2d, PCB_LAYER_ID aLayer_id,
85 const MATERIAL* aMaterialLayer, const SFVEC3F& aLayerColor,
86 float aLayerZOffset );
87
88 void restartRenderState();
89 void renderTracing( uint8_t* ptrPBO );
90 void postProcessShading( uint8_t* ptrPBO );
91 void postProcessBlurFinish( uint8_t* ptrPBO );
92 void renderBlockTracing( uint8_t* ptrPBO , signed int iBlock );
93 void renderFinalColor( uint8_t* ptrPBO, const SFVEC4F& rgbColor,
94 bool applyColorSpaceConversion );
95
96 void renderRayPackets( const SFVEC4F* bgColorY, const RAY* aRayPkt, HITINFO_PACKET* aHitPacket,
97 bool is_testShadow, SFVEC4F* aOutHitColor );
98
99 void renderAntiAliasPackets( const SFVEC4F* aBgColorY, const HITINFO_PACKET* aHitPck_X0Y0,
100 const HITINFO_PACKET* aHitPck_AA_X1Y1, const RAY* aRayPck,
101 SFVEC4F* aOutHitColor );
102
103 // Materials
104 void setupMaterials();
105
106 SFVEC4F shadeHit( const SFVEC4F& aBgColor, const RAY& aRay, HITINFO& aHitInfo,
107 bool aIsInsideObject, unsigned int aRecursiveLevel,
108 bool is_testShadow ) const;
109
116 void createObject( CONTAINER_3D& aDstContainer, const OBJECT_2D* aObject2D, float aZMin,
117 float aZMax, const MATERIAL* aMaterial, const SFVEC3F& aObjColor );
118
119 void addPadsAndVias();
120 void insertHole( const PCB_VIA* aVia );
121 void insertHole( const PAD* aPad );
122 void addCounterborePlating( const BOARD_ITEM& aSource, const SFVEC2F& aCenter,
123 float aInnerRadius, float aDepth, float aSurfaceZ,
124 bool aIsFront );
125 void addCountersinkPlating( const SFVEC2F& aCenter, float aTopInnerRadius,
126 float aBottomInnerRadius, float aSurfaceZ, float aDepth,
127 bool aIsFront );
128 void backfillPostMachine();
129 void load3DModels( CONTAINER_3D& aDstContainer, bool aSkipMaterialInformation, std::stop_token aStop = {} );
130 void addPlaceholderToRaytracer( CONTAINER_3D& aDstContainer, const FOOTPRINT* aFootprint,
131 const glm::mat4& aFpMatrix, bool aHasExtrudedBody = false, float aOpacity = 1.0f );
132 bool addExtrudedBodyToRaytracer( CONTAINER_3D& aDstContainer, const FOOTPRINT* aFootprint );
133 void addModels( CONTAINER_3D& aDstContainer, const S3DMODEL* a3DModel,
134 const glm::mat4& aModelMatrix, float aFPOpacity,
135 bool aSkipMaterialInformation, BOARD_ITEM* aBoardItem );
136
137 MODEL_MATERIALS* getModelMaterial( const S3DMODEL* a3DModel );
138
140
141 void render( uint8_t* ptrPBO );
142 void renderPreview( uint8_t* ptrPBO );
143
144 static SFVEC4F premultiplyAlpha( const SFVEC4F& aInput );
145
146 struct
147 {
156
157 std::vector<std::unique_ptr<BLINN_PHONG_MATERIAL>> m_extrusionMaterials;
158
167
170
173
176
179
181
182 std::list<LIGHT*> m_lights;
183
185
186 /*GLuint m_pboId;
187 GLuint m_pboDataSize;*/
188
190
193
196
197 std::unique_ptr<ACCELERATOR_3D> m_accelerator;
198
200 std::mutex m_hitTestMutex;
201
204
207
209 std::vector< SFVEC2UI > m_blockPositions;
210
213
215 std::vector< SFVEC2UI > m_blockPositionsFast;
216
219
221
223
224 // Display Offset
225 unsigned int m_xoffset;
226 unsigned int m_yoffset;
227
230
231 // Statistics
234};
235
236#define USE_SRGB_SPACE
237
238#ifdef USE_SRGB_SPACE
239extern SFVEC3F ConvertSRGBToLinear( const SFVEC3F& aSRGBcolor );
240extern SFVEC4F ConvertSRGBAToLinear( const SFVEC4F& aSRGBAcolor );
241#else
242#define ConvertSRGBToLinear( v ) ( v )
243#define ConvertSRGBAToLinear( v ) ( v )
244#endif
245
246#endif // RENDER_3D_RAYTRACE_BASE_H
constexpr double PCB_IU_PER_MM
Pcbnew IU is 1 nanometer.
Definition base_units.h:68
define an internal structure to be used by the 3D renders
Blinn Phong based material https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model.
Definition material.h:375
Helper class to handle information needed to display 3D board.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Procedural generation of the shiny brushed metal.
Definition material.h:191
A class used to derive camera objects from.
Definition camera.h:99
Procedural generation of the copper normals.
Definition material.h:72
A light source based only on a directional vector.
Definition light.h:112
Base material class that can be used to derive other material implementations.
Definition material.h:236
Definition pad.h:61
Procedural generation of the plastic normals.
Definition material.h:142
Procedural generation of the shiny plastic normals.
Definition material.h:166
RENDER_3D_BASE(BOARD_ADAPTER &aBoardAdapter, CAMERA &aCamera)
void renderBlockTracing(uint8_t *ptrPBO, signed int iBlock)
virtual void deletePbo()=0
static SFVEC4F premultiplyAlpha(const SFVEC4F &aInput)
BLINN_PHONG_MATERIAL m_NonPlatedCopper
BLINN_PHONG_MATERIAL m_EpoxyBoard
void renderTracing(uint8_t *ptrPBO)
SILK_SCREEN_NORMAL m_silkScreenMaterial
struct RENDER_3D_RAYTRACE_BASE::@013206213056006125230376122042346155134272177300 m_materials
RENDER_3D_RAYTRACE_BASE(BOARD_ADAPTER &aAdapter, CAMERA &aCamera)
void renderFinalColor(uint8_t *ptrPBO, const SFVEC4F &rgbColor, bool applyColorSpaceConversion)
std::mutex m_hitTestMutex
Serializes hover BVH use.
void addCountersinkPlating(const SFVEC2F &aCenter, float aTopInnerRadius, float aBottomInnerRadius, float aSurfaceZ, float aDepth, bool aIsFront)
void InvalidateHitTesting()
Drop the hover BVH so IntersectBoardItem becomes a no-op.
std::vector< std::unique_ptr< BLINN_PHONG_MATERIAL > > m_extrusionMaterials
void renderAntiAliasPackets(const SFVEC4F *aBgColorY, const HITINFO_PACKET *aHitPck_X0Y0, const HITINFO_PACKET *aHitPck_AA_X1Y1, const RAY *aRayPck, SFVEC4F *aOutHitColor)
void insertHole(const PCB_VIA *aVia)
BRUSHED_METAL_NORMAL m_brushedMetalMaterial
std::vector< int > m_blockPositionsWasProcessed
Flag if a position was already processed (cleared each new render).
void postProcessShading(uint8_t *ptrPBO)
static constexpr float MIN_DISTANCE_IU
PLATED_COPPER_NORMAL m_platedCopperMaterial
void createItemsFromContainer(const BVH_CONTAINER_2D *aContainer2d, PCB_LAYER_ID aLayer_id, const MATERIAL *aMaterialLayer, const SFVEC3F &aLayerColor, float aLayerZOffset)
PLASTIC_SHINE_NORMAL m_shinyPlasticMaterial
BOARD_ITEM * IntersectBoardItem(const RAY &aRay)
RT_RENDER_STATE m_renderState
State used on quality render.
bool addExtrudedBodyToRaytracer(CONTAINER_3D &aDstContainer, const FOOTPRINT *aFootprint)
void renderRayPackets(const SFVEC4F *bgColorY, const RAY *aRayPkt, HITINFO_PACKET *aHitPacket, bool is_testShadow, SFVEC4F *aOutHitColor)
int GetWaitForEditingTimeOut() override
Give the interface the time (in ms) that it should wait for editing or movements before (this works f...
void postProcessBlurFinish(uint8_t *ptrPBO)
void addCounterborePlating(const BOARD_ITEM &aSource, const SFVEC2F &aCenter, float aInnerRadius, float aDepth, float aSurfaceZ, bool aIsFront)
std::vector< SFVEC2UI > m_blockPositionsFast
Encode the Morton code positions (on fast preview mode).
SFVEC4F shadeHit(const SFVEC4F &aBgColor, const RAY &aRay, HITINFO &aHitInfo, bool aIsInsideObject, unsigned int aRecursiveLevel, bool is_testShadow) const
void load3DModels(CONTAINER_3D &aDstContainer, bool aSkipMaterialInformation, std::stop_token aStop={})
BVH_CONTAINER_2D * m_antioutlineBoard2dObjects
CONTAINER_2D m_containerWithObjectsToDelete
Store the list of created objects special for RT that will be clear in the end.
std::unique_ptr< ACCELERATOR_3D > m_accelerator
void addPlaceholderToRaytracer(CONTAINER_3D &aDstContainer, const FOOTPRINT *aFootprint, const glm::mat4 &aFpMatrix, bool aHasExtrudedBody=false, float aOpacity=1.0f)
BLINN_PHONG_MATERIAL m_SolderMask
MODEL_MATERIALS * getModelMaterial(const S3DMODEL *a3DModel)
wxSize m_oldWindowsSize
Used to see if the windows size changed.
void renderPreview(uint8_t *ptrPBO)
SOLDER_MASK_NORMAL m_solderMaskMaterial
void addModels(CONTAINER_3D &aDstContainer, const S3DMODEL *a3DModel, const glm::mat4 &aModelMatrix, float aFPOpacity, bool aSkipMaterialInformation, BOARD_ITEM *aBoardItem)
MAP_MODEL_MATERIALS m_modelMaterialMap
Stores materials of the 3D models.
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.
virtual void initPbo()=0
size_t m_blockRenderProgressCount
Save the number of blocks progress of the render.
int64_t m_renderStartTime
Time that the render starts.
std::vector< SFVEC2UI > m_blockPositions
Encode Morton code positions.
void Reload(bool aOnlyLoadCopperAndShapes, std::stop_token aStop={})
Procedural generation of the solder mask.
Definition material.h:122
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
Declare and implement light sources.
Implements a post shader screen space ambient occlusion on software.
SFVEC3F ConvertSRGBToLinear(const SFVEC3F &aSRGBcolor)
std::vector< BLINN_PHONG_MATERIAL > MODEL_MATERIALS
Vector of materials.
@ RT_RENDER_STATE_POST_PROCESS_SHADE
@ RT_RENDER_STATE_POST_PROCESS_BLUR_AND_FINISH
@ RT_RENDER_STATE_FINISH
@ RT_RENDER_STATE_TRACING
@ RT_RENDER_STATE_MAX
SFVEC4F ConvertSRGBAToLinear(const SFVEC4F &aSRGBAcolor)
std::map< const S3DMODEL *, MODEL_MATERIALS > MAP_MODEL_MATERIALS
Maps a S3DMODEL pointer with a created BLINN_PHONG_MATERIAL vector.
Stores the hit information of a ray with a point on the surface of a object.
Definition hitinfo.h:32
Definition ray.h:59
Store the a model based on meshes and materials.
Definition c3dmodel.h:111
glm::vec2 SFVEC2F
Definition xv3d_types.h:38
glm::vec3 SFVEC3F
Definition xv3d_types.h:40
glm::uvec2 SFVEC2UI
Definition xv3d_types.h:34
glm::vec4 SFVEC4F
Definition xv3d_types.h:42