# Polygon Sorting Solution

## Recommended Posts

Working on an assigment for Uni I came up with this fast solution to sorting blended polygons. I have yet to see this technique used elsewhere or talked about in a tutorial so I thought i would share. It doesn't work in all situations but has worked well for my requirements I hope some of you find this useful. Understanding this stuff requires a working knowledge of vectors. As most of you probably know, to draw blended polygons correctly (in opengl at least) you need to draw all the unblended polygons, then the blended polygons from the furtherest to the nearest. However working out at which polygons to draw first can be a bit of a messy problem and most solutions, while computationally cheap can create visual artifacts. The simplest method is to use the polygon's maximum or average depth and draw the polygon accordingly. This breaks down however if two overlapping polygons have the same maximum or average depth (a few geometric primatives have this problem as I found out) at certain camera angles. Not wanting to make my sorting any more computationally expensive I came up with this neat little solution. When two polygons share the same maximum or average depth the polygon with the smallest angle between it's normal and the viewing direction of the camera is drawn first. One need only calculate this when faced with the previous equality and the angles need not be calculated only the cosine of the angles. i.e in 2D cv (camera view) = 0i + 1j p1n (polygon 1 normal) = 0.707i - 0.707j p2n (polygon 2 normal) = 0.866i - 0.5j cv.p1n = -0.707 (135 degrees) cv.p2n = -0.5 (120 degrees) So polygon 2 will be draw first. This technique won't help with overlaping polygons (those polygons that go through over polygons) of course, and I have no idea what would happen if applied to concave polygons but it can help in some situations. I hope it is useful to someone it certainly saved me some pain! Rory Hart Computational Science Student

##### Share on other sites
Hi Rory, thank you for your post. I have been looking into this very type of thing recently, and your information has been the most helpful so far. I will get cracking on this and I'm sure it will help. Thank you.

##### Share on other sites
Note that the polygon sorting problem is generally unsolveable, even if the polygons don't touch each other. There can be recursively overlapping polygons like in http://www.gamedev.net/reference/articles/article668.asp in the "What kind of performance can I expect?"-paragraph.

##### Share on other sites
A good working solution is to separate the transparent polygons from solid ones in mesh model and just sort the objects when rendering, this is very good approach and doesn't demand much logic and calculations.

Another working but not perfect method is that you first render just solid and then just transparent polygons.

But completely solving this problem is not possible. Maybe after medium-market graphical hardware will be able to raycast huge scenes in real time (like 3-5 years from now on).

##### Share on other sites
I have used an example of additive blending code by Jonathan DeKlotz with much success lately:

Data: Graphical data consists (amongst small details), a 3D array of quads, dividing the simulation field into a 3D array of cells.

Code:
glDisable(GL_LIGHTING);glDisable(GL_DEPTH_TEST);glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	glBegin(GL_QUADS);

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628327
• Total Posts
2982089

• 22
• 9
• 9
• 13
• 11