From 5205709399ecef722b8a8b7330443e9e001f652f Mon Sep 17 00:00:00 2001 From: Mario Luzeiro Date: Sun, 27 Sep 2020 15:51:49 +0100 Subject: [PATCH] 3D-Viewer: use LAYER_SELECT_OVERLAY color for selection preserve material transparency while rendering as selected --- .../c3d_render_ogl_legacy.cpp | 9 ++++- .../c3d_render_ogl_legacy.h | 2 + .../3d_render_ogl_legacy/c_ogl_3dmodel.cpp | 39 +++++++------------ .../3d_render_ogl_legacy/c_ogl_3dmodel.h | 6 +-- 3d-viewer/common_ogl/ogl_utils.cpp | 6 +-- 3d-viewer/common_ogl/ogl_utils.h | 4 +- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.cpp b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.cpp index f58e2e72f1..7e6f7f5476 100644 --- a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.cpp +++ b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.cpp @@ -890,6 +890,8 @@ bool C3D_RENDER_OGL_LEGACY::Redraw( glPopMatrix(); } + m_selectionColor = m_boardAdapter.GetItemColor( LAYER_SELECT_OVERLAY ); + // Render 3D Models (Non-transparent) // ///////////////////////////////////////////////////////////////////////// render_3D_models( false, false ); @@ -1349,9 +1351,12 @@ void C3D_RENDER_OGL_LEGACY::render_3D_module( const MODULE* module, glMultMatrixf( glm::value_ptr( mtx ) ); if( aRenderTransparentOnly ) - modelPtr->Draw_transparent( sM.m_Opacity, module->IsSelected() || aIsSelected ); + modelPtr->Draw_transparent( sM.m_Opacity, + module->IsSelected() || aIsSelected, + m_selectionColor ); else - modelPtr->Draw_opaque( module->IsSelected() || aIsSelected ); + modelPtr->Draw_opaque( module->IsSelected() || aIsSelected, + m_selectionColor ); if( m_boardAdapter.GetFlag( FL_RENDER_OPENGL_SHOW_MODEL_BBOX ) ) { diff --git a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.h b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.h index 517c22f300..49da482b26 100644 --- a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.h +++ b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c3d_render_ogl_legacy.h @@ -228,6 +228,8 @@ private: void set_layer_material( PCB_LAYER_ID aLayerID ); SFVEC4F get_layer_color( PCB_LAYER_ID aLayerID ); + + SFVEC3F m_selectionColor; }; #endif // C3D_RENDER_OGL_LEGACY_H_ diff --git a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.cpp b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.cpp index 482a43ed13..58fb2ce603 100644 --- a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.cpp +++ b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.cpp @@ -395,7 +395,7 @@ void C_OGL_3DMODEL::EndDrawMulti() } -void C_OGL_3DMODEL::Draw( bool aTransparent, float aOpacity, bool aUseSelectedMaterial ) const +void C_OGL_3DMODEL::Draw(bool aTransparent, float aOpacity, bool aUseSelectedMaterial , SFVEC3F aSelectionColor ) const { if( aOpacity <= FLT_EPSILON ) return; @@ -418,47 +418,38 @@ void C_OGL_3DMODEL::Draw( bool aTransparent, float aOpacity, bool aUseSelectedMa glTexCoordPointer( 2, GL_FLOAT, sizeof( VERTEX ), reinterpret_cast( offsetof( VERTEX, m_tex_uv ) ) ); - if( aUseSelectedMaterial ) - aOpacity = aOpacity * 0.75f; - const SFVEC4F param = SFVEC4F( 1.0f, 1.0f, 1.0f, aOpacity ); glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (const float*)¶m.x ); // BeginDrawMulti(); - if( aUseSelectedMaterial ) - OGL_SetDiffuseOnlyMaterial( SFVEC3F( 0.0f, 1.0f, 0.0f ), aOpacity ); - for( auto& mat : m_materials ) { if( ( mat.IsTransparent() != aTransparent ) && ( aOpacity >= 1.0f ) ) continue; - if( !aUseSelectedMaterial ) + switch( m_material_mode ) { - switch( m_material_mode ) - { - case MATERIAL_MODE::NORMAL: - OGL_SetMaterial( mat, aOpacity ); - break; + case MATERIAL_MODE::NORMAL: + OGL_SetMaterial( mat, aOpacity, aUseSelectedMaterial, aSelectionColor ); + break; - case MATERIAL_MODE::DIFFUSE_ONLY: - OGL_SetDiffuseOnlyMaterial( mat.m_Diffuse, aOpacity ); - break; + case MATERIAL_MODE::DIFFUSE_ONLY: + OGL_SetDiffuseOnlyMaterial( mat.m_Diffuse, aOpacity ); + break; - case MATERIAL_MODE::CAD_MODE: - OGL_SetDiffuseOnlyMaterial( MaterialDiffuseToColorCAD( mat.m_Diffuse ), aOpacity ); - break; + case MATERIAL_MODE::CAD_MODE: + OGL_SetDiffuseOnlyMaterial( MaterialDiffuseToColorCAD( mat.m_Diffuse ), aOpacity ); + break; - default: - break; - } + default: + break; } - glDrawElements( GL_TRIANGLES, mat.m_render_idx_count, m_index_buffer_type, - reinterpret_cast( mat.m_render_idx_buffer_offset ) ); + glDrawElements( GL_TRIANGLES, mat.m_render_idx_count, m_index_buffer_type, + reinterpret_cast( mat.m_render_idx_buffer_offset ) ); } // EndDrawMulti(); diff --git a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.h b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.h index 460078bc04..0987844cb2 100644 --- a/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.h +++ b/3d-viewer/3d_rendering/3d_render_ogl_legacy/c_ogl_3dmodel.h @@ -53,12 +53,12 @@ public: /** * @brief Draw_opaque - render the model into the current context */ - void Draw_opaque( bool aUseSelectedMaterial ) const { Draw( false, 1.0f, aUseSelectedMaterial ); } + void Draw_opaque( bool aUseSelectedMaterial, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const { Draw( false, 1.0f, aUseSelectedMaterial, aSelectionColor ); } /** * @brief Draw_transparent - render the model into the current context */ - void Draw_transparent( float aOpacity, bool aUseSelectedMaterial ) const { Draw( true, aOpacity, aUseSelectedMaterial ); } + void Draw_transparent( float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ) const { Draw( true, aOpacity, aUseSelectedMaterial, aSelectionColor ); } /** * @brief Have_opaque - return true if have opaque meshs to render @@ -155,7 +155,7 @@ private: VERTEX *aVtxOut, GLuint *aIdxOut, const glm::vec4 &aColor ); - void Draw( bool aTransparent, float aOpacity, bool aUseSelectedMaterial ) const; + void Draw( bool aTransparent, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor ) const; }; #endif // _C_OGL_3DMODEL_H_ diff --git a/3d-viewer/common_ogl/ogl_utils.cpp b/3d-viewer/common_ogl/ogl_utils.cpp index 9058ca2f3f..7d343314b4 100644 --- a/3d-viewer/common_ogl/ogl_utils.cpp +++ b/3d-viewer/common_ogl/ogl_utils.cpp @@ -142,14 +142,14 @@ GLuint OGL_LoadTexture( const CIMAGE &aImage ) } -void OGL_SetMaterial( const SMATERIAL & aMaterial, float aOpacity ) +void OGL_SetMaterial( const SMATERIAL & aMaterial, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor ) { - const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f ); + const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f ); // !TODO: at this moment, diffuse color is added via // glEnableClientState( GL_COLOR_ARRAY ) so this line may has no effect // but can be used for optimization - const SFVEC4F diffuse = SFVEC4F( aMaterial.m_Diffuse, + const SFVEC4F diffuse = SFVEC4F( aUseSelectedMaterial?aSelectionColor:aMaterial.m_Diffuse, ( 1.0f - aMaterial.m_Transparency ) * aOpacity ); const SFVEC4F specular = SFVEC4F( aMaterial.m_Specular, 1.0f ); const SFVEC4F emissive = SFVEC4F( aMaterial.m_Emissive, 1.0f ); diff --git a/3d-viewer/common_ogl/ogl_utils.h b/3d-viewer/common_ogl/ogl_utils.h index 4c814890c5..3de6983d37 100644 --- a/3d-viewer/common_ogl/ogl_utils.h +++ b/3d-viewer/common_ogl/ogl_utils.h @@ -39,8 +39,10 @@ * @brief OGL_SetMaterial - Set OpenGL materials * @param aMaterial: a material structure with parameters to set * @param aOpacity: main model opacity 0.0 full transparente, 1.0 full opaque + * @param aUseSelectedMaterial: if material should be set for selection mode + * @param aSelectionColor: material to use for selection in case it is enabled */ -void OGL_SetMaterial( const SMATERIAL & aMaterial, float aOpacity ); +void OGL_SetMaterial( const SMATERIAL & aMaterial, float aOpacity, bool aUseSelectedMaterial = false, SFVEC3F aSelectionColor = SFVEC3F( 0.0f ) ); /**