Screen Projection and Culling united

无人久伴 提交于 2019-12-02 09:19:35

问题


I am currently dealing with several thousand boxes that i'd like to project onto the screen to determinate their sizes and distances to the camera.

My current approach is to get a sphere representing the box and project that using view and projection matrices and the viewport values.

// PSEUDOCODE

// project box center from world into viewspace
boxCenterInViewSpace = viewMatrix * boxCenter;

// get two points left and right of center
leftPoint = boxCenter - radius;
right = boxCenter + radius;

// project points from view into eye space
leftPoint = projectionMatrix * leftPoint;
rightPoint = projectionMatrix * rightPoint;

// normalize points
leftPoint /= leftPoint.w;
rightPoint /= rightPoint.w;

// move to 0..1 range
leftPoint = leftPoint * 0.5 + 0.5;
rightPoint = rightPoint * 0.5 + 0.5;

// scale to viewport
leftPoint.x = leftPoint.x * viewPort.right + viewPort.left;
leftPoint.y = leftPoint.y * viewPort.bottom + viewPort.top;

rightPoint.x = rightPoint.x * viewPort.right + viewPort.left;
rightPoint.y = rightPoint.y * viewPort.bottom + viewPort.top;

// at this point i check if the node is visible on screen by comparing the points to the viewport

// calculate size
length(rightPoint - leftPoint)

At another point i calculate the distance of the box to the camera.

The first problem is that i won't know if the box is just below the viewport as i just calculate horizontal. Is there a way to project a real sphere onto the screen somehow? Some method that looks like:

float getSizeOfSphereProjectedOnScreen(vec3 midpoint, float radius)

The other question is simpler: In with coordinate space is the z coordinate corresponding to the distance to the camera?

To sum it up i want to calculate:

  1. Is the Box in the view frustum?
  2. What is the size of the Box on the screen?
  3. What is the distance from Box to camera?

To simplify calculations i'd like to use a sphere representation for this but i don't know how to project a sphere.


回答1:


[Updated]

What is the distance from Box to camera?

In [which] coordinate space is the z coordinate corresponding to the distance to the camera?

The answer is none of the usual spaces. The closest one would be in view space (i.e. after you apply the view matrix but not the projection matrix). In view space, the distance to the camera should be sqrt(x*x + y*y + z*z), because the camera is at the origin. (z would be a reasonable approximation only if |x| and |y| were really small relative to |z|.) This is assuming that knowing the distance from the camera to the center of the box is good enough.

I think if you really wanted a space in which the z coordinate corresponds to the distance to the camera, you'd need to map a spherical locus of points sqrt(x*x + y*y + z*z) = d to a plane z = d. I don't know that you can do that with a matrix.

  • Is the Box in the view frustum?
  • What is the size of the Box on the screen?

I think you're on the right track with this, but depending on which direction the camera is facing, your left and right points might not determine how wide the box looks or whether the box intersects the view frustum. See my answer to your other question for a long way to do this.



来源:https://stackoverflow.com/questions/3710393/screen-projection-and-culling-united

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!