1
\$\begingroup\$

I need to find the number of pixels an object occupies on screen. How do I do that using DirectX?

\$\endgroup\$
3
  • 4
    \$\begingroup\$ It would help to explain what it is you are actually doing. Are you try to perform picking, occlusion queries, or something else? \$\endgroup\$ Jul 7, 2017 at 5:54
  • \$\begingroup\$ what I am trying to do is find the number of pixel from a given object. Say I can pick object in a scene, how can I know the current number of pixels. \$\endgroup\$
    – user103062
    Jul 9, 2017 at 11:53
  • 2
    \$\begingroup\$ Why do you have 2 accounts? \$\endgroup\$
    – Bálint
    Mar 15, 2018 at 0:09

3 Answers 3

1
\$\begingroup\$

Apart from rendering the scene to a separate frameBuffer with a certain color, then counting those pixels after the render, you can't really do much.

You could theoretically take every face, transform their vertices to screen space and check which pixels they intersect, but then you would essentially do the same as the first method, just slower.

\$\endgroup\$
0
\$\begingroup\$

As Bálint noted:

You could theoretically take every face, transform their vertices to screen space and check which pixels they intersect, but then you would essentially do the same as the first method, just slower.

This would be slow. However, if you're willing, you could settle for the approximate answer.

Just take every non-back-facing triangle in your object and transform it to screen space. Then, measure the area of each triangle (and perhaps round this number). This gives inaccurate numbers, especially if your object is concave and in an orientation where non-back-facing triangles can overlap. It's much faster than testing individual pixels though.

\$\endgroup\$
1
  • \$\begingroup\$ If you're willing to approximate, counting the pixel coverage of the object's bounding box or bounding sphere might also be useful. \$\endgroup\$
    – DMGregory
    Sep 2, 2018 at 19:17
0
\$\begingroup\$

Raycasting can do this. Cast a ray from the eye position through the viewing frustum for each pixel. Count the rays that intersect that object. This won't give correct numbers if you're using transparent textures or materials though. Good luck.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .