News:

As a consequence of the forum being updated and repaired, the chatbox has been lost.
However, you can still come say hi on our Discord server!

Main Menu

Anybody who can explain Z-buffer and Framebufferobjects?

Started by Luna_blade, 04, March, 2016, 06:12:15 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Luna_blade

Just out of curiosity, I'd like to know how a Z-buffer and framebufferobjects work in a 3D game.
Apparently, both are a solution to the Visibility problem in 3D games.
I spent a lot of time looking for proper explanations, which I didn't find.
I know and understand the painters algorithm, but the other two however...

"Hear the sounds and melodies
Of rilets flowing down
They're the verlasting songs
Whispering all the time
As a warning that behind some rocks
There's a rigid grap even
Oreads fear the tread"

Daddy Poi's Oily Gorillas

Wait, what?

I think buffer is pretty much just an array in RAM?
So I take it that Z-buffer is probably just an array where each value is for each pixel....? Probably holding the layer number or something... well... something representing priority? (My guess is if a pixel in another triangle is lower than this priority, it gets completely ignored.... but if greater, it gets set on top and z-buffer gets updated with the new info about it. - Whether "lower" or "greater" is closer to 0, I'm not sure.)


I have something for blending in Yoshi Magic.. but it's done via flags for each pixel... (1 means blend on next layer, 0 means don't...)
Before blending feature was added, I drew the bottom layer first, after adding the blending feature, I changed Yoshi Magic to draw the top layer first. I chose top-down because that's how the blended layers are selected....


---
Is framebufferobjects like a cache or something? No idea? (Guessing again.)
Golden Sun Docs: Broken Seal - The Lost Age - Dark Dawn | Mario Sports Docs: Mario Golf & Mario Tennis | Misc. Docs
Refer to Yoshi's Lighthouse for any M&L hacking needs...

Sometimes I like to compare apples to oranges. (Figuratively) ... They are both fruits, but which one would you eat more? (If taken literally, I'd probably choose apples.)
Maybe it is over-analyzing, but it doesn't mean the information is useless.


The only GS Discord servers with significance are:
Golden Sun Hacking Community
GS Speedrunning
/r/Golden Sun
GS United Nations
Temple of Kraden

Can you believe how small the Golden Sun Community is?

2+2=5 Don't believe me? Those are rounded decimal numbers. Take that, flat earth theorists! :)

Luna_blade

Well, what I do understand about Z-buffer is, that it is indeed an array in the GPU-RAM and every pixel is a floating point number.
The number corresponds to what the 'depth' of that pixel is. I also get that you start with every pixel each frame set to 0 and then counting up for each object which is more in the front.
But... I don't quite get how the Z-buffer keeps all the objects apart and how it is used to draw after the depth calculations.
"Hear the sounds and melodies
Of rilets flowing down
They're the verlasting songs
Whispering all the time
As a warning that behind some rocks
There's a rigid grap even
Oreads fear the tread"

Daddy Poi's Oily Gorillas

Yes... except in the event that it is a user-created z-index array, I'd argue that floating-point could be any value datatype you choose. (Preferably integer/32-bit?) I'd recommending staying away from floating point (single or double) if you don't need it since it may be slower... (Could also depend on your processor? In which case, I'd be interested in more information.)
Basically... I don't think it's about keeping the objects apart, but about calculating what's in front....
-Like draw one object... its z-indexes are set... draw the next object... and whatever pixels shown from that object alters z-indexes.. but everything that comes behind the object (Which is compared with the z-index for those pixels) is ignored.).
-Objects are kept separate in other table(s)... (Triangle and vertex listings and such).... and those are rendered to the screen every frame...

@after the depth calculations: Do you mean like the HUD one might always make displayed in front?
If you want something to always display in front... you just draw it after you've drawn your 3D map... (For stuff always on top, you may not need to use z-index table anymore... unless you got some more priority-ing going on, maybe..)

