手动混合颜色

发布于 2024-10-16 04:20:03 字数 298 浏览 1 评论 0原文

我有一个问题正在尝试优化。我正在重现 OpenGL 函数,我当前的问题是光栅化 n 点形状并根据点混合颜色。

栅格存储在长度为 Screen.Width * Screen.Height * 3 的 1 个暗淡字节数组中。

我的实现速度相当慢,我想稍微优化它(好吧,很多)。我访问形状中的每个像素并获取距每个点的距离(使用 Sqrt(x^2 + y^2),这就是事情看起来很慢的地方),并使用距离和顶点的颜色来确定该点的颜色特定像素。

我知道有一种更快的方法可以做到这一点。任何帮助都会非常好! 哦,顺便说一句,我正在使用 C# 工作。

I have a problem that I'm trying to optimize. I'm reproducing OpenGL functions and my current problem is rasterizing n-pointed shapes and blending the colors based on the points.

The raster is stored in a 1 dim array of bytes of length Screen.Width * Screen.Height * 3

My implementation is quite slow and I want to optimize it a little (well, a lot). I visit each pixel in the shape and take the distance from each point (using Sqrt(x^2 + y^2), which is where things seem to be slow) and use the distance and the vert's color to determine the color for that specific pixel.

I know there is a faster way to do this. Any help would be excellent!
Oh, I'm working in C# btw.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2024-10-23 04:20:03

假设您有一个(充分的)理由不使用 OpenGL 或 Directx 甚至 GDI+。

我不确定我是否真正理解您尝试光栅化的图形基元。我假设它是一个有 n 个点的多边形。在这样的形状内插入颜色并不完全是微不足道的,因为你有不同的方法来做到这一点。所以问题是,你想要什么?

考虑凸多边形和凹多边形(甚至自相交)的差异。目前还不清楚结果“应该”是什么样子。这就是为什么图形硬件不再担心除三角形之外的任何其他事情。我建议你(几乎)也这样做。

在三角形内插入颜色非常简单且明确。谷歌搜索的流行词是重心坐标

所以,剩下的问题是如何对多边形进行三角测量。

  • 对于凸多边形,您可以使用类似于我假设您当前对所有点使用的插值法来简单地对中点的颜色进行插值。然后将三角形从中点跨越到形状的点,并在这些点内进行插值。
    您需要中间点才能获得正确的颜色。想象一个四边形:右上角和左下角为红色,其他两个角为蓝色。现在,您有两种可能性将四边形分成两个三角形,并且根据您使用的三角形,将有一条恒定颜色(红色或蓝色)的对角线。但是,如果您使用插值颜色(例如深洋红色)插入中点,一切看起来都很好(就像您使用双线性插值一样)。

  • 对于自相交的多边形,事情比任何人想要的都要糟糕。首先手动确定相交,将多边形分割为非自相交多边形,然后单独处理每个多边形(计算自相交引入的新点的混合颜色,就像现在一样)。

  • 对于凹多边形,您需要将多边形分割成凸块,然后按照上述方法处理它们。 但是这会改变颜色插值结果!因此,您需要确保多边形内部(在切割件的边缘)的颜色是正确的。因此,您需要在保存插值颜色的多边形内部引入新点,类似于凸多边形中点的想法。最好的方法是计算形成多边形的点的voronoi cells。这些单元格的角点应该是很好的插值点。

我希望我能够清楚地说明如何处理您的问题。
做所有这些事情都是可能的,但是真的值得付出努力吗?就像“Cody Gray”所说:为什么要重新发明那么复杂的东西?

let's assume you have a (good) reason not to use OpenGL or Directx or even GDI+.

I'm not sure if I really understand the graphical primitive you are trying to rasterize. I assume it's a polygon with n points. Interpolating color inside such a shape is not completely trivial, since you have different approaches to do this. So the question is, what do you want?

Think of the differences in convex and concave (or even self-intersecting) polygons. It's not really clear what the result "should" look like. That's why graphics hardware stops worrying about anything else but triangles. I suggest you do (almost) the same.

Interpolating colors inside a triangle is pretty easy and unambiguous. The buzzword to google for is baryzentric coordinates.

So, the remaining question is how to triangulate your polygon.

  • For convex polygon you can simple once interpolate the color for the mid-point using an interpolation similar to the one I assume you are currently using for all points. Then span triangles from that mid-point to the points of your shape and interpolate inside these.
    You need the mid-point for getting the right colors. Think of a quad: the upper-right and lower-left corner colored in red and the other two corners colored in blue. Now, you have two possibility for splitting the quad into two trianges and depending on which one you use there will be a diagonal line of constant color (either red or blue). But if you insert the mid-point with an interpolated color (something like a dark magenta) everything looks fine (like you were using bilinear interpolation).

  • For self-intersecting polygons things are worse than anyone would like it. First manually determine the intersections, split the polygon in non-self-intersecting polygons and then handle each polygon for itself (calculate the mixed color for the new points introduced by the self-intersetions, like you do now).

  • For concave polygons you need to split the polygon into convex pieces and then handle these as described above. But this will change the color interpolation result! So you need to ensure that the color inside the polygon (at the edges of the cutted pieces) is correct. Therefore you need to introduce new points inside the polygon holding interpolated colors, similar to the idea of the mid-point for convex polygons. The best way to do this, is to calculate the voronoi cells of the points forming the polygons. The corner-points of these cells should be good interpolation points.

I hope I was able to be somewhat clear on how I would handle your problem.
It is possible to do all these things, but is it really worth the effort? Like "Cody Gray" said: why reinventing something that complicated?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文