Skip to content

Commit e2eabd0

Browse files
committed
[62.CAD] Small fixes and edits to CAD example classes
1 parent 07172eb commit e2eabd0

File tree

9 files changed

+75
-23
lines changed

9 files changed

+75
-23
lines changed

62_CAD/DrawResourcesFiller.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ void DrawResourcesFiller::drawPolyline(const CPolylineBase& polyline, uint32_t p
218218
}
219219
}
220220

221+
// TODO[Erfan]: Makes more sense if parameters are: solidColor + fillPattern + patternColor
221222
void DrawResourcesFiller::drawHatch(
222223
const Hatch& hatch,
223224
const float32_t4& foregroundColor,
@@ -241,6 +242,9 @@ void DrawResourcesFiller::drawHatch(
241242
const HatchFillPattern fillPattern,
242243
SIntendedSubmitInfo& intendedNextSubmit)
243244
{
245+
if (color.a == 0.0f) // not visible
246+
return;
247+
244248
uint32_t textureIdx = InvalidTextureIdx;
245249
if (fillPattern != HatchFillPattern::SOLID_FILL)
246250
{

62_CAD/DrawResourcesFiller.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ struct DrawResourcesFiller
211211
// we need to store the clip projection stack to make sure the front is always available in memory
212212
void pushClipProjectionData(const ClipProjectionData& clipProjectionData);
213213
void popClipProjectionData();
214+
const std::deque<ClipProjectionData>& getClipProjectionStack() const { return clipProjections; }
214215

215216
smart_refctd_ptr<IGPUImageView> getMSDFsTextureArray() { return msdfTextureArray; }
216217

@@ -443,7 +444,7 @@ struct DrawResourcesFiller
443444
std::set<uint32_t> msdfTextureArrayIndicesUsed = {}; // indices in the msdf texture array allocator that have been used in the current frame // TODO: make this a dynamic bitset
444445
std::vector<MSDFTextureCopy> msdfTextureCopies = {}; // queued up texture copies
445446
std::unique_ptr<MSDFsLRUCache> msdfLRUCache; // LRU Cache to evict Least Recently Used in case of overflow
446-
static constexpr asset::E_FORMAT MSDFTextureFormat = asset::E_FORMAT::EF_R8G8B8_SNORM;
447+
static constexpr asset::E_FORMAT MSDFTextureFormat = asset::E_FORMAT::EF_R8G8B8A8_SNORM;
447448

448449
bool m_hasInitializedMSDFTextureArrays = false;
449450
};

62_CAD/Hatch.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,12 +1156,13 @@ void lightShaded(std::vector<CPolyline>& polylines, const float64_t2& offset)
11561156

11571157
void shaded(std::vector<CPolyline>& polylines, const float64_t2& offset)
11581158
{
1159+
float64_t2 size = float64_t2(1.0, 1.0)/8.0 * FillPatternShapeExtent;
11591160
for (uint32_t x = 0; x < 8; x++)
11601161
{
11611162
for (uint32_t y = 0; y < 8; y++)
11621163
{
11631164
if (x % 2 != y % 2)
1164-
square(polylines, float64_t2((double)x, (double)y)/8.0 * FillPatternShapeExtent + offset);
1165+
square(polylines, float64_t2((double)x, (double)y)/8.0 * FillPatternShapeExtent + offset, size);
11651166
}
11661167
}
11671168

62_CAD/Hatch.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class Hatch
7676
bool isStraightLineConstantMajor() const;
7777
};
7878

79+
Hatch() = default;
80+
7981
Hatch(std::span<CPolyline> lines, const MajorAxis majorAxis, nbl::system::logger_opt_smart_ptr logger = nullptr, int32_t* debugStep = nullptr, const std::function<void(CPolyline, LineStyleInfo)>& debugOutput = {});
8082

8183
// (temporary)
@@ -91,6 +93,6 @@ class Hatch
9193
static core::smart_refctd_ptr<asset::ICPUImage> generateHatchFillPatternMSDF(nbl::ext::TextRendering::TextRenderer* textRenderer, HatchFillPattern fillPattern, uint32_t2 msdfExtents);
9294

9395
private:
94-
std::vector<CurveHatchBox> hatchBoxes;
96+
std::vector<CurveHatchBox> hatchBoxes = {};
9597
};
9698

62_CAD/Polyline.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ void CPolyline::makeWideWhole(CPolyline& outOffset1, CPolyline& outOffset2, floa
451451

452452
if (!m_closedPolygon)
453453
{
454+
if (outOffset1.getSectionsCount() == 0u || outOffset2.getSectionsCount() == 0u)
455+
return;
456+
454457
nbl::hlsl::float64_t2 beginToBeginConnector[2u];
455458
beginToBeginConnector[0u] = outOffset1.getSectionFirstPoint(outOffset1.getSectionInfoAt(0u));
456459
beginToBeginConnector[1u] = outOffset2.getSectionFirstPoint(outOffset2.getSectionInfoAt(0u));
@@ -468,7 +471,7 @@ void CPolyline::stippleBreakDown(const LineStyleInfo& lineStyle, const OutputPol
468471
return;
469472

470473
// currently only works for road styles with only 2 stipple values (1 draw, 1 gap)
471-
assert(lineStyle.stipplePatternSize == 1);
474+
assert(lineStyle.stipplePatternSize <= 1);
472475

473476
const float64_t patternLen = 1.0 / lineStyle.reciprocalStipplePatternLen;
474477
const float32_t drawSectionNormalizedLen = lineStyle.stipplePattern[0];

62_CAD/Polyline.h

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct PolylineSettings
1313
static constexpr bool ResetLineStyleOnDiscontinuity = false;
1414
};
1515

16-
// holds values for `LineStyle` struct and caculates stipple pattern re values, cant think of better name
16+
// holds values for `LineStyle` struct and caculates stipple pattern processed values, cant think of better name
1717
// Also used for TextStyles aliased with some members here. (temporarily?)
1818
struct LineStyleInfo
1919
{
@@ -23,9 +23,9 @@ struct LineStyleInfo
2323
static constexpr float PatternEpsilon = 1e-3f; // TODO: I think for phase shift in normalized stipple space this is a reasonable value? right?
2424
static const uint32_t StipplePatternMaxSize = LineStyle::StipplePatternMaxSize;
2525

26-
float32_t4 color;
27-
float screenSpaceLineWidth; // alternatively used as TextStyle::italicTiltSlope
28-
float worldSpaceLineWidth; // alternatively used as TextStyle::boldInPixels
26+
float32_t4 color = {};
27+
float screenSpaceLineWidth = 0.0f; // alternatively used as TextStyle::italicTiltSlope
28+
float worldSpaceLineWidth = 0.0f; // alternatively used as TextStyle::boldInPixels
2929

3030
/*
3131
Stippling Values:
@@ -366,7 +366,7 @@ class CPolyline : public CPolylineBase
366366
public:
367367
CPolyline() :
368368
m_Min(float64_t2(nbl::hlsl::numeric_limits<float64_t>::max, nbl::hlsl::numeric_limits<float64_t>::max)),
369-
m_Max(float64_t2(nbl::hlsl::numeric_limits<float64_t>::min, nbl::hlsl::numeric_limits<float64_t>::min)),
369+
m_Max(float64_t2(nbl::hlsl::numeric_limits<float64_t>::lowest, nbl::hlsl::numeric_limits<float64_t>::lowest)),
370370
m_closedPolygon(false)
371371
{}
372372

@@ -619,6 +619,44 @@ class CPolyline : public CPolylineBase
619619
float64_t2 getMin() const { return m_Min; }
620620
float64_t2 getMax() const { return m_Max; }
621621

622+
void transform(const float64_t2x2& rotScale, float64_t2 translate)
623+
{
624+
// transform is linear
625+
for (auto& linePoint : m_linePoints)
626+
linePoint.p = mul(rotScale,linePoint.p) + translate;
627+
for (auto& bezierPoint : m_quadBeziers)
628+
{
629+
bezierPoint.shape.P0 = mul(rotScale,bezierPoint.shape.P0) + translate;
630+
bezierPoint.shape.P1 = mul(rotScale,bezierPoint.shape.P1) + translate;
631+
bezierPoint.shape.P2 = mul(rotScale,bezierPoint.shape.P2) + translate;
632+
}
633+
634+
// not useful for markers:
635+
#if 0
636+
std::array<float64_t2, 4> corners = {
637+
float64_t2{ m_Min.x, m_Min.y },
638+
float64_t2{ m_Max.x, m_Min.y },
639+
float64_t2{ m_Min.x, m_Max.y },
640+
float64_t2{ m_Max.x, m_Max.y }
641+
};
642+
643+
std::array<float64_t2, 4> transformedCorners;
644+
for (uint32_t i = 0; i < 4u; ++i)
645+
transformedCorners[i] = mul(rotScale, corners[i]) + translate;
646+
647+
// Compute new AABB by finding min and max of transformed corners (OBB)
648+
m_Min = { nbl::hlsl::numeric_limits<float64_t>::max, nbl::hlsl::numeric_limits<float64_t>::max };
649+
m_Max = { nbl::hlsl::numeric_limits<float64_t>::lowest, nbl::hlsl::numeric_limits<float64_t>::lowest };
650+
651+
for (const auto& corner : transformedCorners) {
652+
m_Min.x = nbl::core::min(m_Min.x, corner.x);
653+
m_Min.y = nbl::core::min(m_Min.y, corner.y);
654+
m_Max.x = nbl::core::max(m_Max.x, corner.x);
655+
m_Max.y = nbl::core::max(m_Max.y, corner.y);
656+
}
657+
#endif
658+
}
659+
622660
protected:
623661
std::vector<PolylineConnector> m_polylineConnector;
624662
std::vector<SectionInfo> m_sections;
@@ -636,6 +674,7 @@ class CPolyline : public CPolylineBase
636674
m_Max.y = nbl::hlsl::max(m_Max.y, point.y);
637675
}
638676

677+
// AABB
639678
float64_t2 m_Min; // min coordinate of the whole polyline
640679
float64_t2 m_Max; // max coordinate of the whole polyline
641680

62_CAD/SingleLineText.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "SingleLineText.h"
22

3-
SingleLineText::SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace>&& face, const std::string& text)
3+
SingleLineText::SingleLineText(nbl::ext::TextRendering::FontFace* face, const std::string& text)
44
{
5-
m_face = std::move(face);
65
m_glyphBoxes.reserve(text.length());
76

87
m_boundingBox.min = float64_t2(0.0, 0.0);
@@ -12,14 +11,14 @@ SingleLineText::SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::F
1211
float64_t2 currentPos = float32_t2(0.0, 0.0);
1312
for (uint32_t i = 0; i < text.length(); i++)
1413
{
15-
const auto glyphIndex = m_face->getGlyphIndex(wchar_t(text.at(i)));
16-
const auto glyphMetrics = m_face->getGlyphMetrics(glyphIndex);
14+
const auto glyphIndex = face->getGlyphIndex(wchar_t(text.at(i)));
15+
const auto glyphMetrics = face->getGlyphMetrics(glyphIndex);
1716
const bool skipGenerateGlyph = (glyphIndex == 0 || (glyphMetrics.size.x == 0.0 && glyphMetrics.size.y == 0.0));
1817

1918
if (!skipGenerateGlyph)
2019
{
2120
#ifdef VERIFY_DEBUG
22-
msdfgen::Shape shape = m_face->generateGlyphShape(glyphIndex);
21+
msdfgen::Shape shape = face->generateGlyphShape(glyphIndex);
2322
_NBL_BREAK_IF(shape.contours.empty());
2423
#endif
2524
GlyphBox glyphBbox =
@@ -43,6 +42,7 @@ SingleLineText::SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::F
4342
void SingleLineText::Draw(
4443
DrawResourcesFiller& drawResourcesFiller,
4544
SIntendedSubmitInfo& intendedNextSubmit,
45+
nbl::ext::TextRendering::FontFace* face,
4646
const float64_t2& baselineStart,
4747
const float32_t2& scale,
4848
const float32_t& rotateAngle,
@@ -74,8 +74,8 @@ void SingleLineText::Draw(
7474

7575
// float32_t3 xx = float64_t3(0.0, -glyphBox.size.y, 0.0);
7676
const float32_t aspectRatio = static_cast<float32_t>(glm::length(dirV) / glm::length(dirU)); // check if you can just do: (glyphBox.size.y * scale.y) / glyphBox.size.x * scale.x)
77-
const float32_t2 minUV = m_face->getUV(float32_t2(0.0f,0.0f), glyphBox.size, drawResourcesFiller.getMSDFResolution(), MSDFPixelRange);
78-
drawResourcesFiller.drawFontGlyph(m_face.get(), glyphBox.glyphIdx, topLeft, dirU, aspectRatio, minUV, glyphObjectIdx, intendedNextSubmit);
77+
const float32_t2 minUV = face->getUV(float32_t2(0.0f,0.0f), glyphBox.size, drawResourcesFiller.getMSDFResolution(), MSDFPixelRange);
78+
drawResourcesFiller.drawFontGlyph(face, glyphBox.glyphIdx, topLeft, dirU, aspectRatio, minUV, glyphObjectIdx, intendedNextSubmit);
7979
}
8080

8181
}

62_CAD/SingleLineText.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ using namespace nbl::core;
77
using namespace nbl::asset;
88
using namespace nbl::ext::TextRendering;
99

10+
/// This is basically a Text Layout in Font Unit Space + a Draw method with transformations, color and additional parameters
1011
class SingleLineText
1112
{
1213
public:
1314
// constructs and fills the `glyphBoxes`
14-
SingleLineText(core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace>&& face, const std::string& text);
15+
SingleLineText(nbl::ext::TextRendering::FontFace* face, const std::string& text);
1516

1617
struct BoundingBox
1718
{
@@ -30,6 +31,7 @@ class SingleLineText
3031
void Draw(
3132
DrawResourcesFiller& drawResourcesFiller,
3233
SIntendedSubmitInfo& intendedNextSubmit,
34+
nbl::ext::TextRendering::FontFace* face,
3335
const float64_t2& baselineStart = float64_t2(0.0,0.0),
3436
const float32_t2& scale = float64_t2(1.0f, 1.0f),
3537
const float32_t& rotateAngle = 0.0f,
@@ -49,5 +51,4 @@ class SingleLineText
4951

5052
BoundingBox m_boundingBox = {};
5153
std::vector<GlyphBox> m_glyphBoxes;
52-
core::smart_refctd_ptr<nbl::ext::TextRendering::FontFace> m_face;
5354
};

62_CAD/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
10421042

10431043
const auto str = "MSDF: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnoprstuvwxyz '1234567890-=\"!@#$%&*()_+";
10441044
singleLineText = std::unique_ptr<SingleLineText>(new SingleLineText(
1045-
core::smart_refctd_ptr<FontFace>(m_font),
1045+
m_font.get(),
10461046
std::string(str)));
10471047

10481048
drawResourcesFiller.setGlyphMSDFTextureFunction(
@@ -1480,6 +1480,7 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
14801480
// We only support one swapchain mode, surface, the other one is Display which we have not implemented yet.
14811481
retval.swapchainMode = video::E_SWAPCHAIN_MODE::ESM_SURFACE;
14821482
retval.validations = true;
1483+
retval.synchronizationValidation = true;
14831484
return retval;
14841485
}
14851486
protected:
@@ -3061,10 +3062,10 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
30613062
{
30623063
float32_t rotation = 0.0; // nbl::core::PI<float>()* abs(cos(m_timeElapsed * 0.00005));
30633064
float32_t italicTiltAngle = nbl::core::PI<float>() / 9.0f;
3064-
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, float64_t2(0.0,-100.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), 0.0f, 0.0f);
3065-
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, float64_t2(0.0,-150.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), 0.0f, 0.5f);
3066-
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, float64_t2(0.0,-200.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), italicTiltAngle, 0.0f);
3067-
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, float64_t2(0.0,-250.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), italicTiltAngle, 0.5f);
3065+
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, m_font.get(), float64_t2(0.0,-100.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), 0.0f, 0.0f);
3066+
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, m_font.get(), float64_t2(0.0,-150.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), 0.0f, 0.5f);
3067+
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, m_font.get(), float64_t2(0.0,-200.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), italicTiltAngle, 0.0f);
3068+
singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, m_font.get(), float64_t2(0.0,-250.0), float32_t2(1.0, 1.0), rotation, float32_t4(1.0, 1.0, 1.0, 1.0), italicTiltAngle, 0.5f);
30683069
// singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, float64_t2(0.0,-200.0), float32_t2(1.0, 1.0), nbl::core::PI<float>() * abs(cos(m_timeElapsed * 0.00005)));
30693070
// Smaller text to test mip maps
30703071
//singleLineText->Draw(drawResourcesFiller, intendedNextSubmit, float64_t2(0.0,-130.0), float32_t2(0.4, 0.4), rotation);

0 commit comments

Comments
 (0)