//---------------------------------------------------------------------------- // EgalTech 2014-2014 //---------------------------------------------------------------------------- // File : ObjNewGraphics.cpp Data : 23.04.14 Versione : 1.5d5 // Contenuto : Implementazione della classe grafica di un oggetto geometrico. // // // // Modifiche : 10.02.14 DS Creazione modulo. // 23.04.14 DS Agg. gestione materiali. // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "ObjNewGraphics.h" #include "Scene.h" #include "GraphObjs.h" #include "/EgtDev/Include/EGkPolyLine.h" #include "/EgtDev/Include/EGkGdbConst.h" #include "/EgtDev/Include/EGKTriangle3d.h" //--------------------------- Macro ------------------------------------------ // Per funzioni glew dipendenti dal context #define glewGetContext() (( m_pScene != nullptr) ? m_pScene->glewGetContext() : nullptr) //---------------------------------------------------------------------------- ObjNewGraphics::~ObjNewGraphics( void) { Clear() ; } //---------------------------------------------------------------------------- void ObjNewGraphics::Reset( void) { m_bValid = false ; m_nCurrMode = GL_NONE ; } //---------------------------------------------------------------------------- void ObjNewGraphics::Clear( void) { DeleteVaoVbo() ; m_ngaList.clear() ; m_bValid = false ; m_nCurrMode = GL_NONE ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::AddColor( const Color& colC) { return AddColor( colC.GetRed(), colC.GetGreen(), colC.GetBlue(), colC.GetAlpha()) ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::AddMaterial( const Color& colAmb, const Color& colDiff, const Color& colSpec, float fShin) { return AddMaterial( NgAtom::MAT_A, colAmb.GetRed(), colAmb.GetGreen(), colAmb.GetBlue(), colDiff.GetAlpha()) && AddMaterial( NgAtom::MAT_D, colDiff.GetRed(), colDiff.GetGreen(), colDiff.GetBlue(), colDiff.GetAlpha()) && AddMaterial( NgAtom::MAT_S, colSpec.GetRed(), colSpec.GetGreen(), colSpec.GetBlue(), colDiff.GetAlpha()) && AddMaterial( NgAtom::MAT_SH, fShin, 0, 0, 0) ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::AddBackMaterial( const Color& colAmbDiff) { return AddMaterial( NgAtom::MAT_B, colAmbDiff.GetRed(), colAmbDiff.GetGreen(), colAmbDiff.GetBlue(), colAmbDiff.GetAlpha()) ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::AddPoint( const Point3d& ptP, bool bAux) { 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) ; 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, bAux) ; // dichiaro valida la grafica m_bValid = true ; return true ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::AddPolyLine( const PolyLine& PL, bool bAux) { 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) ; 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, bAux) ; // dichiaro valida la grafica m_bValid = true ; return true ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::StartTriangles( int nNum, bool bAux) { if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; // modo corrente deve essere nullo if ( m_nCurrMode != GL_NONE) return false ; // inizializzo emissione triangoli 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) ; // 3 float per le coordinate e 3 per la normale glBufferData( GL_ARRAY_BUFFER, 6 * nNum * SIZEV3F, NULL, GL_STATIC_DRAW) ; // aggiungo i dati in lista AddVerts( GL_TRIANGLES, 0, nVaoId, nVboId, bAux) ; // dichiaro modo triangoli m_nCurrMode = GL_TRIANGLES ; return true ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::AddTriangle( const Triangle3d& Tria, const TriNormals3d& TNrms) { if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; // modo corrente deve essere triangoli if ( m_nCurrMode != GL_TRIANGLES) return false ; // recupero il conteggio dei vertici int nCount = m_ngaList.back().m_nCount ; // emetto i vertici e le normali del triangolo for ( int i = 0 ; i < 3 ; ++ i) { Vert3f v3V( Tria.GetP( i)) ; glBufferSubData( GL_ARRAY_BUFFER, 2 * nCount * SIZEV3F, SIZEV3F, &v3V) ; Vert3f v3N( TNrms.vtN[i]) ; glBufferSubData( GL_ARRAY_BUFFER, ( 2 * nCount + 1) * SIZEV3F, SIZEV3F, &v3N) ; ++ nCount ; } // aggiorno il conteggio m_ngaList.back().m_nCount = nCount ; return true ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::EndTriangles( void) { if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; // modo corrente deve essere triangoli if ( m_nCurrMode != GL_TRIANGLES) return false ; // termino il modo triangoli glVertexPointer( 3, GL_FLOAT, 2 * SIZEV3F, ((void*)(0))) ; glEnableClientState( GL_VERTEX_ARRAY) ; glNormalPointer( GL_FLOAT, 2 * SIZEV3F, ((void*)( 1 * SIZEV3F))) ; glEnableClientState( GL_NORMAL_ARRAY) ; glBindVertexArray( 0) ; // dichiaro modalità standard m_nCurrMode = GL_NONE ; // dichiaro valida la grafica m_bValid = true ; return true ; } //---------------------------------------------------------------------------- bool ObjNewGraphics::Draw( int nStat, int nMark, bool bShowAux) { if ( m_pScene == nullptr || ! m_pScene->MakeCurrent()) return false ; // se vuoto non faccio alcunché if ( m_ngaList.size() == 0) return true ; // gestione stato di visualizzazione if ( nStat == GDB_ST_OFF) return true ; if ( nStat == GDB_ST_SEL) { glPointSize( 5) ; glLineWidth( 3) ; } // ciclo di disegno NGALIST::iterator iIter ; for ( iIter = m_ngaList.begin() ; iIter != m_ngaList.end() ; ++ iIter) { switch ( iIter->m_nType) { case NgAtom::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 NgAtom::VERTS_A : if ( bShowAux && iIter->m_nCount > 0 && iIter->m_nVaoId != 0) { glBindVertexArray( iIter->m_nVaoId) ; glDrawArrays( iIter->m_nMode, 0, iIter->m_nCount) ; glBindVertexArray( 0) ; } break ; case NgAtom::COLOR : glColor4fv( iIter->m_fCol) ; break ; case NgAtom::MAT_A : glMaterialfv( GL_FRONT, GL_AMBIENT, iIter->m_fCol) ; break ; case NgAtom::MAT_D : glMaterialfv( GL_FRONT, GL_DIFFUSE, iIter->m_fCol) ; break ; case NgAtom::MAT_S : glMaterialfv( GL_FRONT, GL_SPECULAR, iIter->m_fCol) ; break ; case NgAtom::MAT_SH : glMaterialf( GL_FRONT, GL_SHININESS, iIter->m_fCol[0]) ; break ; case NgAtom::MAT_B : glMaterialfv( GL_BACK, GL_AMBIENT_AND_DIFFUSE, iIter->m_fCol) ; break ; } } // se marcato, disegno halo if ( nMark == GDB_MK_ON) { // cambio test di depth per non sovrascrivere il disegno appena fatto glDepthFunc( GL_LESS) ; // dimensioni per halo glPointSize( 7) ; glLineWidth( 5) ; // colore di marcatura Color colMark = ( ( GetScene() != nullptr) ? GetScene()->GetMark() : Color( 192, 192, 0)) ; glColor4f( colMark.GetRed(), colMark.GetGreen(), colMark.GetBlue(), colMark.GetAlpha()) ; // ciclo di disegno NGALIST::iterator iIter ; for ( iIter = m_ngaList.begin() ; iIter != m_ngaList.end() ; ++ iIter) { switch ( iIter->m_nType) { case NgAtom::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 NgAtom::VERTS_A : if ( bShowAux && iIter->m_nCount > 0 && iIter->m_nVaoId != 0) { glBindVertexArray( iIter->m_nVaoId) ; glDrawArrays( iIter->m_nMode, 0, iIter->m_nCount) ; glBindVertexArray( 0) ; } break ; // non si impostano i colori, si usa quello di marcatura } } // ripristino test di depth glDepthFunc( GL_LEQUAL) ; } // se necessario, ripristino la normale visualizzazione if ( nStat == GDB_ST_SEL || nMark == GDB_MK_ON) { glPointSize( 3) ; glLineWidth( 1) ; } 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 == NgAtom::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 ; }