Or is it something I'm missing? Like texture calculations and such instead?

---
^May not have spent enough time on this post, so could take some editing.
Golden Sun Docs: Broken Seal - The Lost Age - Dark Dawn | Mario Sports Docs: Mario Golf & Mario Tennis | Misc. Docs
Refer to Yoshi's Lighthouse for any M&L hacking needs...

Sometimes I like to compare apples to oranges. (Figuratively) ... They are both fruits, but which one would you eat more? (If taken literally, I'd probably choose apples.)
Maybe it is over-analyzing, but it doesn't mean the information is useless.


The only GS Discord servers with significance are:
Golden Sun Hacking Community
GS Speedrunning
/r/Golden Sun
GS United Nations
Temple of Kraden

Can you believe how small the Golden Sun Community is?

2+2=5 Don't believe me? Those are rounded decimal numbers. Take that, flat earth theorists! :)

leaf

Fox, at least read the wikipedia article before you try to help someone. The z-buffer is typically stored as a normalized 24-bit or 32-bit fixed point number (not floating point, and not an integer), taking on a value between 0 and 1, where 1 represents the cut-off point for rendering, and 0 represents the camera. This makes the camera size-agnostic, capable of handling both large and small scenes alike. Note that this isn't simply stack ranking their depth; it's precisely calculating *how* far away an object is from the camera.

As I understand it, the z-buffer essentially operates as an evolved version of the reverse painter's algorithm, but instead of operating on entire objects, it operates on individual pixels. The rendering engine examines each object in the scene that could potentially occupy a given pixel, and performs a simple less than check. It then selectively renders only the object with the lowest value (i.e. is nearest to the camera). It's important to note that it's only rendering a pixel, which is only a small sample of the object; it does not render the whole object, so if an object is partially occluded, it will only be partially rendered. This saves an immense amount of processing by reducing the amount of data that needs to be sent to the GPU; there's no need to send the data for 100 objects if only 50 of them are ever visible in the scene, and only half of those 50 objects are ever fully visible. Sending data to the GPU is the primary bottleneck in the rendering pipeline, so while you do end up saving by not doing the additional rendering passes for the hidden objects, the greatest saving ends up being that the data for the hidden objects is never sent to the GPU to begin with. Naturally, this is only useful for opaque objects.

As for a frame buffer object... I don't really know how that relates to solving the depth problem. From some cursory searching, it seems to be an OpenGL-specific thing, though other APIs use similar systems. It's basically a back buffer that can have post-processing effects applied to it. So it calculates the rendering for the next frame in one section of memory, applies whatever effects it needs to, then points to the data for that frame on the next draw call. Use of a back buffer is extremely common in rendering as a means of solving screen tearing, since there's always a complete image for the screen to display on a given frame.
[spoiler=quotes]
[9:00:50 PM] Randel Peltier: Ok...what did I do last night?
[9:01:19 PM] Kain: Something boring, repetitive, and lasted for about sixty seconds.
---
[10:45:08 AM] Salanewt: But yeah, the elemental phalluses are being... Stroked up by Saturos and co., and the energy will go towards... Mt. Muffin, where the Golden Climax will arise.
[7:28:42 PM] Salanewt: An added bonus is that Isaac and co. were trying to stop Saturos and co. because their beliefs state that Mt. Muffin should remain a virgin.
---
[9:54:21 PM] Randel Peltier: Guess the number in my head an you get to pick what I say. Number between 1-10
[9:54:28 PM] leaf: 11
[9:54:36 PM] Randel Peltier: @#$%!
---
[8:38:13 PM] Randel Peltier: Shes like this queen up on a pedastal that I have yet to court.
[8:38:29 PM] Kain: You've tried courting her.
[8:38:43 PM] leaf: and failed spectacularly
[8:38:44 PM] Randel Peltier: Ive tried...shes the best dating game ever.
---
[12:24:35 AM] Salanewt: I need to find a picture of a naked person to put on the Christmas tree next year.
---
[2:19:06 PM] Zeadra: wait... Rief's a guy???
---
[1:09:57 PM] Zeadra: well if you want to know if its a new effect or something weird, just check GS1, if side step is there maby it is the nimble dodge thing
[1:10:35 PM] Kioll: For once, you've contributed something useful.  o.O[/spoiler]

Daddy Poi's Oily Gorillas

I saw that article when I was looking up the Painter's Algorithm article... I did go to the Z-Index article from there, and yes, I did read some of it at that time... not the whole thing because it's long... . (I don't think I was fully paying attention, though..  But I do remember reading about the 16-bit+ part and that 8-bit was rarely used... and was thinking about that in my post.... What I SHOULD have paid attention to and looked up was fixed point... since I didn't exactly realize what that was... And assumed you could use any data type you wanted as long as you had enough bits... (Yes, you can put (invisible) decimal numbers in ints (if number is shifted) ... But not straight forward for numbers that are repeating like 1/3rd, which could be rounded anyway...) based on some quick theories... note that I've not built my own 3D engine yet, but I know that with some research I can do it. (Including sin/cos rotation) Don't exactly have the motivation right now, though.)

And yes, 0 being the camera's position makes a lot of sense.

But since you seem more knowledgeable about terms, then I leave the answers up to you for right now, I guess.

I've known this for a while now, though... And I think of both as floating-point.
32-bit = Single
64-bit = Double
And huh... must have missed Decimal (fixed-point.) (I recognize that keyword Decimal, but I not pay much attention to it. - I get the feeling it is one of those things I looked up in the past, learned it was fixed-point... and then forgot about it, though.) -  I still prefer to use the integer data-types when possible, though... it's just my way of doing things, I guess. (It is possible there may be exceptions, however. But so far, I haven't had to really worry about it in my coding as of yet... - I can maybe think of some things where one may get the computer's date/time (Double), though... But we're not likely to need that here.... )


