#51 Add background to standard legends. Needed a separate legend implementation, now in caf. Cleaned up CategoryLegend to make it ready for the new background.

This commit is contained in:
Jacob Støren 2018-03-08 11:20:27 +01:00
parent 00236c493e
commit c258528dc5
14 changed files with 1258 additions and 107 deletions

View File

@ -82,10 +82,10 @@
#include "cafDisplayCoordTransform.h"
#include "cafFrameAnimationControl.h"
#include "cafPdmUiTreeOrdering.h"
#include "cafOverlayScalarMapperLegend.h"
#include "cvfDrawable.h"
#include "cvfModelBasicList.h"
#include "cvfOverlayScalarMapperLegend.h"
#include "cvfPart.h"
#include "cvfScene.h"
#include "cvfViewport.h"

View File

@ -55,9 +55,9 @@
#include "cafFrameAnimationControl.h"
#include "cafPdmUiTreeOrdering.h"
#include "cafProgressInfo.h"
#include "cafOverlayScalarMapperLegend.h"
#include "cvfModelBasicList.h"
#include "cvfOverlayScalarMapperLegend.h"
#include "cvfPart.h"
#include "cvfScene.h"
#include "cvfTransform.h"

View File

@ -32,6 +32,7 @@
#include "cafCategoryLegend.h"
#include "cafCategoryMapper.h"
#include "cafOverlayScalarMapperLegend.h"
#include "cafFactory.h"
#include "cafPdmFieldCvfColor.h"
@ -39,7 +40,6 @@
#include "cafPdmUiComboBoxEditor.h"
#include "cafPdmUiLineEditor.h"
#include "cvfOverlayScalarMapperLegend.h"
#include "cvfScalarMapperContinuousLinear.h"
#include "cvfScalarMapperContinuousLog.h"
#include "cvfScalarMapperDiscreteLinear.h"
@ -143,7 +143,7 @@ RimLegendConfig::RimLegendConfig()
m_categoryMapper = new caf::CategoryMapper;
cvf::Font* standardFont = RiaApplication::instance()->standardFont();
m_scalarMapperLegend = new cvf::OverlayScalarMapperLegend(standardFont);
m_scalarMapperLegend = new caf::OverlayScalarMapperLegend(standardFont);
m_categoryLegend = new caf::CategoryLegend(standardFont, m_categoryMapper.p());
updateFieldVisibility();
@ -359,7 +359,7 @@ void RimLegendConfig::updateLegend()
// Using Fixed format
NumberFormatType nft = m_tickNumberFormat();
m_scalarMapperLegend->setTickFormat((cvf::OverlayScalarMapperLegend::NumberFormat)nft);
m_scalarMapperLegend->setTickFormat((caf::OverlayScalarMapperLegend::NumberFormat)nft);
// Set the fixed number of digits after the decimal point to the number needed to show all the significant digits.
int numDecimalDigits = m_precision();
@ -510,7 +510,7 @@ void RimLegendConfig::recreateLegend()
// the legend disappeared because of this, so workaround: recreate the legend when needed:
cvf::Font* standardFont = RiaApplication::instance()->standardFont();
m_scalarMapperLegend = new cvf::OverlayScalarMapperLegend(standardFont);
m_scalarMapperLegend = new caf::OverlayScalarMapperLegend(standardFont);
m_categoryLegend = new caf::CategoryLegend(standardFont, m_categoryMapper.p());
updateLegend();

View File

@ -33,7 +33,6 @@ namespace cvf
class ScalarMapperContinuousLog;
class ScalarMapperContinuousLinear;
class OverlayItem;
class OverlayScalarMapperLegend;
class ScalarMapperDiscreteLinear;
class ScalarMapperDiscreteLog;
class ScalarMapper;
@ -44,6 +43,7 @@ namespace caf
{
class CategoryLegend;
class CategoryMapper;
class OverlayScalarMapperLegend;
}
class Rim3dView;
@ -146,7 +146,7 @@ private:
cvf::ref<cvf::ScalarMapperContinuousLinear> m_linSmoothScalarMapper;
cvf::ref<cvf::ScalarMapper> m_currentScalarMapper;
cvf::ref<cvf::OverlayScalarMapperLegend> m_scalarMapperLegend;
cvf::ref<caf::OverlayScalarMapperLegend> m_scalarMapperLegend;
cvf::ref<caf::CategoryMapper> m_categoryMapper;
cvf::ref<caf::CategoryLegend> m_categoryLegend;

View File

@ -40,6 +40,7 @@
#include "RiuViewerCommands.h"
#include "cafCategoryLegend.h"
#include "cafOverlayScalarMapperLegend.h"
#include "cafCeetronPlusNavigation.h"
#include "cafDisplayCoordTransform.h"
#include "cafEffectGenerator.h"
@ -49,7 +50,6 @@
#include "cvfFont.h"
#include "cvfOpenGLResourceManager.h"
#include "cvfOverlayAxisCross.h"
#include "cvfOverlayScalarMapperLegend.h"
#include "cvfRenderQueueSorter.h"
#include "cvfRenderSequence.h"
#include "cvfRendering.h"
@ -908,17 +908,17 @@ void RiuViewer::updateLegendTextAndTickMarkColor(cvf::OverlayItem* legend)
cvf::Color3f contrastColor = computeContrastColor();
cvf::OverlayScalarMapperLegend* scalarMapperLegend = dynamic_cast<cvf::OverlayScalarMapperLegend*>(legend);
caf::OverlayScalarMapperLegend* scalarMapperLegend = dynamic_cast<caf::OverlayScalarMapperLegend*>(legend);
if (scalarMapperLegend)
{
scalarMapperLegend->setColor(contrastColor);
scalarMapperLegend->setTextColor(contrastColor);
scalarMapperLegend->setLineColor(contrastColor);
}
caf::CategoryLegend* categoryLegend = dynamic_cast<caf::CategoryLegend*>(legend);
if (categoryLegend)
{
categoryLegend->setColor(contrastColor);
categoryLegend->setTextColor(contrastColor);
categoryLegend->setLineColor(contrastColor);
}

