//---------------------------------------------------------------------------- // EgalTech 2014-2014 //---------------------------------------------------------------------------- // File : API_Scene.cpp Data : 01.09.14 Versione : 1.5i1 // Contenuto : Funzioni Scene per API. // // // // Modifiche : 01.09.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "API.h" #include "/EgtDev/Include/EInAPI.h" #include "/EgtDev/Include/EgtPointerOwner.h" using namespace std ; //----------------------------------------------------------------------------- BOOL __stdcall EgtInitScene( int nGseCtx, HWND hWnd, int nDriver, int b2Buff, int nColorBits, int nDepthBits) { // recupero il contesto GseContext* pGseCtx = GetGseContext( nGseCtx) ; // verifico GeomDB if ( pGseCtx == nullptr || pGseCtx->m_pGeomDB == nullptr) { LOG_ERROR( GetLogger(), "GeomDB invalid (EgtInitScene)") return FALSE ; } // inizializzazione scena OpenGL PtrOwner pScene( CreateEGrScene()) ; if ( IsNull( pScene)) return FALSE ; HDC hdc = GetDC( hWnd) ; if ( ! pScene->CreateContext( hdc, nDriver, ( b2Buff != 0), nColorBits, nDepthBits)) return FALSE ; pScene->SetBackground( WHITE, WHITE) ; pScene->SetShowMode( SM_SHADING) ; pScene->SetShowCurveDirection( false) ; pScene->SetCamera( CT_TOP) ; pScene->ZoomAll() ; pScene->SetWinRectAttribs( true, BLACK) ; pScene->SetMark( YELLOW) ; pScene->Init( pGseCtx->m_pGeomDB) ; // assegno la scena al contesto pGseCtx->m_hWnd = hWnd ; pGseCtx->m_pScene = Release( pScene) ; // log con info sulla scena string sSceneInfo = pGseCtx->m_pScene->GetOpenGLInfo() + '\n' + pGseCtx->m_pScene->GetGLSLInfo() + '\n' + pGseCtx->m_pScene->GetPixelFormatInfo() ; LOG_INFO( GetLogger(), sSceneInfo.c_str()) return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtSetBackground( int nGseCtx, int nTopRed, int nTopGreen, int nTopBlue, int nBottomRed, int nBottomGreen, int nBottomBlue) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtSetBackground)") return FALSE ; } // imposto lo sfondo pGseCtx->m_pScene->SetBackground( Color( nTopRed, nTopGreen, nTopBlue), Color( nBottomRed, nBottomGreen, nBottomBlue)) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtDraw( int nGseCtx) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtDraw)") return FALSE ; } // eseguo disegno pGseCtx->m_pScene->Draw() ; // valido la finestra disegnata ValidateRgn( pGseCtx->m_hWnd, NULL) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtResize( int nGseCtx, int nW, int nH) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtResize)") return FALSE ; } // eseguo resize pGseCtx->m_pScene->Resize( nW, nH) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtSetShowMode( int nGseCtx, int nShowMode, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtSetShowMode)") return FALSE ; } // imposto il modo di visualizzazione if ( nShowMode >= SM_WIREFRAME && nShowMode <= SM_SHADING) { pGseCtx->m_pScene->SetShowMode( nShowMode) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } else return FALSE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtSetShowCurveDirection( int nGseCtx, int nShow, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtSetShowCurveDirection)") return FALSE ; } // imposto stato pGseCtx->m_pScene->SetShowCurveDirection( nShow != 0) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtZoom( int nGseCtx, int nZoom, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtZoom)") return FALSE ; } const double COEFF_IN = 0.9 ; const double COEFF_OUT = 1 / COEFF_IN ; switch ( nZoom) { case 1 : pGseCtx->m_pScene->ZoomAll() ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; break ; case 2 : pGseCtx->m_pScene->ZoomChange( COEFF_IN) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; break ; case 3 : pGseCtx->m_pScene->ZoomChange( COEFF_OUT) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; break ; } return FALSE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtZoomOnPoint( int nGseCtx, int nWinX, int nWinY, double dCoeff, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtZoomOnPoint)") return FALSE ; } // eseguo zoom pGseCtx->m_pScene->ZoomOnPoint( Point3d( nWinX, nWinY), dCoeff) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtSetWinRect( int nGseCtx, int nPrevX, int nPrevY, int nCurrX, int nCurrY, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtSetWinRect)") return FALSE ; } // disegno finestra per zoom pGseCtx->m_pScene->SetWinRect( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtResetWinRect( int nGseCtx, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtResetWinRect)") return FALSE ; } // cancello finestra per zoom pGseCtx->m_pScene->ResetWinRect() ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtZoomWin( int nGseCtx, int nPrevX, int nPrevY, int nCurrX, int nCurrY, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtZoomWin)") return FALSE ; } // eseguo zoom su finestra pGseCtx->m_pScene->ZoomWin( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtSetView( int nGseCtx, int nView, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtSetView)") return FALSE ; } // imposto vista if ( nView >= CT_TOP && nView <= CT_ISO_NW) { pGseCtx->m_pScene->SetCamera( nView) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } else return FALSE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtPanCamera( int nGseCtx, int nPrevX, int nPrevY, int nCurrX, int nCurrY, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtPanCamera)") return FALSE ; } // eseguo panoramica pGseCtx->m_pScene->PanCamera( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtRotateCamera( int nGseCtx, int nPrevX, int nPrevY, int nCurrX, int nCurrY, BOOL bRedraw) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtRotateCamera)") return FALSE ; } // eseguo rotazione camera pGseCtx->m_pScene->RotateCamera( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ; if ( bRedraw) RedrawWindow( pGseCtx->m_hWnd, nullptr, nullptr, RDW_INVALIDATE) ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtGetCameraDir( int nGseCtx, int* pnDir) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtGetCameraDir)") return FALSE ; } // recupero direzione di vista if ( pnDir != nullptr) *pnDir = pGseCtx->m_pScene->GetCameraDir() ; return TRUE ; } //----------------------------------------------------------------------------- BOOL __stdcall EgtUnProjectPoint( int nGseCtx, int nWinX, int nWinY, double ptP[3]) { // verifico Scena GseContext* pGseCtx = GetGseContext( nGseCtx) ; if ( pGseCtx == nullptr || pGseCtx->m_pScene == nullptr) { LOG_ERROR( GetLogger(), "Scene invalid (EgtUnProjectPoint)") return FALSE ; } // eseguo l'inverso della proiezione (considero Z punto su centro) Point3d ptView( nWinX, nWinY, pGseCtx->m_pScene->GetProjectedCenter().z) ; Point3d ptWorld ; if ( ! pGseCtx->m_pScene->UnProject( ptView, ptWorld)) return 0 ; ptP[0] = ptWorld.x ; ptP[1] = ptWorld.y ; ptP[2] = ptWorld.z ; return TRUE ; }