//---------------------------------------------------------------------------- // EgalTech 2014-2014 //---------------------------------------------------------------------------- // File : SceneGrid.cpp Data : 25.10.14 Versione : 1.5j6 // Contenuto : Implementazione gestione e disegno griglia. // // // // Modifiche : 25.10.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "Scene.h" #include "EGrUtils.h" #include using namespace std ; //---------------------------------------------------------------------------- bool Scene::SetGridShow( bool bShowGrid, bool bShowFrame) { m_bShowGrid = bShowGrid ; m_bShowFrame = bShowFrame ; return true ; } //---------------------------------------------------------------------------- bool Scene::SetGridGeo( double dSnapStep, int nMinLineSstep, int nMajLineSstep, int nExtSstep) { const double MIN_SNAP_STEP = 0.1 ; m_dSnapStep = max( abs( dSnapStep), MIN_SNAP_STEP) ; m_nMinLineSstep = abs( nMinLineSstep) ; m_nMajLineSstep = abs( nMajLineSstep) ; m_nExtSstep = abs( nExtSstep) ; return true ; } //---------------------------------------------------------------------------- bool Scene::SetGridColor( Color colMinLine, Color colMajLine) { m_colMinLine = colMinLine ; m_colMajLine = colMajLine ; return true ; } //---------------------------------------------------------------------------- bool Scene::DrawGrid( void) { // se non devo disegnare, esco subito if ( ! m_bShowGrid && ! m_bShowFrame) return true ; // spessore linee glLineWidth( (float) GetGridLineWidth()) ; // imposto il riferimento glPushMatrix() ; double Matrix[OGLMAT_DIM] ; if ( FrameToOpenGlMatrix( GetGridFrame(), Matrix)) glMultMatrixd( Matrix) ; // semi-estensione della griglia float dExt = float( m_nExtSstep * m_dSnapStep) ; // abbassamento in Z per evitare sovrapposizioni con geometria const float Z_DOWN = float( - 0.01) ; // se richiesto, disegno la griglia if ( m_bShowGrid) { // disegno linee minori (a trattini) glLineStipple( 2, 0xAAAA) ; glEnable( GL_LINE_STIPPLE) ; glBegin( GL_LINES) ; glColor3f( m_colMinLine.GetRed(), m_colMinLine.GetGreen(), m_colMinLine.GetBlue()) ; for ( int i = m_nMinLineSstep ; i <= m_nExtSstep ; i += m_nMinLineSstep) { // se non si sovrappongono alle maggiori if ( ( i % m_nMajLineSstep) != 0) { float dPos = float( i * m_dSnapStep) ; // X + glVertex3f( dPos, - dExt, Z_DOWN) ; glVertex3f( dPos, dExt, Z_DOWN) ; // X - glVertex3f( - dPos, - dExt, Z_DOWN) ; glVertex3f( - dPos, dExt, Z_DOWN) ; // Y + glVertex3f( - dExt, dPos, Z_DOWN) ; glVertex3f( dExt, dPos, Z_DOWN) ; // Y - glVertex3f( - dExt, - dPos, Z_DOWN) ; glVertex3f( dExt, - dPos, Z_DOWN) ; } } glEnd() ; glDisable( GL_LINE_STIPPLE) ; // disegno linee maggiori glBegin( GL_LINES) ; glColor3f( m_colMajLine.GetRed(), m_colMajLine.GetGreen(), m_colMajLine.GetBlue()) ; for ( int i = m_nMajLineSstep ; i <= m_nExtSstep ; i += m_nMajLineSstep) { float dPos = float( i * m_dSnapStep) ; // X + glVertex3f( dPos, - dExt, Z_DOWN) ; glVertex3f( dPos, dExt, Z_DOWN) ; // X - glVertex3f( - dPos, - dExt, Z_DOWN) ; glVertex3f( - dPos, dExt, Z_DOWN) ; // Y + glVertex3f( - dExt, dPos, Z_DOWN) ; glVertex3f( dExt, dPos, Z_DOWN) ; // Y - glVertex3f( - dExt, - dPos, Z_DOWN) ; glVertex3f( dExt, - dPos, Z_DOWN) ; } glEnd() ; } // se richiesto, disegno assi del frame if ( m_bShowFrame) { // calcolo lunghezza asse Z GLint Viewport[4] ; glGetIntegerv(GL_VIEWPORT, Viewport) ; const double LEN_AX = 28 ; double dLenZ = LEN_AX * ( 2 * m_dHalfWidth) / Viewport[2] ; float fLenZ = float( min( m_dSnapStep, dLenZ)) ; glBegin( GL_LINES) ; // asse X+ glColor3f( 0.8f, 0, 0) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( dExt, 0, Z_DOWN) ; // asse Y+ glColor3f( 0, 0.8f, 0) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( 0, dExt, Z_DOWN) ; // asse Z+ glColor3f( 0, 0, 0.8f) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( 0, 0, fLenZ) ; // se richiesta griglia, assi X- e Y- if ( m_bShowGrid) { glColor3f( m_colMajLine.GetRed(), m_colMajLine.GetGreen(), m_colMajLine.GetBlue()) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( -dExt, 0, Z_DOWN) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( 0, -dExt, Z_DOWN) ; } glEnd() ; } // altrimenti, se richiesta sola griglia li disegno come linee principali else if ( m_bShowGrid) { glBegin( GL_LINES) ; glColor3f( m_colMajLine.GetRed(), m_colMajLine.GetGreen(), m_colMajLine.GetBlue()) ; // asse X glVertex3f( - dExt, 0, Z_DOWN) ; glVertex3f( dExt, 0, Z_DOWN) ; // asse Y glVertex3f( 0, - dExt, Z_DOWN) ; glVertex3f( 0, dExt, Z_DOWN) ; glEnd() ; } // ripristino lo stack delle matrici glPopMatrix() ; // ripristino spessore linee glLineWidth( (float) GetLineWidth()) ; return true ; } //---------------------------------------------------------------------------- void Scene::GetGridParam(double* pdSnapStep, int* pnExtStep) const { if ( pdSnapStep != nullptr) *pdSnapStep = m_dSnapStep ; if ( pnExtStep != nullptr) *pnExtStep = m_nExtSstep ; }