Skip to content

Commit

Permalink
Change Label3DWrapper to Label3D
Browse files Browse the repository at this point in the history
- Reimplement the Scenegraph::Label3D for Drawables
- Might convert Scenegraph::Label3D to wrap this!
  • Loading branch information
fluffyfreak authored and impaktor committed Dec 17, 2024
1 parent 946115f commit 81452a9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
24 changes: 14 additions & 10 deletions src/GeoPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

#define DEBUG_CENTROIDS 0

#if DEBUG_PATCHES
#ifdef DEBUG_BOUNDING_SPHERES
#include "graphics/RenderState.h"
#endif

#if DEBUG_CENTROIDS
#include "graphics/Drawables.h"
#endif

Expand Down Expand Up @@ -297,7 +297,7 @@ void GeoPatch::UpdateVBOs(Graphics::Renderer *renderer)
if (m_label3D == nullptr) {
std::stringstream stuff;
stuff << m_PatchID.GetPatchFaceIdx();
m_label3D.reset(new Graphics::Drawables::Label3DWrapper(Pi::renderer, stuff.str()));
m_label3D.reset(new Graphics::Drawables::Label3D(Pi::renderer, stuff.str()));
}
#endif // DEBUG_PATCHES
}
Expand All @@ -315,10 +315,7 @@ void GeoPatch::Render(Graphics::Renderer *renderer, const vector3d &campos, cons
return;

#if DEBUG_PATCHES
if (m_label3D != nullptr) {
const vector3d relpos = m_clipCentroid - campos;
m_label3D->Draw(matrix4x4f(modelView * matrix4x4d::Translation(relpos)));
}
RenderLabelDebug(campos, modelView);
#endif // DEBUG_PATCHES

if (m_kids[0]) {
Expand All @@ -334,10 +331,7 @@ void GeoPatch::RenderImmediate(Graphics::Renderer *renderer, const vector3d &cam
PROFILE_SCOPED()

#if DEBUG_PATCHES
if (m_label3D != nullptr) {
const vector3d relpos = m_clipCentroid - campos;
m_label3D->Draw(matrix4x4f(modelView * matrix4x4d::Translation(relpos)));
}
RenderLabelDebug(campos, modelView);
#endif // DEBUG_PATCHES

if (m_patchVBOData->m_heights) {
Expand Down Expand Up @@ -560,3 +554,13 @@ bool GeoPatch::IsOverHorizon(const vector3d &camPos) const
// not over the horizon
return false;
}

#if DEBUG_PATCHES
void GeoPatch::RenderLabelDebug(const vector3d &campos, const matrix4x4d &modelView) const
{
if (m_label3D != nullptr) {
const vector3d relpos = m_clipCentroid - campos;
m_label3D->Draw(Pi::renderer, matrix4x4f(modelView * matrix4x4d::Translation(relpos)));
}
}
#endif // DEBUG_PATCHES
4 changes: 3 additions & 1 deletion src/GeoPatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ class GeoPatch {
#ifdef DEBUG_BOUNDING_SPHERES
std::unique_ptr<Graphics::Drawables::Sphere3D> m_boundsphere;
#endif
std::unique_ptr<Graphics::Drawables::Label3DWrapper> m_label3D;
std::unique_ptr<Graphics::Drawables::Label3D> m_label3D;

void RenderLabelDebug(const vector3d &campos, const matrix4x4d &modelView) const;
#endif // #if DEBUG_PATCHES
};

Expand Down
53 changes: 45 additions & 8 deletions src/graphics/Drawables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,19 +1031,56 @@ namespace Graphics {
}

//------------------------------------------------------------
Label3DWrapper::Label3DWrapper(Graphics::Renderer *r, const std::string &str)
Label3D::Label3D(Graphics::Renderer *r, const std::string &str)
{
Graphics::Texture *sdfTex = Graphics::TextureBuilder("fonts/label3d.dds", Graphics::LINEAR_CLAMP, true, true, true).GetOrCreateTexture(r, "model");
RefCountedPtr<Text::DistanceFieldFont> m_labelFont;
m_labelFont.Reset(new Text::DistanceFieldFont("fonts/sdf_definition.txt", sdfTex));
m_label3D.reset(new SceneGraph::Label3D(r, m_labelFont));
m_label3D->SetText(str);
m_font.Reset(new Text::DistanceFieldFont("fonts/sdf_definition.txt", sdfTex));

Graphics::MaterialDescriptor matdesc;
matdesc.textures = 1;
matdesc.alphaTest = true;
matdesc.lighting = true;

Graphics::RenderStateDesc rsd;
rsd.depthWrite = false;
rsd.blendMode = Graphics::BLEND_ALPHA;

m_geometry.reset(m_font->CreateVertexArray());
m_material.Reset(r->CreateMaterial("label", matdesc, rsd));
m_material->SetTexture("texture0"_hash, m_font->GetTexture());
m_material->diffuse = Color::WHITE;
m_material->emissive = Color(38, 38, 38);
m_material->specular = Color::WHITE;

SetText(r, str);
}

void Label3D::SetText(Graphics::Renderer *r, const std::string &text)
{
//regenerate geometry
m_geometry->Clear();
m_textMesh.reset();

if (!text.empty()) {
m_font->GetGeometry(*m_geometry, text, vector2f(0.f));

// Happens if none of the characters in the string have glyphs in the SDF font.
// Most noticeably, this means text consisting of entirely Cyrillic
// or Chinese characters will vanish when rendered on a Label3D.
if (m_geometry->IsEmpty()) {
return;
}

//create buffer and upload data
m_textMesh.reset(r->CreateMeshObjectFromArray(m_geometry.get()));
}
}

void Label3DWrapper::Draw(const matrix4x4f &trans)
void Label3D::Draw(Graphics::Renderer *r, const matrix4x4f &trans)
{
if (m_label3D) {
m_label3D->Render(trans, nullptr);
if (r!=nullptr && m_textMesh.get()) {
r->SetTransform(trans);
r->DrawMesh(m_textMesh.get(), m_material.Get());
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/graphics/Drawables.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "graphics/Material.h"
#include "graphics/VertexArray.h"
#include "graphics/VertexBuffer.h"
#include "text/DistanceFieldFont.h"

#include <memory>
#include <string>
Expand Down Expand Up @@ -291,14 +292,19 @@ namespace Graphics {

//------------------------------------------------------------

// Label3DWrapper
class Label3DWrapper {
// Label3D
class Label3D {
public:
Label3DWrapper(Graphics::Renderer *r, const std::string &str);
void Draw(const matrix4x4f &trans);
Label3D(Graphics::Renderer *r, const std::string &str);
void Draw(Graphics::Renderer *r, const matrix4x4f &trans);

private:
std::unique_ptr<SceneGraph::Label3D> m_label3D;
void SetText(Graphics::Renderer *r, const std::string &text);

RefCountedPtr<Graphics::Material> m_material;
std::unique_ptr<Graphics::VertexArray> m_geometry;
std::unique_ptr<Graphics::MeshObject> m_textMesh;
RefCountedPtr<Text::DistanceFieldFont> m_font;
};

} // namespace Drawables
Expand Down

0 comments on commit 81452a9

Please sign in to comment.