//---------------------------------------------------------------------------- // EgalTech 2014-2014 //---------------------------------------------------------------------------- // File : ObjNewGraphics.cpp Data : 10.02.14 Versione : 1.5b1 // Contenuto : Implementazione della classe grafica di un oggetto geometrico. // // // // Modifiche : 10.02.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #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) //---------------------------------------------------------------------------- ObjNewGraphics::~ObjNewGraphics( void) { Clear() ; } //---------------------------------------------------------------------------- void 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) { 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, PL.GetPointNbr() * SIZEV3F, NULL, GL_STATIC_DRAW) ; Point3d ptP ; Vert3f v3V ; 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, 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 ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::Draw( void) { if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; // 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 ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::DeleteVaoVbo( void) { if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; // 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 ; }