简体   繁体   English

投射球的半径

[英]Radius of projected Sphere

i want to refine a previous question : 我想改进以前的问题

How do i project a sphere onto the screen? 如何将球体投影到屏幕上?

(2) gives a simple solution: (2)给出一个简单的解决方案:

approximate radius on screen[CLIP SPACE] = world radius * cot(fov / 2) / Z

with:
fov = field of view angle
Z   = z distance from camera to sphere

result is in clipspace, multiply by viewport size to get size in pixels

Now my problem is that i don't have the FOV. 现在我的问题是我没有FOV。 Only the view and projection matrices are known. 只有视图和投影矩阵是已知的。 (And the viewport size if that does help) (以及视口大小,如果确实有帮助)

Anyone knows how to extract the FOV from the projection matrix? 谁知道如何从投影矩阵中提取FOV?

Update: 更新:

This approximation works better in my case: 在我的情况下,这种近似效果更好:

float radius = glm::atan(radius/distance);
radius *= glm::max(viewPort.width, viewPort.height) / glm::radians(fov);

I'm a bit late to this party. 我参加这个派对有点晚了。 But I came across this thread when I was looking into the same problem. 但是当我调查同样的问题时,我遇到了这个问题。 I spent a day looking into this and worked though some excellent articles I found here: http://www.antongerdelan.net/opengl/virtualcamera.html 我花了一天的时间研究这个问题,然后在这里找到了一些很棒的文章: http//www.antongerdelan.net/opengl/virtualcamera.html

I ended up starting with the projection matrix and working backwards. 我最终开始使用投影矩阵并向后工作。 I got the same formula you mention in your post above. 我在上面的帖子中提到了你提到的相同的公式。 ( where cot(x) = 1/tan(x) ) (其中cot(x)= 1 / tan(x))

radius_pixels = (radius_worldspace / {tan(fovy/2) * D}) * (screen_height_pixels / 2)

(where D is the distance from camera to the target's bounding sphere) (其中D是从相机到目标边界球的距离)

I'm using this approach to determine the radius of an imaginary trackball that I use to rotate my object. 我正在使用这种方法来确定我用来旋转对象的假想轨迹球的半径。

Btw Florian, you can extract the fovy from the Projection matrix as follows: 顺便说一下Florian,你可以从Projection矩阵中提取出如下的fovy:

If you take the Sy component from the Projection matrix as shown here: 如果从Projection矩阵中获取Sy分量,如下所示:

Sx  0   0   0
0   Sy  0   0
0   0   Sz  Pz
0   0  -1   0

where Sy = near / range

and where range = tan(fovy/2) x near

(you can find these definitions at the page I linked above) (你可以在我上面链接的页面找到这些定义)

if you substitute range in the Sy eqn above you get: 如果您在上面的Sy eqn中替换范围,您会得到:

Sy = 1 / tan(fovy/2) = cot(fovy/2)

rearranging: 清理:

tan(fovy/2) = 1 / Sy

taking arctan (the inverse of tan) of both sides we get: 我们得到了两面的arctan(tan的反面):

fovy/2 = arctan(1/Sy)

so, 所以,

fovy = 2 x arctan(1/Sy)

Not sure if you still care - its been a while! 不确定你是否还在乎 - 它已经有一段时间了! - but maybe this will help someone else. - 但也许这会帮助别人。

Update: see below. 更新:见下文。

Since you have the view and projection matrices, here's one way to do it, though it's probably not the shortest: 由于你有视图和投影矩阵,这里有一种方法,尽管它可能不是最短的:

  • transform the sphere's center into view space using the view matrix: call the result point C 使用视图矩阵将球体的中心转换为视图空间:调用结果点C
  • transform a point on the surface of the sphere, eg C+(r, 0, 0) in world coordinates where r is the sphere's world radius, into view space; 将球体表面上的一个点,例如世界坐标中的C +(r,0,0),其中r是球体的世界半径,转换为视图空间; call the result point S 调用结果点S.
  • compute rv = distance from C to S (in view space) compute rv =从C到S的距离(在视图空间中)
  • let point S1 in view coordinates be C + (rv, 0, 0) - ie another point on the surface of the sphere in view space, for which the line C -> S1 is perpendicular to the "look" vector 让视点坐标中的点S1为C +(rv,0,0) - 即视图空间中球体表面上的另一个点,其中线C - > S1垂直于“外观”矢量
  • project C and S1 into screen coords using the projection matrix as Cs and S1s 使用投影矩阵将C和S1投影到屏幕坐标中作为Cs和S1s
  • compute screen radius = distance between Cs and S1s 计算屏幕半径= Cs和S1s之间的距离

But yeah, like Brandorf said, if you can preserve the camera variables, like FOVy, it would be a lot easier. 但是,就像Brandorf说的那样,如果你能保存相机变量,比如FOVy,那就容易多了。 :-) :-)