View File

@ -81,11 +81,11 @@
#include "cafDisplayCoordTransform.h"
#include "cafPdmUiTreeView.h"
#include "cafSelectionManager.h"
#include "cafOverlayScalarMapperLegend.h"
#include "cvfDrawableGeo.h"
#include "cvfHitItemCollection.h"
#include "cvfOverlayAxisCross.h"
#include "cvfOverlayScalarMapperLegend.h"
#include "cvfPart.h"
#include "cvfTransform.h"

View File

@ -54,7 +54,6 @@ namespace cvf {
class Model;
class OverlayImage;
class OverlayItem;
class OverlayScalarMapperLegend;
class RenderSequence;
class Rendering;
class Scene;

View File

@ -14,6 +14,10 @@ add_library( ${PROJECT_NAME}
cafBoxManipulatorGeometryGenerator.h
cafCategoryLegend.cpp
cafCategoryLegend.h
cafOverlayScalarMapperLegend.h
cafOverlayScalarMapperLegend.cpp
cafInternalLegendRenderTools.h
cafInternalLegendRenderTools.cpp
cafCategoryMapper.cpp
cafCategoryMapper.h
cafFixedAtlasFont.h

View File

@ -39,7 +39,7 @@ namespace caf {
//--------------------------------------------------------------------------------------------------
CategoryLegend::CategoryLegend(Font* font, const CategoryMapper* categoryMapper)
: m_sizeHint(200, 200),
m_color(Color3::BLACK),
m_textColor(Color3::BLACK),
m_lineColor(Color3::BLACK),
m_lineWidth(1),
m_font(font),
@ -74,20 +74,11 @@ void CategoryLegend::setSizeHint(const Vec2ui& size)
}
//--------------------------------------------------------------------------------------------------
/// Set color of the text and lines to be rendered
/// Set color of the text
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setColor(const Color3f& color)
void CategoryLegend::setTextColor(const Color3f& color)
{
m_color = color;
}
//--------------------------------------------------------------------------------------------------
/// Returns the color of the text and lines
//--------------------------------------------------------------------------------------------------
const Color3f& CategoryLegend::color() const
{
return m_color;
m_textColor = color;
}
//--------------------------------------------------------------------------------------------------
@ -108,25 +99,6 @@ void CategoryLegend::setTitle(const String& title)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
String CategoryLegend::title() const
{
String title;
for (size_t i = 0; i < m_titleStrings.size(); ++i)
{
title += m_titleStrings[i];
if (i != m_titleStrings.size() - 1)
{
title += "\n";
}
}
return title;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -145,7 +117,7 @@ size_t CategoryLegend::categoryCount() const
//--------------------------------------------------------------------------------------------------
void CategoryLegend::render(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size)
{
render(oglContext, position, size, false);
renderGeneric(oglContext, position, size, false);
}
//--------------------------------------------------------------------------------------------------
@ -153,7 +125,7 @@ void CategoryLegend::render(OpenGLContext* oglContext, const Vec2i& position, co
//--------------------------------------------------------------------------------------------------
void CategoryLegend::renderSoftware(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size)
{
render(oglContext, position, size, true);
renderGeneric(oglContext, position, size, true);
}
//--------------------------------------------------------------------------------------------------
@ -184,7 +156,7 @@ bool CategoryLegend::pick(int oglXCoord, int oglYCoord, const Vec2i& position, c
//--------------------------------------------------------------------------------------------------
/// Set up camera/viewport and render
//--------------------------------------------------------------------------------------------------
void CategoryLegend::render(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size, bool software)
void CategoryLegend::renderGeneric(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size, bool software)
{
if (size.x() <= 0 || size.y() <= 0)
{
@ -216,7 +188,7 @@ void CategoryLegend::render(OpenGLContext* oglContext, const Vec2i& position, co
else
{
const MatrixState matrixState(camera);
renderLegend(oglContext, &layout, matrixState);
renderLegendUsingShaders(oglContext, &layout, matrixState);
textDrawer.render(oglContext, camera);
}
@ -236,7 +208,7 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer, OverlayColorLegendL
CVF_ASSERT(layout);
textDrawer->setVerticalAlignment(TextDrawer::CENTER);
textDrawer->setTextColor(m_color);
textDrawer->setTextColor(m_textColor);
m_visibleCategoryLabels.clear();
@ -296,7 +268,7 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer, OverlayColorLegendL
//--------------------------------------------------------------------------------------------------
/// Draw the legend using shader programs
//--------------------------------------------------------------------------------------------------
void CategoryLegend::renderLegend(OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout, const MatrixState& matrixState)
void CategoryLegend::renderLegendUsingShaders(OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout, const MatrixState& matrixState)
{
CVF_CALLSITE_OPENGL(oglContext);
@ -492,7 +464,7 @@ void CategoryLegend::renderLegendImmediateMode(OpenGLContext* oglContext, Overla
v0[1] = v1[1] = layout->legendRect.min().y() - 0.5f;
v2[1] = v3[1] = layout->legendRect.max().y() - 0.5f;
glColor3fv(m_color.ptr());
glColor3fv(m_textColor.ptr());
glBegin(GL_LINES);
glVertex3fv(v0);
glVertex3fv(v1);
@ -550,14 +522,6 @@ void CategoryLegend::setLineColor(const Color3f& lineColor)
m_lineColor = lineColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const Color3f& CategoryLegend::lineColor() const
{
return m_lineColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -566,13 +530,7 @@ void CategoryLegend::setLineWidth(int lineWidth)
m_lineWidth = lineWidth;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int CategoryLegend::lineWidth() const
{
return m_lineWidth;
}
} // namespace cvf

View File

@ -29,35 +29,21 @@ public:
CategoryLegend(cvf::Font* font, const CategoryMapper* categoryMapper);
virtual ~CategoryLegend();
virtual cvf::Vec2ui sizeHint();
void setSizeHint(const cvf::Vec2ui& size);
virtual void render(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size);
virtual void renderSoftware(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size);
virtual bool pick(int oglXCoord, int oglYCoord, const cvf::Vec2i& position, const cvf::Vec2ui& size);
void setTextColor(const cvf::Color3f& color);
void setLineColor(const cvf::Color3f& lineColor);
void setLineWidth(int lineWidth);
void setTitle(const cvf::String& title);
void setSizeHint(const cvf::Vec2ui& size);
void setColor(const cvf::Color3f& color);
const cvf::Color3f& color() const;
void setLineColor(const cvf::Color3f& lineColor);
const cvf::Color3f& lineColor() const;
void setLineWidth(int lineWidth);
int lineWidth() const;
void setTitle(const cvf::String& title);
cvf::String title() const;
size_t categoryCount() const;
size_t categoryCount() const;
protected:
cvf::Vec2ui sizeHint() override;
void render(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size) override;
void renderSoftware(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size) override;
bool pick(int oglXCoord, int oglYCoord, const cvf::Vec2i& position, const cvf::Vec2ui& size) override;
//==================================================================================================
//
// Helper for storing layout info
//
//==================================================================================================
struct OverlayColorLegendLayoutInfo
{
OverlayColorLegendLayoutInfo(const cvf::Vec2i& pos, const cvf::Vec2ui& setSize)
@ -85,24 +71,23 @@ protected:
cvf::Vec2ui size;
};
void layoutInfo(OverlayColorLegendLayoutInfo* layout);
void render(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size, bool software);
virtual void renderLegend(cvf::OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout, const cvf::MatrixState& matrixState);
virtual void renderLegendImmediateMode(cvf::OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout);
virtual void setupTextDrawer(cvf::TextDrawer* textDrawer, OverlayColorLegendLayoutInfo* layout);
void layoutInfo(OverlayColorLegendLayoutInfo* layout);
void renderGeneric(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size, bool software);
void setupTextDrawer(cvf::TextDrawer* textDrawer, OverlayColorLegendLayoutInfo* layout);
void renderLegendUsingShaders(cvf::OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout, const cvf::MatrixState& matrixState);
void renderLegendImmediateMode(cvf::OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout);
protected:
std::vector<bool> m_visibleCategoryLabels; // Skip labels ending up on top of previous visible label
cvf::Vec2ui m_sizeHint; // Pixel size of the color legend area
cvf::Color3f m_color;
cvf::Color3f m_lineColor;
int m_lineWidth;
std::vector<cvf::String> m_titleStrings;
cvf::ref<cvf::Font> m_font;
std::vector<bool> m_visibleCategoryLabels; // Skip labels ending up on top of previous visible label
cvf::Vec2ui m_sizeHint; // Pixel size of the color legend area
cvf::Color3f m_textColor;
cvf::Color3f m_lineColor;
int m_lineWidth;
std::vector<cvf::String> m_titleStrings;
cvf::ref<cvf::Font> m_font;
cvf::cref<CategoryMapper> m_categoryMapper;
};

View File

@ -0,0 +1,203 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2018- Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafInternalLegendRenderTools.h"
#include "cvfOpenGL.h"
#include "cvfRenderStateDepth.h"
#include "cvfRenderStateLine.h"
#include "cvfRenderStateBlending.h"
#include "cvfShaderProgram.h"
#include "cvfOpenGLContext.h"
#include "cvfOpenGLResourceManager.h"
#include <array>
#include "cvfUniform.h"
#include "cvfRenderState_FF.h"
using namespace cvf;
namespace caf
{
//--------------------------------------------------------------------------------------------------
/// Render a semi transparent background frame
//--------------------------------------------------------------------------------------------------
void InternalLegendRenderTools::renderBackgroundUsingShaders(OpenGLContext* oglContext,
const MatrixState& matrixState,
const Vec2f& size,
const Color4f& backgroundColor,
const Color4f& backgroundFrameColor)
{
CVF_CALLSITE_OPENGL(oglContext);
RenderStateDepth depth(false);
depth.applyOpenGL(oglContext);
RenderStateLine line(1.0f);
line.applyOpenGL(oglContext);
RenderStateBlending blend;
blend.configureTransparencyBlending();
blend.applyOpenGL(oglContext);
// Shader program
ref<ShaderProgram> shaderProgram = oglContext->resourceManager()->getLinkedUnlitColorShaderProgram(oglContext);
CVF_TIGHT_ASSERT(shaderProgram.notNull());
if (shaderProgram->useProgram(oglContext))
{
shaderProgram->clearUniformApplyTracking();
shaderProgram->applyFixedUniforms(oglContext, matrixState);
}
std::array<Vec3f, 4> vertexArray ={
Vec3f(1 , 1, 0.0f),
Vec3f(size.x(), 1, 0.0f),
Vec3f(size.x(), size.y(), 0.0f),
Vec3f(1 , size.y(), 0.0f),
};
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(ShaderProgram::VERTEX);
glVertexAttribPointer(ShaderProgram::VERTEX, 3, GL_FLOAT, GL_FALSE, 0, vertexArray.data());
// Draw frame background
UniformFloat backgroundColorUniform("u_color", backgroundColor);
shaderProgram->applyUniform(oglContext, backgroundColorUniform);
// Triangle indices for the frame background
static const ushort backgroundTriangleIndices[] = { 0, 1, 2, 2, 3, 0};
glDrawRangeElements(GL_TRIANGLES, 0, 3, 6, GL_UNSIGNED_SHORT, backgroundTriangleIndices);
// Draw frame border lines
UniformFloat uniformColor("u_color", backgroundFrameColor);
shaderProgram->applyUniform(oglContext, uniformColor);
static const ushort frameLineIndices[] = { 0, 1,
1, 2,
2, 3,
3, 0 };
glDrawRangeElements(GL_LINES, 0, 3, 8, GL_UNSIGNED_SHORT, frameLineIndices);
glDisableVertexAttribArray(ShaderProgram::VERTEX);
CVF_TIGHT_ASSERT(shaderProgram.notNull());
shaderProgram->useNoProgram(oglContext);
// Reset render states
RenderStateDepth resetDepth;
resetDepth.applyOpenGL(oglContext);
RenderStateLine resetLine;
resetLine.applyOpenGL(oglContext);
RenderStateBlending resetblend;
resetblend.applyOpenGL(oglContext);
CVF_CHECK_OGL(oglContext);
}
//--------------------------------------------------------------------------------------------------
/// Draw a background rectangle using OGL 1.1 compatibility
//--------------------------------------------------------------------------------------------------
void InternalLegendRenderTools::renderBackgroundImmediateMode(OpenGLContext* oglContext,
const Vec2f& size,
const Color4f& backgroundColor,
const Color4f& backgroundFrameColor)
{
RenderStateDepth depth(false);
depth.applyOpenGL(oglContext);
RenderStateLighting_FF lighting(false);
lighting.applyOpenGL(oglContext);
RenderStateBlending blend;
blend.configureTransparencyBlending();
blend.applyOpenGL(oglContext);
// Frame vertices
std::array<Vec3f, 4> vertexArray = {
Vec3f(1 , 1, 0.0f),
Vec3f(size.x(), 1, 0.0f),
Vec3f(size.x(), size.y(), 0.0f),
Vec3f(1 , size.y(), 0.0f),
};
glColor4fv(backgroundColor.ptr());
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(vertexArray[0].ptr());
glVertex3fv(vertexArray[1].ptr());
glVertex3fv(vertexArray[2].ptr());
glVertex3fv(vertexArray[3].ptr());
glEnd();
// Render Line around
{
glColor4fv(backgroundFrameColor.ptr());
glBegin(GL_LINES);
glVertex3fv(vertexArray[0].ptr());
glVertex3fv(vertexArray[1].ptr());
glVertex3fv(vertexArray[1].ptr());
glVertex3fv(vertexArray[2].ptr());
glVertex3fv(vertexArray[2].ptr());
glVertex3fv(vertexArray[3].ptr());
glVertex3fv(vertexArray[3].ptr());
glVertex3fv(vertexArray[0].ptr());
glEnd();
}
// Reset render states
RenderStateLighting_FF resetLighting;
resetLighting.applyOpenGL(oglContext);
RenderStateDepth resetDepth;
resetDepth.applyOpenGL(oglContext);
RenderStateBlending resetblend;
resetblend.applyOpenGL(oglContext);
CVF_CHECK_OGL(oglContext);
}
}

View File

@ -0,0 +1,71 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2018- Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cvfOpenGLContext.h"
#include "cvfMatrixState.h"
#include "cvfColor4.h"
//==================================================================================================
//
// Tools to render legend stuff
//
//==================================================================================================
namespace caf {
class InternalLegendRenderTools
{
using OpenGLContext = cvf::OpenGLContext;
using Color4f = cvf::Color4f;
using String = cvf::String;
using MatrixState = cvf::MatrixState;
using Vec2f = cvf::Vec2f;
public:
static void renderBackgroundUsingShaders(OpenGLContext* oglContext,
const MatrixState& matrixState,
const Vec2f& size,
const Color4f& backgroundColor,
const Color4f& backgroundFrameColor);
static void renderBackgroundImmediateMode(OpenGLContext* oglContext,
const Vec2f& size,
const Color4f& backgroundColor,
const Color4f& backgroundFrameColor);
};
}

View File

@ -0,0 +1,765 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2018- Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cvfBase.h"
#include "cafOverlayScalarMapperLegend.h"
#include "cvfOpenGL.h"
#include "cvfOpenGLResourceManager.h"
#include "cvfGeometryBuilderDrawableGeo.h"
#include "cvfGeometryUtils.h"
#include "cvfViewport.h"
#include "cvfCamera.h"
#include "cvfTextDrawer.h"
#include "cvfFont.h"
#include "cvfShaderProgram.h"
#include "cvfShaderProgramGenerator.h"
#include "cvfShaderSourceProvider.h"
#include "cvfShaderSourceRepository.h"
#include "cvfUniform.h"
#include "cvfMatrixState.h"
#include "cvfBufferObjectManaged.h"
#include "cvfGlyph.h"
#include "cvfRenderStateDepth.h"
#include "cvfRenderStateLine.h"
#include "cafInternalLegendRenderTools.h"
#ifndef CVF_OPENGL_ES
#include "cvfRenderState_FF.h"
#endif
#include "cvfScalarMapper.h"
#include <array>
#include "cvfRenderStateBlending.h"
namespace caf {
using namespace cvf;
//==================================================================================================
///
/// \class cvf::OverlayColorLegend
/// \ingroup Render
///
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
/// Constructor
//--------------------------------------------------------------------------------------------------
OverlayScalarMapperLegend::OverlayScalarMapperLegend(Font* font)
: m_sizeHint(200, 200),
m_textColor(Color3::BLACK),
m_lineColor(Color3::BLACK),
m_backgroundColor(1.0f, 1.0f, 1.0f, 0.8f),
m_backgroundFrameColor(0.0f, 0.0f, 0.0f, 0.5f),
m_isBackgroundEnabled(true),
m_lineWidth(1),
m_font(font),
m_tickNumberPrecision(4),
m_numberFormat(AUTO)
{
CVF_ASSERT(font);
CVF_ASSERT(!font->isEmpty());
m_tickValues.reserve(3);
m_tickValues.add(0.0);
m_tickValues.add(0.5);
m_tickValues.add(1.0);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
OverlayScalarMapperLegend::~OverlayScalarMapperLegend()
{
// Empty destructor to avoid errors with undefined types when cvf::ref's destructor gets called
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec2ui OverlayScalarMapperLegend::sizeHint()
{
return m_sizeHint;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setScalarMapper(const ScalarMapper* scalarMapper)
{
m_scalarMapper = scalarMapper;
if (m_scalarMapper.notNull())
{
std::vector<double> levelValues;
m_scalarMapper->majorTickValues(&levelValues);
m_tickValues.assign(levelValues);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setSizeHint(const Vec2ui& size)
{
m_sizeHint = size;
}
//--------------------------------------------------------------------------------------------------
/// Set color of the text
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setTextColor(const Color3f& color)
{
m_textColor = color;
}
//--------------------------------------------------------------------------------------------------
/// Set the title (text that will be rendered above the legend)
///
/// The legend supports multi-line titles. Separate each line with a '\n' character
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setTitle(const String& title)
{
// Title
if (title.isEmpty())
{
m_titleStrings.clear();
}
else
{
m_titleStrings = title.split("\n");
}
}
//--------------------------------------------------------------------------------------------------
/// Hardware rendering using shader programs
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::render(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size)
{
renderGeneric(oglContext, position, size, false);
}
//--------------------------------------------------------------------------------------------------
/// Software rendering using software
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::renderSoftware(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size)
{
renderGeneric(oglContext, position, size, true);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool OverlayScalarMapperLegend::pick(int oglXCoord, int oglYCoord, const Vec2i& position, const Vec2ui& size)
{
Recti oglRect(position, size.x(), size.y());
OverlayColorLegendLayoutInfo layoutInViewPortCoords(oglRect.min(), Vec2ui(oglRect.width(), oglRect.height()));
layoutInfo(&layoutInViewPortCoords);
Vec2i legendBarOrigin = oglRect.min();
legendBarOrigin.x() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().x());
legendBarOrigin.y() += static_cast<uint>(layoutInViewPortCoords.legendRect.min().y());
Recti legendBarRect = Recti(legendBarOrigin, static_cast<uint>(layoutInViewPortCoords.legendRect.width()), static_cast<uint>(layoutInViewPortCoords.legendRect.height()));
if ((oglXCoord > legendBarRect.min().x()) && (oglXCoord < legendBarRect.max().x()) &&
(oglYCoord > legendBarRect.min().y()) && (oglYCoord < legendBarRect.max().y()))
{
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
/// Set up camera/viewport and render
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::renderGeneric(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size, bool software)
{
if (size.x() <= 0 || size.y() <= 0)
{
return;
}
Camera camera;
camera.setViewport(position.x(), position.y(), size.x(), size.y());
camera.setProjectionAsPixelExact2D();
camera.setViewMatrix(Mat4d::IDENTITY);
camera.applyOpenGL();
camera.viewport()->applyOpenGL(oglContext, Viewport::CLEAR_DEPTH);
// Get layout information
// Todo: Cache this between renderings. Update only when needed.
OverlayColorLegendLayoutInfo layout(position, size);
layoutInfo(&layout);
// Set up text drawer
float maxLegendRightPos = 0;
TextDrawer textDrawer(m_font.p());
setupTextDrawer(&textDrawer, &layout, &maxLegendRightPos);
Vec2f backgroundSize(maxLegendRightPos + 3.0f, (float)size.y());
// Do the actual rendering
if (software)
{
if (m_isBackgroundEnabled) InternalLegendRenderTools::renderBackgroundImmediateMode(oglContext, backgroundSize, m_backgroundColor, m_backgroundFrameColor);
renderLegendImmediateMode(oglContext, &layout);
textDrawer.renderSoftware(oglContext, camera);
}
else
{
const MatrixState matrixState(camera);
if (m_isBackgroundEnabled) InternalLegendRenderTools::renderBackgroundUsingShaders(oglContext, matrixState, backgroundSize, m_backgroundColor, m_backgroundFrameColor);
renderLegendUsingShaders(oglContext, &layout, matrixState);
textDrawer.render(oglContext, camera);
}
CVF_CHECK_OGL(oglContext);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setupTextDrawer(TextDrawer* textDrawer, const OverlayColorLegendLayoutInfo* layout, float* maxLegendRightPos)
{
CVF_ASSERT(layout);
float legendRight = 0.0f;
textDrawer->setVerticalAlignment(TextDrawer::CENTER);
textDrawer->setTextColor(m_textColor);
m_visibleTickLabels.clear();
const float textX = layout->tickX + 5;
const float overlapTolerance = 1.2f * layout->charHeight;
float lastVisibleTextY = 0.0;
size_t numTicks = m_tickValues.size();
size_t it;
for (it = 0; it < numTicks; it++)
{
float textY = static_cast<float>(layout->legendRect.min().y() + layout->tickPixelPos->get(it));
// Always draw first and last tick label. For all others, skip drawing if text ends up
// on top of the previous label.
if (it != 0 && it != (numTicks - 1))
{
if (cvf::Math::abs(textY - lastVisibleTextY) < overlapTolerance)
{
m_visibleTickLabels.push_back(false);
continue;
}
// Make sure it does not overlap the last tick as well
float lastTickY = static_cast<float>(layout->legendRect.max().y() );
if (cvf::Math::abs(textY - lastTickY) < overlapTolerance)
{
m_visibleTickLabels.push_back(false);
continue;
}
}
double tickValue = m_tickValues[it];
String valueString;
switch (m_numberFormat)
{
case FIXED:
valueString = String::number(tickValue, 'f', m_tickNumberPrecision);
break;
case SCIENTIFIC:
valueString = String::number(tickValue, 'e', m_tickNumberPrecision);
break;
default:
valueString = String::number(tickValue);
break;
}
Vec2f pos(textX, textY);
textDrawer->addText(valueString, pos);
float neededRightPos = pos.x() + m_font->textExtent(valueString).x();
legendRight = legendRight >= neededRightPos ? legendRight :neededRightPos;
lastVisibleTextY = textY;
m_visibleTickLabels.push_back(true);
}
float titleY = static_cast<float>(layout->size.y()) - layout->margins.y() - layout->charHeight/2.0f;
for (it = 0; it < m_titleStrings.size(); it++)
{
Vec2f pos(layout->margins.x(), titleY);
textDrawer->addText(m_titleStrings[it], pos);
float neededRightPos = pos.x() + m_font->textExtent(m_titleStrings[it]).x();
legendRight = legendRight >= neededRightPos ? legendRight :neededRightPos;
titleY -= layout->lineSpacing;
}
*maxLegendRightPos = legendRight;
}
//--------------------------------------------------------------------------------------------------
/// Draw the legend using shader programs
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout, const MatrixState& matrixState)
{
CVF_CALLSITE_OPENGL(oglContext);
CVF_TIGHT_ASSERT(layout);
CVF_TIGHT_ASSERT(layout->size.x() > 0);
CVF_TIGHT_ASSERT(layout->size.y() > 0);
RenderStateDepth depth(false);
depth.applyOpenGL(oglContext);
RenderStateLine line(static_cast<float>(m_lineWidth));
line.applyOpenGL(oglContext);
// All vertices. Initialized here to set Z to zero once and for all.
static float vertexArray[] =
{
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f
};
// Per vector convenience pointers
float* v0 = &vertexArray[0];
float* v1 = &vertexArray[3];
float* v2 = &vertexArray[6];
float* v3 = &vertexArray[9];
float* v4 = &vertexArray[12];
// Constant coordinates
v0[0] = v3[0] = layout->x0;
v1[0] = v4[0] = layout->x1;
// Connects
static const ushort trianglesConnects[] = { 0, 1, 4, 0, 4, 3 };
ref<ShaderProgram> shaderProgram = oglContext->resourceManager()->getLinkedUnlitColorShaderProgram(oglContext);
CVF_TIGHT_ASSERT(shaderProgram.notNull());
if (shaderProgram->useProgram(oglContext))
{
shaderProgram->clearUniformApplyTracking();
shaderProgram->applyFixedUniforms(oglContext, matrixState);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(ShaderProgram::VERTEX);
glVertexAttribPointer(ShaderProgram::VERTEX, 3, GL_FLOAT, GL_FALSE, 0, vertexArray);
// Render color bar as one colored quad per pixel
int legendHeightPixelCount = static_cast<int>(layout->tickPixelPos->get(m_tickValues.size()-1) - layout->tickPixelPos->get(0) + 0.01);
if (m_scalarMapper.notNull())
{
int iPx;
for (iPx = 0; iPx < legendHeightPixelCount; iPx++)
{
const Color3ub& clr = m_scalarMapper->mapToColor(m_scalarMapper->domainValue((iPx+0.5)/legendHeightPixelCount));
float y0 = static_cast<float>(layout->legendRect.min().y() + iPx);
float y1 = static_cast<float>(layout->legendRect.min().y() + iPx + 1);
// Dynamic coordinates for rectangle
v0[1] = v1[1] = y0;
v3[1] = v4[1] = y1;
// Draw filled rectangle elements
{
UniformFloat uniformColor("u_color", Color4f(Color3f(clr)));
shaderProgram->applyUniform(oglContext, uniformColor);
#ifdef CVF_OPENGL_ES
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, trianglesConnects);
#else
glDrawRangeElements(GL_TRIANGLES, 0, 4, 6, GL_UNSIGNED_SHORT, trianglesConnects);
#endif
}
}
}
// Render frame
// Dynamic coordinates for tickmarks-lines
bool isRenderingFrame = true;
if (isRenderingFrame)
{
v0[0] = v2[0] = layout->legendRect.min().x()-0.5f;
v1[0] = v3[0] = layout->legendRect.max().x()-0.5f;
v0[1] = v1[1] = layout->legendRect.min().y()-0.5f;
v2[1] = v3[1] = layout->legendRect.max().y()-0.5f;
static const ushort frameConnects[] = { 0, 1, 1, 3, 3, 2, 2, 0};
UniformFloat uniformColor("u_color", Color4f(m_lineColor));
shaderProgram->applyUniform(oglContext, uniformColor);
#ifdef CVF_OPENGL_ES
glDrawElements(GL_LINES, 8, GL_UNSIGNED_SHORT, frameConnects);
#else
glDrawRangeElements(GL_LINES, 0, 3, 8, GL_UNSIGNED_SHORT, frameConnects);
#endif
}
// Render tickmarks
bool isRenderingTicks = true;
if (isRenderingTicks)
{
// Constant coordinates
v0[0] = layout->x0;
v1[0] = layout->x1 - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v2[0] = layout->x1;
v3[0] = layout->tickX - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v4[0] = layout->tickX;
static const ushort tickLinesWithLabel[] = { 0, 4 };
static const ushort tickLinesWoLabel[] = { 2, 3 };
size_t ic;
for (ic = 0; ic < m_tickValues.size(); ic++)
{
float y0 = static_cast<float>(layout->legendRect.min().y() + layout->tickPixelPos->get(ic) - 0.5f);
// Dynamic coordinates for tickmarks-lines
v0[1] = v1[1] = v2[1] = v3[1] = v4[1] = y0;
UniformFloat uniformColor("u_color", Color4f(m_lineColor));
shaderProgram->applyUniform(oglContext, uniformColor);
const ushort * linesConnects;
if ( m_visibleTickLabels[ic])
{
linesConnects = tickLinesWithLabel;
}
else
{
linesConnects = tickLinesWoLabel;
}
#ifdef CVF_OPENGL_ES
glDrawElements(GL_LINES, 2, GL_UNSIGNED_SHORT, linesConnects);
#else
glDrawRangeElements(GL_LINES, 0, 4, 2, GL_UNSIGNED_SHORT, linesConnects);
#endif
}
}
glDisableVertexAttribArray(ShaderProgram::VERTEX);
CVF_TIGHT_ASSERT(shaderProgram.notNull());
shaderProgram->useNoProgram(oglContext);
// Reset render states
RenderStateDepth resetDepth;
resetDepth.applyOpenGL(oglContext);
RenderStateLine resetLine;
resetLine.applyOpenGL(oglContext);
CVF_CHECK_OGL(oglContext);
}
//--------------------------------------------------------------------------------------------------
/// Draw the legend using immediate mode OpenGL
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::renderLegendImmediateMode(OpenGLContext* oglContext, OverlayColorLegendLayoutInfo* layout)
{
#ifdef CVF_OPENGL_ES
CVF_UNUSED(layout);
CVF_FAIL_MSG("Not supported on OpenGL ES");
#else
CVF_TIGHT_ASSERT(layout);
CVF_TIGHT_ASSERT(layout->size.x() > 0);
CVF_TIGHT_ASSERT(layout->size.y() > 0);
RenderStateDepth depth(false);
depth.applyOpenGL(oglContext);
RenderStateLighting_FF lighting(false);
lighting.applyOpenGL(oglContext);
// All vertices. Initialized here to set Z to zero once and for all.
static float vertexArray[] =
{
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
};
// Per vector convenience pointers
float* v0 = &vertexArray[0];
float* v1 = &vertexArray[3];
float* v2 = &vertexArray[6];
float* v3 = &vertexArray[9];
float* v4 = &vertexArray[12];
// Constant coordinates
v0[0] = v3[0] = layout->x0;
v1[0] = v4[0] = layout->x1;
// Render color bar as one colored quad per pixel
int legendHeightPixelCount = static_cast<int>(layout->tickPixelPos->get(m_tickValues.size() - 1) - layout->tickPixelPos->get(0) + 0.01);
if (m_scalarMapper.notNull())
{
int iPx;
for (iPx = 0; iPx < legendHeightPixelCount; iPx++)
{
const Color3ub& clr = m_scalarMapper->mapToColor(m_scalarMapper->domainValue((iPx+0.5)/legendHeightPixelCount));
float y0 = static_cast<float>(layout->legendRect.min().y() + iPx);
float y1 = static_cast<float>(layout->legendRect.min().y() + iPx + 1);
// Dynamic coordinates for rectangle
v0[1] = v1[1] = y0;
v3[1] = v4[1] = y1;
// Draw filled rectangle elements
glColor3ubv(clr.ptr());
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(v0);
glVertex3fv(v1);
glVertex3fv(v4);
glVertex3fv(v3);
glEnd();
}
}
// Render frame
// Dynamic coordinates for tickmarks-lines
bool isRenderingFrame = true;
if (isRenderingFrame)
{
v0[0] = v2[0] = layout->legendRect.min().x()-0.5f;
v1[0] = v3[0] = layout->legendRect.max().x()-0.5f;
v0[1] = v1[1] = layout->legendRect.min().y()-0.5f;
v2[1] = v3[1] = layout->legendRect.max().y()-0.5f;
glColor3fv(m_textColor.ptr());
glBegin(GL_LINES);
glVertex3fv(v0);
glVertex3fv(v1);
glVertex3fv(v1);
glVertex3fv(v3);
glVertex3fv(v3);
glVertex3fv(v2);
glVertex3fv(v2);
glVertex3fv(v0);
glEnd();
}
// Render tickmarks
bool isRenderingTicks = true;
if (isRenderingTicks)
{
// Constant coordinates
v0[0] = layout->x0;
v1[0] = layout->x1 - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v2[0] = layout->x1;
v3[0] = layout->tickX - 0.5f*(layout->tickX - layout->x1) - 0.5f;
v4[0] = layout->tickX;
size_t ic;
for (ic = 0; ic < m_tickValues.size(); ic++)
{
float y0 = static_cast<float>(layout->legendRect.min().y() + layout->tickPixelPos->get(ic) - 0.5f);
// Dynamic coordinates for tickmarks-lines
v0[1] = v1[1] = v2[1] = v3[1] = v4[1] = y0;
glColor3fv(m_textColor.ptr());
glBegin(GL_LINES);
if ( m_visibleTickLabels[ic])
{
glVertex3fv(v0);
glVertex3fv(v4);
}
else
{
glVertex3fv(v2);
glVertex3fv(v3);
}
glEnd();
}
}
// Reset render states
RenderStateLighting_FF resetLighting;
resetLighting.applyOpenGL(oglContext);
RenderStateDepth resetDepth;
resetDepth.applyOpenGL(oglContext);
CVF_CHECK_OGL(oglContext);
#endif // CVF_OPENGL_ES
}
//--------------------------------------------------------------------------------------------------
/// Get layout information
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
{
CVF_TIGHT_ASSERT(layout);
ref<Glyph> glyph = m_font->getGlyph(L'A');
layout->charHeight = static_cast<float>(glyph->height());
layout->lineSpacing = layout->charHeight*1.5f;
layout->margins = Vec2f(4.0f, 4.0f);
float legendWidth = 25.0f;
float legendHeight = static_cast<float>(layout->size.y()) - 2*layout->margins.y() - static_cast<float>(m_titleStrings.size())*layout->lineSpacing - layout->lineSpacing;
layout->legendRect = Rectf(layout->margins.x(), layout->margins.y() + layout->charHeight/2.0f, legendWidth, legendHeight);
if (layout->legendRect.width() < 1 || layout->legendRect.height() < 1)
{
return;
}
layout->x0 = layout->margins.x();
layout->x1 = layout->margins.x() + layout->legendRect.width();
layout->tickX = layout->x1 + 5;
// Build array containing the pixel positions of all the ticks
size_t numTicks = m_tickValues.size();
layout->tickPixelPos = new DoubleArray(numTicks);
size_t i;
for (i = 0; i < numTicks; i++)
{
double t;
if (m_scalarMapper.isNull()) t = 0;
else t = m_scalarMapper->normalizedValue(m_tickValues[i]);
t = Math::clamp(t, 0.0, 1.1);
if (i != numTicks -1)
{
layout->tickPixelPos->set(i, t*layout->legendRect.height());
}
else
{
layout->tickPixelPos->set(i, layout->legendRect.height()); // Make sure we get a value at the top even if the scalarmapper range is zero
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setLineColor(const Color3f& lineColor)
{
m_lineColor = lineColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setBackgroundColor(const Color4f& backgroundColor)
{
m_backgroundColor = backgroundColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setBackgroundFrameColor(const Color4f& backgroundFrameColor)
{
m_backgroundFrameColor = backgroundFrameColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::enableBackground(bool enable)
{
m_isBackgroundEnabled = enable;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setLineWidth(int lineWidth)
{
m_lineWidth = lineWidth;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setTickPrecision(int precision)
{
m_tickNumberPrecision = precision;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setTickFormat(NumberFormat format)
{
m_numberFormat = format;
}
} // namespace cvf

View File

@ -0,0 +1,166 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2018- Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cvfOverlayItem.h"
#include "cvfArray.h"
#include "cvfCamera.h"
#include "cvfString.h"
#include "cvfRect.h"
namespace cvf {
class Font;
class ShaderProgram;
class MatrixState;
class TextDrawer;
class ScalarMapper;
}
namespace caf {
//==================================================================================================
//
// Overlay color legend
//
//==================================================================================================
class OverlayScalarMapperLegend : public cvf::OverlayItem
{
using Font = cvf::Font;
using ScalarMapper = cvf::ScalarMapper;
using OpenGLContext = cvf::OpenGLContext;
using Vec2i = cvf::Vec2i;
using Vec2ui = cvf::Vec2ui;
using Color3f = cvf::Color3f;
using Color4f = cvf::Color4f;
using String = cvf::String;
using DoubleArray = cvf::DoubleArray;
using MatrixState = cvf::MatrixState;
using Vec2f = cvf::Vec2f;
using Rectf = cvf::Rectf;
using TextDrawer = cvf::TextDrawer;
public:
OverlayScalarMapperLegend(Font* font);
virtual ~OverlayScalarMapperLegend();
void setScalarMapper(const ScalarMapper* scalarMapper);
void setSizeHint(const Vec2ui& size);
void setTextColor(const Color3f& color);
void setLineColor(const Color3f& lineColor);
void setLineWidth(int lineWidth);
void setTickPrecision(int precision);
enum NumberFormat { AUTO, SCIENTIFIC, FIXED};
void setTickFormat(NumberFormat format);
void enableBackground(bool enable);
void setBackgroundColor(const Color4f& backgroundColor);
void setBackgroundFrameColor(const Color4f& backgroundFrameColor);
void setTitle(const String& title);
protected:
Vec2ui sizeHint() override;
void render(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size) override;
void renderSoftware(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size) override;
bool pick(int oglXCoord, int oglYCoord, const Vec2i& position, const Vec2ui& size) override;
struct OverlayColorLegendLayoutInfo
{
OverlayColorLegendLayoutInfo(const Vec2i& pos, const Vec2ui& setSize)
{
position = pos;
size = setSize;
}
float charHeight;
float lineSpacing;
Vec2f margins;
float tickX;
float x0, x1;
Rectf legendRect;
cvf::ref<DoubleArray> tickPixelPos;
Vec2i position;
Vec2ui size;
};
void layoutInfo(OverlayColorLegendLayoutInfo* layout);
void renderGeneric(OpenGLContext* oglContext,
const Vec2i& position,
const Vec2ui& size,
bool software);
void renderLegendUsingShaders(OpenGLContext* oglContext,
OverlayColorLegendLayoutInfo* layout,
const MatrixState& matrixState);
void renderLegendImmediateMode(OpenGLContext* oglContext,
OverlayColorLegendLayoutInfo* layout);
void setupTextDrawer(TextDrawer* textDrawer,
const OverlayColorLegendLayoutInfo* layout,
float* maxLegendRightPos);
protected:
DoubleArray m_tickValues; // Ticks between each level + top and bottom of legend (n+1 entries)
std::vector<bool> m_visibleTickLabels; // Skip tick labels ending up on top of previous visible label
int m_tickNumberPrecision;
NumberFormat m_numberFormat;
Vec2ui m_sizeHint; // Pixel size of the color legend area
Color3f m_textColor;
Color3f m_lineColor;
bool m_isBackgroundEnabled;
Color4f m_backgroundColor;
Color4f m_backgroundFrameColor;
int m_lineWidth;
std::vector<String> m_titleStrings;
cvf::ref<Font> m_font;
cvf::cref<ScalarMapper> m_scalarMapper;
};
}