ResInsight/ApplicationLibCode/ModelVisualization/RivTernarySaturationOverlayItem.cpp

259 lines
10 KiB
C++
Raw Normal View History

2014-04-07 06:18:39 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
2014-09-24 00:14:52 -05:00
// Copyright (C) Statoil ASA
// Copyright (C) Ceetron Solutions AS
//
2014-04-07 06:18:39 -05:00
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2014-04-07 06:18:39 -05:00
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
2014-04-07 06:18:39 -05:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RivTernarySaturationOverlayItem.h"
#include "cvfCamera.h"
#include "cvfFont.h"
#include "cvfMatrixState.h"
#include "cvfOpenGL.h"
#include "cvfTextDrawer.h"
#include "cvfViewport.h"
2014-04-07 06:18:39 -05:00
#include "cafInternalLegendRenderTools.h"
2014-04-07 06:18:39 -05:00
#include "cvfRenderStateDepth.h"
#include "cvfRenderStatePolygonOffset.h"
#include "cvfRenderState_FF.h"
2014-04-07 06:18:39 -05:00
//--------------------------------------------------------------------------------------------------
///
2014-04-07 06:18:39 -05:00
//--------------------------------------------------------------------------------------------------
RivTernarySaturationOverlayItem::RivTernarySaturationOverlayItem( cvf::Font* font )
: TitledOverlayFrame( font, 120, 150 )
2014-04-07 06:18:39 -05:00
{
}
//--------------------------------------------------------------------------------------------------
///
2014-04-07 06:18:39 -05:00
//--------------------------------------------------------------------------------------------------
RivTernarySaturationOverlayItem::~RivTernarySaturationOverlayItem()
{
// Empty destructor to avoid errors with undefined types when cvf::ref's destructor gets called
}
//--------------------------------------------------------------------------------------------------
///
2014-04-07 06:18:39 -05:00
//--------------------------------------------------------------------------------------------------
void RivTernarySaturationOverlayItem::setAxisLabelsColor( const cvf::Color3f& color )
2014-04-07 06:18:39 -05:00
{
this->setTextColor( color );
2014-04-07 06:18:39 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2014-04-07 06:18:39 -05:00
//--------------------------------------------------------------------------------------------------
void RivTernarySaturationOverlayItem::render( cvf::OpenGLContext* oglContext,
const cvf::Vec2i& position,
const cvf::Vec2ui& size )
2014-04-07 06:18:39 -05:00
{
renderGeneric( oglContext, position, size, false );
2014-04-07 06:18:39 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2014-04-07 06:18:39 -05:00
//--------------------------------------------------------------------------------------------------
void RivTernarySaturationOverlayItem::renderSoftware( cvf::OpenGLContext* oglContext,
const cvf::Vec2i& position,
const cvf::Vec2ui& size )
2014-04-07 06:18:39 -05:00
{
renderGeneric( oglContext, position, size, true );
2014-04-07 06:18:39 -05:00
}
//--------------------------------------------------------------------------------------------------
/// Set up camera/viewport and render
//--------------------------------------------------------------------------------------------------
void RivTernarySaturationOverlayItem::renderGeneric( cvf::OpenGLContext* oglContext,
const cvf::Vec2i& position,
const cvf::Vec2ui& size,
bool software )
2014-04-07 06:18:39 -05:00
{
if ( size.x() <= 0 || size.y() <= 0 )
2014-04-07 06:18:39 -05:00
{
return;
}
2018-03-08 09:14:13 -06:00
float border = 0.0f;
2018-04-09 04:51:30 -05:00
cvf::Vec2ui sizeFrameBox = size;
2014-04-07 06:18:39 -05:00
cvf::Camera camera;
camera.setViewport( position.x(), position.y(), sizeFrameBox.x(), sizeFrameBox.y() );
2014-04-07 06:18:39 -05:00
camera.setProjectionAsPixelExact2D();
camera.setViewMatrix( cvf::Mat4d::IDENTITY );
2014-04-07 06:18:39 -05:00
camera.applyOpenGL();
camera.viewport()->applyOpenGL( oglContext, cvf::Viewport::CLEAR_DEPTH );
2014-04-07 06:18:39 -05:00
if ( this->backgroundEnabled() )
2018-03-08 09:14:13 -06:00
{
if ( software )
{
caf::InternalLegendRenderTools::renderBackgroundImmediateMode( oglContext,
cvf::Vec2f( sizeFrameBox ),
this->backgroundColor(),
this->backgroundFrameColor() );
2018-03-08 09:14:13 -06:00
}
else
{
const cvf::MatrixState matrixState( camera );
2018-03-08 09:14:13 -06:00
caf::InternalLegendRenderTools::renderBackgroundUsingShaders( oglContext,
matrixState,
cvf::Vec2f( sizeFrameBox ),
this->backgroundColor(),
this->backgroundFrameColor() );
2018-03-08 09:14:13 -06:00
}
border = 8.0f;
2018-03-08 09:14:13 -06:00
}
cvf::TextDrawer textDrawer( this->font() );
textDrawer.setTextColor( this->textColor() );
2014-04-07 06:18:39 -05:00
float lineHeightInPixels = (float)( this->font()->textExtent( "SWAT" ).y() + 2 );
2014-08-01 01:46:46 -05:00
float textPosY = static_cast<float>( size.y() - lineHeightInPixels - border );
for ( size_t it = 0; it < this->titleStrings().size(); it++ )
2014-08-01 01:46:46 -05:00
{
cvf::Vec2f pos( border, textPosY );
textDrawer.addText( this->titleStrings()[it], pos );
2014-08-01 01:46:46 -05:00
textPosY -= lineHeightInPixels;
}
cvf::Vec2f pos( border, textPosY );
textDrawer.addText( "TERNARY", pos );
2014-08-06 08:52:59 -05:00
textPosY -= lineHeightInPixels;
2018-03-08 09:14:13 -06:00
textPosY -= border;
2014-08-06 08:52:59 -05:00
{
cvf::uint sgasTextWidth = this->font()->textExtent( "SGAS" ).x();
textDrawer.addText( "SGAS", cvf::Vec2f( static_cast<float>( ( size.x() / 2 ) - sgasTextWidth / 2 ), textPosY ) );
2014-08-06 08:52:59 -05:00
cvf::uint sgasRangeTextWidth = this->font()->textExtent( m_sgasRange ).x();
2014-08-07 07:44:28 -05:00
textPosY -= lineHeightInPixels;
textDrawer.addText( m_sgasRange,
cvf::Vec2f( static_cast<float>( ( size.x() / 2 ) - sgasRangeTextWidth / 2 ), textPosY ) );
2014-08-06 08:52:59 -05:00
}
2014-08-01 01:46:46 -05:00
textDrawer.addText( "SWAT", cvf::Vec2f( (float)border, (float)( lineHeightInPixels + border ) ) );
textDrawer.addText( m_swatRange, cvf::Vec2f( (float)border, (float)border ) );
2014-05-06 16:29:41 -05:00
{
cvf::uint soilTextWidth = this->font()->textExtent( "SOIL" ).x();
textDrawer.addText( "SOIL",
cvf::Vec2f( static_cast<float>( size.x() - soilTextWidth - border ),
lineHeightInPixels + border ) );
2014-08-06 08:52:59 -05:00
cvf::uint soilRangeTextWidth = this->font()->textExtent( m_soilRange ).x();
float soilRangePos = static_cast<float>( size.x() ) - soilRangeTextWidth - border;
2014-08-06 08:52:59 -05:00
textDrawer.addText( m_soilRange, cvf::Vec2f( soilRangePos, (float)border ) );
}
2014-05-06 16:29:41 -05:00
textDrawer.renderSoftware( oglContext, camera );
2014-04-07 06:18:39 -05:00
2014-08-07 07:44:28 -05:00
textPosY -= 3;
renderAxisImmediateMode( textPosY, 2 * lineHeightInPixels + border, border, sizeFrameBox.x(), oglContext );
2014-04-07 06:18:39 -05:00
CVF_CHECK_OGL( oglContext );
2014-04-07 06:18:39 -05:00
}
//--------------------------------------------------------------------------------------------------
/// Draw the axis using immediate mode OpenGL
//--------------------------------------------------------------------------------------------------
void RivTernarySaturationOverlayItem::renderAxisImmediateMode( float upperBoundY,
float lowerBoundY,
float border,
unsigned int totalWidth,
cvf::OpenGLContext* oglContext )
2014-04-07 06:18:39 -05:00
{
#ifdef CVF_OPENGL_ES
CVF_UNUSED( layout );
CVF_FAIL_MSG( "Not supported on OpenGL ES" );
2014-04-07 06:18:39 -05:00
#else
cvf::RenderStateDepth depth( false );
depth.applyOpenGL( oglContext );
2014-04-07 06:18:39 -05:00
cvf::RenderStateLighting_FF lighting( false );
lighting.applyOpenGL( oglContext );
2014-04-07 06:18:39 -05:00
cvf::Color3ub colA( cvf::Color3::BLUE );
cvf::Color3ub colB( cvf::Color3::GREEN );
cvf::Color3ub colC( cvf::Color3::RED );
2014-04-07 06:18:39 -05:00
// float upperBoundY = static_cast<float>(this->sizeHint().y() - 20);
2014-04-07 06:18:39 -05:00
cvf::Vec3f a( float( border ), lowerBoundY, 0 );
cvf::Vec3f b( static_cast<float>( totalWidth - border ), lowerBoundY, 0 );
cvf::Vec3f c( static_cast<float>( totalWidth / 2 ), upperBoundY, 0 );
2014-04-07 06:18:39 -05:00
// Draw filled rectangle elements
glBegin( GL_TRIANGLE_FAN );
2014-04-07 06:18:39 -05:00
glColor3ubv( colA.ptr() );
glVertex3fv( a.ptr() );
2014-04-07 06:18:39 -05:00
glColor3ubv( colB.ptr() );
glVertex3fv( b.ptr() );
2014-04-07 06:18:39 -05:00
glColor3ubv( colC.ptr() );
glVertex3fv( c.ptr() );
glVertex3fv( c.ptr() );
glEnd();
2014-04-07 06:18:39 -05:00
// Lines
cvf::Color3ub linesColor( cvf::Color3::WHITE );
glColor3ubv( linesColor.ptr() );
glBegin( GL_LINE_LOOP );
glVertex3fv( a.ptr() );
glVertex3fv( b.ptr() );
glVertex3fv( c.ptr() );
2014-04-07 06:18:39 -05:00
glEnd();
cvf::RenderStateDepth resetDepth;
resetDepth.applyOpenGL( oglContext );
2014-04-07 06:18:39 -05:00
// Reset render states
cvf::RenderStateLighting_FF resetLighting;
resetLighting.applyOpenGL( oglContext );
2014-04-07 06:18:39 -05:00
CVF_CHECK_OGL( oglContext );
2014-04-07 06:18:39 -05:00
#endif // CVF_OPENGL_ES
}
2014-05-06 16:29:41 -05:00
//--------------------------------------------------------------------------------------------------
///
2014-05-06 16:29:41 -05:00
//--------------------------------------------------------------------------------------------------
void RivTernarySaturationOverlayItem::setRangeText( const cvf::String& soilRange,
const cvf::String& sgasRange,
const cvf::String& swatRange )
2014-05-06 16:29:41 -05:00
{
m_soilRange = soilRange;
m_sgasRange = sgasRange;
m_swatRange = swatRange;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec2ui RivTernarySaturationOverlayItem::preferredSize()
{
2020-11-06 03:46:38 -06:00
return { 140, 180 }; // Could do more elaborate text width checks.
}