EgtGraphics 1.6j1 :
- aggiunto TextureMgr per gestire le textures caricate - fatte modifiche per visualizzare gli oggetti con le textures (solo opachi).
This commit is contained in:
+149
-41
@@ -24,29 +24,49 @@ using namespace std ;
|
||||
//----------------------------------------------------------------------------
|
||||
TextureMgr::TextureMgr( void)
|
||||
{
|
||||
// inserisco le texture standard calcolate : Chessboard e Lines
|
||||
|
||||
m_pScene = nullptr ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
TextureMgr::~TextureMgr( void)
|
||||
{
|
||||
Clear() ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::Clear( void)
|
||||
{
|
||||
// verifico il contesto OpenGL
|
||||
if ( m_pScene == nullptr || ! m_pScene->MakeCurrent())
|
||||
return false ;
|
||||
// scarico le texture da OpenGL
|
||||
for ( auto iIter = m_umTextData.begin() ; iIter != m_umTextData.end() ; ++ iIter) {
|
||||
GLuint nTextId = iIter->second.nTexId ;
|
||||
glDeleteTextures( 1, &nTextId) ;
|
||||
}
|
||||
// pulisco i dati
|
||||
m_umTextData.clear() ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::LoadTexture( const string& sName, const string& sFile, double dMMxPix, double dDimX, double dDimY, bool bRepeat)
|
||||
TextureMgr::LoadTexture( const string& sName, const string& sFile,
|
||||
double dMMxPix, double dDimX, double dDimY, int nRepeat)
|
||||
{
|
||||
// verifico il contesto OpenGL
|
||||
if ( m_pScene == nullptr || ! m_pScene->MakeCurrent())
|
||||
return false ;
|
||||
|
||||
// verifico se immagine già caricata
|
||||
auto iIter = m_umTextData.find( sName) ;
|
||||
if ( iIter != m_umTextData.end())
|
||||
return ( EqualNoCase( iIter->second.sFile, sFile) && iIter->second.bRepeat == bRepeat) ;
|
||||
return ( EqualNoCase( iIter->second.sFile, sFile) && iIter->second.nRepeat == nRepeat) ;
|
||||
|
||||
// se texture calcolata
|
||||
if ( sFile.empty())
|
||||
return LoadCalcTexture( sName, dMMxPix, dDimX, dDimY, nRepeat) ;
|
||||
|
||||
// leggo l'immagine dal file
|
||||
FIBITMAP* pDib = nullptr ;
|
||||
@@ -61,6 +81,10 @@ TextureMgr::LoadTexture( const string& sName, const string& sFile, double dMMxPi
|
||||
FreeImage_Unload( pTmpDib) ;
|
||||
}
|
||||
|
||||
// recupero le dimensioni in pixel originali dell'immagine
|
||||
int nOriWidth = FreeImage_GetWidth( pDib) ;
|
||||
int nOriHeight = FreeImage_GetHeight( pDib) ;
|
||||
|
||||
// se necessario e possibile, la riduco alle dimensioni caricabili da OpenGL corrente
|
||||
if ( ! TestImageWithOpenGL( pDib))
|
||||
return false ;
|
||||
@@ -69,13 +93,27 @@ TextureMgr::LoadTexture( const string& sName, const string& sFile, double dMMxPi
|
||||
int nWidth = FreeImage_GetWidth( pDib) ;
|
||||
int nHeight = FreeImage_GetHeight( pDib) ;
|
||||
|
||||
// determino il flag di ripetizione
|
||||
GLint nFlag ;
|
||||
switch ( nRepeat) {
|
||||
default : // TXR_CLAMP
|
||||
nFlag = (( m_pScene->GetOpenGLver() < 13) ? GL_CLAMP : GL_CLAMP_TO_BORDER) ;
|
||||
break ;
|
||||
case TXR_REPEAT :
|
||||
nFlag = GL_REPEAT ;
|
||||
break ;
|
||||
case TXR_MIRROR :
|
||||
nFlag = (( m_pScene->GetOpenGLver() < 14) ? GL_REPEAT : GL_MIRRORED_REPEAT) ;
|
||||
break ;
|
||||
}
|
||||
|
||||
// carico l'immagine in OpenGL come texture
|
||||
GLuint nTexName ;
|
||||
glPixelStorei( GL_UNPACK_ALIGNMENT, 4) ;
|
||||
glGenTextures( 1, &nTexName) ;
|
||||
glBindTexture( GL_TEXTURE_2D, nTexName) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, ( bRepeat ? GL_REPEAT : GL_CLAMP_TO_BORDER)) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, ( bRepeat ? GL_REPEAT : GL_CLAMP_TO_BORDER)) ;
|
||||
glBindTexture( GL_TEXTURE_2D, nTexName) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, nFlag) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, nFlag) ;
|
||||
float color[] = { 0.9f, 0.9f, 0.9f, 1.0f} ;
|
||||
glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ;
|
||||
@@ -94,12 +132,12 @@ TextureMgr::LoadTexture( const string& sName, const string& sFile, double dMMxPi
|
||||
|
||||
// se dati mm/pixel determino le dimensioni fisiche della texture
|
||||
if ( dMMxPix > EPS_SMALL && ( dDimX < EPS_SMALL || dDimY < EPS_SMALL)) {
|
||||
dDimX = dMMxPix * nWidth ;
|
||||
dDimY = dMMxPix * nHeight ;
|
||||
dDimX = dMMxPix * nOriWidth ;
|
||||
dDimY = dMMxPix * nOriHeight ;
|
||||
}
|
||||
|
||||
// salvo i dati
|
||||
TextureData textData( sFile, dDimX, dDimY, nWidth, nHeight, nTexName, bRepeat) ;
|
||||
TextureData textData( sFile, dDimX, dDimY, nWidth, nHeight, nTexName, nRepeat) ;
|
||||
m_umTextData.emplace( sName, textData) ;
|
||||
|
||||
return true ;
|
||||
@@ -107,8 +145,96 @@ TextureMgr::LoadTexture( const string& sName, const string& sFile, double dMMxPi
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::UnloadTexture( const std::string& sName)
|
||||
TextureMgr::LoadCalcTexture( const string& sName,
|
||||
double dMMxPix, double dDimX, double dDimY, int nRepeat)
|
||||
{
|
||||
// verifico il contesto OpenGL
|
||||
if ( m_pScene == nullptr || ! m_pScene->MakeCurrent())
|
||||
return false ;
|
||||
|
||||
// verifico se immagine già caricata
|
||||
auto iIter = m_umTextData.find( sName) ;
|
||||
if ( iIter != m_umTextData.end())
|
||||
return ( iIter->second.sFile.empty() && iIter->second.nRepeat == nRepeat) ;
|
||||
|
||||
// le texture calcolate sono : Chessboard e Lines
|
||||
if ( ! EqualNoCase( sName, "Chessboard") && ! EqualNoCase( sName, "Lines"))
|
||||
return false ;
|
||||
bool bChess = ( EqualNoCase( sName, "Chessboard")) ;
|
||||
|
||||
// creo l'immagine
|
||||
const int IMG_WIDTH = 64 ;
|
||||
const int IMG_HEIGHT = 64 ;
|
||||
GLubyte Image[IMG_HEIGHT][IMG_WIDTH][4] ;
|
||||
for ( int i = 0 ; i < IMG_HEIGHT ; ++ i) {
|
||||
for ( int j = 0 ; j < IMG_WIDTH ; ++ j) {
|
||||
int c ;
|
||||
if ( bChess)
|
||||
c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255 ;
|
||||
else
|
||||
c = ((i & 0x8) == 0) * 255 ;
|
||||
Image[i][j][0] = (GLubyte) c ;
|
||||
Image[i][j][1] = (GLubyte) c ;
|
||||
Image[i][j][2] = (GLubyte) c ;
|
||||
Image[i][j][3] = (GLubyte) 255 ;
|
||||
}
|
||||
}
|
||||
|
||||
// determino il flag di ripetizione
|
||||
GLint nFlag ;
|
||||
switch ( nRepeat) {
|
||||
default : // TXR_CLAMP
|
||||
nFlag = (( m_pScene->GetOpenGLver() < 13) ? GL_CLAMP : GL_CLAMP_TO_BORDER) ;
|
||||
break ;
|
||||
case TXR_REPEAT :
|
||||
nFlag = GL_REPEAT ;
|
||||
break ;
|
||||
case TXR_MIRROR :
|
||||
nFlag = (( m_pScene->GetOpenGLver() < 14) ? GL_REPEAT : GL_MIRRORED_REPEAT) ;
|
||||
break ;
|
||||
}
|
||||
|
||||
// carico l'immagine in OpenGL come texture
|
||||
GLuint nTexName ;
|
||||
glPixelStorei( GL_UNPACK_ALIGNMENT, 4) ;
|
||||
glGenTextures( 1, &nTexName) ;
|
||||
glBindTexture( GL_TEXTURE_2D, nTexName) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, nFlag) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, nFlag) ;
|
||||
float color[] = { 0.9f, 0.9f, 0.9f, 1.0f} ;
|
||||
glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) ;
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) ;
|
||||
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, IMG_WIDTH, IMG_HEIGHT,
|
||||
0, GL_BGRA, GL_UNSIGNED_BYTE, &Image) ;
|
||||
|
||||
// verifico che la texture sia stata veramente caricata
|
||||
if ( ! glIsTexture( nTexName))
|
||||
return false ;
|
||||
|
||||
// rendo non corrente questa texture
|
||||
glBindTexture( GL_TEXTURE_2D, 0) ;
|
||||
|
||||
// se dati mm/pixel determino le dimensioni fisiche della texture
|
||||
if ( dMMxPix > EPS_SMALL && ( dDimX < EPS_SMALL || dDimY < EPS_SMALL)) {
|
||||
dDimX = dMMxPix * IMG_WIDTH ;
|
||||
dDimY = dMMxPix * IMG_HEIGHT ;
|
||||
}
|
||||
|
||||
// salvo i dati
|
||||
TextureData textData( "", dDimX, dDimY, IMG_WIDTH, IMG_HEIGHT, nTexName, nRepeat) ;
|
||||
m_umTextData.emplace( sName, textData) ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::UnloadTexture( const string& sName)
|
||||
{
|
||||
// verifico il contesto OpenGL
|
||||
if ( m_pScene == nullptr || ! m_pScene->MakeCurrent())
|
||||
return false ;
|
||||
// cerco la texture, se non la trovo già ok
|
||||
auto iIter = m_umTextData.find( sName) ;
|
||||
if ( iIter == m_umTextData.end())
|
||||
@@ -123,7 +249,16 @@ TextureMgr::UnloadTexture( const std::string& sName)
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::GetPixels( const std::string& sName, int& nWidth, int& nHeight)
|
||||
TextureMgr::Exists( const string& sName)
|
||||
{
|
||||
// ricerca della texture di nome dato
|
||||
auto iIter = m_umTextData.find( sName) ;
|
||||
return ( iIter != m_umTextData.end()) ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::GetPixels( const string& sName, int& nWidth, int& nHeight)
|
||||
{
|
||||
// ricerca della texture di nome dato
|
||||
auto iIter = m_umTextData.find( sName) ;
|
||||
@@ -137,7 +272,7 @@ TextureMgr::GetPixels( const std::string& sName, int& nWidth, int& nHeight)
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::GetDimensions( const std::string& sName, double& dDimX, double& dDimY)
|
||||
TextureMgr::GetDimensions( const string& sName, double& dDimX, double& dDimY)
|
||||
{
|
||||
// ricerca della texture di nome dato
|
||||
auto iIter = m_umTextData.find( sName) ;
|
||||
@@ -151,7 +286,7 @@ TextureMgr::GetDimensions( const std::string& sName, double& dDimX, double& dDim
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
TextureMgr::ChangeDimensions( const std::string& sName, double dDimX, double dDimY)
|
||||
TextureMgr::ChangeDimensions( const string& sName, double dDimX, double dDimY)
|
||||
{
|
||||
// ricerca della texture di nome dato
|
||||
auto iIter = m_umTextData.find( sName) ;
|
||||
@@ -251,30 +386,3 @@ TextureMgr::TestImageWithOpenGL( FIBITMAP*& pDib)
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
//// !!! PROVVISORIO Textures
|
||||
//#define checkImageWidth 64
|
||||
//#define checkImageHeight 64
|
||||
//static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
|
||||
//static GLuint texName;
|
||||
//
|
||||
//void makeCheckImage(void)
|
||||
//{
|
||||
// int i, j, c;
|
||||
//
|
||||
// for (i = 0; i < checkImageHeight; i++) {
|
||||
// for (j = 0; j < checkImageWidth; j++) {
|
||||
// c = (((i&0x8)==0)^((j&0x8)==0))*255;
|
||||
// checkImage[i][j][0] = (GLubyte) c;
|
||||
// checkImage[i][j][1] = (GLubyte) c;
|
||||
// checkImage[i][j][2] = (GLubyte) c;
|
||||
// checkImage[i][j][3] = (GLubyte) 255;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//#include "\EgtDev\Extern\FreeImage\Include\FreeImage.h"
|
||||
//#include "\EgtDev\Include\EgtStringConverter.h"
|
||||
//#include <string>
|
||||
|
||||
Reference in New Issue
Block a user