gl_engine: support simple hairline stroke rendering

Reduce alpha if stroke width is too thin to mock hairline rendering
This commit is contained in:
RuiwenTang 2024-07-03 20:53:31 +08:00 committed by Hermet Park
parent 76f98008e8
commit 9bed64a4e7
3 changed files with 21 additions and 3 deletions

View file

@ -27,6 +27,7 @@
#include "tvgGlCommon.h" #include "tvgGlCommon.h"
#include "tvgMath.h" #include "tvgMath.h"
#define MIN_GL_STROKE_WIDTH 0.5f
#define MVP_MATRIX(w, h) \ #define MVP_MATRIX(w, h) \
float mvp[4*4] = { \ float mvp[4*4] = { \

View file

@ -643,6 +643,14 @@ void GlRenderer::drawPrimitive(GlShape& sdata, uint8_t r, uint8_t g, uint8_t b,
a = MULTIPLY(a, sdata.opacity); a = MULTIPLY(a, sdata.opacity);
if (flag & RenderUpdateFlag::Stroke) {
float strokeWidth = sdata.rshape->strokeWidth();
if (strokeWidth < MIN_GL_STROKE_WIDTH) {
float alpha = strokeWidth / MIN_GL_STROKE_WIDTH;
a = MULTIPLY(a, static_cast<uint8_t>(alpha * 255));
}
}
// matrix buffer // matrix buffer
{ {
const auto& matrix = sdata.geometry->getTransformMatrix(); const auto& matrix = sdata.geometry->getTransformMatrix();
@ -800,6 +808,15 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, RenderUpdateFla
}); });
} }
float alpha = 1.0f;
if (flag & RenderUpdateFlag::GradientStroke) {
float strokeWidth = sdata.rshape->strokeWidth();
if (strokeWidth < MIN_GL_STROKE_WIDTH) {
alpha = strokeWidth / MIN_GL_STROKE_WIDTH;
}
}
// gradient block // gradient block
{ {
GlBindingResource gradientBinding{}; GlBindingResource gradientBinding{};
@ -820,7 +837,7 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, RenderUpdateFla
gradientBlock.stopColors[i * 4 + 0] = stops[i].r / 255.f; gradientBlock.stopColors[i * 4 + 0] = stops[i].r / 255.f;
gradientBlock.stopColors[i * 4 + 1] = stops[i].g / 255.f; gradientBlock.stopColors[i * 4 + 1] = stops[i].g / 255.f;
gradientBlock.stopColors[i * 4 + 2] = stops[i].b / 255.f; gradientBlock.stopColors[i * 4 + 2] = stops[i].b / 255.f;
gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f; gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f * alpha;
nStops++; nStops++;
} }
gradientBlock.nStops[0] = nStops * 1.f; gradientBlock.nStops[0] = nStops * 1.f;
@ -856,7 +873,7 @@ void GlRenderer::drawPrimitive(GlShape& sdata, const Fill* fill, RenderUpdateFla
gradientBlock.stopColors[i * 4 + 0] = stops[i].r / 255.f; gradientBlock.stopColors[i * 4 + 0] = stops[i].r / 255.f;
gradientBlock.stopColors[i * 4 + 1] = stops[i].g / 255.f; gradientBlock.stopColors[i * 4 + 1] = stops[i].g / 255.f;
gradientBlock.stopColors[i * 4 + 2] = stops[i].b / 255.f; gradientBlock.stopColors[i * 4 + 2] = stops[i].b / 255.f;
gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f; gradientBlock.stopColors[i * 4 + 3] = stops[i].a / 255.f * alpha;
nStops++; nStops++;
} }
gradientBlock.nStops[0] = nStops * 1.f; gradientBlock.nStops[0] = nStops * 1.f;

View file

@ -148,7 +148,7 @@ private:
Array<float>* mResGlPoints; Array<float>* mResGlPoints;
Array<uint32_t>* mResIndices; Array<uint32_t>* mResIndices;
Matrix mMatrix; Matrix mMatrix;
float mStrokeWidth = 1.f; float mStrokeWidth = MIN_GL_STROKE_WIDTH;
float mMiterLimit = 4.f; float mMiterLimit = 4.f;
StrokeCap mStrokeCap = StrokeCap::Square; StrokeCap mStrokeCap = StrokeCap::Square;
StrokeJoin mStrokeJoin = StrokeJoin::Bevel; StrokeJoin mStrokeJoin = StrokeJoin::Bevel;