2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-04-17 16:10:04 +00:00
|
|
|
* Copyright (C) 2020 Oleg Endo <olegendo@gcc.gnu.org>
|
2016-07-19 17:35:25 +00:00
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2024-03-03 18:11:42 +00:00
|
|
|
* Copyright (C) 2015-2024 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
#ifndef _MODEL_3D_H_
|
|
|
|
#define _MODEL_3D_H_
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-04-17 16:10:04 +00:00
|
|
|
#include <vector>
|
2016-07-19 17:35:25 +00:00
|
|
|
#include <plugins/3dapi/c3dmodel.h>
|
|
|
|
#include "../../common_ogl/openGL_includes.h"
|
2021-10-21 13:29:19 +00:00
|
|
|
#include "../raytracing/shapes3D/bbox_3d.h"
|
2024-03-03 18:11:42 +00:00
|
|
|
#include <3d_enums.h>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-06-03 01:29:09 +00:00
|
|
|
#include <wx/chartype.h>
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
class MODEL_3D
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2021-01-02 21:05:29 +00:00
|
|
|
* Load a 3D model.
|
2020-12-13 23:02:57 +00:00
|
|
|
*
|
|
|
|
* @note This must be called inside a gl context.
|
2021-01-02 21:05:29 +00:00
|
|
|
*
|
2020-12-13 23:02:57 +00:00
|
|
|
* @param a3DModel a 3d model data to load.
|
|
|
|
* @param aMaterialMode a mode to render the materials of the model.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2021-01-02 21:05:29 +00:00
|
|
|
MODEL_3D( const S3DMODEL& a3DModel, MATERIAL_MODE aMaterialMode );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
~MODEL_3D();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Render the model into the current context.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2021-01-05 22:19:00 +00:00
|
|
|
void DrawOpaque( bool aUseSelectedMaterial, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const
|
2020-12-13 23:02:57 +00:00
|
|
|
{
|
2023-03-02 22:29:08 +00:00
|
|
|
Draw( false, 1.0f, aUseSelectedMaterial, aSelectionColor, nullptr, nullptr );
|
2020-12-13 23:02:57 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Render the model into the current context.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2021-01-05 22:19:00 +00:00
|
|
|
void DrawTransparent( float aOpacity, bool aUseSelectedMaterial,
|
|
|
|
SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const
|
2020-12-13 23:02:57 +00:00
|
|
|
{
|
2023-03-02 22:29:08 +00:00
|
|
|
Draw( true, aOpacity, aUseSelectedMaterial, aSelectionColor, nullptr, nullptr );
|
2020-12-13 23:02:57 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2023-03-02 22:29:08 +00:00
|
|
|
/**
|
|
|
|
* Render the model into the current context.
|
|
|
|
* if aModelWorldMatrix and aCameraWorldPos is provided,
|
|
|
|
* it renders the material groups sorted.
|
|
|
|
*/
|
|
|
|
void Draw( bool aTransparent, float aOpacity, bool aUseSelectedMaterial,
|
|
|
|
const SFVEC3F& aSelectionColor,
|
|
|
|
const glm::mat4 *aModelWorldMatrix,
|
|
|
|
const SFVEC3F *aCameraWorldPos ) const;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Return true if have opaque meshes to render.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2021-01-05 22:19:00 +00:00
|
|
|
bool HasOpaqueMeshes() const { return m_have_opaque_meshes; }
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Return true if have transparent mesh's to render.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2021-01-05 22:19:00 +00:00
|
|
|
bool HasTransparentMeshes() const { return m_have_transparent_meshes; }
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-02-04 01:16:19 +00:00
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Draw main bounding box of the model.
|
2016-02-04 01:16:19 +00:00
|
|
|
*/
|
2021-01-05 22:19:00 +00:00
|
|
|
void DrawBbox() const;
|
2016-02-04 01:16:19 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Draw individual bounding boxes of each mesh.
|
2016-02-04 01:16:19 +00:00
|
|
|
*/
|
2021-01-05 22:19:00 +00:00
|
|
|
void DrawBboxes() const;
|
2016-02-04 01:16:19 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Get the main bounding box.
|
|
|
|
* @return the main model bounding box.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2021-01-02 21:05:29 +00:00
|
|
|
const BBOX_3D& GetBBox() const { return m_model_bbox; }
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-04-17 16:10:04 +00:00
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Set some basic render states before drawing multiple models.
|
2020-04-17 16:10:04 +00:00
|
|
|
*/
|
2020-09-02 16:22:12 +00:00
|
|
|
static void BeginDrawMulti( bool aUseColorInformation );
|
2020-04-17 16:10:04 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-13 23:02:57 +00:00
|
|
|
* Cleanup render states after drawing multiple models.
|
2020-04-17 16:10:04 +00:00
|
|
|
*/
|
|
|
|
static void EndDrawMulti();
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
private:
|
2021-01-02 21:05:29 +00:00
|
|
|
static const wxChar* m_logTrace;
|
2020-04-17 16:10:04 +00:00
|
|
|
|
|
|
|
// the material mode that was used to generate the rendering data.
|
|
|
|
// FIXME: this can be selected at run-time and does not require re-creation
|
|
|
|
// of the whole model objects.
|
2021-01-05 22:19:00 +00:00
|
|
|
MATERIAL_MODE m_materialMode;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D m_model_bbox; ///< global bounding box for this model
|
|
|
|
std::vector<BBOX_3D> m_meshes_bbox; ///< individual bbox for each mesh
|
2020-04-17 16:10:04 +00:00
|
|
|
|
|
|
|
// unified vertex format for mesh rendering.
|
|
|
|
struct VERTEX
|
|
|
|
{
|
|
|
|
glm::vec3 m_pos;
|
|
|
|
glm::u8vec4 m_nrm; // only 3 components used
|
|
|
|
glm::u8vec4 m_color; // regular color
|
|
|
|
glm::u8vec4 m_cad_color; // "CAD" mode rendering color
|
|
|
|
glm::vec2 m_tex_uv;
|
|
|
|
};
|
|
|
|
|
|
|
|
// vertex buffer and index buffers that include all the individual meshes
|
|
|
|
// lumped together.
|
|
|
|
GLuint m_vertex_buffer = 0;
|
|
|
|
GLuint m_index_buffer = 0;
|
|
|
|
GLenum m_index_buffer_type = GL_INVALID_ENUM;
|
|
|
|
|
|
|
|
// internal material definition
|
|
|
|
// all meshes are grouped by material for rendering purposes.
|
|
|
|
struct MATERIAL : SMATERIAL
|
|
|
|
{
|
|
|
|
unsigned int m_render_idx_buffer_offset = 0;
|
|
|
|
unsigned int m_render_idx_count = 0;
|
|
|
|
|
2023-03-02 22:29:08 +00:00
|
|
|
BBOX_3D m_bbox; ///< bounding box for this material group, used for transparent material ordering
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
MATERIAL( const SMATERIAL& aOther ) : SMATERIAL( aOther ) { }
|
2020-04-17 16:10:04 +00:00
|
|
|
bool IsTransparent() const { return m_Transparency > FLT_EPSILON; }
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<MATERIAL> m_materials;
|
|
|
|
|
|
|
|
// a model can consist of transparent and opaque parts. remember which
|
|
|
|
// ones are present during initial buffer and data setup. use it later
|
|
|
|
// during rendering.
|
|
|
|
bool m_have_opaque_meshes = false;
|
|
|
|
bool m_have_transparent_meshes = false;
|
|
|
|
|
|
|
|
// vertex buffer and index buffer for the bounding boxes.
|
|
|
|
// the first box is always the outer box, followed by inner boxes (one for each mesh).
|
|
|
|
static constexpr unsigned int bbox_vtx_count = 8;
|
|
|
|
static constexpr unsigned int bbox_idx_count = 24;
|
|
|
|
|
|
|
|
GLuint m_bbox_vertex_buffer = 0;
|
|
|
|
GLuint m_bbox_index_buffer = 0;
|
|
|
|
GLenum m_bbox_index_buffer_type = GL_INVALID_ENUM;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
static void MakeBbox( const BBOX_3D& aBox, unsigned int aIdxOffset, VERTEX* aVtxOut,
|
|
|
|
GLuint* aIdxOut, const glm::vec4& aColor );
|
2015-12-08 07:31:57 +00:00
|
|
|
};
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
#endif // _MODEL_3D_H_
|