3d_read_mesh.cpp 5.95 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4
 * Copyright (C) 2015 Mario Luzeiro <mrluzeiro@gmail.com>
5
 * Copyright (C) 2015 Jean-Pierre Charras, jp.charras@wanadoo.fr
6
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
7
 * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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
 */

27 28
/**
 * @file 3d_read_mesh.cpp
29
 */
plyatov's avatar
plyatov committed
30

31 32 33 34
#include <fctsys.h>
#include <common.h>
#include <macros.h>
#include <kicad_string.h>
35
#include <pgm_base.h>
36
#define GLM_FORCE_RADIANS
37
#include <gal/opengl/glm/gtc/matrix_transform.hpp>
38
#include <3d_viewer.h>
jean-pierre charras's avatar
jean-pierre charras committed
39
#include <info3d_visu.h>
40 41
#include "3d_struct.h"
#include "modelparsers.h"
jean-pierre charras's avatar
jean-pierre charras committed
42

43

44
S3D_MODEL_PARSER *S3D_MODEL_PARSER::Create( S3D_MASTER* aMaster,
45
                                            const wxString aExtension )
46 47
{
    if ( aExtension == wxT( "x3d" ) )
48
        return new X3D_MODEL_PARSER( aMaster );
49
    else if ( aExtension == wxT( "wrl" ) )
50
        return new VRML_MODEL_PARSER( aMaster );
51 52 53

    return NULL;
 }
plyatov's avatar
plyatov committed
54

55

56
int S3D_MASTER::ReadData( S3D_MODEL_PARSER* aParser )
57
{    
dickelbeck's avatar
dickelbeck committed
58
    if( m_Shape3DName.IsEmpty() )
59 60 61 62
    {
        //DBG( printf("m_Shape3DName.IsEmpty") );
        return -1;
    }
dickelbeck's avatar
dickelbeck committed
63

64 65 66 67 68 69 70 71 72 73
    if( m_Shape3DFullFilename.IsEmpty() )
    {
        //DBG( printf("m_Shape3DFullFilename.IsEmpty") );
        return -1;
    }

    if( aParser == NULL )
        return -1;

    wxString filename = m_Shape3DFullFilename;
74

75
#ifdef __WINDOWS__
76
    filename.Replace( wxT( "/" ), wxT( "\\" ) );
77
#else
78
    filename.Replace( wxT( "\\" ), wxT( "/" ) );
79 80
#endif

81
    if( wxFileName::FileExists( filename ) )
82
    {
83
        wxFileName fn( filename );
84

85 86 87 88 89
        if( aParser->Load( filename ) )
        {
            // Invalidate bounding boxes
            m_fastAABBox.Reset();
            m_BBox.Reset();
90

91
            m_parser = aParser;
92

93 94
            return 0;
        }
dickelbeck's avatar
dickelbeck committed
95
    }
96

97 98 99 100
    wxLogDebug( wxT( "3D shape '%s' not found, even tried '%s' after env var substitution." ),
        GetChars( m_Shape3DName ),
        GetChars( filename ) );

dickelbeck's avatar
dickelbeck committed
101
    return -1;
plyatov's avatar
plyatov committed
102
}
103

104

105 106 107 108 109 110 111 112 113 114
void S3D_MASTER::Render( bool aIsRenderingJustNonTransparentObjects,
                         bool aIsRenderingJustTransparentObjects )
{
    if( m_parser == NULL )
        return;
    
    double aVrmlunits_to_3Dunits = g_Parm_3D_Visu.m_BiuTo3Dunits * UNITS3D_TO_UNITSPCB;

    glScalef( aVrmlunits_to_3Dunits, aVrmlunits_to_3Dunits, aVrmlunits_to_3Dunits );

115 116 117
    glTranslatef( m_MatPosition.x * SCALE_3D_CONV,
                  m_MatPosition.y * SCALE_3D_CONV,
                  m_MatPosition.z * SCALE_3D_CONV );
118

119 120 121
    glRotatef( -m_MatRotation.z, 0.0f, 0.0f, 1.0f );
    glRotatef( -m_MatRotation.y, 0.0f, 1.0f, 0.0f );
    glRotatef( -m_MatRotation.x, 1.0f, 0.0f, 0.0f );
122

123
    glScalef( m_MatScale.x, m_MatScale.y, m_MatScale.z );
124

125 126 127 128
    for( unsigned int idx = 0; idx < m_parser->childs.size(); idx++ )
        m_parser->childs[idx]->openGL_RenderAllChilds( aIsRenderingJustNonTransparentObjects,
                                                       aIsRenderingJustTransparentObjects );
}
129 130


131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
CBBOX &S3D_MASTER::getBBox( )
{
    if( !m_BBox.IsInitialized() )
        calcBBox();

    return m_BBox;
}


CBBOX &S3D_MASTER::getFastAABBox( )
{
    if( !m_fastAABBox.IsInitialized() )
        calcBBox();
    
    return m_fastAABBox;
}


void S3D_MASTER::calcBBox()
{
    if( m_parser == NULL )
        return;
153

154 155
    bool firstBBox = true;
    
156
    for( unsigned int idx = 0; idx < m_parser->childs.size(); idx++ )
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        if( firstBBox )
        {
            firstBBox = false;
            m_BBox = m_parser->childs[idx]->getBBox();
        }
        else
            m_BBox.Union( m_parser->childs[idx]->getBBox() );

    // Calc transformation matrix to apply in AABBox

    float aVrmlunits_to_3Dunits = g_Parm_3D_Visu.m_BiuTo3Dunits * UNITS3D_TO_UNITSPCB;

    glm::mat4 fullTransformMatrix;

    fullTransformMatrix = glm::scale( glm::mat4(), S3D_VERTEX(  aVrmlunits_to_3Dunits,
                                                                aVrmlunits_to_3Dunits,
                                                                aVrmlunits_to_3Dunits ) );

    fullTransformMatrix = glm::translate( fullTransformMatrix,  S3D_VERTEX( m_MatPosition.x * SCALE_3D_CONV,
                                                                            m_MatPosition.y * SCALE_3D_CONV,
                                                                            m_MatPosition.z * SCALE_3D_CONV) );
    
    if( m_MatRotation.z != 0.0 )
180
        fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians(-(float)m_MatRotation.z), S3D_VERTEX( 0.0f, 0.0f, 1.0f ) );
181
    if( m_MatRotation.y != 0.0 )
182
        fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians(-(float)m_MatRotation.y), S3D_VERTEX( 0.0f, 1.0f, 0.0f ) );
183
    if( m_MatRotation.x != 0.0 )
184
        fullTransformMatrix = glm::rotate( fullTransformMatrix, glm::radians(-(float)m_MatRotation.x), S3D_VERTEX( 1.0f, 0.0f, 0.0f ) );
185 186 187 188 189 190 191

     fullTransformMatrix = glm::scale( fullTransformMatrix, S3D_VERTEX( m_MatScale.x, m_MatScale.y, m_MatScale.z ) );
    
    // Apply transformation
    m_fastAABBox = m_BBox;
    m_fastAABBox.ApplyTransformationAA( fullTransformMatrix );
}