Archived

This topic is now archived and is closed to further replies.

nipplerash

Alpha Blending And Z-Buffer

Recommended Posts

Now there''s a title to grab everyones attention, ahem, and now that I have your attention... this may get a bit waffly and hand wavey. I''m writing code to create a stereo 3d demo. It''s quite cool, what I''ve figured you have to do is render everything twice per scene (a left image and a right image) shifting the camera correspondingly, this is quite easy to do, (but watch out for your UpVector, it needs to be well defined) I have no textures for my models, I just use two materials (red/blue) and pass them into the device when I render the meshes. I use alpablending to mix the colours together (yeah I''m English), this is where I get a problem, when I have two faces that lie on the same plane I get a graphical artifact similar to that of the blood in half life, and it looks quite messy. However I can turn off the z-buffer and its problem solved but at a cost: now I have all the back faces blended into the image also I have this dis-benifit of not having a z-buffer I''ve tried messing about with the alpha-blending flags and z-buffer bit depth but to no avail! Can you help?

Share this post


Link to post
Share on other sites
I''m afraid you''re not going to like my answer. The Zbuffer basically keeps track of the closest point to the camer per pixel drawn. It doesn''t care if it''s transparent due to color keys or alpha channels. If a triangle, or quad as probably in your case, is drawn, then it won''t draw anything behind.

So basically, ZBuffering is great when you are working with triangles that aren''t transparent in any way. When you are, you have to sort these traingles yourself from front to back. Most games will use zbuffering, draw all not-alpha triangles, then keep only ztesting on, and draw the alpha stuff from front to back.

I think your solutoin is you''re going to have to start sorting from front to back. Very slow unless you use some kind of linked list. That gets into another mess, if you want to program it yourself. But I could help ya if you need more,

--Vic--

The future of 2D game development:
Flat Red Ball

Share this post


Link to post
Share on other sites
Thanks for the response. To clarify, you say when the z-buffering is enabled I get the graphical artifact beacuse its not bothering to drawing all the pixel since it thinks they''re hidden behind other pixels which I want them blending to.now if I was to sort the rendering order, would I do it per triangle or per mesh, changing the vetex data every frame is going to kill the frame rate, and sorting by mesh won''t work since the models are quite complex and are needed to appear the same distance away from the user. I think a linked list might work if I was rendering windows and the like, but I''m not sure that it would be applicable to my applications.

Share this post


Link to post
Share on other sites
Actually you can sort faster than you think. Just use a Radix or Binary sort as some call it.

Its the fastest for large amounts of polygons since it is a n(logN) algorithm unlike the bubble sort it is a n(0*logN).
Using a recursive Radix sort on a linked list might be what you are looking for in respect to ordering them correctly. It only needs 4 passes to sort each list. if you have combined lists that are related by index you can use a hueristic feature that bubble sort uses. And you would only have to sort them if the position moves.

Share this post


Link to post
Share on other sites
I would need to sort the vetex data every time I changed the position, angular position, camera position. I think I would need a world space transformation so I can order my vertex data relative to the camera, I would have to lock vetex buffer and rewrite it (I''m not sure how long that would take) And how would I store the vertex information in a list anyway.
Even then I have no garantee that the triangle aren''t intersecting each other! Incidently which is faster qsort/radix or are those not comparible?

Share this post


Link to post
Share on other sites
Well I sort about 250,000 polygons in under 1 second with it.

Yes I do know the issues with doing it that way but hey its not as expensive as most might lead you to beleive. it will make your engine more flexible when loading other objects also. Implementing a QuadTree, BSP and PVS system would greatly reduce the amount of sorting needed.

It could also be possible to create a linked list with the closest adjcent polygons with a struct like so

struct Ojb{
Vertex *List;

}

struct obj{
obj *leftSide;
obj *RightSide;
obj *Bottom;
Ojb *Coords;
}

Using this type of linked list you can narrow the amount of space needed for the vertex by using the info in only one of the original sides. Creating an indexing array that indexes into a points to the correct polygon in question instead of actually sorting the polygons you can sort the list based on it. Now you may find other solutions but the idea is the sort the most with the least amount of effort.

Radix is the fastest by far for lots of polys but slow with under 10,000 Because a quick or bubble sort compare and need not pass more than once. But a radix sort must go through them atleast 4 times (Well that depends on the length of the data type actually(could be a double double and you would need to sort 12 times)) A radix sort does not compare data at all rather id creates and index table and an offset table . Using the index table it could how many of each number there are. Then using an offset buffer char[255] it creates and indexed list of where each item will go and keeps count of at what location. Then you dump your data back into another array and return it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hey come on people, all he needs to do is:

1. Clear color and z buffer
2. set camera to left eye viewport
3. Render scene with color write mask = red only.
4. Clear z buffer
5. set camera to right eye viewport
6. Render scene with color write mask = green only.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
to continue my above post, as you can see there is no sorting whatsoever involved.

if you want other colors than pure red or green or blue, you will have to render the left eye to the back buffer, the right eye to a pbuffer / rendertarget texture, and blend these two together.

still, this is much faster and easier than sorting, and allows you to take advantage of the z buffer.

Share this post


Link to post
Share on other sites
Hey AP, if I knew you I''d prob give you a kiss, that works a treat, really simple as well. I''m going to have a look at renderng some textures to a back buffer to enhance it''s looks, thanks alot!

Share this post


Link to post
Share on other sites
Just a note : I''ve worked for a pretty long time on stereoscopic images and unfortunately, several low end video cards can''t use color write masks. All GeForce can do it, but crap like G400 or S3 Savage can''t.

Share this post


Link to post
Share on other sites
Anyone wanna explain what AP said in simple english . It sounds like a much more intuitive way than sorting ass poly''s everytime something changes, but I really don''t get how it works.

Share this post


Link to post
Share on other sites
Illumini, here goes.

The problem: You can''t use ZBuffer in a scene when everything is transparent (Here, you need a semitransparent blue image and a semitransparent red image, each showing the same scene but with a slight bias in angle/whatever)

Solution 1: Get really smart and do your own Z-buffering work of hiding the polygons that have to be hidden. Thus sorting the polygons, etc etc...

AP''s solution: You render the blue scene -normally- (Z-buffer active and everything), save the resulting image. Render the red scene next in another buffer or on a surface meant to receive renders, whichever trick allows you to have both images in memory.
Then you just need to blend the two images - the polygon work is already done.

Share this post


Link to post
Share on other sites