045f3e480c
- aggiunta gestione flag di edge per non disegnare in wireframe/hiddenline (per poligoni).
428 lines
15 KiB
C++
428 lines
15 KiB
C++
//----------------------------------------------------------------------------
|
|
// 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 e Costanti -------------------------------
|
|
// Per funzioni glew dipendenti dal context
|
|
#define glewGetContext() (( m_pScene != nullptr) ? m_pScene->glewGetContext() : nullptr)
|
|
// Dimensioni flag per edge di triangoli
|
|
const size_t SIZEFLAG = sizeof( unsigned char) ;
|
|
// Dimensioni record del buffer ( vertice + normale + flag)
|
|
const size_t SIZEVNF = ( 2 * SIZEV3F + SIZEFLAG) ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
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::AddLines( const PNTVECTOR& vPnt, 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) ;
|
|
int nCount = int( vPnt.size()) ;
|
|
glBufferData( GL_ARRAY_BUFFER, nCount * SIZEV3F, NULL, GL_STATIC_DRAW) ;
|
|
Vert3f v3V ;
|
|
for ( int i = 0 ; i < nCount ; ++ i) {
|
|
v3V.Set( vPnt[i]) ;
|
|
glBufferSubData( GL_ARRAY_BUFFER, i * SIZEV3F, SIZEV3F, &v3V) ;
|
|
}
|
|
glVertexPointer( 3, GL_FLOAT, 0, ((void*)(0))) ;
|
|
glEnableClientState( GL_VERTEX_ARRAY) ;
|
|
glBindVertexArray( 0) ;
|
|
// aggiungo i dati in lista
|
|
AddVerts( GL_LINES, 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) ;
|
|
// NumTria * 3 * dimensione( vertice+normale+flag)
|
|
glBufferData( GL_ARRAY_BUFFER, nNum * 3 * SIZEVNF, 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 TriFlags3d& TFlags, 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, nCount * SIZEVNF, SIZEV3F, &v3V) ;
|
|
Vert3f v3N( TNrms.vtN[i]) ;
|
|
glBufferSubData( GL_ARRAY_BUFFER, nCount * SIZEVNF + SIZEV3F, SIZEV3F, &v3N) ;
|
|
unsigned char cFlag = ( TFlags.bFlag[i] ? 1 : 0) ;
|
|
glBufferSubData( GL_ARRAY_BUFFER, nCount * SIZEVNF + 2 * SIZEV3F, SIZEFLAG, &cFlag) ;
|
|
++ 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, SIZEVNF, ((void*)(0))) ;
|
|
glEnableClientState( GL_VERTEX_ARRAY) ;
|
|
glNormalPointer( GL_FLOAT, SIZEVNF, ((void*)( 1 * SIZEV3F))) ;
|
|
glEnableClientState( GL_NORMAL_ARRAY) ;
|
|
glEdgeFlagPointer( SIZEVNF, ((void*)( 2 * SIZEV3F))) ;
|
|
glEnableClientState( GL_EDGE_FLAG_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 bSurfSha, bool bShowAux)
|
|
{
|
|
if ( ! m_bValid || 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( 2) ;
|
|
}
|
|
|
|
// colori speciali per superficie shading selezionata o marcata
|
|
bool bStdCol = true ;
|
|
float fSelMarkCol[4] = { 1, 1, 1, 1} ;
|
|
float fSelMarkBackCol[4] = { 0.75, 0.75, 0.75, 1} ;
|
|
if ( bSurfSha) {
|
|
if ( nMark == GDB_MK_ON) {
|
|
bStdCol = false ;
|
|
Color colMark = ( ( GetScene() != nullptr) ? GetScene()->GetMark() : Color( 192, 192, 0)) ;
|
|
colMark.GetFloat( fSelMarkCol) ;
|
|
Color colMarkBack = GetSurfBackColor( colMark) ;
|
|
colMarkBack.GetFloat( fSelMarkBackCol) ;
|
|
}
|
|
else if ( nStat == GDB_ST_SEL) {
|
|
bStdCol = false ;
|
|
Color colSelSurf = ( ( GetScene() != nullptr) ? GetScene()->GetSelSurf() : Color( 255, 255, 192)) ;
|
|
colSelSurf.GetFloat( fSelMarkCol) ;
|
|
Color colSelSurfBack = GetSurfBackColor( colSelSurf) ;
|
|
colSelSurfBack.GetFloat( fSelMarkBackCol) ;
|
|
}
|
|
}
|
|
|
|
// 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( ( bStdCol ? iIter->m_fCol : fSelMarkCol)) ;
|
|
break ;
|
|
case NgAtom::MAT_A :
|
|
glMaterialfv( GL_FRONT, GL_AMBIENT, ( bStdCol ? iIter->m_fCol : fSelMarkCol)) ;
|
|
break ;
|
|
case NgAtom::MAT_D :
|
|
glMaterialfv( GL_FRONT, GL_DIFFUSE, ( bStdCol ? iIter->m_fCol : fSelMarkCol)) ;
|
|
break ;
|
|
case NgAtom::MAT_S :
|
|
glMaterialfv( GL_FRONT, GL_SPECULAR, ( bStdCol ? iIter->m_fCol : fSelMarkCol)) ;
|
|
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, ( bStdCol ? iIter->m_fCol : fSelMarkBackCol)) ;
|
|
break ;
|
|
}
|
|
}
|
|
|
|
// se marcato e non superficie in shading, disegno halo
|
|
if ( nMark == GDB_MK_ON && ! bSurfSha) {
|
|
// cambio test di depth per non sovrascrivere il disegno appena fatto
|
|
glDepthFunc( GL_LESS) ;
|
|
|
|
// dimensioni per halo
|
|
glPointSize( 7) ;
|
|
glLineWidth( 4) ;
|
|
|
|
// 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 ;
|
|
}
|