Files
EgtGraphics/SceneGrid.cpp
Riccardo Elitropi 76d7496e2e EgtGraphics 2.4j1 :
- aggiunta funzione per esportazione della griglia.
2022-10-20 16:15:29 +02:00

214 lines
6.9 KiB
C++

//----------------------------------------------------------------------------
// 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 <algorithm>
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 = max( abs( nMajLineSstep), m_nMinLineSstep) ;
m_nExtSstep = max( abs( nExtSstep), m_nMajLineSstep) ;
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 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), 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 +
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
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 +
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& dSnapStep, int& nExtStep) const
{
dSnapStep = m_dSnapStep ;
nExtStep = m_nExtSstep ;
}
//----------------------------------------------------------------------------
bool
Scene::GetShowGrid()
{
return m_bShowGrid ;
}