代码之家  ›  专栏  ›  技术社区  ›  fho

屏幕投影与剔除

  •  1
  • fho  · 技术社区  · 14 年前

    我目前正在处理几千个盒子,我想投影到屏幕上,以确定它们的大小和距离相机。

    我目前的方法是使用视图和投影矩阵以及视口值来获取表示长方体和项目的球体。

    // 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)
    

    在另一点上,我计算盒子到相机的距离。

    第一个问题是,当我只计算水平方向时,我不知道长方体是否就在视口下方。有没有办法把一个真正的球体投射到屏幕上?一些方法看起来像:

    float getSizeOfSphereProjectedOnScreen(vec3 midpoint, float radius)
    

    总而言之,我想计算一下:

    1. 盒子在视图中是平截头体吗?
    2. 屏幕上的盒子有多大?
    3. 从盒子到相机的距离是多少?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Community CDub    4 年前

    [更新]

    从盒子到相机的距离是多少?

    在 哪个坐标空间是z 离摄像机有多远?

    sqrt(x*x + y*y + z*z) ,因为相机在原点。(只有当| x |和| y |相对于| z |非常小时,z才是一个合理的近似值。)这是假设知道从相机到 盒子的底部足够好了。

    我想如果你真的想要一个空间,在这个空间中z坐标与相机的距离相对应,你需要绘制一个点的球形轨迹 sqrt(x*x + y*y + z*z) = d 到飞机上去 z = d . 我不知道你能用矩阵做到。

    • 盒子在视图中是平截头体吗?
    • 屏幕上的盒子有多大?

    我认为这是正确的,但取决于相机朝向哪个方向,你的左右点可能无法确定长方体看起来有多宽,或者长方体是否与视锥体相交。看看我的答案 your other question 在很长的一段时间里。

    推荐文章