Files
TestEGr/TestEGrView.cpp
T
Dario Sassi 15039d47c7 TestEgr :
- aggiunta gestione modalità di visualizzazione.
2014-04-04 09:42:02 +00:00

402 lines
11 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2013-2014
//----------------------------------------------------------------------------
// File : TestEGrView.cpp Data : 05.02.14 Versione : 1.5b3
// Contenuto : Implementazione della classe gestione vista OpenGL.
//
//
//
// Modifiche : 29.01.14 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "TestEGrView.h"
#include "TestEGrUtils.h"
#include "resource.h"
#include "/EgtDev/Include/EgtPerfCounter.h"
#include "/EgtDev/Include/EgtILogger.h"
#include "/EgtDev/Include/EgnStringUtils.h"
#include "/EgtDev/Include/EgnStringConverter.h"
#include "/EgtDev/Include/EGrScene.h"
using namespace std ;
//----------------------------------------------------------------------------
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//----------------------------------------------------------------------------
BEGIN_MESSAGE_MAP( TestEGrView, CWnd)
ON_WM_PAINT()
ON_WM_MBUTTONDOWN()
ON_WM_MBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_MOUSEWHEEL()
END_MESSAGE_MAP()
//----------------------------------------------------------------------------
TestEGrView::TestEGrView( void)
{
m_pDC = nullptr ;
m_pScene = nullptr ;
m_nStatus = ST_NULL ;
m_PrevPoint.SetPoint( 0, 0) ;
}
//----------------------------------------------------------------------------
TestEGrView::~TestEGrView( void)
{
if ( m_pScene != nullptr)
delete m_pScene ;
m_pScene = nullptr ;
if ( m_pDC != nullptr)
delete m_pDC ;
m_pDC = nullptr ;
}
//----------------------------------------------------------------------------
bool
TestEGrView::Create( CWnd* pParent, int nID)
{
if ( pParent == nullptr ||
pParent->GetDlgItem( nID) == nullptr)
return false ;
CString className = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
NULL, NULL, NULL) ;
CRect rectWin ;
pParent->GetDlgItem( nID)->GetWindowRect( rectWin) ;
pParent->ScreenToClient( rectWin) ;
if ( CreateEx( 0, className, L"TestEGrView",
WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, rectWin, pParent, nID) != 0) {
SetWindowPos( &wndTop, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE) ;
SetCursor( AfxGetApp()->LoadCursor( IDC_POINTER)) ;
m_pDC = new CClientDC( this) ;
m_pScene = CreateEGrScene() ;
return ( m_pDC != nullptr && m_pScene != nullptr) ;
}
else
return false ;
}
//----------------------------------------------------------------------------
bool
TestEGrView::StartScene( int nDriver, bool b2Buff, int nColorBits, int nDepthBits)
{
if ( m_pScene == nullptr)
return false ;
// creazione della scena
if ( ! m_pScene->CreateContext( GetSafeHdc(), nDriver, b2Buff, nColorBits, nDepthBits))
return false ;
// impostazione dati di vista
m_pScene->SetCamera( CT_ISO_SW) ;
m_pScene->ZoomAll() ;
return true ;
}
//----------------------------------------------------------------------------
bool
TestEGrView::GetSceneInfo( string& sInfo)
{
if ( m_pScene == nullptr)
return false ;
sInfo = m_pScene->GetOpenGLInfo() + '\n' +
m_pScene->GetGLSLInfo() + '\n' +
m_pScene->GetPixelFormatInfo() ;
return true ;
}
//----------------------------------------------------------------------------
void
TestEGrView::Destroy( void)
{
if ( m_pScene != nullptr)
m_pScene->Destroy() ;
DestroyWindow() ;
}
//----------------------------------------------------------------------------
bool
TestEGrView::Reshape( int nX, int nY, int nW, int nH)
{
// se non è ancora stata creata, esco subito
if ( m_pDC == nullptr || m_pScene == nullptr)
return true ;
MoveWindow( nX, nY, nW, nH, FALSE) ;
m_pScene->Reshape( nW, nH) ;
return true ;
}
//----------------------------------------------------------------------------
void
TestEGrView::OnPaint( void)
{
if ( m_pScene == nullptr)
return ;
PerformanceCounter Counter ;
// ridisegno finestra OpenGL
Counter.Start() ;
m_pScene->Draw() ;
ValidateRgn( NULL) ;
Counter.Stop() ;
// emetto info
string sOut = "Redraw time = " + ToString( Counter.GetTime(), 2) + " ms" ;
::OutInfo( sOut, false) ;
}
//----------------------------------------------------------------------------
void
TestEGrView::Redraw( void)
{
RedrawWindow() ;
}
//----------------------------------------------------------------------------
void
TestEGrView::ShowMode( UINT nID)
{
switch ( nID) {
case IDC_WIREFRAME :
m_pScene->SetShowMode( SM_WIREFRAME) ;
break ;
case IDC_HIDDENLINE :
m_pScene->SetShowMode( SM_HIDDENLINE) ;
break ;
case IDC_SHADING :
m_pScene->SetShowMode( SM_SHADING) ;
break ;
default :
return ;
break ;
}
RedrawWindow() ;
}
//----------------------------------------------------------------------------
void
TestEGrView::Zoom( UINT nID)
{
if ( m_pScene == nullptr)
return ;
const double COEFF_IN = 0.9 ;
const double COEFF_OUT = 1 / COEFF_IN ;
switch ( nID) {
case IDC_ZOOM_ALL :
m_pScene->ZoomAll() ;
break ;
case IDC_ZOOM_IN :
m_pScene->ZoomChange( COEFF_IN) ;
break ;
case IDC_ZOOM_OUT :
m_pScene->ZoomChange( COEFF_OUT) ;
break ;
default :
return ;
break ;
}
RedrawWindow() ;
}
//----------------------------------------------------------------------------
void
TestEGrView::View( UINT nID)
{
if ( m_pScene == nullptr)
return ;
switch ( nID) {
case IDC_VIEW_TOP :
m_pScene->SetCamera( CT_TOP) ;
break ;
case IDC_VIEW_FRONT :
m_pScene->SetCamera( CT_FRONT) ;
break ;
case IDC_VIEW_BACK :
m_pScene->SetCamera( CT_BACK) ;
break ;
case IDC_VIEW_LEFT :
m_pScene->SetCamera( CT_LEFT) ;
break ;
case IDC_VIEW_RIGHT :
m_pScene->SetCamera( CT_RIGHT) ;
break ;
case IDC_VIEW_ISO :
m_pScene->SetCamera( CT_ISO_SW) ;
break ;
default :
return ;
break ;
}
RedrawWindow() ;
}
//----------------------------------------------------------------------------
void
TestEGrView::OnMButtonDown( UINT nFlags, CPoint point)
{
if ( nFlags & MK_SHIFT) {
m_nStatus = ST_ZOOMWIN ;
SetCursor( AfxGetApp()->LoadCursor( IDC_ZOOMWIN)) ;
}
else if ( nFlags & MK_CONTROL) {
m_nStatus = ST_ROT ;
SetCursor( AfxGetApp()->LoadCursor( IDC_ROTATE)) ;
}
else {
m_nStatus = ST_PAN ;
SetCursor( AfxGetApp()->LoadCursor( IDC_PAN)) ;
}
// salvo il punto di riferimento
m_PrevPoint = point ;
}
//----------------------------------------------------------------------------
void
TestEGrView::OnMButtonUp( UINT nFlags, CPoint point)
{
// eseguo ZOOMWINDOWN
if ( m_nStatus == ST_ZOOMWIN) {
// annullo i dati del rettangolo di definizione della nuova vista
if ( m_pScene != nullptr)
m_pScene->ResetWinRect() ;
// eseguo lo zoom
if ( m_pScene != nullptr)
m_pScene->ZoomWin( Point3d( m_PrevPoint.x, m_PrevPoint.y), Point3d( point.x, point.y)) ;
// aggiorno il disegno
RedrawWindow() ;
}
// reset dello stato se non NULL
if ( m_nStatus != ST_NULL) {
m_nStatus = ST_NULL ;
SetCursor( AfxGetApp()->LoadCursor( IDC_POINTER)) ;
}
}
//----------------------------------------------------------------------------
void
TestEGrView::OnMouseMove( UINT nFlags, CPoint point)
{
// visualizzo le coordinate del puntatore
ShowCursorCoordinates( Point3d( point.x, point.y)) ;
// se in modalità ZOOMWINDOW
if ( m_nStatus == ST_ZOOMWIN && nFlags & MK_MBUTTON) {
SetCursor( AfxGetApp()->LoadCursor( IDC_ZOOMWIN)) ;
// aggiorno i dati del rettangolo di definizione della nuova vista
if ( m_pScene != nullptr)
m_pScene->SetWinRect( Point3d( m_PrevPoint.x, m_PrevPoint.y), Point3d( point.x, point.y)) ;
// aggiorno il disegno
RedrawWindow() ;
// il punto di riferimento deve rimanere quello originale
}
// se in modalità ROTATE
else if ( m_nStatus == ST_ROT && nFlags & MK_MBUTTON) {
SetCursor( AfxGetApp()->LoadCursor( IDC_ROTATE)) ;
// eseguo la rotazione
if ( m_pScene != nullptr)
m_pScene->RotateCamera( Point3d( m_PrevPoint.x, m_PrevPoint.y), Point3d( point.x, point.y)) ;
// aggiorno il disegno
RedrawWindow() ;
// salvo il punto di riferimento
m_PrevPoint = point ;
}
// se in modalità PAN
else if ( m_nStatus == ST_PAN && nFlags & MK_MBUTTON) {
SetCursor( AfxGetApp()->LoadCursor( IDC_PAN)) ;
// eseguo il pan
if ( m_pScene != nullptr)
m_pScene->PanCamera( Point3d( m_PrevPoint.x, m_PrevPoint.y), Point3d( point.x, point.y)) ;
// aggiorno il disegno
RedrawWindow() ;
// salvo il punto di riferimento
m_PrevPoint = point ;
}
// altrimenti reset dello stato e cursore standard
else {
SetCursor( AfxGetApp()->LoadCursor( IDC_POINTER)) ;
m_nStatus = ST_NULL ;
}
}
//----------------------------------------------------------------------------
BOOL
TestEGrView::OnMouseWheel( UINT nFlags, short zDelta, CPoint point)
{
// devo essere nello stato NULL
if ( m_nStatus != ST_NULL)
return 0 ;
// calcolo coefficiente
double dCoeff = 1 - 0.1 * abs( zDelta) / WHEEL_DELTA ;
if ( zDelta < 0)
dCoeff = 1 / dCoeff ;
// porto il punto in coordinate client
ScreenToClient( &point) ;
// eseguo lo zoom sul punto
if ( m_pScene != nullptr)
m_pScene->ZoomOnPoint( Point3d( point.x, point.y), dCoeff) ;
// aggiorno il disegno
RedrawWindow() ;
return 0 ;
}
//----------------------------------------------------------------------------
void
TestEGrView::ShowCursorCoordinates( const Point3d& ptWin)
{
if ( m_pScene == nullptr)
return ;
// trasformo il punto da coordinate view a coordinate mondo
Point3d ptView( ptWin.x, ptWin.y, m_pScene->GetProjectedCenter().z) ;
Point3d ptWorld ;
if ( ! m_pScene->UnProject( ptView, ptWorld))
return ;
// visualizzo le coordinate
std::string sCoord ;
switch ( m_pScene->GetCameraDir()) {
case CT_TOP :
case CT_BOTTOM :
sCoord = "X=" + ToString( ptWorld.x, 3) + " Y=" + ToString( ptWorld.y, 3) ;
break ;
case CT_FRONT :
case CT_BACK :
sCoord = "X=" + ToString( ptWorld.x, 3) + " Z=" + ToString( ptWorld.z, 3) ;
break ;
case CT_LEFT :
case CT_RIGHT :
sCoord = "Y=" + ToString( ptWorld.y, 3) + " Z=" + ToString( ptWorld.z, 3) ;
break ;
default :
sCoord = " " ;
break ;
}
GetParent()->GetDlgItem( IDC_COORD)->SetWindowText( stringtoW( sCoord)) ;
}