2017-09-07 16:51:41 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2017-09-07 16:51:41 +00:00
|
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/rendering/TextRenderer.h>
|
|
|
|
|
|
|
|
#include <vtkm/Transform3D.h>
|
|
|
|
#include <vtkm/cont/TryExecute.h>
|
2021-10-02 21:08:03 +00:00
|
|
|
#include <vtkm/rendering/TextRendererBatcher.h>
|
2017-09-07 16:51:41 +00:00
|
|
|
|
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace rendering
|
|
|
|
{
|
|
|
|
|
2017-09-14 21:07:06 +00:00
|
|
|
TextRenderer::TextRenderer(const vtkm::rendering::Canvas* canvas,
|
2017-09-07 16:51:41 +00:00
|
|
|
const vtkm::rendering::BitmapFont& font,
|
2021-10-02 21:08:03 +00:00
|
|
|
const vtkm::rendering::Canvas::FontTextureType& fontTexture,
|
|
|
|
vtkm::rendering::TextRendererBatcher* textBatcher)
|
2017-09-07 16:51:41 +00:00
|
|
|
: Canvas(canvas)
|
|
|
|
, Font(font)
|
|
|
|
, FontTexture(fontTexture)
|
2021-10-02 21:08:03 +00:00
|
|
|
, TextBatcher(textBatcher)
|
2017-09-07 16:51:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-31 16:20:38 +00:00
|
|
|
void TextRenderer::RenderText(const vtkm::Vec2f_32& position,
|
2017-09-07 16:51:41 +00:00
|
|
|
vtkm::Float32 scale,
|
|
|
|
vtkm::Float32 angle,
|
|
|
|
vtkm::Float32 windowAspect,
|
2019-07-31 16:20:38 +00:00
|
|
|
const vtkm::Vec2f_32& anchor,
|
2017-09-07 16:51:41 +00:00
|
|
|
const vtkm::rendering::Color& color,
|
|
|
|
const std::string& text)
|
|
|
|
{
|
|
|
|
vtkm::Matrix<vtkm::Float32, 4, 4> translationMatrix =
|
|
|
|
Transform3DTranslate(position[0], position[1], 0.f);
|
|
|
|
vtkm::Matrix<vtkm::Float32, 4, 4> scaleMatrix = Transform3DScale(1.0f / windowAspect, 1.0f, 1.0f);
|
2019-07-31 16:20:38 +00:00
|
|
|
vtkm::Vec3f_32 rotationAxis(0.0f, 0.0f, 1.0f);
|
2017-09-07 16:51:41 +00:00
|
|
|
vtkm::Matrix<vtkm::Float32, 4, 4> rotationMatrix = Transform3DRotate(angle, rotationAxis);
|
|
|
|
vtkm::Matrix<vtkm::Float32, 4, 4> transform =
|
|
|
|
vtkm::MatrixMultiply(translationMatrix, vtkm::MatrixMultiply(scaleMatrix, rotationMatrix));
|
|
|
|
RenderText(transform, scale, anchor, color, text);
|
|
|
|
}
|
|
|
|
|
2019-07-31 16:20:38 +00:00
|
|
|
void TextRenderer::RenderText(const vtkm::Vec3f_32& origin,
|
|
|
|
const vtkm::Vec3f_32& right,
|
|
|
|
const vtkm::Vec3f_32& up,
|
2017-09-07 16:51:41 +00:00
|
|
|
vtkm::Float32 scale,
|
2019-07-31 16:20:38 +00:00
|
|
|
const vtkm::Vec2f_32& anchor,
|
2017-09-07 16:51:41 +00:00
|
|
|
const vtkm::rendering::Color& color,
|
|
|
|
const std::string& text)
|
|
|
|
{
|
2019-07-31 16:20:38 +00:00
|
|
|
vtkm::Vec3f_32 n = vtkm::Cross(right, up);
|
2017-09-07 16:51:41 +00:00
|
|
|
vtkm::Normalize(n);
|
|
|
|
|
|
|
|
vtkm::Matrix<vtkm::Float32, 4, 4> transform = MatrixHelpers::WorldMatrix(origin, right, up, n);
|
2017-09-18 03:40:24 +00:00
|
|
|
transform = vtkm::MatrixMultiply(Canvas->GetModelView(), transform);
|
|
|
|
transform = vtkm::MatrixMultiply(Canvas->GetProjection(), transform);
|
2017-09-07 16:51:41 +00:00
|
|
|
RenderText(transform, scale, anchor, color, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextRenderer::RenderText(const vtkm::Matrix<vtkm::Float32, 4, 4>& transform,
|
|
|
|
vtkm::Float32 scale,
|
2019-07-31 16:20:38 +00:00
|
|
|
const vtkm::Vec2f_32& anchor,
|
2017-09-07 16:51:41 +00:00
|
|
|
const vtkm::rendering::Color& color,
|
2018-01-29 19:39:21 +00:00
|
|
|
const std::string& text,
|
|
|
|
const vtkm::Float32& depth)
|
2017-09-07 16:51:41 +00:00
|
|
|
{
|
|
|
|
vtkm::Float32 textWidth = this->Font.GetTextWidth(text);
|
|
|
|
vtkm::Float32 fx = -(0.5f + 0.5f * anchor[0]) * textWidth;
|
|
|
|
vtkm::Float32 fy = -(0.5f + 0.5f * anchor[1]);
|
|
|
|
vtkm::Float32 fz = 0;
|
|
|
|
|
2021-10-02 21:08:03 +00:00
|
|
|
using ScreenCoordsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Id4>;
|
|
|
|
using TextureCoordsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Vec4f_32>;
|
2017-09-07 16:51:41 +00:00
|
|
|
ScreenCoordsArrayHandle screenCoords;
|
|
|
|
TextureCoordsArrayHandle textureCoords;
|
|
|
|
{
|
2020-01-29 22:34:03 +00:00
|
|
|
screenCoords.Allocate(static_cast<vtkm::Id>(text.length()));
|
|
|
|
textureCoords.Allocate(static_cast<vtkm::Id>(text.length()));
|
|
|
|
ScreenCoordsArrayHandle::WritePortalType screenCoordsPortal = screenCoords.WritePortal();
|
|
|
|
TextureCoordsArrayHandle::WritePortalType textureCoordsPortal = textureCoords.WritePortal();
|
|
|
|
vtkm::Vec4f_32 charVertices, charUVs, charCoords;
|
|
|
|
for (std::size_t i = 0; i < text.length(); ++i)
|
|
|
|
{
|
|
|
|
char c = text[i];
|
|
|
|
char nextchar = (i < text.length() - 1) ? text[i + 1] : 0;
|
|
|
|
Font.GetCharPolygon(c,
|
|
|
|
fx,
|
|
|
|
fy,
|
|
|
|
charVertices[0],
|
|
|
|
charVertices[2],
|
|
|
|
charVertices[3],
|
|
|
|
charVertices[1],
|
|
|
|
charUVs[0],
|
|
|
|
charUVs[2],
|
|
|
|
charUVs[3],
|
|
|
|
charUVs[1],
|
|
|
|
nextchar);
|
|
|
|
charVertices = charVertices * scale;
|
|
|
|
vtkm::Id2 p0 = Canvas->GetScreenPoint(charVertices[0], charVertices[3], fz, transform);
|
|
|
|
vtkm::Id2 p1 = Canvas->GetScreenPoint(charVertices[2], charVertices[1], fz, transform);
|
|
|
|
charCoords = vtkm::Id4(p0[0], p1[1], p1[0], p0[1]);
|
|
|
|
screenCoordsPortal.Set(static_cast<vtkm::Id>(i), charCoords);
|
|
|
|
textureCoordsPortal.Set(static_cast<vtkm::Id>(i), charUVs);
|
|
|
|
}
|
2017-09-07 16:51:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 21:08:03 +00:00
|
|
|
this->TextBatcher->BatchText(screenCoords, textureCoords, color, depth);
|
2017-09-07 16:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace vtkm::rendering
|