Files
TestEGr/TestEgrView.cpp
T
Dario Sassi 3231a86583 TestEGr :
- spostata classe Scene nella libreria EgtGraphics.
2014-02-13 18:19:13 +00:00

320 lines
9.0 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 "resource.h"
#include "/EgtDev/Include/EgtILogger.h"
#include "/EgtDev/Include/EgnStringUtils.h"
#include "/EgtDev/Include/EgnStringConverter.h"
#include "/EgtDev/Include/EGrScene.h"
//----------------------------------------------------------------------------
#ifdef _DEBUG
#define new DEBUG_NEW
#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,
IGeomDB* pGeomDB, ILogger* pLogger)
{
if ( m_pScene == nullptr)
return false ;
// creazione della scena
m_pScene->Init( pGeomDB, pLogger) ;
if ( ! m_pScene->CreateContext( GetSafeHdc(), nDriver, b2Buff, nColorBits, nDepthBits))
return false ;
// impostazione dati di vista
m_pScene->SetCamera( CT_ISO_SW) ;
m_pScene->ZoomAll() ;
LOG_INFO( pLogger, m_pScene->GetOpenGLInfo().c_str())
LOG_INFO( pLogger, m_pScene->GetGLSLInfo().c_str())
LOG_INFO( pLogger, m_pScene->GetPixelFormatInfo().c_str())
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 ;
// ridisegno finestra OpenGL
m_pScene->Draw() ;
ValidateRgn( NULL) ;
}
//----------------------------------------------------------------------------
void
TestEgrView::Zoom( UINT nID)
{
if ( m_pScene == nullptr)
return ;
const double COEFF_PLUS = 0.9 ;
const double COEFF_MINUS = 1 / COEFF_PLUS ;
switch ( nID) {
case IDC_ZOOM_ALL :
m_pScene->ZoomAll() ;
break ;
case IDC_ZOOM_P :
m_pScene->ZoomChange( COEFF_PLUS) ;
break ;
case IDC_ZOOM_M :
m_pScene->ZoomChange( COEFF_MINUS) ;
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_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)
{
if ( m_nStatus == ST_ROT || m_nStatus == ST_PAN) {
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à ROTATE
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)
{
// 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)) ;
}