这是我在做什么的屏幕截图。目前,我无法在这个矩形中绘制弯曲的边框。
我的第一个解决方案是:在矩形后面画一个四等分圆,但是如果我调整形状的不透明度,如您所见,四等分圆就会显示出来。
我知道这对你们来说很基础,但我不是很擅长数学。
我确实尝试重用弧的计算边缘并添加边框的大小,但结果我得到了这个。
我也认为贝塞尔曲线可以替代,但我认为重用计算的顶点并添加所有缺失的顶点会更有效。另外,我不知道如何计算贝塞尔曲线的曲线点,并且找到合适的数量t
会在计算上非常昂贵,所以我没有实现它。
这是我如何绘制内部四分之一圆的代码,我想我可以重用它。
void drawArc(int x, int y,
int startAngle, int endAngle,
uint32_t radiusX, uint32_t radiusY,
int border_x, int border_y,
const rgb color,
const rgb bcX, const rgb bcY,
uint8_t opacity)
{
if (radiusX <= 0 || radiusY <= 0) return;
static constexpr float DTR = 3.14159 / 180;
float cx, cy;
int step;
static std::vector<float> verts;
static std::vector<uint8_t> colors;
if (startAngle < endAngle)
{
step = +1;
++ endAngle;
} else
{
step = -1;
-- endAngle;
}
verts.clear();
colors.clear();
verts.push_back(x);
verts.push_back(y);
colors.push_back(color[R]);
colors.push_back(color[G]);
colors.push_back(color[B]);
colors.push_back(opacity);
while (startAngle != endAngle)
{
cx = cos(DTR * startAngle) * radiusX;
cy = sin(DTR * startAngle) * radiusY;
verts.push_back(x + cx);
verts.push_back(y - cy);
colors.push_back(color[R]);
colors.push_back(color[G]);
colors.push_back(color[B]);
colors.push_back(opacity);
startAngle += step;
}
drawElements(GL_POLYGON, sizeof(arcIndices) / sizeof(arcIndices[0]), GL_FLOAT,
&verts[0], &colors[0], &arcIndices[0]);
if (border_x != 0 || border_y != 0)
{
//remove (x, y)
verts.erase(verts.begin(), verts.begin() + 2);
// float px, py;
//
// px = *(verts.begin() + 0);
// py = *(verts.begin() + 1);
//
// glPointSize(5);
//
// glBegin(GL_POINTS);
//
// glColor3ub(0,0,255);
// glVertex2i(px, py);
//
// px = *(verts.end() - 2);
// py = *(verts.end() - 1);
//
// glColor3ub(255,0,0);
// glVertex2i(px , py);
// glEnd();
//attempting to reuse the edges
//I think the last vertices are opposed
//that's why I got a crossed out lines??
for (int i = 0;i <= 90; ++i)
{
verts.push_back(verts[i + 0] + border_x);
verts.push_back(verts[i + 1] + border_y);
colors.push_back(bcX[R]);
colors.push_back(bcX[G]);
colors.push_back(bcX[B]);
colors.push_back(opacity);
}
//91 = steps from 0-90 degree revolution
//182 = 91 * 2
unsigned int index[182 + 91 * 2];
for (int i = 0;i < 182 + 91 * 2; ++i)
index[i] = i;
drawElements(GL_LINE_LOOP, verts.size() / 2, GL_FLOAT,
&verts[0], &colors[0], &index[0]);
}
}
编辑:
我不能只重用之前预先计算的 (x,y) 吗?
抱歉图片使用过多
红点是我所指的预先计算的 (x, y) 并且只是在此基础上附加下一个弧。
我要渲染很多这样的东西,所以我需要尽可能高效(不要过多地使用三角函数)。
更新:
stencil buffer
这是我使用Andon M. Coleman建议的结果:
顺便说一句,如您所见,我正在尝试使用 OpenGL 模拟我自己的 UI:D