From 17dc39c75893a76dbd1bcfa3eb98b50aa7b01db0 Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Thu, 1 Oct 2015 20:48:12 +0000 Subject: [PATCH] EgtGraphics : - migliorata gestione textures. --- Scene.h | 6 ++++- SceneGeom.cpp | 60 ++++++++++++++++++++++-------------------------- SceneTexture.cpp | 37 ++++++++++++++++++++++++++--- TextureMgr.cpp | 37 +++++++++++++++++++++++++++-- TextureMgr.h | 4 +++- 5 files changed, 105 insertions(+), 39 deletions(-) diff --git a/Scene.h b/Scene.h index fcb3bcb..00191f6 100644 --- a/Scene.h +++ b/Scene.h @@ -163,7 +163,11 @@ class Scene : public IEGrScene return true ; } // Texture virtual bool LoadTexture( const std::string& sName, const std::string& sFile, - double dDimX, double dDimY, bool bRepeat) ; + double dMMxPix, double dDimX, double dDimY, bool bRepeat) ; + virtual bool UnloadTexture( const std::string& sName) ; + virtual bool GetTexturePixels( const std::string& sName, int& nWidth, int& nHeight) ; + virtual bool GetTextureDimensions( const std::string& sName, double& dDimX, double& dDimY) ; + virtual bool ChangeTextureDimensions( const std::string& sName, double dDimX, double dDimY) ; public : // Basic diff --git a/SceneGeom.cpp b/SceneGeom.cpp index 63abd60..f309ce0 100644 --- a/SceneGeom.cpp +++ b/SceneGeom.cpp @@ -416,46 +416,42 @@ Scene::DrawGeoObj( const IGdbIterator& iIter, int nPass, const MdStMkCol& siObj) // eventuale texture string sTxtName ; - if ( iIter.GetInfo( "!TXT", sTxtName)) { - - Frame3d frRef ; - iIter.GetGlobFrame( frRef) ; + if ( iIter.GetInfo( "!TNA", sTxtName)) { + // rendo corrente la texture GLuint texName ; - double dTextDimS = 400 ; - double dTextDimT = 200 ; - m_TextMgr.GetTextureData( sTxtName, texName, dTextDimS, dTextDimT) ; + double dTextDimS ; + double dTextDimT ; + if ( m_TextMgr.GetTextureData( sTxtName, texName, dTextDimS, dTextDimT)) { + glEnable( GL_TEXTURE_2D) ; + glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ; + glBindTexture( GL_TEXTURE_2D, texName) ; + } - glEnable( GL_TEXTURE_2D) ; - glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ; - glBindTexture( GL_TEXTURE_2D, texName) ; - - GLfloat PlaneS[4] ; - GLfloat PlaneT[4] ; - Point3d ptOri(-4009.4378,-2415.5604,0) ; - ptOri.ToLoc( frRef) ; - Vector3d vtX = X_AX ; - vtX.ToLoc( frRef) ; - //vtX.Rotate( Z_AX, 30) ; - Vector3d vtY = Y_AX ; - vtY.ToLoc( frRef) ; - //vtY.Rotate( Z_AX, 30) ; + // recupero il riferimento dell' oggetto + //Frame3d frRef ; + //iIter.GetGlobFrame( frRef) ; + // recupero il riferimento della texture + Frame3d frTxt ; + iIter.GetInfo( "!TFR", frTxt) ; + //frTxt.ToLoc( frRef) ; // creo piano per la S - PlaneS[0] = GLfloat( vtX.x / dTextDimS) ; - PlaneS[1] = GLfloat( vtX.y / dTextDimS) ; - PlaneS[2] = GLfloat( vtX.z / dTextDimS) ; - PlaneS[3] = GLfloat( - vtX * ( ptOri - ORIG) / dTextDimS) ; + GLfloat PlaneS[4] ; + PlaneS[0] = GLfloat( frTxt.VersX().x / dTextDimS) ; + PlaneS[1] = GLfloat( frTxt.VersX().y / dTextDimS) ; + PlaneS[2] = GLfloat( frTxt.VersX().z / dTextDimS) ; + PlaneS[3] = GLfloat( - frTxt.VersX() * ( frTxt.Orig() - ORIG) / dTextDimS) ; // creo piano per la T - PlaneT[0] = GLfloat( vtY.x / dTextDimT) ; - PlaneT[1] = GLfloat( vtY.y / dTextDimT) ; - PlaneT[2] = GLfloat( vtY.z / dTextDimT) ; - PlaneT[3] = GLfloat( - vtY * ( ptOri - ORIG) / dTextDimT) ; - // setto generazione texture automatica + GLfloat PlaneT[4] ; + PlaneT[0] = GLfloat( frTxt.VersY().x / dTextDimT) ; + PlaneT[1] = GLfloat( frTxt.VersY().y / dTextDimT) ; + PlaneT[2] = GLfloat( frTxt.VersY().z / dTextDimT) ; + PlaneT[3] = GLfloat( - frTxt.VersY() * ( frTxt.Orig() - ORIG) / dTextDimT) ; + // setto generazione automatica coordinate S e T per texture glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) ; glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) ; glTexGenfv( GL_S, GL_OBJECT_PLANE, PlaneS) ; glTexGenfv( GL_T, GL_OBJECT_PLANE, PlaneT) ; - glEnable( GL_TEXTURE_GEN_S) ; glEnable( GL_TEXTURE_GEN_T) ; } @@ -463,7 +459,7 @@ Scene::DrawGeoObj( const IGdbIterator& iIter, int nPass, const MdStMkCol& siObj) // eseguo visualizzazione bool bOk = pGraphics->Draw( siObj.nStat, siObj.nMark, bSurfSha, nAlpha, bShowAux) ; - // !!! per textures !!! + // disabilito textures glDisable( GL_TEXTURE_GEN_S) ; glDisable( GL_TEXTURE_GEN_T) ; glDisable( GL_TEXTURE_2D) ; diff --git a/SceneTexture.cpp b/SceneTexture.cpp index 6a4dbe3..d16f1a8 100644 --- a/SceneTexture.cpp +++ b/SceneTexture.cpp @@ -15,11 +15,42 @@ #include "stdafx.h" #include "Scene.h" +using namespace std ; + //---------------------------------------------------------------------------- bool -Scene::LoadTexture( const std::string& sName, const std::string& sFile, - double dDimX, double dDimY, bool bRepeat) +Scene::LoadTexture( const string& sName, const string& sFile, + double dMMxPix, double dDimX, double dDimY, bool bRepeat) { - return m_TextMgr.LoadTexture( sName, sFile, dDimX, dDimY, bRepeat) ; + return m_TextMgr.LoadTexture( sName, sFile, dMMxPix, dDimX, dDimY, bRepeat) ; } +//---------------------------------------------------------------------------- +bool +Scene::UnloadTexture( const string& sName) +{ + return m_TextMgr.UnloadTexture( sName) ; +} + +//---------------------------------------------------------------------------- +bool +Scene::GetTexturePixels( const string& sName, int& nWidth, int& nHeight) +{ + return m_TextMgr.GetPixels( sName, nWidth, nHeight) ; +} + +//---------------------------------------------------------------------------- +bool +Scene::GetTextureDimensions( const string& sName, double& dDimX, double& dDimY) +{ + return m_TextMgr.GetDimensions( sName, dDimX, dDimY) ; +} + +//---------------------------------------------------------------------------- +bool +Scene::ChangeTextureDimensions( const string& sName, double dDimX, double dDimY) +{ + return m_TextMgr.ChangeDimensions( sName, dDimX, dDimY) ; +} + + diff --git a/TextureMgr.cpp b/TextureMgr.cpp index 3ac4abe..ab5b89f 100644 --- a/TextureMgr.cpp +++ b/TextureMgr.cpp @@ -21,7 +21,6 @@ using namespace std ; - //---------------------------------------------------------------------------- TextureMgr::TextureMgr( void) { @@ -42,7 +41,7 @@ TextureMgr::~TextureMgr( void) //---------------------------------------------------------------------------- bool -TextureMgr::LoadTexture( const string& sName, const string& sFile, double dDimX, double dDimY, bool bRepeat) +TextureMgr::LoadTexture( const string& sName, const string& sFile, double dMMxPix, double dDimX, double dDimY, bool bRepeat) { // verifico se immagine già caricata auto iIter = m_umTextData.find( sName) ; @@ -93,6 +92,12 @@ TextureMgr::LoadTexture( const string& sName, const string& sFile, double dDimX, // 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 * nWidth ; + dDimY = dMMxPix * nHeight ; + } + // salvo i dati TextureData textData( sFile, dDimX, dDimY, nWidth, nHeight, nTexName, bRepeat) ; m_umTextData.emplace( sName, textData) ; @@ -116,6 +121,34 @@ TextureMgr::UnloadTexture( const std::string& sName) return true ; } +//---------------------------------------------------------------------------- +bool +TextureMgr::GetPixels( const std::string& sName, int& nWidth, int& nHeight) +{ + // ricerca della texture di nome dato + auto iIter = m_umTextData.find( sName) ; + if ( iIter == m_umTextData.end()) + return false ; + // restituisco le dimensioni in pixel + nWidth = iIter->second.nWidth ; + nHeight = iIter->second.nHeight ; + return true ; +} + +//---------------------------------------------------------------------------- +bool +TextureMgr::GetDimensions( const std::string& sName, double& dDimX, double& dDimY) +{ + // ricerca della texture di nome dato + auto iIter = m_umTextData.find( sName) ; + if ( iIter == m_umTextData.end()) + return false ; + // restituisco le dimensioni fisiche + dDimX = iIter->second.dDimX ; + dDimY = iIter->second.dDimY ; + return true ; +} + //---------------------------------------------------------------------------- bool TextureMgr::ChangeDimensions( const std::string& sName, double dDimX, double dDimY) diff --git a/TextureMgr.h b/TextureMgr.h index 87b48be..a356a7d 100644 --- a/TextureMgr.h +++ b/TextureMgr.h @@ -44,7 +44,9 @@ class TextureMgr public : bool LoadTexture( const std::string& sName, const std::string& sPath, - double dDimX, double dDimY, bool bRepeat) ; + double dMMxPix, double dDimX, double dDimY, bool bRepeat) ; + bool GetPixels( const std::string& sName, int& nWidth, int& nHeight) ; + bool GetDimensions( const std::string& sName, double& dDimX, double& dDimY) ; bool ChangeDimensions( const std::string& sName, double dDimX, double dDimY) ; bool UnloadTexture( const std::string& sName) ; bool GetTextureData( const std::string& sName, unsigned int& nId, double& dDimX, double& dDimY) ;