Update: Here's a more efficient variant on the above: make an inverse of the projection matrix. 更新:以上是上述更有效的变体:制作投影矩阵的逆矩阵。 Use it to transform the viewport edges back into view space. 使用它将视口边缘转换回视图空间。 Then you won't have to project every box into screen coordinates. 然后,您不必将每个框都投影到屏幕坐标中。

Even better, do the same with the view matrix and transform the camera frustum back into world space. 更好的是,对视图矩阵执行相同操作,并将相机视锥反射回到世界空间。 That would be more efficient for comparing many boxes against; 这对于比较许多盒子来说会更有效率; but harder to figure out the math. 但更难弄清楚数学。

The answer posted at your link radiusClipSpace = radius * cot(fov / 2) / Z , where fov is the angle of the field of view, and Z is the z-distance to the sphere, definitely works. 答案发布在您的链接radiusClipSpace = radius * cot(fov / 2) / Z ,其中fov是视野的角度,Z是到球体的z距离,绝对有效。 However, keep in mind that radiusClipSpace must be multiplied by the viewport's width to get a pixel measure. 但是,请记住, radiusClipSpace必须乘以视口的宽度才能获得像素度量。 The value measured in radiusClipSpace will be a value between 0 and 1 if the object fits on the screen. 如果对象适合屏幕,则radiusClipSpace中测量的值将介于0和1之间。

An alternative solution may be to use the solid angle of the sphere. 替代解决方案可以是使用球体的立体角 The solid angle subtended by a sphere in a sky is basically the area it covers when projected to the unit sphere. 天空中球体所对的立体角基本上是投射到单位球体时所覆盖的区域

在此输入图像描述

The formulae are given at this link but roughly what I'm doing is: 公式在这个链接给出,但大致我正在做的是:

if( (!radius && !distance) || fabsf(radius) > fabsf(distance) )
  ; // NAN conditions. do something special.

theta=arcsin( radius/distance )
sphereSolidAngle = ( 1 - cosf( theta ) ) ; // not multiplying by 2PI since below ratio used only
frustumSolidAngle = ( 1 - cosf( fovy / 2 ) ) / M_PI ; // I cheated here. I assumed
// the solid angle of a frustum is (conical), then divided by PI
// to turn it into a square (area unit square=area unit circle/PI)

numPxCovered = 768.f*768.f * sphereSolidAngle / frustumSolidAngle ; // 768x768 screen
radiusEstimate = sqrtf( numPxCovered/M_PI ) ; // area=pi*r*r

This works out to roughly the same numbers as radius * cot(fov / 2) / Z . 这与radius * cot(fov / 2) / Z数字大致相同。 If you only want an estimate of the area covered by the sphere's projection in px, this may be an easy way to go. 如果你只是想在PX球体投影覆盖的区域的估计,这可能是一个简单的方法去。

I'm not sure if a better estimate of the solid angle of the frustum could be found easily. 我不确定是否可以很容易地找到更好的估算平截头体的立体角。 This method involves more comps than radius * cot(fov / 2) / Z . 该方法涉及比radius * cot(fov / 2) / Z更多的comps。

The FOV is not directly stored in the projection matrix, but rather used when you call gluPerspective to build the resulting matrix. FOV不直接存储在投影矩阵中,而是在调用gluPerspective来构建结果矩阵时使用。

The best approach would be to simply keep all of your camera variables in their own class, such as a frustum class, whose member variables are used when you call gluPerspective or similar. 最好的方法是简单地将所有相机变量保存在自己的类中,例如视锥体类,当调用gluPerspective或类似时,会使用其成员变量。

It may be possible to get the FOVy back out of the matrix, but the math required eludes me. 有可能将FOVy从矩阵中取回,但所需的数学计算使我无法理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 屏幕空间中投影球体的半径 - Radius of projected sphere in screen space 从 Sphere Loc 和 Radius 获取 Sphere 表面上的随机 XYZ - Get Random XYZ on surface of Sphere from Sphere Loc and Radius 球体中心点和表面上 3 个点的半径 - Sphere center point and radius from 3 points on the surface 在给定半径、中心和 3D position 的球体上查找点 - Find point on sphere given radius, center and 3D position 为什么这段代码没有正确执行计算? 给定半径的球体的体积 - Why is this code not performing the calculation correctly? Volume of a sphere given a radius 在距点给定半径内的球面上平均生成点 - Evenly generate points on a sphere within a given radius from a point 如何从3点和半径找到球心? - How do I find the sphere center from 3 points and radius? 如何找到给定 dims+1 点的任意维球体的中心和半径 - How to find the center and radius of an any dimensional sphere giving dims+1 points 在特定半径的球体内生成一个点,该点围绕特定的给定点(而不是0,0,0)-Python - Generate a point within a sphere of a certain radius that's around a SPECIFIC given point (not at 0,0,0) - Python 如何减小球体中心点和点 P 之间的半径并找到点 P 的新坐标? - How to decrease the radius between a sphere centerpoint and a point P and find the new coordinates of point P?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM