1

I want to know if there is a way to know if a specific pixel is shown (e.g i want to get all positions that is behind the scene, which means all background objects that is not shown in the near plane).

  • Can you explain exactly what you are trying to achieve ? – Abhishek Bansal Mar 09 '14 at 07:31
  • You can access depth buffer as explained here though.. if that is what you are trying to achieve.. http://stackoverflow.com/questions/6340724/how-to-copy-depth-buffer-to-a-texture-on-the-gpu – Abhishek Bansal Mar 09 '14 at 07:39
  • I want to check if some position in the world space is shown in the near plane –  Mar 09 '14 at 14:46
  • This is a bit ambiguous. The near plane is a plane. Positions behind the near plane converge to a point so this probably isn't what you want. Do you want to check if something is visible? Do you want to check if something is behind the camera? Do you want to find the position of something on-screen (in pixels)? – jozxyqk Mar 11 '14 at 12:51

1 Answers1

0

It's probably faster and easier to do some simple calculations yourself CPU side. For example...

clipSpacePoint = projectionMatrix * viewMatrix * modelMatrix * vec4f(0, 0, 0, 1); //using the model's origin and transform matrix
//or
clipSpacePoint = projectionMatrix * viewMatrix * vec4f(x, y, z, 1); //using the model's position

clipSpacePoint.xyz /= clipSpacePoint.w; //possible division by zero

//point is visible if clipSpacePoint.w is positive and clipSpacePoint.xyz are between -1 and 1

Alternatively, and to better answer your question, you can use the occlusion query to check how many fragments have been rendered during a draw call. An example application might be checking if a bright light is visible and drawing a lens flare if it is. The occlusion query, like many OpenGL calls, run asynchronously so if you need the results right away you can stall the rendering pipeline. If you don't mind waiting a frame or two for the result it may run a bit faster.

This won't tell you where the pixels were drawn though. Two ways to find the pixel locations come to mind...

  1. You could write pixel coordinates in your fragment shader to a texture, use the histopyramid/stream compaction method to move them to the start of the texture, and read them back from the GPU.

  2. If you don't mind using recent GL features, ARB_atomic_counter can be used in the fragment shader to create a unique index which you could then use to write the fragment's coordinates to a buffer or texture with ARB_image_load_store. You'll probably also want to enable the early depth test for this too.

These are both far more complex than doing the point in box check or an occlusion query.

jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • could you explain more or give me an example ?? –  Mar 10 '14 at 23:21
  • @PsykOoO Could you be more specific? I've listed four potential methods and suggested the first. Is there some part of the code you want me to elaborate on? – jozxyqk Mar 11 '14 at 05:36
  • How i can find the pixel location using The second way that you told me ?? –  Mar 11 '14 at 12:37
  • As said, the occlusion query won't give you the locations (unless you repeatedly render to tiles within the window, which would not be fast). The easiest way to find the rough location is CPU side with your own matrix transformations. To get the exact pixel locations is pretty complicated if you want them quickly and probably more trouble than its worth. – jozxyqk Mar 11 '14 at 12:47