diff --git a/SceneBasic.cpp b/SceneBasic.cpp index ddfbb2d..8afbae0 100644 --- a/SceneBasic.cpp +++ b/SceneBasic.cpp @@ -946,17 +946,54 @@ bool Scene::GetPixelZ( const Point3d& ptWin, double& dWinZ) { glGetError() ; + + // recupero la ViewPort corrente GLint Viewport[ 4] ; glGetIntegerv( GL_VIEWPORT, Viewport) ; - float dZ ; - glReadBuffer( GL_FRONT) ; - glReadPixels( int( ptWin.x), Viewport[3] - int( ptWin.y), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &dZ) ; - if ( glGetError() == GL_NO_ERROR && dZ > 0.001 && dZ < 0.999) { - dWinZ = dZ ; - return true ; + + // inizializzo i parametri come per la selezione, senza però effettuarla ! + m_bSelect = true ; + m_ptSelCent = ptWin ; + m_nSelW = 13 ; // riducibile ... + m_nSelH = 13 ; // riducibile ... + + // imposto la pick-matrix e la proiezione ridotta + if ( ! Prepare()) { + m_bSelect = false ; + dWinZ = GetProjectedCenter().z ; + return false ; } - dWinZ = GetProjectedCenter().z ; - return false ; + + // forzo rendering opaco per il picking: niente blending, depth write ON + glDisable( GL_LIGHTING) ; + glDisable( GL_BLEND) ; + glEnable( GL_DEPTH_TEST) ; + glDepthMask( GL_TRUE) ; + glClear( GL_DEPTH_BUFFER_BIT) ; // pulizia buffer depth per futura scrittura + + // disegno la scena nella viewport ridotta : questo scrive il depth buffer ( se fossi nella vera + // modalità di selezione GL_SELECT non scriverei le depth nel buffer, in qaunto non c'è una + // modalità di rendering) + DrawGroup( GDB_ID_ROOT, 1, MdStMkCol( GDB_MD_STD, GDB_ST_ON, GDB_MK_OFF, m_colDef)) ; + glFlush() ; // per sicurezza attendo + + // leggo la Z del pizel corrente, le coordinate x e y non cambiano rispetto alla viewport originale + glReadBuffer( GL_FRONT) ; + float dZ ; + bool bOk = true ; + glReadPixels( int( ptWin.x), int( Viewport[3] - ptWin.y), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &dZ) ; + if ( glGetError() == GL_NO_ERROR && dZ > 0.001f && dZ < 0.999f) + dWinZ = dZ ; + else { + bOk = false ; + dWinZ = GetProjectedCenter().z ; + } + + // reset dei parametri + m_bSelect = false ; + Prepare() ; + + return bOk ; } //----------------------------------------------------------------------------