KiCad PCB EDA Suite
c_ogl_3dmodel.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 <olegendo@gcc.gnu.org>
5  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
6  * Copyright (C) 1992-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 
31 #ifndef _C_OGL_3DMODEL_H_
32 #define _C_OGL_3DMODEL_H_
33 
34 #include <vector>
35 #include <plugins/3dapi/c3dmodel.h>
36 #include "../../common_ogl/openGL_includes.h"
37 #include "../3d_render_raytracing/shapes3D/cbbox.h"
38 #include "../../3d_enums.h"
39 
42 {
43 public:
49  C_OGL_3DMODEL( const S3DMODEL &a3DModel, MATERIAL_MODE aMaterialMode );
50 
52 
56  void Draw_opaque( bool aUseSelectedMaterial, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const { Draw( false, 1.0f, aUseSelectedMaterial, aSelectionColor ); }
57 
61  void Draw_transparent( float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const { Draw( true, aOpacity, aUseSelectedMaterial, aSelectionColor ); }
62 
66  bool Have_opaque() const { return m_have_opaque_meshes; }
67 
72 
76  void Draw_bbox() const;
77 
81  void Draw_bboxes() const;
82 
87  const CBBOX &GetBBox() const { return m_model_bbox; }
88 
92  static void BeginDrawMulti( bool aUseColorInformation );
93 
97  static void EndDrawMulti();
98 
99 private:
100  static const wxChar *m_logTrace;
101 
102  // the material mode that was used to generate the rendering data.
103  // FIXME: this can be selected at run-time and does not require re-creation
104  // of the whole model objects.
106 
108  std::vector<CBBOX> m_meshes_bbox;
109 
110  // unified vertex format for mesh rendering.
111  struct VERTEX
112  {
113  glm::vec3 m_pos;
114  glm::u8vec4 m_nrm; // only 3 components used
115  glm::u8vec4 m_color; // regular color
116  glm::u8vec4 m_cad_color; // "CAD" mode rendering color
117  glm::vec2 m_tex_uv;
118  };
119 
120  // vertex buffer and index buffers that include all the individual meshes
121  // lumped together.
122  GLuint m_vertex_buffer = 0;
123  GLuint m_index_buffer = 0;
124  GLenum m_index_buffer_type = GL_INVALID_ENUM;
125 
126  // internal material definition
127  // all meshes are grouped by material for rendering purposes.
129  {
130  unsigned int m_render_idx_buffer_offset = 0;
131  unsigned int m_render_idx_count = 0;
132 
133  MATERIAL( const SMATERIAL &aOther ) : SMATERIAL( aOther ) { }
134  bool IsTransparent() const { return m_Transparency > FLT_EPSILON; }
135  };
136 
137  std::vector<MATERIAL> m_materials;
138 
139  // a model can consist of transparent and opaque parts. remember which
140  // ones are present during initial buffer and data setup. use it later
141  // during rendering.
142  bool m_have_opaque_meshes = false;
144 
145  // vertex buffer and index buffer for the bounding boxes.
146  // the first box is always the outer box, followed by inner boxes (one for each mesh).
147  static constexpr unsigned int bbox_vtx_count = 8;
148  static constexpr unsigned int bbox_idx_count = 24;
149 
152  GLenum m_bbox_index_buffer_type = GL_INVALID_ENUM;
153 
154  static void MakeBbox( const CBBOX &aBox, unsigned int aIdxOffset,
155  VERTEX *aVtxOut, GLuint *aIdxOut,
156  const glm::vec4 &aColor );
157 
158  void Draw( bool aTransparent, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor ) const;
159 };
160 
161 #endif // _C_OGL_3DMODEL_H_
define an internal structure to be used by the 3D renders
static void EndDrawMulti()
EndDrawMulti - cleanup render states after drawing multiple models.
void Draw_transparent(float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) const
Draw_transparent - render the model into the current context.
Definition: c_ogl_3dmodel.h:61
static constexpr unsigned int bbox_vtx_count
void Draw_opaque(bool aUseSelectedMaterial, SFVEC3F aSelectionColor=SFVEC3F(0.0f)) const
Draw_opaque - render the model into the current context.
Definition: c_ogl_3dmodel.h:56
static const wxChar * m_logTrace
C_OGL_3DMODEL(const S3DMODEL &a3DModel, MATERIAL_MODE aMaterialMode)
C_OGL_3DMODEL - Load a 3d model.
unsigned int m_render_idx_buffer_offset
MATERIAL_MODE m_material_mode
unsigned int m_render_idx_count
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition: c3dmodel.h:44
const CBBOX & GetBBox() const
GetBBox - Get main bbox.
Definition: c_ogl_3dmodel.h:87
static constexpr unsigned int bbox_idx_count
bool m_have_transparent_meshes
CBBOX m_model_bbox
global bounding box for this model
GLenum m_bbox_index_buffer_type
bool Have_opaque() const
Have_opaque - return true if have opaque meshs to render.
Definition: c_ogl_3dmodel.h:66
GLuint m_bbox_vertex_buffer
MATERIAL_MODE
Render 3d model shape materials mode.
Definition: 3d_enums.h:117
MATERIAL(const SMATERIAL &aOther)
GLenum m_index_buffer_type
bool Have_transparent() const
Have_transparent - return true if have transparent meshs to render.
Definition: c_ogl_3dmodel.h:71
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
void Draw_bboxes() const
Draw_bboxes - draw individual bounding boxes of each mesh.
GLuint m_vertex_buffer
GLuint m_bbox_index_buffer
static void BeginDrawMulti(bool aUseColorInformation)
BeginDrawMulti - set some basic render states before drawing multiple models.
std::vector< MATERIAL > m_materials
static void MakeBbox(const CBBOX &aBox, unsigned int aIdxOffset, VERTEX *aVtxOut, GLuint *aIdxOut, const glm::vec4 &aColor)
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
void Draw(bool aTransparent, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor) const
GLuint m_index_buffer
void Draw_bbox() const
Draw_bbox - draw main bounding box of the model.
bool IsTransparent() const
std::vector< CBBOX > m_meshes_bbox
individual bbox for each mesh
bool m_have_opaque_meshes
CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:39