joe_coolish
joe_coolish

Reputation: 7259

Manually blending colors

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.

Upvotes: 0

Views: 490

Answers (1)

Knowleech
Knowleech

Reputation: 1055

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?

Upvotes: 3

Related Questions