开发者

View frustum culling a box in Java

开发者 https://www.devze.com 2023-03-12 21:14 出处:网络
Given a view frustum (defined by x,y,z camera position, rotation [0-360) and pitch [0-180), as well as a viewing angle (e.g. 45)) what is the (preferably fastest) Java code for determining if a box (d

Given a view frustum (defined by x,y,z camera position, rotation [0-360) and pitch [0-180), as well as a viewing angle (e.g. 45)) what is the (preferably fastest) Java code for determining if a box (defined by two opposing corner points) is partly or entirely within that frustum?

More precisely, how can I complete the following?

static boolean isBoxInFrustum(float cx, float cy, float cz, // Vector camera,
        float rotation, float pitch, float angle, 
        float p1x, float p1y, float p1z,    // Vector point1,
        float p2x, float p2y, float p2z) {  // Vector point2
    //...
}

A C++ implementation can be found at http://www.lighthouse3d.com/tutorials/view-frustum-culling/

edit: Here's the 2d version, which is only 4 lines and seems to me easy enough to understand at a glance - how can it be modif开发者_JS百科ied to be a 3d check?

static boolean isPointInFrustum(
        Vector cam, float rot, float pitch, float ang, Vector point) {
    Vector diff = cam.minus(point);
    float deg = Maths.arctan(diff.y, diff.x) + rot + 360;
    deg %= 360;
    return (deg > 180-ang && deg < 180+ang);
}


Besides basic syntax, how is the Java code any different from the C++ code? I notice that the C++ code on that site is a lot more object oriented than your interface above. Can't you build a FrustumR class like in the Lighthouse code, and a Vec3 class, and then add a method:

int FrustumR.pointInFrustum(Vec3 p)

Since the body of that method doesn't deal with pointers (only arithmetic), you should be able to basically paste it and it will work in Java.

If you can't implement those classes, for whatever reason, you should be able to translate the C++ code by changing references to members of those classes into references to your arguments.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号