Tuesday, February 17, 2009

(0) Comments

Rendering tutorial

welcome to infomix.blogspot.com

Sorting faces and application Z buffers

There's affluence of being that can go amiss if you're aggravating to affectation faces and added 3D altar on a 2D display. I'll accord an overview of accepted problems, accord accessible solutions for these problems, and explain what I did in the case I apparent it in my apprehension engine.

Faces appearance up in the amiss order

Rendering 3D faces, such as the walls and roof locations of our house, can be alarming if you're accomplishing it for the aboriginal time and accord ambagious after-effects if you don't watch it carefully. There's a bit of plan complex in announcement faces correctly, and I'll accord a aboriginal important address you'll accept to apparatus to accomplish abiding the faces of your objects don't look like crap.



I'm not traveling to altercate how you can draw faces or polygons on your screen, as it is out of ambit for this tutorial. There's a huge bulk of abstract accessible on this subject. If you're autograph a pixel based renderer (in adverse to my engine that works absolutely with vectors) you ability wish to do (gouraud or phong) concealment or texturing of your faces, but I'm not traveling to altercate this.

A affair that can go amiss is apparent in the account to the right. Faces appearance up in the amiss adjustment (this is not a bug in my apprehension engine, I can calmly about-face it on and off from my capital program). Some faces should be in foreground of others, but they appearance up covered by the face that is declared to be in the back. Your computer is blind of this and just displays them in the adjustment you acquaint him to. So it's a acceptable abstraction to array your faces afore announcement them, such that your apprehension engine displays the faces in the aback afore assuming the ones in front.

The catechism is how to actuate which face is in foreground of another. The acknowledgment is simple (but not sound, as I will appearance later). For a face, you can calmly actuate the abyss of the face by searching at the depth, or Z ethics of the coordinates basic the face. A face is in foreground of addition one if all of its coordinates' Z ethics are abate than the added face's coordinates' depths. We can accomplish it easier by accretion the boilerplate abyss of all coordinates that accomplish up a face, and analyze these depths. We can array all faces with abbreviating boilerplate depth, and affectation the faces in that order. The account on the appropriate shows the aforementioned appearance of the abode as the one above, but with all of its faces in the appropriate order.



There's one endure affair I'd like to appearance on this matter. Some humans don't apperceive how to array correctly. I accept met acceptance who can't transform a allocation algorithm that after-effects in an accretion adjustment to a allocation algorithm with abbreviating order, so don't be afraid (it alone takes two abnormal to acquisition the "<" or ">" and accomplish it the added one). I told you to array them with a abbreviating abyss and not the added way around! The account on the appropriate shows you the after-effects of allocation aggregate the amiss way. It gives some affectionate of an chaotic appearance of the house. If you see this in your apprehension engine, don't appear accusatory with me because I'll just laugh.



How I apparent it in my engine, and why I can about-face it on and off so easily

I will accommodate an addition way of allocation faces that was accessible in the architecture of my engine. Since my renderer uses the FIG architecture as an achievement format, I was able to corruption a affection in the FIG architecture that accustomed me to put faces in foreground of others area needed. The FIG book architecture has a abyss band system, in which you can accredit all 2D altar to a assertive abyss alignment from 0 to 999. So, in adjustment to put faces in foreground of one another, I can resort to this as a simulation of allocation the faces myself. The FIG architecture doesn't accept any data on what happens if you accept added than one article in the aforementioned layer. It is not defined whether one article is fatigued on top of the added if they're both on one layer, so it wouldn't advice me allocation them anyways.

Before I alpha apprehension my 3D altar to 2D objects, I aboriginal concern all the altar what affectionate of a abyss ambit they will crave for announcement them after in 2D objects. Aloft rendering, I accord the altar a block of FIG's layers and let the altar themselves amount out in which layers they accord in adjustment to body a FIG book in which the added altar end up in the added layers. This allows me to calmly assemble acceptable 2D images of my 3D worlds. I am alone bound to 1000 abyss layers, but it fits my accepted needs.

This abyss absorber is frequently accepted as a Z buffer.

Since I accredit a block of the layers aloft rendering, I can do air-conditioned things with my apprehension engine (although these will alone be acclimated in this tutorial). For example, I can calmly about-face the minimum and best band they may use and appearance you what affectionate of angel you ability apprehend if you don't apperceive what allocation is all about and do it upside down (as apparent in the third angel in this tutorial part). I can aswell accredit alone one band to the accomplished apple and appearance you what goes amiss if you don't array (the aboriginal angel in this part).

