diff --git a/EgtGraphics.rc b/EgtGraphics.rc index 3c1683c..74f0d99 100644 Binary files a/EgtGraphics.rc and b/EgtGraphics.rc differ diff --git a/GraphObjs.h b/GraphObjs.h index 0a15649..3e023e0 100644 --- a/GraphObjs.h +++ b/GraphObjs.h @@ -35,7 +35,6 @@ class Vert3f { } ; const size_t SIZEV3F = sizeof( Vert3f) ; - //---------------------------------------------------------------------------- // Raccolte di Vert3f typedef std::vector V3FVECTOR ; // vettore di vertici diff --git a/ObjEGrGraphics.h b/ObjEGrGraphics.h index 5ab7839..c3ce081 100644 --- a/ObjEGrGraphics.h +++ b/ObjEGrGraphics.h @@ -13,12 +13,12 @@ #pragma once -#include "GraphObjs.h" #include "/EgtDev/Include/EgkObjGraphics.h" #include "/EgtDev/Include/EgkGeoObj.h" -#include "/EgtDev/Include/EGkPolyLine.h" class Scene ; +class Color ; +class PolyLine ; //---------------------------------------------------------------------------- class ObjEGrGraphics : public IObjGraphics @@ -27,6 +27,9 @@ class ObjEGrGraphics : public IObjGraphics virtual bool IsValid( void) = 0 ; virtual void SetScene( Scene* pScene) = 0 ; virtual Scene* GetScene( void) = 0 ; + virtual void Clear( void) = 0 ; + virtual bool AddColor( const Color& colC) = 0 ; + virtual bool AddPoint( const Point3d& ptP) = 0 ; virtual bool AddPolyLine( const PolyLine& PL) = 0 ; virtual bool Draw( void) = 0 ; } ; diff --git a/ObjNewGraphics.cpp b/ObjNewGraphics.cpp index e85e762..9c39984 100644 --- a/ObjNewGraphics.cpp +++ b/ObjNewGraphics.cpp @@ -15,6 +15,8 @@ #include "stdafx.h" #include "ObjNewGraphics.h" #include "Scene.h" +#include "GraphObjs.h" +#include "/EgtDev/Include/EGkPolyLine.h" #define glewGetContext() (( m_pScene != nullptr) ? m_pScene->glewGetContext() : nullptr) @@ -22,8 +24,7 @@ //---------------------------------------------------------------------------- ObjNewGraphics::~ObjNewGraphics( void) { - // cancello eventuali vecchi vertici - DeleteVaoVbo() ; + Clear() ; } //---------------------------------------------------------------------------- @@ -33,6 +34,58 @@ ObjNewGraphics::Reset( void) m_bValid = false ; } +//---------------------------------------------------------------------------- +void +ObjNewGraphics::Clear( void) +{ + DeleteVaoVbo() ; + m_ngaList.clear() ; + m_bValid = false ; +} + +//---------------------------------------------------------------------------- +bool +ObjNewGraphics::AddColor( const Color& colC) +{ + return AddColor( colC.GetRed(), colC.GetGreen(), colC.GetBlue(), colC.GetAlpha()) ; +} + +//---------------------------------------------------------------------------- +bool +ObjNewGraphics::AddPoint( const Point3d& ptP) +{ + if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) + return false ; + + // definisco i buffer per la scheda grafica e vi inserisco i nuovi vertici + unsigned int nVaoId ; + glGenVertexArrays( 1, &nVaoId) ; + if ( nVaoId == 0) + return false ; + glBindVertexArray( nVaoId) ; + unsigned int nVboId ; + glGenBuffers( 1, &nVboId) ; + if ( nVboId == 0) { + glBindVertexArray( 0) ; + glDeleteVertexArrays( 1, &nVaoId) ; + return false ; + } + glBindBuffer( GL_ARRAY_BUFFER, nVboId) ; + glBufferData( GL_ARRAY_BUFFER, 1 * SIZEV3F, NULL, GL_STATIC_DRAW) ; + Vert3f v3V( ptP.x, ptP.y, ptP.z) ; + glBufferSubData( GL_ARRAY_BUFFER, 0, SIZEV3F, &v3V) ; + int nCount = 1 ; + glVertexPointer( 3, GL_FLOAT, 0, ((void*)(0))) ; + glEnableClientState( GL_VERTEX_ARRAY) ; + glBindVertexArray( 0) ; + // aggiungo i dati in lista + AddVerts( GL_POINTS, nCount, nVaoId, nVboId) ; + // dichiaro valida la grafica + m_bValid = true ; + + return true ; +} + //---------------------------------------------------------------------------- bool ObjNewGraphics::AddPolyLine( const PolyLine& PL) @@ -40,30 +93,36 @@ ObjNewGraphics::AddPolyLine( const PolyLine& PL) if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; - // cancello eventuali vecchi vertici - DeleteVaoVbo() ; - // imposto il nuovo stato - m_bValid = true ; - m_nMode = GL_LINE_STRIP ; - // inserisco i nuovi vertici - glGenVertexArrays( 1, &m_nVaoId) ; - if ( m_nVaoId == 0) + // definisco i buffer per la scheda grafica e vi inserisco i nuovi vertici + unsigned int nVaoId ; + glGenVertexArrays( 1, &nVaoId) ; + if ( nVaoId == 0) return false ; - glBindVertexArray( m_nVaoId) ; - glGenBuffers( 1, &m_nVboId) ; - glBindBuffer( GL_ARRAY_BUFFER, m_nVboId) ; + glBindVertexArray( nVaoId) ; + unsigned int nVboId ; + glGenBuffers( 1, &nVboId) ; + if ( nVboId == 0) { + glBindVertexArray( 0) ; + glDeleteVertexArrays( 1, &nVaoId) ; + return false ; + } + glBindBuffer( GL_ARRAY_BUFFER, nVboId) ; glBufferData( GL_ARRAY_BUFFER, PL.GetPointNbr() * SIZEV3F, NULL, GL_STATIC_DRAW) ; Point3d ptP ; Vert3f v3V ; - m_nCount = 0 ; + int nCount = 0 ; for ( bool bFound = PL.GetFirstPoint( ptP) ; bFound ; bFound = PL.GetNextPoint( ptP)) { v3V.Set( ptP.x, ptP.y, ptP.z) ; - glBufferSubData( GL_ARRAY_BUFFER, m_nCount * SIZEV3F, SIZEV3F, &v3V) ; - ++ m_nCount ; + glBufferSubData( GL_ARRAY_BUFFER, nCount * SIZEV3F, SIZEV3F, &v3V) ; + ++ nCount ; } glVertexPointer( 3, GL_FLOAT, 0, ((void*)(0))) ; glEnableClientState( GL_VERTEX_ARRAY) ; glBindVertexArray( 0) ; + // aggiungo i dati in lista + AddVerts( GL_LINE_STRIP, nCount, nVaoId, nVboId) ; + // dichiaro valida la grafica + m_bValid = true ; return true ; } @@ -75,14 +134,21 @@ ObjNewGraphics::Draw( void) if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; - // se vuoto non faccio alcunché - if ( m_nCount == 0) - return true ; - - if ( m_nVaoId != 0) { - glBindVertexArray( m_nVaoId) ; - glDrawArrays( m_nMode, 0, m_nCount) ; - glBindVertexArray( 0) ; + // ciclo di disegno + NGALIST::iterator iIter ; + for ( iIter = m_ngaList.begin() ; iIter != m_ngaList.end() ; ++ iIter) { + switch ( iIter->m_nType) { + case NewGrAtom::VERTS : + if ( iIter->m_nCount > 0 && iIter->m_nVaoId != 0) { + glBindVertexArray( iIter->m_nVaoId) ; + glDrawArrays( iIter->m_nMode, 0, iIter->m_nCount) ; + glBindVertexArray( 0) ; + } + break ; + case NewGrAtom::COLOR : + glColor4f( iIter->m_fRed, iIter->m_fGreen, iIter->m_fBlue, iIter->m_fAlpha) ; + break ; + } } return true ; @@ -95,13 +161,16 @@ ObjNewGraphics::DeleteVaoVbo( void) if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; - // cancellazione VAO/VBO - if ( m_nCount > 0) { - glBindBuffer( GL_ARRAY_BUFFER, 0) ; - glDeleteBuffers( 1, &m_nVboId) ; - glBindVertexArray( 0) ; - glDeleteVertexArrays( 1, &m_nVaoId) ; - m_nCount = 0 ; + // ciclo di cancellazione VAO/VBO + NGALIST::iterator iIter ; + for ( iIter = m_ngaList.begin() ; iIter != m_ngaList.end() ; ++ iIter) { + if ( iIter->m_nType == NewGrAtom::VERTS && iIter->m_nCount > 0) { + glBindBuffer( GL_ARRAY_BUFFER, 0) ; + glDeleteBuffers( 1, &iIter->m_nVboId) ; + glBindVertexArray( 0) ; + glDeleteVertexArrays( 1, &iIter->m_nVaoId) ; + iIter->m_nCount = 0 ; + } } return true ; diff --git a/ObjNewGraphics.h b/ObjNewGraphics.h index 55a512a..601df9d 100644 --- a/ObjNewGraphics.h +++ b/ObjNewGraphics.h @@ -14,35 +14,93 @@ #pragma once #include "ObjEGrGraphics.h" +#include + + +//---------------------------------------------------------------------------- +// Definizione di NewGrAtom +class NewGrAtom { + public : + NewGrAtom( void) : m_nType( NONE), m_nMode( 0), m_nCount( 0), m_nVaoId( 0), m_nVboId( 0) {} + + public : + enum GrType { NONE = 0, VERTS = 1, COLOR = 2} ; + + public : + GrType m_nType ; + union { + int m_nMode ; + float m_fRed ; + } ; + union { + int m_nCount ; + float m_fGreen ; + } ; + union { + unsigned int m_nVaoId ; + float m_fBlue ; + } ; + union { + unsigned int m_nVboId ; + float m_fAlpha ; + } ; +} ; + +//---------------------------------------------------------------------------- +// Lista di NewGrAtom +typedef std::list NGALIST ; + //---------------------------------------------------------------------------- class ObjNewGraphics : public ObjEGrGraphics { - public : - ObjNewGraphics( void) : m_pScene( nullptr), m_bValid( false), m_nMode( 0), m_nCount( 0) {} + public : // IObjGraphics virtual ~ObjNewGraphics( void) ; virtual void Reset( void) ; - public : + public : // ObjEGrGraphics virtual bool IsValid( void) { return m_bValid ; } virtual void SetScene( Scene* pScene) { m_pScene = pScene ; } virtual Scene* GetScene( void) { return m_pScene ; } + virtual void Clear( void) ; + virtual bool AddColor( const Color& colC) ; + virtual bool AddPoint( const Point3d& ptP) ; virtual bool AddPolyLine( const PolyLine& PL) ; virtual bool Draw( void) ; + public : + ObjNewGraphics( void) : m_pScene( nullptr), m_bValid( false) {} + private : bool DeleteVaoVbo( void) ; + bool AddNewGrAtom( const NewGrAtom& nga) + { try { m_ngaList.push_back( nga) ; } + catch(...) { return false ; } + return true ; } + bool AddVerts( int nMode, int nCount, unsigned int nVaoId, unsigned int nVboId) + { NewGrAtom nga ; + nga.m_nType = NewGrAtom::VERTS ; + nga.m_nMode = nMode ; nga.m_nCount = nCount ; + nga.m_nVaoId = nVaoId ; nga.m_nVboId = nVboId ; + return AddNewGrAtom( nga) ; } + bool AddColor( float fRed, float fGreen, float fBlue, float fAlpha = 1) + { NewGrAtom nga ; + nga.m_nType = NewGrAtom::COLOR ; + nga.m_fRed = fRed ; nga.m_fGreen = fGreen ; nga.m_fBlue = fBlue ; nga.m_fAlpha = fAlpha ; + return AddNewGrAtom( nga) ; } private : Scene* m_pScene ; // puntatore alla scena bool m_bValid ; // flag - int m_nMode ; // modo di utilizzo dei vertici - int m_nCount ; // numero di vertici nel VBO - unsigned int m_nVaoId ; // Id del VAO - unsigned int m_nVboId ; // Id del VBO + NGALIST m_ngaList ; // lista di atomi per nuova grafica + + //int m_nMode ; // modo di utilizzo dei vertici + //int m_nCount ; // numero di vertici nel VBO + //unsigned int m_nVaoId ; // Id del VAO + //unsigned int m_nVboId ; // Id del VBO } ; //---------------------------------------------------------------------------- diff --git a/ObjOldGraphics.cpp b/ObjOldGraphics.cpp index 6949b2d..6eb7b83 100644 --- a/ObjOldGraphics.cpp +++ b/ObjOldGraphics.cpp @@ -15,11 +15,13 @@ #include "stdafx.h" #include "ObjOldGraphics.h" #include "OpenGL.h" +#include "/EgtDev/Include/EGkColor.h" +#include "/EgtDev/Include/EGkPolyLine.h" //---------------------------------------------------------------------------- ObjOldGraphics::~ObjOldGraphics( void) { - m_v3fList.clear() ; + Clear() ; } //---------------------------------------------------------------------------- @@ -29,19 +31,51 @@ ObjOldGraphics::Reset( void) m_bValid = false ; } +//---------------------------------------------------------------------------- +void +ObjOldGraphics::Clear( void) +{ + m_ogaList.clear() ; + m_bValid = false ; +} + +//---------------------------------------------------------------------------- +bool +ObjOldGraphics::AddColor( const Color& colC) +{ + return AddColor( colC.GetRed(), colC.GetGreen(), colC.GetBlue(), colC.GetAlpha()) ; +} + +//---------------------------------------------------------------------------- +bool +ObjOldGraphics::AddPoint( const Point3d& ptP) +{ + // start + AddStart( GL_POINTS) ; + // inserisco il nuovo vertice + AddVert3f( ptP.x, ptP.y, ptP.z) ; + // fine + AddEnd() ; + // dichiaro valida la grafica + m_bValid = true ; + + return true ; +} + //---------------------------------------------------------------------------- bool ObjOldGraphics::AddPolyLine( const PolyLine& PL) { - // cancello eventuali vecchi vertici - m_v3fList.clear() ; - // imposto il nuovo stato - m_bValid = true ; - m_nMode = GL_LINE_STRIP ; + // start + AddStart( GL_LINE_STRIP) ; // inserisco i nuovi vertici Point3d ptP ; for ( bool bFound = PL.GetFirstPoint( ptP) ; bFound ; bFound = PL.GetNextPoint( ptP)) - m_v3fList.push_back( Vert3f( (float)ptP.x, (float)ptP.y, (float)ptP.z)) ; + AddVert3f( ptP.x, ptP.y, ptP.z) ; + // fine + AddEnd() ; + // dichiaro valida la grafica + m_bValid = true ; return true ; } @@ -51,16 +85,27 @@ bool ObjOldGraphics::Draw( void) { // se vuoto non faccio alcunché - if ( m_v3fList.size() == 0) + if ( m_ogaList.size() == 0) return true ; // ciclo di disegno - glBegin( m_nMode) ; - V3FLIST::iterator iIter ; - for ( iIter = m_v3fList.begin() ; iIter != m_v3fList.end() ; ++ iIter) { - glVertex3f( iIter->x, iIter->y, iIter->z) ; + OGALIST::iterator iIter ; + for ( iIter = m_ogaList.begin() ; iIter != m_ogaList.end() ; ++ iIter) { + switch ( iIter->m_nType) { + case OldGrAtom::START : + glBegin( iIter->m_nMode) ; + break ; + case OldGrAtom::VERT : + glVertex3f( iIter->m_fX, iIter->m_fY, iIter->m_fZ) ; + break ; + case OldGrAtom::COLOR : + glColor4f( iIter->m_fRed, iIter->m_fGreen, iIter->m_fBlue, iIter->m_fAlpha) ; + break ; + case OldGrAtom::END : + glEnd() ; + break ; + } } - glEnd() ; return true ; } diff --git a/ObjOldGraphics.h b/ObjOldGraphics.h index c3e2d7b..346b986 100644 --- a/ObjOldGraphics.h +++ b/ObjOldGraphics.h @@ -14,30 +14,96 @@ #pragma once #include "ObjEGrGraphics.h" +#include + +class Color ; + +//---------------------------------------------------------------------------- +// Definizione di OldGrAtom +class OldGrAtom { + public : + OldGrAtom( void) : m_nType( NONE), m_fX( 0), m_fY( 0), m_fZ( 0), m_fW( 1) {} + + public : + enum GrType { NONE = 0, START = 1, VERT = 2, COLOR = 3, END = 4} ; + + public : + GrType m_nType ; + union { + int m_nMode ; + float m_fX ; + float m_fRed ; + } ; + union { + float m_fY ; + float m_fGreen ; + } ; + union { + float m_fZ ; + float m_fBlue ; + } ; + union { + float m_fW ; + float m_fAlpha ; + } ; +} ; + +//---------------------------------------------------------------------------- +// Lista di OldGrAtom +typedef std::list OGALIST ; //---------------------------------------------------------------------------- class ObjOldGraphics : public ObjEGrGraphics { - public : - ObjOldGraphics( void) : m_pScene( nullptr), m_bValid( false), m_nMode( 0) {} + public : // IObjGraphics virtual ~ObjOldGraphics( void) ; virtual void Reset( void) ; - public : + public : // ObjEGrGraphics virtual bool IsValid( void) { return m_bValid ; } virtual void SetScene( Scene* pScene) { m_pScene = pScene ; } virtual Scene* GetScene( void) { return m_pScene ; } + virtual void Clear( void) ; + virtual bool AddColor( const Color& colC) ; + virtual bool AddPoint( const Point3d& ptP) ; virtual bool AddPolyLine( const PolyLine& PL) ; virtual bool Draw( void) ; + public : + ObjOldGraphics( void) : m_pScene( nullptr), m_bValid( false) {} + + private : + bool AddOldGrAtom( const OldGrAtom& oga) + { try { m_ogaList.push_back( oga) ; } + catch(...) { return false ; } + return true ; } + bool AddStart( int nMode) + { OldGrAtom oga ; + oga.m_nType = OldGrAtom::START ; + oga.m_nMode = nMode ; + return AddOldGrAtom( oga) ; } + bool AddVert3f( double dX, double dY, double dZ) + { OldGrAtom oga ; + oga.m_nType = OldGrAtom::VERT ; + oga.m_fX = float( dX) ; oga.m_fY = float( dY) ; oga.m_fZ = float( dZ) ; + return AddOldGrAtom( oga) ; } + bool AddColor( float fRed, float fGreen, float fBlue, float fAlpha = 1) + { OldGrAtom oga ; + oga.m_nType = OldGrAtom::COLOR ; + oga.m_fRed = fRed ; oga.m_fGreen = fGreen ; oga.m_fBlue = fBlue ; oga.m_fAlpha = fAlpha ; + return AddOldGrAtom( oga) ; } + bool AddEnd( void) + { OldGrAtom oga ; + oga.m_nType = OldGrAtom::END ; + return AddOldGrAtom( oga) ; } + private : Scene* m_pScene ; // puntatore alla scena bool m_bValid ; // flag - int m_nMode ; // modo di utilizzo dei vertici - V3FLIST m_v3fList ; // lista di vertici + OGALIST m_ogaList ; // lista di atomi per vecchia grafica } ; //---------------------------------------------------------------------------- diff --git a/SceneAux.cpp b/SceneAux.cpp index 3fd84b5..9a3d720 100644 --- a/SceneAux.cpp +++ b/SceneAux.cpp @@ -13,9 +13,10 @@ //--------------------------- Include ---------------------------------------- #include "stdafx.h" -#include "Scene.h" #include "ObjOldGraphics.h" #include "ObjNewGraphics.h" +#include "Scene.h" +#include "GraphObjs.h" #include "/EgtDev/Include/EgtILogger.h" #include "/EgtDev/Include/EGnStringUtils.h" #include "/EgtDev/Include/EGkFrame3d.h" diff --git a/SceneBasic.cpp b/SceneBasic.cpp index b7c3fbb..3ec0785 100644 --- a/SceneBasic.cpp +++ b/SceneBasic.cpp @@ -625,6 +625,9 @@ Scene::Draw( void) if ( m_pGeomDB != nullptr) m_pGeomDB->GetDefaultColor( m_colDef) ; + // imposto la dimensione dei punti + glPointSize( 3) ; + // disegno le geometrie del DB DrawGroup( GDB_ID_ROOT) ; diff --git a/SceneGeom.cpp b/SceneGeom.cpp index 34973fe..7d0ca9c 100644 --- a/SceneGeom.cpp +++ b/SceneGeom.cpp @@ -21,6 +21,8 @@ #include "/EgtDev/Include/EGkFrame3d.h" #include "/EgtDev/Include/EgtPointerOwner.h" #include "/EgtDev/Include/EGkGdbIterator.h" +#include "/EgtDev/Include/EGkGeoVector3d.h" +#include "/EgtDev/Include/EGkGeoPoint3d.h" #include "/EgtDev/Include/EGkCurve.h" using namespace std ; @@ -146,26 +148,67 @@ Scene::DrawGeoObj( IGeoObj* pGeoObj) pGraphics->SetScene( this) ; pGeoObj->SetObjGraphics( pGraphics) ; } - // leggo il tipo di oggetto geometrico - int nGeoType = pGeoObj->GetType() ; - // se curva - if ( ( nGeoType & GEO_CURVE) != 0) { - // recupero la curva - const ICurve* pCurve = GetCurve( pGeoObj) ; - if ( pCurve == nullptr) - return false ; - // se la grafica associata non è valida la ricalcolo - ObjEGrGraphics* pGraphics = GetObjEGrGraphics( pGeoObj) ; - if ( ! pGraphics->IsValid()) { + + // se la grafica associata non è valida la ricalcolo + ObjEGrGraphics* pGraphics = GetObjEGrGraphics( pGeoObj) ; + if ( ! pGraphics->IsValid()) { + // leggo il tipo di oggetto geometrico + int nGeoType = pGeoObj->GetType() ; + // se vettore + if ( nGeoType == GEO_VECT3D) { + // recupero il vettore + const IGeoVector3d* pVector = GetGeoVector3d( pGeoObj) ; + if ( pVector == nullptr) + return false ; + // calcolo la grafica + PolyLine PL ; + pVector->GetDrawWithArrowHead( 0.1, PL) ; + pGraphics->Clear() ; + pGraphics->AddPolyLine( PL) ; + } + // se punto + else if ( nGeoType == GEO_PNT3D) { + // recupero il punto + const IGeoPoint3d* pPoint = GetGeoPoint3d( pGeoObj) ; + if ( pPoint == nullptr) + return false ; + // calcolo la grafica + pGraphics->Clear() ; + pGraphics->AddPoint( pPoint->GetPoint()) ; + } + // se riferimento + else if ( nGeoType == GEO_FRAME3D) { + // recupero il riferimento + const IGeoFrame3d* pFrame = GetGeoFrame3d( pGeoObj) ; + if ( pFrame == nullptr) + return false ; + // aggiorno la grafica + PolyLine plX, plY, plZ ; + pFrame->GetDrawWithArrowHeads( 10, 0.1, plX, plY, plZ) ; + pGraphics->Clear() ; + pGraphics->AddColor( RED) ; + pGraphics->AddPolyLine( plX) ; + pGraphics->AddColor( GREEN) ; + pGraphics->AddPolyLine( plY) ; + pGraphics->AddColor( BLUE) ; + pGraphics->AddPolyLine( plZ) ; + } + // se curva + else if ( ( nGeoType & GEO_CURVE) != 0) { + // recupero la curva + const ICurve* pCurve = GetCurve( pGeoObj) ; + if ( pCurve == nullptr) + return false ; + // calcolo la grafica PolyLine PL ; pCurve->ApproxWithLines( 0.1, 5, PL) ; + pGraphics->Clear() ; pGraphics->AddPolyLine( PL) ; } - // visualizzo la grafica associata - pGraphics->Draw() ; } - return true ; + // visualizzo la grafica associata + return pGraphics->Draw() ; } //----------------------------------------------------------------------------