KiCad PCB EDA Suite
Loading...
Searching...
No Matches
3d_model.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) 2020 Oleg Endo <[email protected]>
5 * Copyright (C) 2015-2016 Mario Luzeiro <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef _MODEL_3D_H_
23#define _MODEL_3D_H_
24
25#include <kicad_gl/kiglad.h> // Must be included first
26
27#include <memory>
28#include <mutex>
29#include <vector>
32#include <3d_enums.h>
33
34#include <wx/chartype.h>
35
37{
38public:
47 MODEL_3D( const S3DMODEL& a3DModel, MATERIAL_MODE aMaterialMode );
48
49 ~MODEL_3D();
50
54 void DrawOpaque( bool aUseSelectedMaterial, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const
55 {
56 Draw( false, 1.0f, aUseSelectedMaterial, aSelectionColor, nullptr, nullptr );
57 }
58
62 void DrawTransparent( float aOpacity, bool aUseSelectedMaterial,
63 SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const
64 {
65 Draw( true, aOpacity, aUseSelectedMaterial, aSelectionColor, nullptr, nullptr );
66 }
67
73 void Draw( bool aTransparent, float aOpacity, bool aUseSelectedMaterial,
74 const SFVEC3F& aSelectionColor,
75 const glm::mat4 *aModelWorldMatrix,
76 const SFVEC3F *aCameraWorldPos ) const;
77
81 bool HasOpaqueMeshes() const { return m_have_opaque_meshes; }
82
87
91 void DrawBbox() const;
92
96 void DrawBboxes() const;
97
102 const BBOX_3D& GetBBox() const { return m_model_bbox; }
103
107 static void BeginDrawMulti( bool aUseColorInformation );
108
112 static void EndDrawMulti();
113
114private:
115 static const wxChar* m_logTrace;
116
117 // the material mode that was used to generate the rendering data.
118 // FIXME: this can be selected at run-time and does not require re-creation
119 // of the whole model objects.
121
123 std::vector<BBOX_3D> m_meshes_bbox;
124
125 // unified vertex format for mesh rendering.
126 struct VERTEX
127 {
128 glm::vec3 m_pos;
129 glm::u8vec4 m_nrm; // only 3 components used
130 glm::u8vec4 m_color; // regular color
131 glm::u8vec4 m_cad_color; // "CAD" mode rendering color
132 glm::vec2 m_tex_uv;
133 };
134
135 // vertex buffer and index buffers that include all the individual meshes
136 // lumped together.
137 GLuint m_vertex_buffer = 0;
138 GLuint m_index_buffer = 0;
139 GLenum m_index_buffer_type = GL_INVALID_ENUM;
140
141 // internal material definition
142 // all meshes are grouped by material for rendering purposes.
144 {
146 unsigned int m_render_idx_count = 0;
147
150
151 MATERIAL( const SMATERIAL& aOther ) : SMATERIAL( aOther ) { }
152 bool IsTransparent() const { return m_Transparency > FLT_EPSILON; }
153 };
154
155 std::vector<MATERIAL> m_materials;
156
157 // a model can consist of transparent and opaque parts. remember which
158 // ones are present during initial buffer and data setup. use it later
159 // during rendering.
162
163 // vertex buffer and index buffer for the bounding boxes.
164 // the first box is always the outer box, followed by inner boxes (one for each mesh).
165 static constexpr unsigned int bbox_vtx_count = 8;
166 static constexpr unsigned int bbox_idx_count = 24;
167
170 GLenum m_bbox_index_buffer_type = GL_INVALID_ENUM;
171
172 static void MakeBbox( const BBOX_3D& aBox, unsigned int aIdxOffset, VERTEX* aVtxOut,
173 GLuint* aIdxOut, const glm::vec4& aColor );
174};
175
176
181{
182public:
183 MODEL_3D_DEFERRED( const S3DMODEL& a3DModel, MATERIAL_MODE aMaterialMode ) :
184 m_s3dModel( std::make_shared<S3DMODEL>( a3DModel ) ),
185 m_materialNode( aMaterialMode )
186 {
187 }
188
190
191 void Reset()
192 {
193 std::lock_guard<std::recursive_mutex> lock( m_mutex );
194
195 m_openglModel3D.reset();
196 m_s3dModel.reset();
197 }
198
199 std::shared_ptr<MODEL_3D> MakeOrGet()
200 {
201 std::lock_guard<std::recursive_mutex> lock( m_mutex );
202
203 if( !m_openglModel3D && !m_s3dModel )
204 throw std::runtime_error( "Both OpenGL model and S3D model are null" );
205
206 if( !m_openglModel3D )
207 m_openglModel3D = std::make_shared<MODEL_3D>( *m_s3dModel, m_materialNode );
208
209 m_s3dModel.reset();
210
211 return m_openglModel3D;
212 }
213
214private:
215 std::shared_ptr<MODEL_3D> m_openglModel3D;
216 std::recursive_mutex m_mutex;
217
218 std::shared_ptr<S3DMODEL> m_s3dModel;
220};
221
222#endif // _MODEL_3D_H_
declared enumerations and flags
MATERIAL_MODE
Render 3d model shape materials mode.
Definition 3d_enums.h:67
Bounding Box class definition.
define an internal structure to be used by the 3D renders
std::shared_ptr< S3DMODEL > m_s3dModel
Definition 3d_model.h:218
std::shared_ptr< MODEL_3D > m_openglModel3D
Definition 3d_model.h:215
std::shared_ptr< MODEL_3D > MakeOrGet()
Definition 3d_model.h:199
MATERIAL_MODE m_materialNode
Definition 3d_model.h:219
std::recursive_mutex m_mutex
Definition 3d_model.h:216
MODEL_3D_DEFERRED(const S3DMODEL &a3DModel, MATERIAL_MODE aMaterialMode)
Definition 3d_model.h:183
GLuint m_vertex_buffer
Definition 3d_model.h:137
void DrawBbox() const
Draw main bounding box of the model.
Definition 3d_model.cpp:566
bool m_have_opaque_meshes
Definition 3d_model.h:160
static void EndDrawMulti()
Cleanup render states after drawing multiple models.
Definition 3d_model.cpp:400
GLenum m_index_buffer_type
Definition 3d_model.h:139
MODEL_3D(const S3DMODEL &a3DModel, MATERIAL_MODE aMaterialMode)
Load a 3D model.
Definition 3d_model.cpp:86
std::vector< MATERIAL > m_materials
Definition 3d_model.h:155
static void MakeBbox(const BBOX_3D &aBox, unsigned int aIdxOffset, VERTEX *aVtxOut, GLuint *aIdxOut, const glm::vec4 &aColor)
Definition 3d_model.cpp:47
bool HasTransparentMeshes() const
Return true if have transparent mesh's to render.
Definition 3d_model.h:86
GLuint m_index_buffer
Definition 3d_model.h:138
void Draw(bool aTransparent, float aOpacity, bool aUseSelectedMaterial, const SFVEC3F &aSelectionColor, const glm::mat4 *aModelWorldMatrix, const SFVEC3F *aCameraWorldPos) const
Render the model into the current context.
Definition 3d_model.cpp:413
static void BeginDrawMulti(bool aUseColorInformation)
Set some basic render states before drawing multiple models.
Definition 3d_model.cpp:384
GLuint m_bbox_index_buffer
Definition 3d_model.h:169
static constexpr unsigned int bbox_idx_count
Definition 3d_model.h:166
MATERIAL_MODE m_materialMode
Definition 3d_model.h:120
void DrawBboxes() const
Draw individual bounding boxes of each mesh.
Definition 3d_model.cpp:585
static const wxChar * m_logTrace
Definition 3d_model.h:115
bool m_have_transparent_meshes
Definition 3d_model.h:161
std::vector< BBOX_3D > m_meshes_bbox
individual bbox for each mesh
Definition 3d_model.h:123
GLenum m_bbox_index_buffer_type
Definition 3d_model.h:170
static constexpr unsigned int bbox_vtx_count
Definition 3d_model.h:165
BBOX_3D m_model_bbox
global bounding box for this model
Definition 3d_model.h:122
GLuint m_bbox_vertex_buffer
Definition 3d_model.h:168
bool HasOpaqueMeshes() const
Return true if have opaque meshes to render.
Definition 3d_model.h:81
void DrawTransparent(float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) const
Render the model into the current context.
Definition 3d_model.h:62
void DrawOpaque(bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) const
Render the model into the current context.
Definition 3d_model.h:54
const BBOX_3D & GetBBox() const
Get the main bounding box.
Definition 3d_model.h:102
STL namespace.
Manage a bounding box defined by two SFVEC3F min max points.
Definition bbox_3d.h:39
MATERIAL(const SMATERIAL &aOther)
Definition 3d_model.h:151
bool IsTransparent() const
Definition 3d_model.h:152
unsigned int m_render_idx_count
Definition 3d_model.h:146
BBOX_3D m_bbox
bounding box for this material group, used for transparent material ordering.
Definition 3d_model.h:148
unsigned int m_render_idx_buffer_offset
Definition 3d_model.h:145
glm::vec3 m_pos
Definition 3d_model.h:128
glm::vec2 m_tex_uv
Definition 3d_model.h:132
glm::u8vec4 m_cad_color
Definition 3d_model.h:131
glm::u8vec4 m_color
Definition 3d_model.h:130
glm::u8vec4 m_nrm
Definition 3d_model.h:129
Store the a model based on meshes and materials.
Definition c3dmodel.h:111
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition c3dmodel.h:40
glm::vec3 SFVEC3F
Definition xv3d_types.h:40