//---------------------------------------------------------------------------- // 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 "/EgtDev/Include/EgtNumUtils.h" #include using namespace std ; //--------------------------- Constants -------------------------------------- const double MIN_SNAP_STEP = 0.1 ; //---------------------------------------------------------------------------- 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) { if ( dSnapStep > 0) m_dSnapStep = max( dSnapStep, MIN_SNAP_STEP) ; if ( nMinLineSstep > 0) m_nMinLineSstep = nMinLineSstep ; if ( nMajLineSstep > 0) m_nMajLineSstep = max( nMajLineSstep, m_nMinLineSstep) ; m_nExtSstep = max( abs( nExtSstep), m_nMajLineSstep) ; m_dGridMinX = -INFINITO ; m_dGridMaxX = INFINITO ; m_dGridMinY = -INFINITO ; m_dGridMaxY = INFINITO ; return true ; } //---------------------------------------------------------------------------- bool Scene::SetGridGeoAdv( double dSnapStep, int nMinLineSstep, int nMajLineSstep, double dXmin, double dXmax, double dYmin, double dYmax) { if ( dSnapStep > 0) m_dSnapStep = max( dSnapStep, MIN_SNAP_STEP) ; if ( nMinLineSstep > 0) m_nMinLineSstep = nMinLineSstep ; if ( nMajLineSstep > 0) m_nMajLineSstep = max( nMajLineSstep, m_nMinLineSstep) ; if ( dXmin > dXmax) swap( dXmin, dXmax) ; if ( dYmin > dYmax) swap( dYmin, dYmax) ; int nExtSstep = int( ceil( max( max( abs( dXmin), abs( dYmin)), max( abs( dXmax), abs( dYmax))) / m_dSnapStep)) ; m_nExtSstep = max( abs( nExtSstep), m_nMajLineSstep) ; m_dGridMinX = dXmin ; m_dGridMaxX = dXmax ; m_dGridMinY = dYmin ; m_dGridMaxY = dYmax ; return true ; } //---------------------------------------------------------------------------- bool Scene::SetGridColor( Color colMinLine, Color colMajLine) { m_colMinLine = colMinLine ; m_colMajLine = colMajLine ; return true ; } //---------------------------------------------------------------------------- static int GetSpecialCoeff( double dVal) { if ( dVal < 1.99) return 1 ; else if ( dVal < 4.99) return 2 ; else if ( dVal < 9.99) return 5 ; else if ( dVal < 24.99) return 10 ; else if ( dVal < 49.99) return 25 ; else if ( dVal < 99.99) return 50 ; else return 100 ; } //---------------------------------------------------------------------------- bool Scene::DrawGrid( void) { // se richista griglia avanzata ... if ( m_nGridFrame_Flag != 1) return DrawRectangularGrid() ; // 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) ; float dExtMinX = max( float( m_dGridMinX), -dExt) ; float dExtMaxX = min( float( m_dGridMaxX), dExt) ; float dExtMinY = max( float( m_dGridMinY), -dExt) ; float dExtMaxY = min( float( m_dGridMaxY), dExt) ; // 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), se non troppe double dMinRapp = min( m_dHalfWidth, m_dHalfHeight) / ( m_dSnapStep * m_nMinLineSstep) ; if ( dMinRapp < 60) { 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 + if ( dPos >= dExtMinX && dPos <= dExtMaxX) { glVertex3f( dPos, dExtMinY, Z_DOWN) ; glVertex3f( dPos, dExtMaxY, Z_DOWN) ; } // X - if ( -dPos >= dExtMinX && -dPos <= dExtMaxX) { glVertex3f( -dPos, dExtMinY, Z_DOWN) ; glVertex3f( -dPos, dExtMaxY, Z_DOWN) ; } // Y + if ( dPos >= dExtMinY && dPos <= dExtMaxY) { glVertex3f( dExtMinX, dPos, Z_DOWN) ; glVertex3f( dExtMaxX, dPos, Z_DOWN) ; } // Y - if ( -dPos >= dExtMinY && -dPos <= dExtMaxY) { glVertex3f( dExtMinX, -dPos, Z_DOWN) ; glVertex3f( dExtMaxX, -dPos, Z_DOWN) ; } } } glEnd() ; glDisable( GL_LINE_STIPPLE) ; } // disegno linee maggiori double dMaxRapp = min( m_dHalfWidth, m_dHalfHeight) / ( m_dSnapStep * m_nMajLineSstep) ; int nCoeff = GetSpecialCoeff( 0.06 * dMaxRapp) ; glBegin( GL_LINES) ; glColor3f( m_colMajLine.GetRed(), m_colMajLine.GetGreen(), m_colMajLine.GetBlue()) ; for ( int i = min( nCoeff * m_nMajLineSstep, m_nExtSstep) ; i <= m_nExtSstep ; i += nCoeff * m_nMajLineSstep) { float dPos = float( i * m_dSnapStep) ; // X + if ( dPos >= dExtMinX && dPos <= dExtMaxX) { glVertex3f( dPos, dExtMinY, Z_DOWN) ; glVertex3f( dPos, dExtMaxY, Z_DOWN) ; } // X - if ( -dPos >= dExtMinX && -dPos <= dExtMaxX) { glVertex3f( -dPos, dExtMinY, Z_DOWN) ; glVertex3f( -dPos, dExtMaxY, Z_DOWN) ; } // Y + if ( dPos >= dExtMinY && dPos <= dExtMaxY) { glVertex3f( dExtMinX, dPos, Z_DOWN) ; glVertex3f( dExtMaxX, dPos, Z_DOWN) ; } // Y - if ( -dPos >= dExtMinY && -dPos <= dExtMaxY) { glVertex3f( dExtMinX, -dPos, Z_DOWN) ; glVertex3f( dExtMaxX, -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+ if ( 0 >= dExtMinY && 0 <= dExtMaxY && 0 >= dExtMinY) { glColor3f( 0.8f, 0, 0) ; glVertex3f( max( 0.0f, dExtMinX), 0, Z_DOWN) ; glVertex3f( dExtMaxX, 0, Z_DOWN) ; } // asse Y+ if ( 0 >= dExtMinX && 0 <= dExtMaxX && 0 <= dExtMaxY) { glColor3f( 0, 0.8f, 0) ; glVertex3f( 0, max( 0.0f, dExtMinY), Z_DOWN) ; glVertex3f( 0, dExtMaxY, Z_DOWN) ; } // asse Z+ if ( 0 >= dExtMinX && 0 <= dExtMaxX && 0 >= dExtMinY && 0 <= dExtMaxY) { 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()) ; if ( 0 >= dExtMinY && 0 <= dExtMaxY && 0 >= dExtMinX) { glVertex3f( min( 0.0f, dExtMaxX), 0, Z_DOWN) ; glVertex3f( dExtMinX, 0, Z_DOWN) ; } if ( 0 >= dExtMinX && 0 <= dExtMaxX && 0 >= dExtMinY) { glVertex3f( 0, min( 0.0f, dExtMaxY), Z_DOWN) ; glVertex3f( 0, dExtMinY, 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 if ( 0 >= dExtMinY && 0 <= dExtMaxY) { glVertex3f( dExtMinX, 0, Z_DOWN) ; glVertex3f( dExtMaxX, 0, Z_DOWN) ; } // asse Y if ( 0 >= dExtMinX && 0 <= dExtMaxX) { glVertex3f( 0, dExtMinY, Z_DOWN) ; glVertex3f( 0, dExtMaxY, Z_DOWN) ; } glEnd() ; } // ripristino lo stack delle matrici glPopMatrix() ; // ripristino spessore linee glLineWidth( (float) GetLineWidth()) ; return true ; } //---------------------------------------------------------------------------- bool Scene::DrawRectangularGrid() { // se non devo disegnare, esco subito if ( ! m_bShowGrid && ! m_bShowFrame) return true ; // controllo dimensione del rettangolo if ( m_dGridWidth < m_dSnapStep || m_dGridHeight < m_dSnapStep) return true ; // controllo dove devo posizionare l'origine degli assi bool bStandardOrig = m_nGridFrame_Flag == 2 ? true : false ; // 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_y = ( float) floor( m_dGridHeight * ( bStandardOrig ? 0.5 : 1.0)) ; float dExt_x = ( float) floor( m_dGridWidth * ( bStandardOrig ? 0.5 : 1.0)) ; int nX_Step = ( int) floor( dExt_x / m_dSnapStep) ; int nY_Step = ( int) floor( dExt_y / 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), se non troppe double dMinRapp = min( m_dHalfWidth, m_dHalfHeight) / ( m_dSnapStep * m_nMinLineSstep) ; if ( dMinRapp < 60) { 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 <= nX_Step ; i += m_nMinLineSstep) { if (( i % m_nMajLineSstep) != 0) { float dPos = float( i * m_dSnapStep) ; // X + glVertex3f( dPos, bStandardOrig ? - dExt_y : 0.0, Z_DOWN) ; glVertex3f( dPos, dExt_y, Z_DOWN) ; if ( bStandardOrig) { // X - glVertex3f( - dPos, - dExt_y, Z_DOWN) ; glVertex3f( - dPos, dExt_y, Z_DOWN) ; } } } for ( int i = m_nMinLineSstep ; i <= nY_Step ; i += m_nMinLineSstep) { if (( i % m_nMajLineSstep) != 0) { float dPos = float( i * m_dSnapStep) ; // Y + glVertex3f( bStandardOrig ? - dExt_x : 0.0, dPos, Z_DOWN) ; glVertex3f( dExt_x, dPos, Z_DOWN) ; if ( bStandardOrig) { // Y - glVertex3f( - dExt_x, - dPos, Z_DOWN) ; glVertex3f( dExt_x, - dPos, Z_DOWN) ; } } } glEnd() ; glDisable( GL_LINE_STIPPLE) ; } // disegno linee maggiori double dMaxRapp = min( m_dHalfWidth, m_dHalfHeight) / ( m_dSnapStep * m_nMajLineSstep) ; int nCoeff = GetSpecialCoeff( 0.06 * dMaxRapp) ; glBegin( GL_LINES) ; glColor3f( m_colMajLine.GetRed(), m_colMajLine.GetGreen(), m_colMajLine.GetBlue()) ; for ( int i = min( nCoeff * m_nMajLineSstep, ( int)dExt_x) ; i <= ( int)nX_Step ; i += nCoeff * m_nMajLineSstep) { float dPos = float( i * m_dSnapStep) ; // X + glVertex3f( dPos, bStandardOrig ? - dExt_y : 0.0, Z_DOWN) ; glVertex3f( dPos, dExt_y, Z_DOWN) ; if ( bStandardOrig) { // X - glVertex3f( - dPos, - dExt_y, Z_DOWN) ; glVertex3f( - dPos, dExt_y, Z_DOWN) ; } } for ( int i = min( nCoeff * m_nMajLineSstep, ( int)dExt_y) ; i <= ( int)nY_Step ; i += nCoeff * m_nMajLineSstep) { float dPos = float( i * m_dSnapStep) ; // Y + glVertex3f( bStandardOrig ? - dExt_x : 0.0, dPos, Z_DOWN) ; glVertex3f( dExt_x, dPos, Z_DOWN) ; if ( bStandardOrig) { // Y - glVertex3f( - dExt_x, - dPos, Z_DOWN) ; glVertex3f( dExt_x, - 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( m_cColX.GetRed(), m_cColX.GetGreen(), m_cColX.GetBlue()) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( dExt_x, 0, Z_DOWN) ; // asse Y+ glColor3f( m_cColY.GetRed(), m_cColY.GetGreen(), m_cColY.GetBlue()) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( 0, dExt_y, Z_DOWN) ; // asse Z+ glColor3f( m_cColZ.GetRed(), m_cColZ.GetGreen(), m_cColZ.GetBlue()) ; 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( bStandardOrig ? - dExt_x : 0.0, 0, Z_DOWN) ; glVertex3f( 0, 0, Z_DOWN) ; glVertex3f( 0, bStandardOrig ? - dExt_y : 0.0, Z_DOWN) ; glLineWidth( (float) GetGridLineWidth()) ; } 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( bStandardOrig ? - dExt_x : 0.0, 0, Z_DOWN) ; glVertex3f( dExt_x, 0, Z_DOWN) ; // asse Y glVertex3f( 0, bStandardOrig ? - dExt_y : 0.0, Z_DOWN) ; glVertex3f( 0, dExt_y, Z_DOWN) ; glEnd() ; } // ripristino lo stack delle matrici glPopMatrix() ; // ripristino spessore linee glLineWidth(( float) GetLineWidth()) ; return true ; } //---------------------------------------------------------------------------- bool Scene::SetGlobGridParameters( int nPosFlag, double dLenX, double dLenY) { // controllo validit� dei parametri... if ( dLenX < GRID_MIN_LEN_X || dLenY < GRID_MIN_LEN_Y || nPosFlag < 0 || nPosFlag > 3 ) { m_bShowFrame = false ; m_bShowGrid = false ; return false ; } if ( nPosFlag == 0) { m_bShowFrame = false ; m_bShowGrid = false ; return true ; } // setto i parametri membro m_dGridWidth = dLenX ; m_dGridHeight = dLenY ; m_nGridFrame_Flag = nPosFlag ; return true ; } //---------------------------------------------------------------------------- void Scene::GetGridParam( double& dSnapStep, int& nExtStep) const { dSnapStep = m_dSnapStep ; nExtStep = m_nExtSstep ; } //---------------------------------------------------------------------------- bool Scene::GetShowGrid() { return m_bShowGrid ; }