-----
TL;DR = Basically, this post doesn't actually contribute to the topic itself.... so moving on...
Golden Sun Docs: Broken Seal - The Lost Age - Dark Dawn | Mario Sports Docs: Mario Golf & Mario Tennis | Misc. Docs
Refer to Yoshi's Lighthouse for any M&L hacking needs...

Sometimes I like to compare apples to oranges. (Figuratively) ... They are both fruits, but which one would you eat more? (If taken literally, I'd probably choose apples.)
Maybe it is over-analyzing, but it doesn't mean the information is useless.


The only GS Discord servers with significance are:
Golden Sun Hacking Community
GS Speedrunning
/r/Golden Sun
GS United Nations
Temple of Kraden

Can you believe how small the Golden Sun Community is?

2+2=5 Don't believe me? Those are rounded decimal numbers. Take that, flat earth theorists! :)

Luna_blade

Quote from: Fox@after the depth calculations: Do you mean like the HUD one might always make displayed in front?
If you want something to always display in front... you just draw it after you've drawn your 3D map... (For stuff always on top, you may not need to use z-index table anymore... unless you got some more priority-ing going on, maybe..)
Well after depth calculations, the GPU starts to actually draw the frame. That's more what I meant.

Quote from: leaf on 05, March, 2016, 12:45:40 AM
As I understand it, the z-buffer essentially operates as an evolved version of the reverse painter's algorithm, but instead of operating on entire objects, it operates on individual pixels. The rendering engine examines each object in the scene that could potentially occupy a given pixel, and performs a simple less than check. It then selectively renders only the object with the lowest value (i.e. is nearest to the camera). It's important to note that it's only rendering a pixel, which is only a small sample of the object; it does not render the whole object, so if an object is partially occluded, it will only be partially rendered. (quote shortened)
So if I understand correct: first when a polygon is rendered it's pixel are give a depth value, then they are placed in the Z-buffer. And when the depth calculations are over, every pixel from this object that is visible, is drawn.

I think I understand now. Isn't it however a bit silly that the GPU still has to fetch all the colors and texture for the given drawn pixel?
Also, by using a Z-buffer, you can't just draw triangles anymore (not that it mattered for untextured things though), but have to use something else?
"Hear the sounds and melodies
Of rilets flowing down
They're the verlasting songs
Whispering all the time
As a warning that behind some rocks
There's a rigid grap even
Oreads fear the tread"

leaf

How is it silly? Yes, it needs to fetch the texture data, but it only fetches that texture data once for a given object, regardless of how many pixels it takes up on screen.

Second part, not sure what you're asking, but I think that's my fault for the way I explained it. You still need to know the geometry of an object to use z-culling, but you get the advantage of fewer draw calls. Calculating geometry is done by the CPU, and is no where near as big of a deal as the process of actually rendering a texture or a light to screen.

As I alluded to earlier, sending data to the GPU is the slowest part of the rendering pipeline. It's the physical act of data being sent across from the CPU to the GPU that is slow, but it needs to be done for each draw call, which writes the display data to the buffer. Thus, we can improve rendering time by reducing the number of draw calls. In general, each texture that gets rendered to the screen needs to be a separate draw call, and game engines like unity will often batch all objects with the same texture in one draw call; this optimization is possible thanks to the z-buffer.

For example, suppose you're using the painter's algorithm. You perform a draw call for the far objects, then the nearer objects, then the nearer-nearer objects, and so on. Even if some of these objects use the same texture, they still need to be separate draw calls, because one might get overwritten later, and the buffer only stores a flat image. But, with z-culling, you calculate ahead of time which objects should actually be rendered. So if something is half-occluded, you're only writing to half as many pixels when you draw that object, and those pixels don't get written to by any other objects in the scene*. This isn't a big deal on its own (writing to a pixel is relatively cheap, performance-wise), but what happens if you have another object with the same texture in the scene, also visible in the viewing plane? Both objects can now be rendered in the same draw call, because you've calculated ahead of time which objects should be rendered in the scene. You end up only sending that texture data to the GPU once, instead of multiple times.

*(Except lights. Baked lighting is one additional draw call after all other objects are rendered, and each dynamic light is another.)
[spoiler=quotes]
[9:00:50 PM] Randel Peltier: Ok...what did I do last night?
[9:01:19 PM] Kain: Something boring, repetitive, and lasted for about sixty seconds.
---
[10:45:08 AM] Salanewt: But yeah, the elemental phalluses are being... Stroked up by Saturos and co., and the energy will go towards... Mt. Muffin, where the Golden Climax will arise.
[7:28:42 PM] Salanewt: An added bonus is that Isaac and co. were trying to stop Saturos and co. because their beliefs state that Mt. Muffin should remain a virgin.
---
[9:54:21 PM] Randel Peltier: Guess the number in my head an you get to pick what I say. Number between 1-10
[9:54:28 PM] leaf: 11
[9:54:36 PM] Randel Peltier: @#$%!
---
[8:38:13 PM] Randel Peltier: Shes like this queen up on a pedastal that I have yet to court.
[8:38:29 PM] Kain: You've tried courting her.
[8:38:43 PM] leaf: and failed spectacularly
[8:38:44 PM] Randel Peltier: Ive tried...shes the best dating game ever.
---
[12:24:35 AM] Salanewt: I need to find a picture of a naked person to put on the Christmas tree next year.
---
[2:19:06 PM] Zeadra: wait... Rief's a guy???
---
[1:09:57 PM] Zeadra: well if you want to know if its a new effect or something weird, just check GS1, if side step is there maby it is the nimble dodge thing
[1:10:35 PM] Kioll: For once, you've contributed something useful.  o.O[/spoiler]

Luna_blade

"Hear the sounds and melodies
Of rilets flowing down
They're the verlasting songs
Whispering all the time
As a warning that behind some rocks
There's a rigid grap even
Oreads fear the tread"