diff --git a/EGrScene.cpp b/EGrScene.cpp new file mode 100644 index 0000000..39667d7 --- /dev/null +++ b/EGrScene.cpp @@ -0,0 +1,767 @@ +//---------------------------------------------------------------------------- +// EgalTech 2013-2014 +//---------------------------------------------------------------------------- +// File : Scene.cpp Data : 03.02.14 Versione : 1.5b1 +// Contenuto : Implementazione della classe gestione scena. +// +// +// +// Modifiche : 29.01.14 DS Creazione modulo. +// +// +//---------------------------------------------------------------------------- + +//--------------------------- Include ---------------------------------------- +#include "stdafx.h" +#include "EGrScene.h" +#include "/EgtDev/Include/EGnStringUtils.h" +#include "/EgtDev/Include/EGkFrame3d.h" + +using namespace std ; + +//--------------------------- Constants -------------------------------------- +static const double MIN_DIST_CAMERA = 10 ; +static const double STD_DIST_CAMERA = 1000 ; +static const double MIN_EXTENSION = 100 ; + +//--------------------------- Structs ---------------------------------------- +struct Vert3f { + GLfloat x ; + GLfloat y ; + GLfloat z ; +} ; +#define SET_VERT3F( P, X, Y, Z) {P.x=X;P.y=Y;P.z=Z;} + + +//---------------------------------------------------------------------------- +EGrScene::EGrScene( void) +{ + // Context data + m_hDC = nullptr ; + m_hRC = nullptr ; + memset( &m_wglewc, 0, sizeof( WGLEWContext)) ; + memset( &m_glewc, 0, sizeof( GLEWContext)) ; + m_bNewWay = false ; + // Viewport + m_nViewportW = 0 ; + m_nViewportH = 0 ; + // View data + SetCenter( ORIG) ; + SetCamera( 0, 0) ; + // Geom data + SetExtension( BBox3d( -MIN_EXTENSION, -MIN_EXTENSION, -MIN_EXTENSION, + MIN_EXTENSION, MIN_EXTENSION, MIN_EXTENSION)) ; +} + +//---------------------------------------------------------------------------- +EGrScene::~EGrScene( void) +{ +} + +//---------------------------------------------------------------------------- +bool +EGrScene::CreateContext( HDC hDC, bool bGenDriver, bool bDouBuff, int nColorBits, int nDepthBits) +{ + // verifico validità Device Context + if ( hDC == nullptr) + return false ; + m_hDC = hDC ; + + // assegno formato pixel + PIXELFORMATDESCRIPTOR pfd ; + memset( &pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)) ; + pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR) ; + pfd.nVersion = 1 ; + pfd.dwFlags = ( bDouBuff ? PFD_DOUBLEBUFFER : 0) | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW ; + pfd.iPixelType = PFD_TYPE_RGBA ; + pfd.cColorBits = nColorBits ; + pfd.cDepthBits = nDepthBits ; + pfd.iLayerType = PFD_MAIN_PLANE ; + + int nPixelFormat = ChoosePixelFormat( m_hDC, &pfd) ; + if ( nPixelFormat == 0) + return false ; + if ( bGenDriver) { + int nPixFormGen = ChooseGenPixelFormat( nPixelFormat, bDouBuff, nColorBits, nDepthBits) ; + if ( nPixFormGen != 0) + nPixelFormat = nPixFormGen ; + } + if ( ! SetPixelFormat( m_hDC, nPixelFormat, &pfd)) + return false ; + + HGLRC tempContext = wglCreateContext( m_hDC) ; + wglMakeCurrent( m_hDC, tempContext) ; + + GLenum WglewInitResult ; + WglewInitResult = wglewInit() ; + + if ( WglewInitResult != GLEW_OK) { + AfxMessageBox( _T("WGLEW is not initialized!")) ; + } + + int attribs[] = { + WGL_CONTEXT_MAJOR_VERSION_ARB, 3, + WGL_CONTEXT_MINOR_VERSION_ARB, 0, + WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, + 0 + } ; + + if ( wglewIsSupported( "WGL_ARB_create_context") == 1) { + m_hRC = wglCreateContextAttribsARB( m_hDC, 0, attribs) ; + wglMakeCurrent( m_hDC, NULL) ; + wglDeleteContext( tempContext) ; + wglMakeCurrent( m_hDC, m_hRC) ; + } + else { + //It's not possible to make a GL 3.x context. Use the old style context (GL 2.1 and before) + m_hRC = tempContext; + } + + // verifico validità Rendering Context + if ( m_hRC == nullptr) + return false ; + + GLenum GlewInitResult ; + glewExperimental = GL_TRUE ; + GlewInitResult = glewInit() ; + + if ( GlewInitResult != GLEW_OK) { + AfxMessageBox( _T("GLEW is not initialized!")) ; + } + + // verifico se posso lavorare in modalità nuova + m_bNewWay = ( glewIsSupported( "GL_VERSION_3_0") == 1) ; + + return true ; +} + +//---------------------------------------------------------------------------- +int +EGrScene::ChooseGenPixelFormat( int nPfd, bool bDouBuff, int nColorBits, int nDepthBits) +{ + PIXELFORMATDESCRIPTOR pfdR ; + + + // verifica del pixel format proposto + int nTotPfd = DescribePixelFormat( m_hDC, nPfd, sizeof(PIXELFORMATDESCRIPTOR), &pfdR) ; + if ( pfdR.dwFlags & PFD_GENERIC_FORMAT && + ! ( pfdR.dwFlags & PFD_GENERIC_ACCELERATED)) + return nPfd ; + + // eventuale ricerca di un pixel format soddisfacente + int nIOpt = 0 ; + int nErrOpt = 1000 ; + for ( int nI = 1 ; nI <= nTotPfd ; nI ++) { + DescribePixelFormat( m_hDC, nI, sizeof(PIXELFORMATDESCRIPTOR), &pfdR) ; + if ( pfdR.dwFlags & PFD_GENERIC_FORMAT && + ! (pfdR.dwFlags & PFD_GENERIC_ACCELERATED) && + ( ! bDouBuff || pfdR.dwFlags & PFD_DOUBLEBUFFER)) { + int nErr = abs( nColorBits - pfdR.cColorBits) + + abs( nDepthBits - pfdR.cDepthBits) ; + if ( nErr < nErrOpt) { + nErrOpt = nErr ; + nIOpt = nI ; + } + } + } + + return nIOpt ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::MakeCurrent( void) +{ + // se RC della scena non è definito, errore + if ( m_hRC == nullptr) + return false ; + + // se RC della scena non è quello corrente, lo imposto + HGLRC hRC = wglGetCurrentContext() ; + if ( m_hRC != hRC) + return ( wglMakeCurrent( m_hDC, m_hRC) != 0) ; + + return true ; +} + +//---------------------------------------------------------------------------- +string +EGrScene::GetOpenGLInfo( void) +{ + string sInfo = "OpenGL " ; + + if ( MakeCurrent()) { + const char* p ; + if ( ( p = reinterpret_cast ( glGetString( GL_VERSION))) != nullptr) + sInfo += p ; + sInfo += " " ; + if ( ( p = reinterpret_cast ( glGetString( GL_VENDOR))) != nullptr) + sInfo += p ; + sInfo += " " ; + if ( ( p = reinterpret_cast ( glGetString( GL_RENDERER))) != nullptr) + sInfo += p ; + } + else + sInfo += "ERROR" ; + + return sInfo ; +} + +//---------------------------------------------------------------------------- +string +EGrScene::GetGLSLInfo( void) +{ + string sInfo = "GLSL " ; + + if ( MakeCurrent()) { + const char* p ; + if ( ( p = reinterpret_cast ( glGetString( GL_SHADING_LANGUAGE_VERSION))) != nullptr) + sInfo += p ; + else + sInfo += "NONE" ; + } + else + sInfo += "ERROR" ; + + return sInfo ; +} + +//---------------------------------------------------------------------------- +string +EGrScene::GetPixelFormatInfo( void) +{ + string sInfo = "PixFmt " ; + + if ( MakeCurrent()) { + int nPixFmt ; + PIXELFORMATDESCRIPTOR Pfd ; + if ( ( nPixFmt = GetPixelFormat( m_hDC)) > 0 && + DescribePixelFormat( m_hDC, nPixFmt, sizeof(PIXELFORMATDESCRIPTOR), &Pfd) > 0) { + // indice + sInfo += ToString( nPixFmt) ; + // tipo di driver + if ( ! ( Pfd.dwFlags & PFD_GENERIC_FORMAT)) + sInfo += " ICD" ; + else if ( Pfd.dwFlags & PFD_GENERIC_ACCELERATED) + sInfo += " MCD" ; + else + sInfo += " GEN" ; + // singolo o doppio buffer + sInfo += ( ( Pfd.dwFlags & PFD_DOUBLEBUFFER) ? " BUFF2" : " BUFF1") ; + // RGBA o COLORINDEX + sInfo += ( ( Pfd.iPixelType == PFD_TYPE_RGBA) ? " RGBA" : " CI") ; + // color bits + sInfo += ToString( Pfd.cColorBits) ; + // depth bits + sInfo += " Z" + ToString( Pfd.cDepthBits) ; + } + else + sInfo = "NO DESCRIBE" ; + } + else + sInfo += "ERROR" ; + + return sInfo ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::SetCenter( const Point3d& ptCenter) +{ + // assegno il punto + m_ptCenter = ptCenter ; + // invalido ExtView + m_bExtViewOk = false ; + return true ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::SetCamera( double dAngVertDeg, double dAngOrizzDeg, double dDist) +{ + // assegno la direzione e la distanza + m_vtDirCamera.FromSpherical( 1, dAngVertDeg, dAngOrizzDeg) ; + if ( dDist < EPS_SMALL) + m_dDistCamera = STD_DIST_CAMERA ; + else + m_dDistCamera = __max( dDist, MIN_DIST_CAMERA) ; + // invalido Up e ExtView + m_bUpOk = false ; + m_bExtViewOk = false ; + return true ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::SetCamera( CameraDir nDir, double dDist) +{ + const double ANG_VERT_ISOVIEW = 54.73561032 ; + double dAngVertDeg ; + double dAngOrizzDeg ; + + + switch ( nDir) { + case CT_TOP : + dAngVertDeg = 0 ; + dAngOrizzDeg = 0 ; + break ; + case CT_FRONT : + dAngVertDeg = 90 ; + dAngOrizzDeg = -90 ; + break ; + case CT_RIGHT : + dAngVertDeg = 90 ; + dAngOrizzDeg = 0 ; + break ; + case CT_BACK : + dAngVertDeg = 90 ; + dAngOrizzDeg = 90 ; + break ; + case CT_LEFT : + dAngVertDeg = 90 ; + dAngOrizzDeg = 180 ; + break ; + case CT_BOTTOM : + dAngVertDeg = 180 ; + dAngOrizzDeg = 0 ; + break ; + case CT_ISO_SW : + dAngVertDeg = ANG_VERT_ISOVIEW ; + dAngOrizzDeg = -135 ; + break ; + case CT_ISO_SE : + dAngVertDeg = ANG_VERT_ISOVIEW ; + dAngOrizzDeg = -45 ; + break ; + case CT_ISO_NE : + dAngVertDeg = ANG_VERT_ISOVIEW ; + dAngOrizzDeg = 45 ; + break ; + case CT_ISO_NW : + dAngVertDeg = ANG_VERT_ISOVIEW ; + dAngOrizzDeg = 135 ; + break ; + default : + return false ; + break ; + } + + return SetCamera( dAngVertDeg, dAngOrizzDeg, dDist) ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::CalcDirUp( void) +{ + // verifico se il calcolo è necessario + if ( m_bUpOk) + return true ; + // direzione di vista + Vector3d vtView = - m_vtDirCamera ; + // direzione perpendicolare giacente nel piano XY + Vector3d vtPerpXY = vtView ^ Z_AX ; + if ( ! vtPerpXY.Normalize()) { + if ( vtView.z > 0) + vtPerpXY = X_AX ; + else + vtPerpXY = - X_AX ; + } + // direzione Up + m_vtUp = vtPerpXY ^ vtView ; + m_bUpOk = m_vtUp.Normalize() ; + // invalido ExtView + m_bExtViewOk = false ; + return m_bUpOk ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::SetExtension( const BBox3d& b3Ext) +{ + if ( b3Ext.IsEmpty()) + return false ; + + m_b3ExtWorld = b3Ext ; + m_bExtViewOk = false ; + return true ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::CalcExtView( void) +{ + // verifico se il calcolo è necessario + if ( m_bExtViewOk && m_bUpOk) + return true ; + // calcolo direzione camera Up + if ( ! CalcDirUp()) + return false ; + // calcolo il riferimento di vista + Frame3d frView ; + Vector3d vtZ = m_vtDirCamera ; + Vector3d vtY = m_vtUp ; + Vector3d vtX = vtY ^ vtZ ; + if ( ! frView.Set( m_ptCenter + m_dDistCamera * vtZ, vtX, vtY, vtZ)) + return false ; + // calcolo l'estensione nel riferimento di vista + m_b3ExtView = m_b3ExtWorld ; + m_b3ExtView.ToLoc( frView) ; + m_bExtViewOk = true ; + return m_bExtViewOk ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::ZoomAll( void) +{ + // traslo il centro di vista nel centro del box + Point3d ptExtCent ; + if ( ! m_b3ExtWorld.GetCenter( ptExtCent)) + return false ; + m_ptCenter = ptExtCent ; + m_bExtViewOk = false ; + // calcolo estensione nel riferimento di vista + if ( ! CalcExtView()) + return false ; + // calcolo dati di ingombro + Point3d ptMin ; + Point3d ptMax ; + m_b3ExtView.GetMinMax( ptMin, ptMax) ; + m_dHalfWidth = 1.05 * max( fabs( ptMin.x), fabs( ptMax.x)) ; + m_dHalfHeight = 1.05 * max( fabs( ptMin.y), fabs( ptMax.y)) ; + CalcDimViewFromExtView() ; + m_dZNear = - ptMax.z - 100 ; + m_dZFar = - ptMin.z + 100 ; + return true ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::CalcDimViewFromExtView( void) +{ + Point3d ptMin ; + Point3d ptMax ; + m_b3ExtView.GetMinMax( ptMin, ptMax) ; + double dHalfWidth = 1.05 * max( fabs( ptMin.x), fabs( ptMax.x)) ; + double dHalfHeight = 1.05 * max( fabs( ptMin.y), fabs( ptMax.y)) ; + + return AdjustDimView( dHalfWidth, dHalfHeight) ; +} + +//---------------------------------------------------------------------------- +bool +EGrScene::AdjustDimView( double dHalfWidth, double dHalfHeight) +{ + // se non assegnate, recupero dimensioni vista + if ( m_nViewportW == 0) { + HWND hWnd = ::WindowFromDC( m_hDC) ; + RECT rect ; + if ( ! ::GetClientRect( hWnd, &rect)) + return false ; + m_nViewportW = rect.right ; + m_nViewportH = rect.bottom ; + } + + // adatto il rapporto tra le dimensioni calcolate a quello delle dimensioni della vista + double dAspect = m_nViewportH / ( double) m_nViewportW ; + if ( dAspect * dHalfWidth > dHalfHeight) { + m_dHalfWidth = dHalfWidth ; + m_dHalfHeight = dAspect * dHalfWidth ; + } + else { + m_dHalfWidth = dHalfHeight / dAspect ; + m_dHalfHeight = dHalfHeight ; + } + + return true ; +} + +//---------------------------------------------------------------------------- +void +EGrScene::Prepare( void) +{ + MakeCurrent() ; + + // imposto il colore dello sfondo + glClearColor( 0.0, 0.0, 1.0, 0.0) ; + + // eventuale ricalcolo parametri di vista + CalcDirUp() ; + + // do other preparations here + glMatrixMode( GL_PROJECTION) ; + glLoadIdentity() ; + glOrtho( - m_dHalfWidth, m_dHalfWidth, - m_dHalfHeight, m_dHalfHeight, m_dZNear, m_dZFar) ; + + GLenum nErr = glGetError() ; + + glMatrixMode( GL_MODELVIEW) ; + glLoadIdentity() ; + Point3d ptCamera = m_ptCenter + m_dDistCamera * m_vtDirCamera ; + gluLookAt( ptCamera.x, ptCamera.y, ptCamera.z, + m_ptCenter.x, m_ptCenter.y, m_ptCenter.z, + m_vtUp.x, m_vtUp.y, m_vtUp.z) ; + + nErr = glGetError() ; +} + +//---------------------------------------------------------------------------- +void +EGrScene::Reshape( int nW, int nH) +{ + if ( m_hDC == nullptr) + return ; + + // assegno le dimensioni della vista + m_nViewportW = nW ; + m_nViewportH = nH ; + + // aggiorno la vista + if ( MakeCurrent()) + glViewport( 0, 0, m_nViewportW, m_nViewportH) ; + + // aggiorno + AdjustDimView( m_dHalfWidth, m_dHalfHeight) ; + Prepare() ; + + // ridisegno + Draw() ; +} + +//---------------------------------------------------------------------------- +void +EGrScene::Draw( void) +{ + + MakeCurrent() ; + + //-------------------------------- + + //-------------------------------- + glClear( GL_COLOR_BUFFER_BIT) ; + + for ( int i = 0 ; i < NUM_VAO ; ++ i) { + if ( m_vaoID[i] != 0) { + glBindVertexArray( m_vaoID[i]) ; + glDrawArrays( m_nMode[i], 0, m_nCount[i]) ; + } + } + //-------------------------------- + glFlush() ; + SwapBuffers( m_hDC) ; +} + +//---------------------------------------------------------------------------- +void +EGrScene::Destroy( void) +{ + wglMakeCurrent( nullptr, nullptr) ; + if ( m_hRC != nullptr) { + wglDeleteContext( m_hRC) ; + m_hRC = nullptr ; + } +} + +//---------------------------------------------------------------------------- +void +EGrScene::SetData( int nType) +{ + const int SQUARE = 1 ; + const int CUBE = 2 ; + const int TRIANGLE = 3 ; + + switch ( nType) { + case SQUARE : + SetSquare() ; + break; + case CUBE : + SetCube() ; + break; + case TRIANGLE : + default : + SetTriangle() ; + break; + } +} + +//---------------------------------------------------------------------------- +void +EGrScene::SetSquare( void) +{ + if ( ! MakeCurrent()) + return ; + + size_t nSizeV3f = sizeof( Vert3f) ; + Vert3f Point[4] ; + SET_VERT3F( Point[0], -0.5f, -0.5f, 0.0f) + SET_VERT3F( Point[1], -0.5f, 0.5f, 0.0f) + SET_VERT3F( Point[2], 0.5f, 0.5f, 0.0f) + SET_VERT3F( Point[3], 0.5f, -0.5f, 0.0f) + + // inizializzo VAOs a non allocati + for ( int i = 0 ; i < NUM_VAO ; ++ i) + m_vaoID[i] = 0 ; + + if ( m_bNewWay) { + // VAO allocation + glGenVertexArrays( 1, &m_vaoID[0]) ; + if ( m_vaoID[0] == 0) + return ; + // First VAO setup + glBindVertexArray( m_vaoID[0]) ; + glGenBuffers( 1, &m_vboID[0]) ; + glBindBuffer( GL_ARRAY_BUFFER, m_vboID[0]) ; + m_nMode[0] = GL_LINE_STRIP ; + m_nCount[0] = 5 ; + glBufferData( GL_ARRAY_BUFFER, 5 * nSizeV3f, NULL, GL_STATIC_DRAW) ; + glBufferSubData( GL_ARRAY_BUFFER, 0 * nSizeV3f, nSizeV3f, &Point[0]) ; + glBufferSubData( GL_ARRAY_BUFFER, 1 * nSizeV3f, nSizeV3f, &Point[1]) ; + glBufferSubData( GL_ARRAY_BUFFER, 2 * nSizeV3f, nSizeV3f, &Point[2]) ; + glBufferSubData( GL_ARRAY_BUFFER, 3 * nSizeV3f, nSizeV3f, &Point[3]) ; + glBufferSubData( GL_ARRAY_BUFFER, 4 * nSizeV3f, nSizeV3f, &Point[0]) ; + glVertexAttribPointer( (GLuint)0, 4, GL_FLOAT, GL_FALSE, 0, 0) ; + glEnableVertexAttribArray( 0) ; + glBindVertexArray( 0) ; + } +} + +//---------------------------------------------------------------------------- +void +EGrScene::SetCube( void) +{ + if ( ! MakeCurrent()) + return ; + + size_t nSizeV3f = sizeof( Vert3f) ; + Vert3f Point[8] ; + SET_VERT3F( Point[0], -0.5f, -0.5f, 0.0f) + SET_VERT3F( Point[1], -0.5f, 0.5f, 0.0f) + SET_VERT3F( Point[2], 0.5f, 0.5f, 0.0f) + SET_VERT3F( Point[3], 0.5f, -0.5f, 0.0f) + SET_VERT3F( Point[4], -0.5f, -0.5f, 1.0f) + SET_VERT3F( Point[5], -0.5f, 0.5f, 1.0f) + SET_VERT3F( Point[6], 0.5f, 0.5f, 1.0f) + SET_VERT3F( Point[7], 0.5f, -0.5f, 1.0f) + + // inizializzo VAOs a non allocati + for ( int i = 0 ; i < NUM_VAO ; ++ i) + m_vaoID[i] = 0 ; + + if ( m_bNewWay) { + // VAO allocation + glGenVertexArrays( 3, &m_vaoID[0]) ; + if ( m_vaoID[0] == 0) + return ; + // First VAO setup + glBindVertexArray( m_vaoID[0]) ; + glGenBuffers( 1, &m_vboID[0]) ; + glBindBuffer( GL_ARRAY_BUFFER, m_vboID[0]) ; + m_nMode[0] = GL_LINE_STRIP ; + m_nCount[0] = 5 ; + glBufferData( GL_ARRAY_BUFFER, 5 * nSizeV3f, NULL, GL_STATIC_DRAW) ; + glBufferSubData( GL_ARRAY_BUFFER, 0 * nSizeV3f, nSizeV3f, &Point[0]) ; + glBufferSubData( GL_ARRAY_BUFFER, 1 * nSizeV3f, nSizeV3f, &Point[1]) ; + glBufferSubData( GL_ARRAY_BUFFER, 2 * nSizeV3f, nSizeV3f, &Point[2]) ; + glBufferSubData( GL_ARRAY_BUFFER, 3 * nSizeV3f, nSizeV3f, &Point[3]) ; + glBufferSubData( GL_ARRAY_BUFFER, 4 * nSizeV3f, nSizeV3f, &Point[0]) ; + glVertexAttribPointer( (GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0) ; + glEnableVertexAttribArray( 0) ; + glBindVertexArray( 0) ; + // Second VAO setup + glBindVertexArray( m_vaoID[1]) ; + glGenBuffers( 1, &m_vboID[1]) ; + glBindBuffer( GL_ARRAY_BUFFER, m_vboID[1]) ; + m_nMode[1] = GL_LINE_STRIP ; + m_nCount[1] = 5 ; + glBufferData( GL_ARRAY_BUFFER, 5 * nSizeV3f, NULL, GL_STATIC_DRAW) ; + glBufferSubData( GL_ARRAY_BUFFER, 0 * nSizeV3f, nSizeV3f, &Point[4]) ; + glBufferSubData( GL_ARRAY_BUFFER, 1 * nSizeV3f, nSizeV3f, &Point[5]) ; + glBufferSubData( GL_ARRAY_BUFFER, 2 * nSizeV3f, nSizeV3f, &Point[6]) ; + glBufferSubData( GL_ARRAY_BUFFER, 3 * nSizeV3f, nSizeV3f, &Point[7]) ; + glBufferSubData( GL_ARRAY_BUFFER, 4 * nSizeV3f, nSizeV3f, &Point[4]) ; + glVertexAttribPointer( (GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0) ; + glEnableVertexAttribArray( 0) ; + glBindVertexArray( 0) ; + // Third VAO setup + glBindVertexArray( m_vaoID[2]) ; + glGenBuffers( 1, &m_vboID[2]) ; + glBindBuffer( GL_ARRAY_BUFFER, m_vboID[2]) ; + m_nMode[2] = GL_LINES ; + m_nCount[2] = 8 ; + glBufferData( GL_ARRAY_BUFFER, 8 * nSizeV3f, NULL, GL_STATIC_DRAW) ; + glBufferSubData( GL_ARRAY_BUFFER, 0 * nSizeV3f, nSizeV3f, &Point[0]) ; + glBufferSubData( GL_ARRAY_BUFFER, 1 * nSizeV3f, nSizeV3f, &Point[4]) ; + glBufferSubData( GL_ARRAY_BUFFER, 2 * nSizeV3f, nSizeV3f, &Point[1]) ; + glBufferSubData( GL_ARRAY_BUFFER, 3 * nSizeV3f, nSizeV3f, &Point[5]) ; + glBufferSubData( GL_ARRAY_BUFFER, 4 * nSizeV3f, nSizeV3f, &Point[2]) ; + glBufferSubData( GL_ARRAY_BUFFER, 5 * nSizeV3f, nSizeV3f, &Point[6]) ; + glBufferSubData( GL_ARRAY_BUFFER, 6 * nSizeV3f, nSizeV3f, &Point[3]) ; + glBufferSubData( GL_ARRAY_BUFFER, 7 * nSizeV3f, nSizeV3f, &Point[7]) ; + glVertexAttribPointer( (GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0) ; + glEnableVertexAttribArray( 0) ; + glBindVertexArray( 0) ; + } +} + +//---------------------------------------------------------------------------- +void +EGrScene::SetTriangle( void) +{ + MakeCurrent() ; + + GLfloat TriangleA[] = { + -0.3f, 0.5f, -1.0f, 1.0f, + -0.8f, -0.5f, -1.0f, 1.0f, + 0.2f, -0.5f, -1.0f, 1.0f, + -0.3f, 0.5f, -1.0f, 1.0f + } ; + + GLfloat TriangleB[] = { + -0.2f, 0.5f, -1.0f, 1.0f, + 0.3f, -0.5f, -1.0f, 1.0f, + 0.8f, 0.5f, -1.0f, 1.0f, + -0.2f, 0.5f, -1.0f, 1.0f + } ; + + // inizializzo VAOs a non allocati + for ( int i = 0 ; i < NUM_VAO ; ++ i) + m_vaoID[i] = 0 ; + + if ( m_bNewWay) { + // VAOs allocation + glGenVertexArrays( 2, &m_vaoID[0]) ; + + // First VAO setup + glBindVertexArray( m_vaoID[0]) ; + glGenBuffers( 1, &m_vboID[0]) ; //VBO allocation + glBindBuffer( GL_ARRAY_BUFFER, m_vboID[0]) ; + glBufferData( GL_ARRAY_BUFFER, sizeof(TriangleA), TriangleA, GL_STATIC_DRAW) ; + glVertexAttribPointer( (GLuint)0, 4, GL_FLOAT, GL_FALSE, 0, 0) ; + glEnableVertexAttribArray( 0) ; + glBindVertexArray( 0) ; + m_nMode[0] = GL_LINE_STRIP ; + m_nCount[0] = 4 ; + // Second VAO setup + glBindVertexArray( m_vaoID[1]) ; + glGenBuffers( 1, &m_vboID[1]) ; //VBO allocation + glBindBuffer( GL_ARRAY_BUFFER, m_vboID[1]) ; + glBufferData( GL_ARRAY_BUFFER, sizeof(TriangleB), TriangleB, GL_STATIC_DRAW) ; + glVertexAttribPointer( (GLuint)0, 4, GL_FLOAT, GL_FALSE, 0, 0) ; + glEnableVertexAttribArray( 0) ; + glBindVertexArray( 0) ; + m_nMode[1] = GL_LINE_STRIP ; + m_nCount[1] = 4 ; + } +} + +//---------------------------------------------------------------------------- +void +EGrScene::ResetData( void) +{ + if ( m_bNewWay && MakeCurrent()) { + glBindBuffer( GL_ARRAY_BUFFER, 0) ; + glDeleteBuffers( NUM_VAO, m_vboID) ; + glBindVertexArray( 0) ; + glDeleteVertexArrays( NUM_VAO, m_vaoID) ; + } +} diff --git a/EGrScene.h b/EGrScene.h new file mode 100644 index 0000000..1b5ccfd --- /dev/null +++ b/EGrScene.h @@ -0,0 +1,100 @@ +//---------------------------------------------------------------------------- +// EgalTech 2013-2014 +//---------------------------------------------------------------------------- +// File : Scene.h Data : 03.02.14 Versione : 1.5b1 +// Contenuto : Dichiarazione della classe gestione scena. +// +// +// +// Modifiche : 29.01.14 DS Creazione modulo. +// +// +//---------------------------------------------------------------------------- + +#pragma once + +//---------------------------------------------------------------------------- +#include "/EgtDev/Include/EGkPoint3d.h" +#include "/EgtDev/Include/EGkBBox3d.h" +#define GLEW_MX +#include "/EgtDev/Extern/GLEW/Include/glew.h" +#include "/EgtDev/Extern/GLEW/Include/wglew.h" +#include + + +//---------------------------------------------------------------------------- +class EGrScene +{ + public : + enum CameraDir { CT_NONE = 0, CT_TOP = 1, CT_FRONT = 2, CT_RIGHT = 3, CT_BACK = 4, CT_LEFT = 5, + CT_BOTTOM = 6, CT_ISO_SW = 7, CT_ISO_SE = 8, CT_ISO_NE = 9, CT_ISO_NW = 10} ; + public: + EGrScene( void); + virtual ~EGrScene( void); + bool CreateContext( HDC hDC, bool bGenDriver, bool bDouBuff, int nColorBits, int nDepthBits) ; + bool IsValid( void) + { return ( m_hDC != nullptr && m_hRC != nullptr) ; } + std::string GetOpenGLInfo( void) ; + std::string GetGLSLInfo( void) ; + std::string GetPixelFormatInfo( void) ; + void Prepare( void) ; + void Reshape( int nW, int nH) ; + void Draw( void) ; + void Destroy( void) ; + + bool SetCenter( const Point3d& ptCenter) ; + bool SetCamera( double dAngVertDeg, double dAngOrizzDeg, double dDist = 0) ; + bool SetCamera( CameraDir nDir, double dDist = 0) ; + + bool SetExtension( const BBox3d& b3Ext) ; + bool ZoomAll( void) ; + + void SetData( int nType) ; + void ResetData( void) ; + + private : + int ChooseGenPixelFormat( int nPfd, bool bDouBuff, int nColorBits, int nDepthBits) ; + bool MakeCurrent( void) ; + GLEWContext* glewGetContext( void) + { return &m_glewc ; } + WGLEWContext* wglewGetContext( void) + { return &m_wglewc ; } + + bool CalcDirUp( void) ; + bool CalcExtView( void) ; + bool CalcDimViewFromExtView( void) ; + bool AdjustDimView( double dHalfWidth, double dHalfHeight) ; + + void SetTriangle( void) ; + void SetSquare( void) ; + void SetCube( void) ; + + private : + HDC m_hDC ; // Device Context + HGLRC m_hRC ; // OpenGL Rendering Context + WGLEWContext m_wglewc ; // WGLEW Context + GLEWContext m_glewc ; // GLEW Context + bool m_bNewWay ; // flag che indica nuova modalità (OpenGL 3.0 in poi) + + Point3d m_ptCenter ; // centro verso cui è rivolta la camera + Vector3d m_vtDirCamera ; // versore direzione dal centro alla camera + double m_dDistCamera ; // distanza dal centro alla camera + Vector3d m_vtUp ; // versore direzione Su della camera + bool m_bUpOk ; // flag per stato aggiornamento di Up + int m_nViewportW ; // larghezza della finestra + int m_nViewportH ; // altezza della finestra + + BBox3d m_b3ExtWorld ; // estensione della geometria + BBox3d m_b3ExtView ; // estensione nel riferimento di vista + bool m_bExtViewOk ; // flag per stato aggiornamento di EView + double m_dHalfWidth ; + double m_dHalfHeight ; + double m_dZNear ; + double m_dZFar ; + + static const int NUM_VAO = 3 ; + GLuint m_vaoID[NUM_VAO] ; // VAOs + GLuint m_vboID[NUM_VAO] ; // VBOs + int m_nMode[NUM_VAO] ; // modo di utilizzo dei vertici + int m_nCount[NUM_VAO] ; // contatore di vertici +} ; diff --git a/OpenGLRenderer.cpp b/OpenGLRenderer.cpp deleted file mode 100644 index c839921..0000000 --- a/OpenGLRenderer.cpp +++ /dev/null @@ -1,316 +0,0 @@ -// OpenGLRenderer.cpp : implementation file -// - -#include "stdafx.h" -#include "OpenGLRenderer.h" - - -// OpenGLRenderer - -//---------------------------------------------------------------------------- -OpenGLRenderer::OpenGLRenderer( void) -{ - m_hdc = nullptr ; - m_hrc = nullptr ; - memset( &m_wglewc, 0, sizeof( WGLEWContext)) ; - memset( &m_glewc, 0, sizeof( GLEWContext)) ; -} - -//---------------------------------------------------------------------------- -OpenGLRenderer::~OpenGLRenderer( void) -{ -} - -//---------------------------------------------------------------------------- -BEGIN_MESSAGE_MAP( OpenGLRenderer, CWnd) -END_MESSAGE_MAP() - - -// OpenGLRenderer message handlers - -bool -OpenGLRenderer::CreateGLContext( CRect rect, CWnd *parent) -{ - CString className = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW | CS_OWNDC, NULL, (HBRUSH)GetStockObject(WHITE_BRUSH), NULL) ; //default background colour - CreateEx( 0, className, _T("OpenGL with MFC/CDialog"), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, rect, parent, 0) ; - - m_rectWin = rect ; - if ( ! InitContext()) { - MessageBox( _T("ERROR Creating InitContext")) ; - return false ; - } - - // Setup the OpenGL Window's timer to render - //m_unpTimer = SetTimer( 1, 20, 0) ; - - return true ; -} - -//---------------------------------------------------------------------------- -bool -OpenGLRenderer::InitContext( void) -{ - PIXELFORMATDESCRIPTOR pfd ; - memset( &pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)) ; - pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR) ; - pfd.nVersion = 1 ; - pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW ; - pfd.iPixelType = PFD_TYPE_RGBA ; - pfd.cColorBits = 32 ; - pfd.cDepthBits = 32 ; - pfd.iLayerType = PFD_MAIN_PLANE ; - - m_hdc = GetDC()->m_hDC ; - - int nPixelFormat = ChoosePixelFormat( m_hdc, &pfd) ; - if ( nPixelFormat == 0) - return false ; - BOOL bResult = SetPixelFormat (m_hdc, nPixelFormat, &pfd) ; - if ( ! bResult) - return false ; - - HGLRC tempContext = wglCreateContext( m_hdc) ; - wglMakeCurrent( m_hdc, tempContext) ; - - GLenum WglewInitResult ; - WglewInitResult = wglewInit() ; - - if ( WglewInitResult != GLEW_OK) { - AfxMessageBox( _T("WGLEW is not initialized!")) ; - } - - int attribs[] = { - WGL_CONTEXT_MAJOR_VERSION_ARB, 3, - WGL_CONTEXT_MINOR_VERSION_ARB, 3, - WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, - //WGL_CONTEXT_FLAGS_ARB, 0, - 0 - }; - - if ( wglewIsSupported( "WGL_ARB_create_context") == 1) { - m_hrc = wglCreateContextAttribsARB( m_hdc, 0, attribs) ; - wglMakeCurrent( m_hdc, NULL) ; - wglDeleteContext( tempContext) ; - wglMakeCurrent( m_hdc, m_hrc) ; - } - else { - //It's not possible to make a GL 3.x context. Use the old style context (GL 2.1 and before) - m_hrc = tempContext; - } - - if ( ! m_hrc) - return false; - - GLenum GlewInitResult; - glewExperimental = GL_TRUE ; - GlewInitResult = glewInit() ; - - if ( GlewInitResult != GLEW_OK) { - AfxMessageBox( _T("GLEW is not initialized!")) ; - } - - CString str; - str.Format(_T("OpenGL version: %s\n"),(CString)glGetString(GL_VERSION)); - TRACE(str); - return true; -} - -//---------------------------------------------------------------------------- -void -OpenGLRenderer::PrepareScene( void) -{ - wglMakeCurrent( m_hdc, m_hrc) ; - glClearColor(0.0, 0.0, 1.0, 0.0); //background to clear with. - - GLenum qq = glGetError() ; - //do other preparations here - glMatrixMode( GL_PROJECTION) ; - qq = glGetError() ; - glLoadIdentity() ; - qq = glGetError() ; - glOrtho( -10, 10, -10, 10, 1, 1000) ; - - qq = glGetError() ; - - glMatrixMode( GL_MODELVIEW) ; - qq = glGetError() ; - glLoadIdentity() ; - qq = glGetError() ; - gluLookAt( 0, 0, 10, - 0, 0, -100, - 0, 1, 0) ; - - qq = glGetError() ; - - wglMakeCurrent( m_hdc, NULL) ; -} - -//---------------------------------------------------------------------------- -void -OpenGLRenderer::Reshape( int nW, int nH) -{ - if ( m_hdc == NULL) - return ; - - m_rectWin.SetRect( m_rectWin.left, m_rectWin.top, m_rectWin.left + nW, m_rectWin.top + nH) ; - MoveWindow( m_rectWin, FALSE) ; - - if ( wglMakeCurrent( m_hdc, m_hrc)) - glViewport( 0, 0, (GLsizei)nW, (GLsizei)nH) ; - wglMakeCurrent( m_hdc, NULL) ; - - DrawScene() ; -} - -//---------------------------------------------------------------------------- -void -OpenGLRenderer::DrawScene( void) -{ - - wglMakeCurrent( m_hdc, m_hrc) ; - - //-------------------------------- - - //-------------------------------- - glClear( GL_COLOR_BUFFER_BIT) ; - - for ( int i=0; i<2; i++) { - if ( m_vaoID[i] != 0) { - glBindVertexArray( m_vaoID[i]) ; - glDrawArrays( GL_LINE_STRIP, 0, m_GLSizeCount) ; - } - } - //-------------------------------- - glFlush() ; - SwapBuffers( m_hdc) ; - wglMakeCurrent( m_hdc, NULL) ; -} - -//---------------------------------------------------------------------------- -void -OpenGLRenderer::SetData( int iType) -{ - const int SQUARE = 1 ; - //const int CUBE = 2 ; - const int TRIANGLE = 3 ; - m_iShapeType = iType ; - - switch ( iType) { - case SQUARE : - SetSquare() ; - break; - case TRIANGLE: - SetTriangle() ; - break; - default: - SetTriangle() ; - } -} - -//---------------------------------------------------------------------------- -void -OpenGLRenderer::SetSquare( void) -{ - wglMakeCurrent( m_hdc, m_hrc) ; - - // First simple object - m_GLSizeCount = 5 ; - m_GLIntSize = 4 ; - GLfloat Square[] = { - -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, 0.5f, 0.0f, 1.0f, - 0.5f, 0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.0f, 1.0f - } ; - - // inizializzo VAOs a non allocati - m_vaoID[0] = 0 ; - m_vaoID[1] = 0 ; - - // VAO allocation - glGenVertexArrays( 1, &m_vaoID[0]) ; - if ( m_vaoID[0] == 0) - return ; - - // First VAO setup - glBindVertexArray( m_vaoID[0]) ; - glGenBuffers( 1, &m_vboID[0]) ; - glBindBuffer( GL_ARRAY_BUFFER, m_vboID[0]) ; - glBufferData( GL_ARRAY_BUFFER, sizeof( Square), Square, GL_STATIC_DRAW) ; - glVertexAttribPointer( (GLuint)0, m_GLIntSize, GL_FLOAT, GL_FALSE, 0, 0) ; - glEnableVertexAttribArray( 0) ; - glBindVertexArray( 0) ; - - wglMakeCurrent( m_hdc, NULL) ; -} - -//---------------------------------------------------------------------------- -void -OpenGLRenderer::SetTriangle( void) -{ - wglMakeCurrent( m_hdc, m_hrc) ; - - m_GLIntSize = 4; //number of floats per item. i.e. number of floats per point in space. - m_GLSizeCount = 4; //number of items. ie sizeof(TriangleA)/m_GLIntSize - - GLfloat TriangleA[] = { - -0.3f, 0.5f, -1.0f, 1.0f, - -0.8f, -0.5f, -1.0f, 1.0f, - 0.2f, -0.5f, -1.0f, 1.0f, - -0.3f, 0.5f, -1.0f, 1.0f - } ; - - GLfloat TriangleB[] = { - -0.2f, 0.5f, -1.0f, 1.0f, - 0.3f, -0.5f, -1.0f, 1.0f, - 0.8f, 0.5f, -1.0f, 1.0f, - -0.2f, 0.5f, -1.0f, 1.0f - } ; - - // inizializzo VAOs a non allocati - m_vaoID[0] = 0 ; - m_vaoID[1] = 0 ; - - // VAOs allocation - glGenVertexArrays( 2, &m_vaoID[0]) ; - - // First VAO setup - glBindVertexArray( m_vaoID[0]) ; - glGenBuffers( 1, &m_vboID[0]) ; //VBO allocation - glBindBuffer( GL_ARRAY_BUFFER, m_vboID[0]) ; - glBufferData( GL_ARRAY_BUFFER, sizeof(TriangleA), TriangleA, GL_STATIC_DRAW) ; - glVertexAttribPointer( (GLuint)0, m_GLIntSize, GL_FLOAT, GL_FALSE, 0, 0) ; - glEnableVertexAttribArray( 0) ; - glBindVertexArray( 0) ; - - // Second VAO setup - glBindVertexArray( m_vaoID[1]) ; - glGenBuffers( 1, &m_vboID[1]) ; //VBO allocation - glBindBuffer( GL_ARRAY_BUFFER, m_vboID[1]) ; - glBufferData( GL_ARRAY_BUFFER, sizeof(TriangleB), TriangleB, GL_STATIC_DRAW) ; - glVertexAttribPointer( (GLuint)0, m_GLIntSize, GL_FLOAT, GL_FALSE, 0, 0) ; - glEnableVertexAttribArray( 0) ; - glBindVertexArray( 0) ; - - wglMakeCurrent( m_hdc, NULL) ; -} - -//---------------------------------------------------------------------------- -void -OpenGLRenderer::DestroyScene( void) -{ - wglMakeCurrent( m_hdc, m_hrc) ; - - glBindBuffer( GL_ARRAY_BUFFER, 0) ; - glDeleteBuffers( 2, m_vboID) ; - - glBindVertexArray( 0) ; - glDeleteVertexArrays( 2, m_vaoID) ; - - wglMakeCurrent( nullptr, nullptr) ; - if ( m_hrc != nullptr) { - wglDeleteContext( m_hrc) ; - m_hrc = nullptr ; - } -} diff --git a/OpenGLRenderer.h b/OpenGLRenderer.h deleted file mode 100644 index 4d2986b..0000000 --- a/OpenGLRenderer.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#define GLEW_MX - -#include "/EgtDev/Extern/GLEW/Include/glew.h" -#include "/EgtDev/Extern/GLEW/Include/wglew.h" - - -// OpenGLRenderer - -class OpenGLRenderer : public CWnd -{ - public: - OpenGLRenderer( void); - virtual ~OpenGLRenderer( void); - bool CreateGLContext( CRect rect, CWnd *parent); - void PrepareScene( void) ; // Scene preparation - void Reshape( int nW, int nH) ; - void DrawScene( void) ; // Draw scene - void DestroyScene( void) ; // Cleanup - - CRect& GetWinRect( void) - { return m_rectWin ; } - - void SetData( int iType) ; // Creates VAO and VBOs and fill them with data - - GLEWContext* glewGetContext( void) - { return &m_glewc ; } - WGLEWContext* wglewGetContext( void) - { return &m_wglewc ; } - - protected : - void SetTriangle( void) ; - void SetSquare( void) ; - bool InitContext( void) ; // Creates OpenGL Rendering Context - - HDC m_hdc ; // Device Context - HGLRC m_hrc ; // OpenGL Rendering Context - WGLEWContext m_wglewc ; // WGLEW Context - GLEWContext m_glewc ; // GLEW Context - - CRect m_rectWin ; - - GLuint m_vaoID[2] ; // 2 vertex array objects - GLuint m_vboID[2] ; // 2 VBOs - int m_GLIntSize ; - int m_GLSizeCount ; - int m_iShapeType ; //1,3 for triangle & square. - - DECLARE_MESSAGE_MAP() -} ; diff --git a/TestEGr.cpp b/TestEGr.cpp index 1f2d7bd..c4a0832 100644 --- a/TestEGr.cpp +++ b/TestEGr.cpp @@ -15,6 +15,8 @@ #include "stdafx.h" #include "TestEGr.h" #include "TestEGrDlg.h" +#include "/EgtDev/Include/EGkDllMain.h" +#include "/EgtDev/Include/ENkDllMain.h" #include "/EgtDev/Include/EGnDllMain.h" #include "/EgtDev/Include/EgnGetModuleVer.h" #include "/EgtDev/Include/EGnStringConverter.h" @@ -98,11 +100,11 @@ CTestEGrApp::InitInstance( void) // versione delle librerie LOG_INFO( &logger, GetEGnVersion()) - //LOG_INFO( &logger, GetENkVersion()) - //LOG_INFO( &logger, GetEGkVersion()) + LOG_INFO( &logger, GetENkVersion()) + LOG_INFO( &logger, GetEGkVersion()) // Dialog interface - CTestEGrDlg dlg ; + CTestEGrDlg dlg( &logger) ; m_pMainWnd = &dlg ; dlg.DoModal() ; diff --git a/TestEGr.rc b/TestEGr.rc index e75a790..189394c 100644 Binary files a/TestEGr.rc and b/TestEGr.rc differ diff --git a/TestEGr.vcxproj b/TestEGr.vcxproj index 03dac0c..3e3dc93 100644 --- a/TestEGr.vcxproj +++ b/TestEGr.vcxproj @@ -197,18 +197,20 @@ + - + + - + Create Create @@ -217,6 +219,7 @@ + diff --git a/TestEGr.vcxproj.filters b/TestEGr.vcxproj.filters index b5ec1f6..43fa667 100644 --- a/TestEGr.vcxproj.filters +++ b/TestEGr.vcxproj.filters @@ -21,6 +21,9 @@ File di risorse + + File di risorse + @@ -35,7 +38,10 @@ File di intestazione - + + File di intestazione + + File di intestazione @@ -49,7 +55,10 @@ File di origine - + + File di origine + + File di origine diff --git a/TestEGrDlg.cpp b/TestEGrDlg.cpp index 9ad1cf4..42daffa 100644 --- a/TestEGrDlg.cpp +++ b/TestEGrDlg.cpp @@ -17,6 +17,7 @@ #include "TestEGrDlg.h" #include "resource.h" #include "/EgtDev/Include/EgtIniFile.h" +#include "/EgtDev/Include/EgtILogger.h" #include "afxdialogex.h" //---------------------------------------------------------------------------- @@ -27,7 +28,7 @@ //---------------------------------------------------------------------------- // CAboutDlg dialog used for App About -class CAboutDlg : public CDialogEx +class CAboutDlg : public CDialog { public : CAboutDlg( void) ; @@ -39,7 +40,7 @@ class CAboutDlg : public CDialogEx } ; //---------------------------------------------------------------------------- -CAboutDlg::CAboutDlg( void) : CDialogEx( IDD_ABOUTBOX) +CAboutDlg::CAboutDlg( void) : CDialog( IDD_ABOUTBOX) { } @@ -47,41 +48,42 @@ CAboutDlg::CAboutDlg( void) : CDialogEx( IDD_ABOUTBOX) BOOL CAboutDlg::OnInitDialog( void) { - CDialogEx::OnInitDialog() ; + CDialog::OnInitDialog() ; GetDlgItem( IDC_EXEVER)->SetWindowText( stringtoW( GetExeNameVer())) ; return TRUE ; } -BEGIN_MESSAGE_MAP( CAboutDlg, CDialogEx) +BEGIN_MESSAGE_MAP( CAboutDlg, CDialog) END_MESSAGE_MAP() //---------------------------------------------------------------------------- // CTestEGrDlg dialog //---------------------------------------------------------------------------- -CTestEGrDlg::CTestEGrDlg( CWnd* pParent) - : CDialogEx( IDD_TESTEGR_DIALOG, pParent) +CTestEGrDlg::CTestEGrDlg( ILogger* pLogger, CWnd* pParent) + : CDialog( IDD_TESTEGR_DIALOG, pParent) { m_hIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME) ; - m_bOpenGLWindowsExists = false ; + m_pLogger = pLogger ; } //---------------------------------------------------------------------------- void CTestEGrDlg::DoDataExchange( CDataExchange* pDX) { - CDialogEx::DoDataExchange( pDX) ; + CDialog::DoDataExchange( pDX) ; } //---------------------------------------------------------------------------- -BEGIN_MESSAGE_MAP( CTestEGrDlg, CDialogEx) +BEGIN_MESSAGE_MAP( CTestEGrDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_SIZE() ON_WM_CLOSE() + ON_COMMAND( IDC_ZOOM_ALL, OnZoomAll) ON_COMMAND( IDC_CLOSE, OnClose) END_MESSAGE_MAP() @@ -89,7 +91,7 @@ END_MESSAGE_MAP() BOOL CTestEGrDlg::OnInitDialog( void) { - CDialogEx::OnInitDialog() ; + CDialog::OnInitDialog() ; // Add "About..." menu item to system menu. @@ -114,20 +116,22 @@ CTestEGrDlg::OnInitDialog( void) SetIcon( m_hIcon, TRUE) ; // Set big icon SetIcon( m_hIcon, FALSE) ; // Set small icon - StartOpenGL() ; + // salvo le dimensioni del client + CRect rectClient ; + GetClientRect( &rectClient) ; + m_nPrevCx = rectClient.right ; + m_nPrevCy = rectClient.bottom ; + + // visualizzazione scena + StartScene() ; + + LOG_INFO( m_pLogger, m_Scene.GetOpenGLInfo().c_str()) + LOG_INFO( m_pLogger, m_Scene.GetGLSLInfo().c_str()) + LOG_INFO( m_pLogger, m_Scene.GetPixelFormatInfo().c_str()) return TRUE ; // return TRUE unless you set the focus to a control } -//---------------------------------------------------------------------------- -void -CTestEGrDlg::OnClose( void) -{ - m_OGL_Window.DestroyScene() ; - - EndDialog( IDCLOSE) ; -} - //---------------------------------------------------------------------------- void CTestEGrDlg::OnSysCommand( UINT nID, LPARAM lParam) @@ -137,7 +141,7 @@ CTestEGrDlg::OnSysCommand( UINT nID, LPARAM lParam) dlgAbout.DoModal() ; } else { - CDialogEx::OnSysCommand( nID, lParam) ; + CDialog::OnSysCommand( nID, lParam) ; } } @@ -162,9 +166,9 @@ CTestEGrDlg::OnPaint( void) dc.DrawIcon( x, y, m_hIcon) ; } else { - CDialogEx::OnPaint() ; - m_OGL_Window.DrawScene() ; - m_OGL_Window.ValidateRgn( NULL) ; + m_Scene.Draw() ; + m_View.ValidateRgn( NULL) ; + CDialog::OnPaint() ; } } @@ -182,7 +186,7 @@ CTestEGrDlg::OnSize( UINT nType, int cx, int cy) const int SIDEBAR_W = 200 ; - CDialogEx::OnSize( nType, cx, cy) ; + CDialog::OnSize( nType, cx, cy) ; // se ridotta a icona non si fa alcunché if ( cx <= 0 || cy <= 0 || nType == SIZE_MINIMIZED) @@ -191,50 +195,87 @@ CTestEGrDlg::OnSize( UINT nType, int cx, int cy) // se cambiata dimensione if ( nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED) { // spostamento bottoni - CWnd* pBtn = GetDlgItem( IDC_CLOSE) ; - if ( pBtn != nullptr) { - CRect rect ; - pBtn->GetWindowRect( rect) ; - pBtn->MoveWindow( cx - rect.Width() / 2 - SIDEBAR_W / 2, cy - rect.Height(), - rect.Width(), rect.Height(), TRUE) ; - } + MoveDlgItem( IDC_ZOOM_ALL, cx, cy) ; + MoveDlgItem( IDC_CLOSE, cx, cy) ; // adattamento finestra - if ( m_bOpenGLWindowsExists) { - CRect rect = m_OGL_Window.GetWinRect() ; - m_OGL_Window.Reshape( cx - rect.left - SIDEBAR_W, cy - rect.top) ; + if ( m_Scene.IsValid()) { + int nX = 0 ; + int nY = 0 ; + int nW = cx - nX - SIDEBAR_W ; + int nH = cy - nY ; + m_View.MoveWindow( nX, nY, nW, nH, FALSE) ; + m_Scene.Reshape( nW, nH) ; } + // salvo la nuova dimensione + m_nPrevCx = cx ; + m_nPrevCy = cy ; } } //---------------------------------------------------------------------------- void -CTestEGrDlg::StartOpenGL( void) +CTestEGrDlg::MoveDlgItem( int nID, int cx, int cy) { - CRect rectWin ; - - - // recupero i parametri - int nDouBuff = GetPrivateProfileInt( "OpenGL", "DoubleBuffer", 1, AfxGetApp()->m_pszProfileName) ; - int nColorBits = GetPrivateProfileInt( "OpenGL", "ColorBits", 16, AfxGetApp()->m_pszProfileName) ; - int nDepthBits = GetPrivateProfileInt( "OpenGL", "DepthBits", 16, AfxGetApp()->m_pszProfileName) ; - bool bGenDriver = ( GetPrivateProfileInt( "OpenGL", "GenDriver", 0, AfxGetApp()->m_pszProfileName) == 1) ; - - - - GetDlgItem( IDC_SCENE)->GetWindowRect( rectWin) ; - ScreenToClient( rectWin) ; - - if ( m_bOpenGLWindowsExists) { - m_OGL_Window.DestroyScene() ; - m_bOpenGLWindowsExists = false ; - } - - // Create OpenGL Control window - if ( ! m_bOpenGLWindowsExists) { - m_OGL_Window.CreateGLContext( rectWin, this) ; - m_OGL_Window.PrepareScene() ; - m_OGL_Window.SetData( 1) ; - m_bOpenGLWindowsExists = true ; - } + CWnd* pBtn ; + if ( ( pBtn = GetDlgItem( nID)) != nullptr) { + CRect rect ; + pBtn->GetWindowRect( rect) ; + ScreenToClient( &rect) ; + pBtn->SetWindowPos( NULL, rect.left + cx - m_nPrevCx, rect.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER) ; + } } +//---------------------------------------------------------------------------- +void +CTestEGrDlg::OnClose( void) +{ + m_Scene.ResetData() ; + m_Scene.Destroy() ; + m_View.DestroyWindow() ; + + EndDialog( IDCLOSE) ; +} + +//---------------------------------------------------------------------------- +void +CTestEGrDlg::OnZoomAll( void) +{ + m_Scene.ZoomAll() ; + RedrawWindow() ; +} + +//---------------------------------------------------------------------------- +void +CTestEGrDlg::StartScene( void) +{ + // recupero i parametri + bool bGenDriver = ( GetPrivateProfileInt( "OpenGL", "GenDriver", 0, AfxGetApp()->m_pszProfileName) == 1) ; + bool bDouBuff = ( GetPrivateProfileInt( "OpenGL", "DoubleBuffer", 1, AfxGetApp()->m_pszProfileName) == 1) ; + int nColorBits = GetPrivateProfileInt( "OpenGL", "ColorBits", 16, AfxGetApp()->m_pszProfileName) ; + int nDepthBits = GetPrivateProfileInt( "OpenGL", "DepthBits", 16, AfxGetApp()->m_pszProfileName) ; + + // se già esistente, devo prima distruggerla + if ( m_Scene.IsValid()) { + m_Scene.Destroy() ; + m_View.DestroyWindow() ; + } + + // creazione della finestra + m_View.Create( this, IDC_SCENE) ; + + // creazione della scena + m_Scene.CreateContext( m_View.GetDC()->m_hDC, bGenDriver, bDouBuff, nColorBits, nDepthBits) ; + + // impostazione dati geometrie + m_Scene.SetData( 2) ; + m_Scene.SetExtension( BBox3d( -0.5, -0.5, 0, 0.5, 0.5, 1)) ; + + // impostazione dati di vista + m_Scene.SetCenter( Point3d( 100, 0, 0)) ; + m_Scene.SetCamera( EGrScene::CT_ISO_SW) ; + m_Scene.ZoomAll() ; + + // calcolo + m_Scene.Prepare() ; + +} diff --git a/TestEGrDlg.h b/TestEGrDlg.h index 4195296..7bf9190 100644 --- a/TestEGrDlg.h +++ b/TestEGrDlg.h @@ -13,33 +13,41 @@ #pragma once -#include "OpenGLRenderer.h" +#include "TestEgrView.h" +#include "EGrScene.h" +class ILogger ; + //---------------------------------------------------------------------------- -class CTestEGrDlg : public CDialogEx +class CTestEGrDlg : public CDialog { public : - CTestEGrDlg( CWnd* pParent = NULL) ; + CTestEGrDlg( ILogger* pLogger = nullptr, CWnd* pParent = nullptr) ; protected : virtual void DoDataExchange( CDataExchange* pDX) ; protected : virtual BOOL OnInitDialog( void) ; - afx_msg void OnClose( void) ; afx_msg void OnSysCommand( UINT nID, LPARAM lParam) ; afx_msg void OnPaint( void) ; afx_msg HCURSOR OnQueryDragIcon( void) ; afx_msg void OnSize( UINT nType, int cx, int cy) ; + afx_msg void OnZoomAll( void) ; + afx_msg void OnClose( void) ; private : - void StartOpenGL( void) ; + void MoveDlgItem( int nID, int cx, int cy) ; + void StartScene( void) ; private : - HICON m_hIcon ; - bool m_bOpenGLWindowsExists ; - OpenGLRenderer m_OGL_Window ; + HICON m_hIcon ; + TestEgrView m_View ; + EGrScene m_Scene ; + ILogger* m_pLogger ; + int m_nPrevCx ; + int m_nPrevCy ; DECLARE_MESSAGE_MAP() } ; diff --git a/TestEGrView.h b/TestEGrView.h new file mode 100644 index 0000000..b6de0c3 --- /dev/null +++ b/TestEGrView.h @@ -0,0 +1,14 @@ + + +#pragma once + +#include + +// TestEgrView + +class TestEgrView : public CWnd +{ + public : + TestEgrView( void) ; + bool Create( CWnd* pParent, int nID) ; +} ; \ No newline at end of file diff --git a/TestEgrView.cpp b/TestEgrView.cpp new file mode 100644 index 0000000..37c5c95 --- /dev/null +++ b/TestEgrView.cpp @@ -0,0 +1,37 @@ +// TestEgrView + + +//--------------------------- Include ---------------------------------------- +#include "stdafx.h" +#include "TestEgrView.h" +#include "resource.h" + +//---------------------------------------------------------------------------- +TestEgrView::TestEgrView( void) +{ + +} + +//---------------------------------------------------------------------------- +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, + AfxGetApp()->LoadCursor( IDC_POINTER), 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) ; + return true ; + } + else + return false ; +} diff --git a/res/POINTER.cur b/res/POINTER.cur new file mode 100644 index 0000000..cc79e95 Binary files /dev/null and b/res/POINTER.cur differ diff --git a/resource.h b/resource.h index 52f9b3d..5abf7f6 100644 Binary files a/resource.h and b/resource.h differ diff --git a/stdafx.h b/stdafx.h index 9b8d868..2faedf6 100644 --- a/stdafx.h +++ b/stdafx.h @@ -47,12 +47,8 @@ #pragma comment(lib, EGTEXTDIR "GLEW/lib/x32/glew32mx.lib") #endif #pragma comment(lib, EGTLIBDIR "EgtGeneral" EGTLIBVER ".lib") - - - - - - +#pragma comment(lib, EGTLIBDIR "EgtNumKernel" EGTLIBVER ".lib") +#pragma comment(lib, EGTLIBDIR "EgtGeomKernel" EGTLIBVER ".lib") #ifdef _UNICODE #if defined _M_IX86