KiCad PCB EDA Suite
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 (C) 2015-2020 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, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef _MODEL_3D_H_
27 #define _MODEL_3D_H_
28 
29 #include <vector>
30 #include <plugins/3dapi/c3dmodel.h>
31 #include "../../common_ogl/openGL_includes.h"
32 #include "../raytracing/shapes3D/bbox_3d.h"
33 #include "../../3d_enums.h"
34 
35 #include <wx/chartype.h>
36 
37 class MODEL_3D
38 {
39 public:
48  MODEL_3D( const S3DMODEL& a3DModel, MATERIAL_MODE aMaterialMode );
49 
50  ~MODEL_3D();
51 
55  void DrawOpaque( bool aUseSelectedMaterial, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const
56  {
57  Draw( false, 1.0f, aUseSelectedMaterial, aSelectionColor );
58  }
59 
63  void DrawTransparent( float aOpacity, bool aUseSelectedMaterial,
64  SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const
65  {
66  Draw( true, aOpacity, aUseSelectedMaterial, aSelectionColor );
67  }
68 
72  bool HasOpaqueMeshes() const { return m_have_opaque_meshes; }
73 
78 
82  void DrawBbox() const;
83 
87  void DrawBboxes() const;
88 
93  const BBOX_3D& GetBBox() const { return m_model_bbox; }
94 
98  static void BeginDrawMulti( bool aUseColorInformation );
99 
103  static void EndDrawMulti();
104 
105 private:
106  static const wxChar* m_logTrace;
107 
108  // the material mode that was used to generate the rendering data.
109  // FIXME: this can be selected at run-time and does not require re-creation
110  // of the whole model objects.
112 
114  std::vector<BBOX_3D> m_meshes_bbox;
115 
116  // unified vertex format for mesh rendering.
117  struct VERTEX
118  {
119  glm::vec3 m_pos;
120  glm::u8vec4 m_nrm; // only 3 components used
121  glm::u8vec4 m_color; // regular color
122  glm::u8vec4 m_cad_color; // "CAD" mode rendering color
123  glm::vec2 m_tex_uv;
124  };
125 
126  // vertex buffer and index buffers that include all the individual meshes
127  // lumped together.
128  GLuint m_vertex_buffer = 0;
129  GLuint m_index_buffer = 0;
130  GLenum m_index_buffer_type = GL_INVALID_ENUM;
131 
132  // internal material definition
133  // all meshes are grouped by material for rendering purposes.
135  {
136  unsigned int m_render_idx_buffer_offset = 0;
137  unsigned int m_render_idx_count = 0;
138 
139  MATERIAL( const SMATERIAL& aOther ) : SMATERIAL( aOther ) { }
140  bool IsTransparent() const { return m_Transparency > FLT_EPSILON; }
141  };
142 
143  std::vector<MATERIAL> m_materials;
144 
145  // a model can consist of transparent and opaque parts. remember which
146  // ones are present during initial buffer and data setup. use it later
147  // during rendering.
148  bool m_have_opaque_meshes = false;
150 
151  // vertex buffer and index buffer for the bounding boxes.
152  // the first box is always the outer box, followed by inner boxes (one for each mesh).
153  static constexpr unsigned int bbox_vtx_count = 8;
154  static constexpr unsigned int bbox_idx_count = 24;
155 
158  GLenum m_bbox_index_buffer_type = GL_INVALID_ENUM;
159 
160  static void MakeBbox( const BBOX_3D& aBox, unsigned int aIdxOffset, VERTEX* aVtxOut,
161  GLuint* aIdxOut, const glm::vec4& aColor );
162 
163  void Draw( bool aTransparent, float aOpacity, bool aUseSelectedMaterial,
164  SFVEC3F& aSelectionColor ) const;
165 };
166 
167 #endif // _MODEL_3D_H_
define an internal structure to be used by the 3D renders
MATERIAL(const SMATERIAL &aOther)
Definition: 3d_model.h:139
bool HasOpaqueMeshes() const
Return true if have opaque meshes to render.
Definition: 3d_model.h:72
unsigned int m_render_idx_count
Definition: 3d_model.h:137
Manage a bounding box defined by two SFVEC3F min max points.
Definition: bbox_3d.h:41
static void MakeBbox(const BBOX_3D &aBox, unsigned int aIdxOffset, VERTEX *aVtxOut, GLuint *aIdxOut, const glm::vec4 &aColor)
Definition: 3d_model.cpp:51
GLuint m_bbox_index_buffer
Definition: 3d_model.h:157
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition: c3dmodel.h:44
GLuint m_bbox_vertex_buffer
Definition: 3d_model.h:156
glm::u8vec4 m_nrm
Definition: 3d_model.h:120
static constexpr unsigned int bbox_vtx_count
Definition: 3d_model.h:153
GLuint m_vertex_buffer
Definition: 3d_model.h:128
static constexpr unsigned int bbox_idx_count
Definition: 3d_model.h:154
bool m_have_transparent_meshes
Definition: 3d_model.h:149
GLuint m_index_buffer
Definition: 3d_model.h:129
void DrawBbox() const
Draw main bounding box of the model.
Definition: 3d_model.cpp:492
glm::vec3 m_pos
Definition: 3d_model.h:119
std::vector< MATERIAL > m_materials
Definition: 3d_model.h:143
static const wxChar * m_logTrace
Definition: 3d_model.h:106
bool HasTransparentMeshes() const
Return true if have transparent mesh's to render.
Definition: 3d_model.h:77
MATERIAL_MODE
Render 3d model shape materials mode.
Definition: 3d_enums.h:118
bool m_have_opaque_meshes
Definition: 3d_model.h:148
void DrawBboxes() const
Draw individual bounding boxes of each mesh.
Definition: 3d_model.cpp:511
glm::vec2 m_tex_uv
Definition: 3d_model.h:123
MATERIAL_MODE m_materialMode
Definition: 3d_model.h:111
GLenum m_index_buffer_type
Definition: 3d_model.h:130
bool IsTransparent() const
Definition: 3d_model.h:140
void DrawOpaque(bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) const
Render the model into the current context.
Definition: 3d_model.h:55
static void EndDrawMulti()
Cleanup render states after drawing multiple models.
Definition: 3d_model.cpp:401
MODEL_3D(const S3DMODEL &a3DModel, MATERIAL_MODE aMaterialMode)
Load a 3D model.
Definition: 3d_model.cpp:90
glm::u8vec4 m_cad_color
Definition: 3d_model.h:122
void DrawTransparent(float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) const
Render the model into the current context.
Definition: 3d_model.h:63
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
std::vector< BBOX_3D > m_meshes_bbox
individual bbox for each mesh
Definition: 3d_model.h:114
void Draw(bool aTransparent, float aOpacity, bool aUseSelectedMaterial, SFVEC3F &aSelectionColor) const
Definition: 3d_model.cpp:414
const BBOX_3D & GetBBox() const
Get the main bounding box.
Definition: 3d_model.h:93
glm::u8vec4 m_color
Definition: 3d_model.h:121
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
GLenum m_bbox_index_buffer_type
Definition: 3d_model.h:158
static void BeginDrawMulti(bool aUseColorInformation)
Set some basic render states before drawing multiple models.
Definition: 3d_model.cpp:385
unsigned int m_render_idx_buffer_offset
Definition: 3d_model.h:136
BBOX_3D m_model_bbox
global bounding box for this model
Definition: 3d_model.h:113