There's some added absorbing things I can do with those chunks of layers, too. I can acquaint the renderer to put assertive altar in a ambit of layers that is on top of some added objects, although the article on top should be in the bottom. I accept acclimated this ambush to actualize the apple angle in the pictures. First, I draw the abode in the basal bisected of the accessible layers. After that, I use the top layers to affectation the X, Y and Z axes and the wireframe of the abode on top of it. Using wireframes and dotted and abject band styles, the axes and the wireframe assume to be allotment of the accomplished rendered images, but it's fraud. You may allege me of cheating, but I anticipate it's artlessly an accomplished affection of my renderer. Of advance you can do this with your engine too by advisedly allocation some altar amiss afore announcement them.

Some faces still appearance up wrong

I accept already acicular out that the use of boilerplate base is not sound, and may aftereffect in adulterated images. I will now go into that.



In the account on the appropriate you can see what goes amiss in my apprehension engine if I try to cede 2 accurately crafted faces. In the apple view, the dejected face can alone be apparent as a line. It should, however, be bright from the apple appearance that the dejected face is declared to sit afterpiece to the camera, or in foreground of the red face if we're announcement both faces. However, as the red face is bigger than the dejected face, and back it has some coordinates that are abundant afterpiece to the camera than the dejected face's coordinates, it's boilerplate abyss becomes abate than the boilerplate abyss of the coordinates in the dejected face. This after-effects in the red face getting rendered in foreground of the dejected face. And this can be a difficult botheration to solve. I accept currently no band-aid for this in my apprehension engine. There are a brace of means to break this botheration if you absolutely charge to get it out of the way.

One ambush would be to assassinate an algorithm for "hidden band removal" or "hidden polygon removal" on your polygons. But this is not simple to implement. The hidden band abatement algorithm seeks band segments of your polygons that are hidden afterwards faces that should appear in foreground of them, and again cuts the band segments (together with chunks of the polygon that are hidden) out of the polygons. As such, alone the arresting allotment (or parts, if one polygon cuts through addition one) of a polygon charcoal in the apprehension engine, and the faces appearance up correctly. It is appealing complicated to get a acceptable alive hidden band remover to plan in a 3d engine in adjustment for faces to appearance up correctly. There's affluence of accessible cases to plan out to accomplish abiding all band segments that are hidden are removed, and added tricks (like acid polygons up to amount out its basic triangles) are bare to ascertain whether a band articulation is hidden or not. Sticking to hidden band abatement is easier than investigating hidden polygon articulation removal, as acid out a polygon allotment from a polygon is a lot harder than acid out a band articulation from a line. I will not altercate this address added as it is out of ambit for this tutorial.

Faces don't cut anniversary other

A endure affair I charge to point out is faces that bisect anniversary other. They can actualize adulterated renderings as can be apparent in the angel on the right. The dejected and red face bisect anniversary added forth the X axis, but they do not appearance up intersected if rendered in the camera view.



Indeed, if we affectation the faces in our camera view, we ask the computer to cede a red and a dejected face. The computer has no abstraction they should intersect, and that they should appearance up in parts. Solving this botheration is aswell a adventure on its own. I'll accord some pointers on how you could apparatus a solution. I accept not implemented this in my apprehension engine. (Note that the apple appearance in the angel on the appropriate shows up accurately as I've been cheating by assuming two red faces in the apple appearance instead of alone one)

In adjustment to ascertain the intersections of faces, it's a acceptable abstraction to triangulate your polygons first. Triangulation is consistently accessible to ascertain intersections of faces. It can aswell be actual accessible if audition hidden curve and hidden polygon segments as acicular out in a hidden articulation abatement algorithm as above.

In the circle algorithm, you will accept to ascertain faces, or triangles that bisect anniversary other, which is absolutely achievable with a bit of math. According to the intesections you ability accept found, you can cut up the triangles into abate triangles afterward the intersections of the faces. Keep on accomplishing this until no added intersections were found, and the botheration is solved. Note that you can assassinate this algorithm afore apprehension it to the screen, as the intersections are absolute of the circling of your altar (intersections circle along) and you alone charge to acquisition the intersections already if announcement the altar from altered camera views. This is in adverse with the hidden band abatement algorithm, which requires you to recompute the hidden band segments if the altar are beheld from a altered angle, as the polygons will awning added hidden segments.



0 Responses to "Rendering tutorial"

Post a Comment