• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mchart

Linear Depth Buffer

3 posts in this topic

Hi I'm new here I hope I'm not making mistakes with this post! if so please tell me! 

 

I'm  rendering a scene on a render target, I've then to access its depth information in a post-processing stage. In this stage I need linear depth info. I've been browsing the internet quite a lot and found a lot of different approaches/alternatives. What do you think is the way to go? I'm a bit of a newbie with this stuff so can you give me an hint/pointer on the possible GLSL code?

 

Thank you very much :) 

 

Micheal

0

Share this post


Link to post
Share on other sites

Typically you'll read the value from your non-linear depth buffer and reconstruct the view-space z position at that point.

// let s and t be two values that satisfy the following two equations:

(s*f + t)/f = 1    // f is the distance from the eye to far plane
(s*n + t)/n = -1   // n is the distance from the eye to near plane

// the above is simply the mapping the occurs when you do a projection transform
// s and t are found in your projection matrix at positions 3,3 and 3,4, respectively

// let d be the value in your depth buffer

// the (xxx + 1) * 0.5 is to map the [-1,1] clip-space coordinates to the [0,1] space in the depth buffer
d = ((s*z + t)/z + 1) * 0.5  
2d = (s*z + t)/z + 1
2d - 1 = (s*z + t)/z
2d*z - z = s*z + t
2d*z - z - s*z = t
z*(2d - 1 - s) = t
z = t / (2d - 1 - s)   // reconstructed z value

That math is just off the top of my head and might not be totally correct, but you should be able to get the picture from that. You basically just want to use your knowledge of the projection matrix you used when transforming the verts to do the inverse of that transformation and reconstruct the view-space z.

Edited by Samith
0

Share this post


Link to post
Share on other sites

Typically you'll read the value from your non-linear depth buffer and reconstruct the view-space z position at that point.

// let s and t be two values that satisfy the following two equations:

(s*f + t)/f = 1    // f is the distance from the eye to far plane
(s*n + t)/n = -1   // n is the distance from the eye to near plane

// the above is simply the mapping the occurs when you do a projection transform
// s and t are found in your projection matrix at positions 3,3 and 3,4, respectively

// let d be the value in your depth buffer

// the (xxx + 1) * 0.5 is to map the [-1,1] clip-space coordinates to the [0,1] space in the depth buffer
d = ((s*z + t)/z + 1) * 0.5  
2d = (s*z + t)/z + 1
2d - 1 = (s*z + t)/z
2d*z - z = s*z + t
2d*z - z - s*z = t
z*(2d - 1 - s) = t
z = t / (2d - 1 - s)   // reconstructed z value

That math is just off the top of my head and might not be totally correct, but you should be able to get the picture from that. You basically just want to use your knowledge of the projection matrix you used when transforming the verts to do the inverse of that transformation and reconstruct the view-space z.

 

Thank you Samith, just one question. In which space t and s are distances eye-near/eye-far? If I look in my projection matrix (built via glm::perspective) at position 3,3 and 3,4 what I see is something like -1.03 and -1 where my clipping planes are [0.1, 8] 

0

Share this post


Link to post
Share on other sites

Thank you Samith, just one question. In which space t and s are distances eye-near/eye-far? If I look in my projection matrix (built via glm::perspective) at position 3,3 and 3,4 what I see is something like -1.03 and -1 where my clipping planes are [0.1, 8] 

 

Yep. The s and t values are computed given the near and far planes. In my post above I said that s and t are two values that satisfy the equations (s*f + t)/f = 1 and (s*n + t)/n = -1, but I didn't provide any actually solution for s and t tongue.png

 

If you are interested, the solutions for s and t can be found like so:

(sf + t)/f = 1
(sn + t)/n = -1

// here you have two linear equations and two unknowns. you can solve
// for s and t with your basic high school algebra techniques

eq1: sf + t = f
eq2: sn + t = -n

// subtract eq2 from eq1
eq3: s(f-n) = f+n
     s = (f+n)/(f-n)

// substitute s into eq1 and solve for t
eq4: sf + t = f
     f(f+n)/(f-n) + t = f
     f(f+n) + t(f-n) = f(f-n)
     f(f+n) - f(f-n) = -t(f-n)
     ff+fn - ff+fn   = -t(f+n)
     -2fn/(f+n)      = t

// now you can check s and t given a near and far plane of 0.1 and 8.0
// s = (f+n)/(f-n) = (8-0.1)/(8+0.1) =~ 1.025
// t = 2fn/(f+n)  =~ -0.19

Some caveats: these computations depend on some nitpicky details about your projection. For example, if you use your projection matrix to transform a right handed coordinate space into the left handed clip space, then the original equations will be slightly different (ie s*-f + t = f instead of sf + t = f, etc). Also, the s and t values will be found in your projection matrix in the 3,3 location and 3,4 location if your projection matrix is column major, but they'll be in 3,3 and 4,3 if your projection matrix is row major.

 

I don't know enough about your exact environment to give you all the answers, but I hope the algebra I've posted at least gets you started down the right path.

Edited by Samith
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0