Jump to content
  • Advertisement
Sign in to follow this  
Mihumihu

Getting a distance to thr fragment.

This topic is 813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I try to get the distance to the 3d objects by clicking, and I've made some "float to rgba" coded framebuffer texture, then gets a rgba and decode to the float back. And it's good till I use blending(you understand)...But I know that there some other way to get the distance to the fragment trough the depth, but I dont understand how to make it step by step. 

Please could anybody help me to understand that problem, how to make depth texture, and how to retrieve real distance from eye to pixel from that.

Thanks!

Share this post


Link to post
Share on other sites
Advertisement

Getting the distance to a 3D object from a depth texture shouldn't be too difficult. The steps to create a depth texture are, enable Depth Testing, bind the depth texture for rendering, perform your operations on (or using data from) the depth texture.

 

This is how you create a depth texture in OpenGL:

GLuint id;
int width = 512;
int height = 512;

// create a depth texture
glGenTextures(1, &id); 
glBindTexture(GL_TEXTURE_2D, id); 
glTexImage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
// set some parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0); // unbind

To use the depth texture within your shaders is the exact same way as you would any other texture, bind it to a texture slot and set a uniform for it. Make sure you also have enabled depth testing.

glEnable(GL_DEPTH_TEST);

glUniform1i(glGetUniformLocation(shaderProgram, "DepthTexture"), 1);

 

Within your shader you can access it like so:

uniform sampler2D DepthTexture;

 

When you sample your depth texture within a shader the Z component will store the depth of the fragment. i.e:

float depth = texture(DepthTexture, UV).z

 

You might want to bind the depth texture to a Framebuffer, but that is exactly the same as attaching any other texture apart from you need to use 'GL_DEPTH_ATTACHMENT' rather than something like 'GL_COLOR_ATTACHMENT0'.

Share this post


Link to post
Share on other sites

Getting the distance to a 3D object from a depth texture shouldn't be too difficult. The steps to create a depth texture are, enable Depth Testing, bind the depth texture for rendering, perform your operations on (or using data from) the depth texture.

 

This is how you create a depth texture in OpenGL:

GLuint id;
int width = 512;
int height = 512;

// create a depth texture
glGenTextures(1, &id); 
glBindTexture(GL_TEXTURE_2D, id); 
glTexImage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
// set some parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0); // unbind

To use the depth texture within your shaders is the exact same way as you would any other texture, bind it to a texture slot and set a uniform for it. Make sure you also have enabled depth testing.

glEnable(GL_DEPTH_TEST);

glUniform1i(glGetUniformLocation(shaderProgram, "DepthTexture"), 1);

 

Within your shader you can access it like so:

uniform sampler2D DepthTexture;

 

When you sample your depth texture within a shader the Z component will store the depth of the fragment. i.e:

float depth = texture(DepthTexture, UV).z

 

You might want to bind the depth texture to a Framebuffer, but that is exactly the same as attaching any other texture apart from you need to use 'GL_DEPTH_ATTACHMENT' rather than something like 'GL_COLOR_ATTACHMENT0'.

Thanks! But how to calculate distance from camera eye to the pixel of the depth? And what if I use logarithm z ? Is it not matter?

Edited by Turbochist

Share this post


Link to post
Share on other sites

You should have either an orthographic or perspective matrix which you would define a near and far plane. For example, the near plane is 1 and far plane is 10. When you do your depth testing, this is done for you automatically as part of the graphics pipeline, but you end up with a greyscale image where black is closest to the camera and white is further away (between the range 0 to 1). To get the depth just test the Z component of the returned result from querying the texture within your shader:

 

uniform smapler2D depth;

float depth = texture(depth, uv).z;

 

However, if you want a Z value from between the near and far plane that isn't within the range of 0 and 1; I would take a look at this: http://stackoverflow.com/questions/6652253/getting-the-true-z-value-from-the-depth-buffer

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!