Files
EgtGraphics/ObjNewGraphics.cpp
T
Dario Sassi cb34a7b71a EgtGraphics 1.5d2 :
- prima implementazione dello shading.
2014-04-04 09:40:34 +00:00

308 lines
9.5 KiB
C++

//----------------------------------------------------------------------------
// 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"
#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::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) ;
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) ;
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::StartTriangles( int nNum)
{
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) ;
// dichiaro modo triangoli
m_nCurrMode = GL_TRIANGLES ;
return true ;
}
//----------------------------------------------------------------------------
bool
ObjNewGraphics::AddTriangle( const Triangle3d& Tria)
{
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 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( Tria.GetN()) ;
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)
{
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 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 ;
}
}
// se marcato, disegno halo
if ( nMark == GDB_MK_ON) {
// 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 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 :
// si usa il colore di marcatura
break ;
}
}
}
// 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 == 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 ;
}