Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Interpolating color across a polygon


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 joekarl   Members   -  Reputation: 101

Like
0Likes
Like

Posted 12 October 2012 - 01:39 PM

I'm working on a software rasterizer just for grins and I'm to the point where I want to implement smoothing between different colors at vertices across a polygon.

From what I've read, it consists of calculating a scan line, calculating the colors at the ends of the scan line, and then linearly interpolating across that scan line to get the color at a single point.

My question is how to calculate the color at the end of the scan line for polygons with 4+ points. 3 points is pretty straight forward (though I haven't actually done the calculations yet), but 4 is a bit harder to wrap my head around.

Anyone have any good resources (or code) to get me started?

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13624

Like
0Likes
Like

Posted 12 October 2012 - 01:47 PM

The point at the end of the scan line belongs to a side of the polygon. You interpolate between the two vertices involved. What's the difficulty?

#3 eppo   Crossbones+   -  Reputation: 2507

Like
0Likes
Like

Posted 12 October 2012 - 02:12 PM

You should triangulate/triple the mesh. Directly drawing quads and N-gons tends to cause artifacts, due to (floating point) precision errors (points on a 4+ polygon seldom lie on a single plane).

Plus, writing a triangle rasterizer is as you say more straightforward.

#4 joekarl   Members   -  Reputation: 101

Like
0Likes
Like

Posted 12 October 2012 - 02:18 PM

Okay, so I'm doing some clipping to cut down on the surface area that I'm actually drawing. Right now thats being done before I'm rasterizing the polygon which is resulting in non-triangle polygons when I get to rasterization. So how should I go about avoiding those 4+ vertice polygons?

#5 Álvaro   Crossbones+   -  Reputation: 13624

Like
0Likes
Like

Posted 12 October 2012 - 02:31 PM

Since your polygons are convex, you can just pick a vertex and convert the polygon to a fan around it, in the obvious way.

#6 Krypt0n   Crossbones+   -  Reputation: 2600

Like
0Likes
Like

Posted 16 October 2012 - 12:08 AM

if you want smooth interpolation, you can calculate per pixel that you set, the distances to the 4 vertices and divide by the sum of those 4 distances, that way you'll get weights to blend the 4 vertex colors, just using the edge color between two vertices or splitting quads in two triangles, won't make it look smooth.

keep in mind, that you also need to interpolate perspective correctly, if you rasterize 3d triangles.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS