EgtGeomKernel 1.5d7 :
- aggiunti materiali e loro gestione - aggiunta gestione libreria materiali.
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
//----------------------------------------------------------------------------
|
||||
// EgalTech 2014-2014
|
||||
//----------------------------------------------------------------------------
|
||||
// File : GdbMaterialMgr.cpp Data : 24.04.14 Versione : 1.5d7
|
||||
// Contenuto : Implementazione classe gestore materiali in GeomDB.
|
||||
//
|
||||
//
|
||||
//
|
||||
// Modifiche : 24.04.14 DS Creazione modulo.
|
||||
//
|
||||
//
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
//--------------------------- Include ----------------------------------------
|
||||
#include "stdafx.h"
|
||||
#include "GdbMaterialMgr.h"
|
||||
#include "NgeWriter.h"
|
||||
#include "NgeReader.h"
|
||||
#include "/EgtDev/Include/EGnStringUtils.h"
|
||||
|
||||
using namespace std ;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
GdbMaterialMgr::GdbMaterialMgr( void)
|
||||
{
|
||||
// riservo spazio per 48 materiali
|
||||
m_GdbMats.reserve( 48) ;
|
||||
// inserisco i materiali standard
|
||||
string sName ;
|
||||
Material matM ;
|
||||
for ( int i = 0 ; GetStdMaterial( i, sName, matM) ; ++ i)
|
||||
AddMaterial( sName, matM) ;
|
||||
// salvo il numero dei materiali standard
|
||||
m_nStdNum = int( m_GdbMats.size()) ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::Clear( void)
|
||||
{
|
||||
// cancello i materiali custom
|
||||
if ( int( m_GdbMats.size()) > m_nStdNum)
|
||||
m_GdbMats.resize( m_nStdNum) ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::Save( NgeWriter& ngeOut) const
|
||||
{
|
||||
// intestazione libreria
|
||||
ngeOut.WriteKey( NGE_MAT_LIB) ;
|
||||
// numero di materiali custom
|
||||
int nCustNum = int( m_GdbMats.size()) - m_nStdNum ;
|
||||
ngeOut.WriteInt( nCustNum, ";", true) ;
|
||||
// scrittura dei materiali custom
|
||||
for ( int i = m_nStdNum ; i < int( m_GdbMats.size()) ; ++ i) {
|
||||
// Nome, Id, Tipo (1=materiale senza texture)
|
||||
string sMatName = ( m_GdbMats[i].sName.empty() ? "$$" : m_GdbMats[i].sName) ;
|
||||
ngeOut.WriteString( sMatName, ",") ;
|
||||
ngeOut.WriteInt( i + 1, ",") ;
|
||||
ngeOut.WriteInt( 1, ";", true) ;
|
||||
// dati del materiale
|
||||
ngeOut.WriteCol( m_GdbMats[i].matM.GetAmbient(), ";") ;
|
||||
ngeOut.WriteCol( m_GdbMats[i].matM.GetDiffuse(), ";") ;
|
||||
ngeOut.WriteCol( m_GdbMats[i].matM.GetSpecular(), ";") ;
|
||||
ngeOut.WriteDouble( m_GdbMats[i].matM.GetShininess(), ";", true, 1) ;
|
||||
}
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::Load( NgeReader& ngeIn)
|
||||
{
|
||||
// lettura eventuale intestazione libreria
|
||||
int nKey ;
|
||||
if ( ! ngeIn.ReadKey( nKey))
|
||||
return false ;
|
||||
if ( nKey != NGE_MAT_LIB) {
|
||||
ngeIn.UngetKey() ;
|
||||
return true ;
|
||||
}
|
||||
// lettura numero di materiali custom
|
||||
int nCustNum ;
|
||||
if ( ! ngeIn.ReadInt( nCustNum, ";", true))
|
||||
return false ;
|
||||
// lettura dei materiali custom
|
||||
for ( int i = 0 ; i < nCustNum ; ++ i) {
|
||||
// Nome, Id, Tipo (1=materiale senza texture)
|
||||
string sMatName ;
|
||||
if ( ! ngeIn.ReadString( sMatName, ","))
|
||||
return false ;
|
||||
int nInd ;
|
||||
if ( ! ngeIn.ReadInt( nInd, ","))
|
||||
return false ;
|
||||
int nType ;
|
||||
if ( ! ngeIn.ReadInt( nType, ";", true))
|
||||
return false ;
|
||||
// dati del materiale
|
||||
Color colAmb ;
|
||||
if ( ! ngeIn.ReadCol( colAmb, ";"))
|
||||
return false ;
|
||||
Color colDiff ;
|
||||
if ( ! ngeIn.ReadCol( colDiff, ";"))
|
||||
return false ;
|
||||
Color colSpec ;
|
||||
if ( ! ngeIn.ReadCol( colSpec, ";"))
|
||||
return false ;
|
||||
double dShin ;
|
||||
if ( ! ngeIn.ReadDouble( dShin, ";", true))
|
||||
return false ;
|
||||
// inserisco il materiale
|
||||
int nNew = AddMaterial( sMatName, Material( colAmb, colDiff, colSpec, dShin)) ;
|
||||
if ( nNew == GDB_MT_NULL || nNew != nInd)
|
||||
return false ;
|
||||
}
|
||||
// sistemazione dei materiali cancellati
|
||||
for ( int i = m_nStdNum ; i < int( m_GdbMats.size()) ; ++ i) {
|
||||
if ( m_GdbMats[i].sName == "$$")
|
||||
EraseMaterial( i + 1) ;
|
||||
}
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
int
|
||||
GdbMaterialMgr::AddMaterial( const string& sName, const Material& matM)
|
||||
{
|
||||
// verifico validità nome
|
||||
if ( ! IsValidName( sName))
|
||||
return GDB_MT_NULL ;
|
||||
// se il materiale già esiste ritorno errore
|
||||
if ( FindMaterial( sName) > 0)
|
||||
return GDB_MT_NULL ;
|
||||
// provo ad inserire al posto del primo cancellato
|
||||
int nId = FindFirstErased() ;
|
||||
if ( nId > GDB_MT_NULL) {
|
||||
m_GdbMats[nId-1].sName = sName ;
|
||||
m_GdbMats[nId-1].matM = matM ;
|
||||
return nId ;
|
||||
}
|
||||
// aggiungo il materiale nella collezione
|
||||
GdbMaterial gdbmatM ;
|
||||
gdbmatM.sName = sName ;
|
||||
gdbmatM.matM = matM ;
|
||||
try { m_GdbMats.push_back( gdbmatM) ; }
|
||||
catch (...) { return GDB_MT_NULL ; }
|
||||
return int( m_GdbMats.size()) ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
int
|
||||
GdbMaterialMgr::FindFirstErased( void) const
|
||||
{
|
||||
for ( int i = 0 ; i < int( m_GdbMats.size()) ; ++ i) {
|
||||
if ( m_GdbMats[i].sName.empty())
|
||||
return ( i + 1) ;
|
||||
}
|
||||
return GDB_MT_NULL ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::EraseMaterial( int nId)
|
||||
{
|
||||
// controllo indice ( 1 based)
|
||||
if ( nId <= GDB_MT_NULL || nId > int( m_GdbMats.size()))
|
||||
return false ;
|
||||
// se standard, non posso cancellarlo
|
||||
if ( nId <= m_nStdNum)
|
||||
return false ;
|
||||
// se già cancellato, non devo fare alcunchè
|
||||
if ( m_GdbMats[nId-1].sName.empty())
|
||||
return true ;
|
||||
// lo cancello
|
||||
m_GdbMats[nId-1].sName.clear() ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
int
|
||||
GdbMaterialMgr::FindMaterial( const string& sName) const
|
||||
{
|
||||
// verifico validità nome
|
||||
if ( ! IsValidName( sName))
|
||||
return GDB_MT_NULL ;
|
||||
// cerco nella collezione (confronto tra nomi in maiuscolo)
|
||||
string sToFind = sName ;
|
||||
ToUpper( sToFind) ;
|
||||
string sUpName ;
|
||||
for ( int i = 0 ; i < int( m_GdbMats.size()) ; ++ i) {
|
||||
sUpName = m_GdbMats[i].sName ;
|
||||
ToUpper( sUpName) ;
|
||||
if ( sToFind == sUpName)
|
||||
return ( i + 1) ;
|
||||
}
|
||||
return GDB_MT_NULL ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::GetMaterial( int nId, Material& matM) const
|
||||
{
|
||||
if ( ! ExistsMaterial( nId))
|
||||
return false ;
|
||||
// recupero i dati del materiale
|
||||
matM = m_GdbMats[nId-1].matM ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::GetMaterialName( int nId, string& sName) const
|
||||
{
|
||||
if ( ! ExistsMaterial( nId))
|
||||
return false ;
|
||||
// recupero il nome del materiale
|
||||
sName = m_GdbMats[nId-1].sName ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::IsCustomMaterial( int nId, bool& bCustom) const
|
||||
{
|
||||
if ( ! ExistsMaterial( nId))
|
||||
return false ;
|
||||
// recupero il tipo del materiale
|
||||
bCustom = ( nId > m_nStdNum) ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::ModifyMaterial( int nId, const Material& matM)
|
||||
{
|
||||
if ( ! ExistsMaterial( nId))
|
||||
return false ;
|
||||
// se standard, non è modificabile
|
||||
if ( nId <= m_nStdNum)
|
||||
return false ;
|
||||
// modifico i dati del materiale nella collezione
|
||||
m_GdbMats[nId-1].matM = matM ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
GdbMaterialMgr::ModifyMaterialName( int nId, const string& sName)
|
||||
{
|
||||
if ( ! ExistsMaterial( nId))
|
||||
return false ;
|
||||
// se standard, non è modificabile
|
||||
if ( nId <= m_nStdNum)
|
||||
return false ;
|
||||
// verifico validità nome
|
||||
if ( ! IsValidName( sName))
|
||||
return false ;
|
||||
// modifico il nome del materiale nella collezione
|
||||
m_GdbMats[nId-1].sName = sName ;
|
||||
return true ;
|
||||
}
|
||||
Reference in New Issue
Block a user