几天前我开始高效地绘制贝塞尔曲线,并且我发现了由Charles Loop和Jim Blinn开发的这个方法,它看起来很有趣。在经过大量的算法实验之后,我似乎无法让它能够渲染三次曲线。 Quadratics很好,没有问题。Bézier曲线,Loop和Blinn风格
我发现的唯一的资源至今如下:
Resolution Independent Curve Rendering using Programmable Graphics Hardware
为了让测试快速启动和运行,我在做这个XNA。基本上,我将纹理坐标与顶点传递给GPU,应用透视变换并使用像素着色器中所有文章中提到的公式来渲染最终结果。如何,问题(我认为)在我如何计算纹理坐标奠定了。查看此代码:
public void Update()
{
float a1 = Vector3.Dot(p1, Vector3.Cross(p4, p3));
float a2 = Vector3.Dot(p2, Vector3.Cross(p1, p4));
float a3 = Vector3.Dot(p3, Vector3.Cross(p2, p2));
float d1 = a1 - 2 * a2 + 3 * a3;
float d2 = -a2 + 3 * a3;
float d3 = 3 * a3;
float discr = d1 * d1 * (3 * d2 * d2 - 4 * d1 * d3);
if (discr > 0)
{
Type = CurveTypes.Serpentine;
float ls = 3 * d2 - (float)Math.Sqrt(9 * d2 * d2 - 12 * d1 * d3);
float lt = 6 * d1;
float ms = 3 * d2 + (float)Math.Sqrt(9 * d2 * d2 - 12 * d1 * d3);
float mt = 6 * d1;
TexCoord1 = new Vector3(ls * ms, (float)Math.Pow(ls, 3), (float)Math.Pow(ms, 3));
TexCoord2 = new Vector3((3 * ls * ms - ls * mt - lt * ms)/3, ls * ls * (ls - lt), ms * ms * (ms - mt));
TexCoord3 = new Vector3((lt * (mt - 2 * ms) + ls * (3 * ms - 2 * mt))/3, (float)Math.Pow(lt - ls, 2) * ls, (float)Math.Pow(mt - ms, 2) * ms);
TexCoord4 = new Vector3((lt - ls) * (mt - ms), -(float)Math.Pow(lt - ls, 3), -(float)Math.Pow(mt - ms, 3));
}
else if (discr == 0)
{
Type = CurveTypes.Cusp;
}
else if (discr < 0)
{
Type = CurveTypes.Loop;
}
}
原谅这个烂摊子,它只是一些测试代码。 p1 ... p4是世界空间中的控制点,TexCoord1 ... TexCoord4是相应的纹理坐标。这是对GPU Gems文章中所述内容的复制。
这里有一些问题,首先在计算a3时,我们对这两个参数都使用p2,这当然总是会产生一个(0,0,0)向量,并且总会得到该点和p3的点积给我们0。这非常没用,所以他们为什么会在文章中提到这一点?
这当然会使discr错误,我们甚至不能确定它是什么类型的曲线。
经过一段时间的摆弄这段代码之后,我决定尝试完成他们在Loop和Blinn论文中完成的原因。从那我得到这样的事情:
public void Update()
{
Matrix m1 = new Matrix(
p4.X, p4.Y, 1, 0,
p3.X, p3.Y, 1, 0,
p2.X, p2.Y, 1, 0,
0, 0, 0, 1);
Matrix m2 = new Matrix(
p4.X, p4.Y, 1, 0,
p3.X, p3.Y, 1, 0,
p1.X, p1.Y, 1, 0,
0, 0, 0, 1);
Matrix m3 = new Matrix(
p4.X, p4.Y, 1, 0,
p2.X, p2.Y, 1, 0,
p1.X, p1.Y, 1, 0,
0, 0, 0, 1);
Matrix m4 = new Matrix(
p3.X, p3.Y, 1, 0,
p2.X, p2.Y, 1, 0,
p1.X, p1.Y, 1, 0,
0, 0, 0, 1);
float det1 = m1.Determinant();
float det2 = -m2.Determinant();
float det3 = m3.Determinant();
float det4 = -m4.Determinant();
float tet1 = det1 * det3 - det2 * det2;
float tet2 = det2 * det3 - det1 * det4;
float tet3 = det2 * det4 - det3 * det3;
float discr = 4 * tet1 * tet3 - tet2 * tet2;
if (discr > 0)
{
Type = CurveTypes.Serpentine;
float ls = 2 * det2;
float lt = det3 + (float)((1/Math.Sqrt(3)) * Math.Sqrt(3 * det3 * det3 - 4 * det2 * det4));
float ms = 2 * det2;
float mt = det3 - (float)((1/Math.Sqrt(3)) * Math.Sqrt(3 * det3 * det3 - 4 * det2 * det4));
TexCoord1 = new Vector3(lt * mt, (float)Math.Pow(lt, 3), (float)Math.Pow(mt, 3));
TexCoord2 = new Vector3(-ms * lt - ls * mt, -3 * ls * lt * lt, -3 * ms * mt * mt);
TexCoord3 = new Vector3(ls * ms, 3 * ls * ls * lt, 3 * ms * ms * mt);
TexCoord4 = new Vector3(0, -ls * ls * ls, -ms * ms * ms);
}
else if (discr == 0)
{
Type = CurveTypes.Cusp;
}
else if (discr < 0)
{
Type = CurveTypes.Loop;
}
}
猜猜看,那也没有用。不管怎样,现在discr似乎至少有点正确。至少它有正确的标志,当控制点排列成尖头时,它是零。尽管我仍然得到相同的视觉效果,除了曲线随机消失一段时间(像素着色器公式始终大于零)并在将控制点移回更多方形后返回。这里是像素着色器代码的方式:
PixelToFrame PixelShader(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
if(pow(PSIn.TexCoords.x, 3) - PSIn.TexCoords.y * PSIn.TexCoords.z > 0)
{
Output.Color = float4(0,0,0,0.1);
}
else
{
Output.Color = float4(0,1,0,1);
}
return Output;
}
这是关于我现在可以想到的所有有用的信息。有谁知道发生了什么事?因为我用完了。
我开始自己实施这个方法,当我有一些工作时会回发。只是想让你知道这个问题没有被放弃:) – Ani 2012-04-25 16:31:00
@ananthonline真棒!请做! – Roliga 2012-04-26 14:32:35
我也有这个问题。你有没有得到它的工作?你能回答我的问题吗? http://stackoverflow.com/questions/15519142/resolution-independent-cubic-bezier-drawing-on-gpu-blinn-loop – scippie 2013-03-20 12:27:29