在Java3d中旋转观察平台

发布于 2024-09-14 19:25:59 字数 2458 浏览 3 评论 0原文

以下代码将一个立方体放置在 (0, 0, 0) 处,另一个放置在 (0, .5, .5) 处,每个立方体的尺寸为 (.5, .5, .5)。我正在尝试将屏幕旋转到这样的视图 good 但我得到了这个视图 替代文本。另外,我意识到我把颜色搞反了。

不管怎样,这是我到目前为止的代码:

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Positioning {
    private Color3f lightBlue;
    private Color3f aquaGreen;
    private Color3f white;
    private Color3f teal;
    private BranchGroup group;
    private SimpleUniverse universe;

    public Positioning() {
        lightBlue = new Color3f(0.0f, 0.749019608f, 1.0f);
        aquaGreen = new Color3f(0.439215686f, 0.858823529f, 0.576470588f);
        white = new Color3f(1.0f, 1.0f, 1.0f);
        teal = new Color3f(0.196078431f, 0.6f, 0.8f);

        universe = new SimpleUniverse();
        group = new BranchGroup();

        addCube(0.5f, 0.5f, 0.5f, new Vector3f(0.0f, 0.0f, 0.0f), lightBlue);
        addCube(0.5f, 0.5f, 0.5f, new Vector3f(0.5f, 0.0f, 0.5f), aquaGreen);

        //add light
        DirectionalLight light1 = new DirectionalLight(white, new Vector3f(0.0f, 7.0f, -12.0f));
        light1.setInfluencingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
        group.addChild(light1);

        //look at the right spot
        Transform3D lookAt = new Transform3D();
        lookAt.lookAt(new Point3d(0.0, 0.0, 3.0), new Point3d(0.0, 0.0, 0.0), new Vector3d(1.0, 1.0, 0.0));
        lookAt.invert();
        universe.getViewingPlatform().getViewPlatformTransform().setTransform(lookAt);
        universe.addBranchGraph(group);
    }

    public void addCube(float x, float y, float z, Vector3f position, Color3f color) {
        TransformGroup tg = new TransformGroup();
        Transform3D trans = new Transform3D();

        Appearance app = new Appearance();
        Material mat = new Material();
        mat.setDiffuseColor(color);
        mat.setSpecularColor(color);
        app.setMaterial(mat);
        Box b = new Box(x, y, z, app);

        //move into position and add to the branch group
        trans.setTranslation(position);
        tg.setTransform(trans);
        tg.addChild(b);

        group.addChild(tg);
    }

    public static void main(String[] args) {
        new Positioning();
    }
}

所以现在画布是黑色的,我认为这可能是我在 LookAt 函数上的定位。我也不太确定向上向量的用途。关于如何解决这个问题有什么想法吗?

The following code puts a cube at (0, 0, 0) and another at (0, .5, .5) and each cube is (.5, .5, .5) in dimension. I'm trying to rotate the view that the screen gets to one like this good but instead I get this view alt text. Also, I realize I got the colors backwards.

Anyway, this is my code so far:

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Positioning {
    private Color3f lightBlue;
    private Color3f aquaGreen;
    private Color3f white;
    private Color3f teal;
    private BranchGroup group;
    private SimpleUniverse universe;

    public Positioning() {
        lightBlue = new Color3f(0.0f, 0.749019608f, 1.0f);
        aquaGreen = new Color3f(0.439215686f, 0.858823529f, 0.576470588f);
        white = new Color3f(1.0f, 1.0f, 1.0f);
        teal = new Color3f(0.196078431f, 0.6f, 0.8f);

        universe = new SimpleUniverse();
        group = new BranchGroup();

        addCube(0.5f, 0.5f, 0.5f, new Vector3f(0.0f, 0.0f, 0.0f), lightBlue);
        addCube(0.5f, 0.5f, 0.5f, new Vector3f(0.5f, 0.0f, 0.5f), aquaGreen);

        //add light
        DirectionalLight light1 = new DirectionalLight(white, new Vector3f(0.0f, 7.0f, -12.0f));
        light1.setInfluencingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
        group.addChild(light1);

        //look at the right spot
        Transform3D lookAt = new Transform3D();
        lookAt.lookAt(new Point3d(0.0, 0.0, 3.0), new Point3d(0.0, 0.0, 0.0), new Vector3d(1.0, 1.0, 0.0));
        lookAt.invert();
        universe.getViewingPlatform().getViewPlatformTransform().setTransform(lookAt);
        universe.addBranchGraph(group);
    }

    public void addCube(float x, float y, float z, Vector3f position, Color3f color) {
        TransformGroup tg = new TransformGroup();
        Transform3D trans = new Transform3D();

        Appearance app = new Appearance();
        Material mat = new Material();
        mat.setDiffuseColor(color);
        mat.setSpecularColor(color);
        app.setMaterial(mat);
        Box b = new Box(x, y, z, app);

        //move into position and add to the branch group
        trans.setTranslation(position);
        tg.setTransform(trans);
        tg.addChild(b);

        group.addChild(tg);
    }

    public static void main(String[] args) {
        new Positioning();
    }
}

So right now the canvas is black and I think it might be my positioning on the lookAt function. I'm also not exactly for sure what the up vector is for. Any ideas on how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-09-21 19:25:59

我不熟悉 Java,但通常您可以使用某种“lookAt”函数来设置视图矩阵。快速查看文档后发现 这个

顺便说一句,setNominalViewingTransform 仅沿 z 轴平移,因此您显示的屏幕截图正是我期望看到的。您尝试自己设置视图变换是正确的。

编辑

lookAt函数的向上方向而言,这只是控制相机滚动的一种方式。换句话说,通过指定视点和中心点(相机所注视的点),您仅定义了一个方向。相机仍然可以绕该轴旋转 360 度。如果你想以正确的方式向上看,那么只需在你的系统中指定一个“向上”方向的向量 - 如果是 y 那么它就是 (0,1,0)。如果你想颠倒过来看东西,那就是(0,-1,0)。

请注意,您不必确保“向上”矢量垂直于眼睛和中心定义的方向。只要它不平行于该方向,那么该方法应该自动使用您提供的垂直于该方向的“向上”向量的分量。

编辑2:

根据您的编辑,您似乎已经为“向上”矢量放置了一个位置,而不是一个方向。它应该只是向量 (0,1,0)。话虽如此,它仍然无法解决您的问题,因为不正确的“向上”矢量仅意味着相机最终可能会在自己的轴上滚动,从而使其上下颠倒、侧面倾斜等,但它不会。 t影响方向。

因此,眼睛和中心位置一定有问题,或者矩阵仍然没有倒置。您对 invert() 的编辑看起来是错误的,因为您没有在 lookAt Transform3D 上调用它,但我认为这只是一个拼写错误。你什么时候更新问题的?

我会尝试从更简单的视角开始。尝试沿着 z 轴查看类似这样的内容

    Transform3D lookAt = new Transform3D();
    lookAt.lookAt( new  Point3d( 0.0, 0.0, 1.0 )
                 , new  Point3d( 0.0, 0.0, 0.0 )
                 , new Vector3d( 0.0, 1.0, 0.0) );
    lookAt.invert();

会发生什么?

如果仍然变黑,请尝试更改眼点沿 z 轴的位置,也许尝试 2.0、3.0,然后也尝试一些负值。

另一种可能性是,您的相机正在朝正确的方向寻找,但由于视锥体剪切,对象被剔除。您可能需要查看文档以了解默认剪裁平面的设置。当您沿 z 轴更改眼睛位置时,这可能会出现在上面的示例中。如果您可以看到对象的某些 z 值,但看不到其他值,则可能是剪切问题。

让我知道你过得怎么样。

I'm not familiar with Java but usually you can set up a view matrix using some sort of "lookAt" function. A quick look at the docs came up with this.

BTW, setNominalViewingTransform only translates along the z-axis so the screenshot you showed is what I would expect to see. You are correct to try to set the view transform yourself.

Edit

As far as the up direction for the lookAt function is concerned this is just a way of controlling the roll of the camera. In other words by specifying the eye point and the centre point (the point the camera is looking at) you have only defined a direction. The camera could still rotate 360 degrees on that axis. If you want to look at things the right way up then just specify a vector in the direction of "up" in your system - if that is y then it would be (0,1,0). If you wanted to look at things upside down then it would be (0,-1,0).

Note that you don't have to make sure that the "up" vector is perpendicular to the direction defined by eye and centre. As long as it's not parallel to that direction then the method should automatically just use the component of your supplied "up" vector that is perpendicular to the direction.

Edit2:

Based on your edits it looks as if you have put in a position for the "up" vector rather than a direction. It should just be the vector (0,1,0). Having said that it still won't fix your problem as an incorrect "up" vector just means that the camera might end up rolled around on its own axis so that it's upside-down, on its side, etc., but it won't affect the direction.

Therefore there must be something wrong with the eye and centre positions or the matrix is still not inverted. Your edit for the invert() looks wrong as you haven't called it on your lookAt Transform3D but I assume that's just a typo. when you updated the question?

I would try a simpler viewing angle to start with. Just try to look along the z-axis with something like

    Transform3D lookAt = new Transform3D();
    lookAt.lookAt( new  Point3d( 0.0, 0.0, 1.0 )
                 , new  Point3d( 0.0, 0.0, 0.0 )
                 , new Vector3d( 0.0, 1.0, 0.0) );
    lookAt.invert();

What happens with that?

If you still get black then try changing the eye point's position along the z-axis, perhaps try 2.0, 3.0 and then a few negative values too.

The other possibility is that your camera is looking in the right direction but the objects are being culled due to frustum clipping. You might want to check the documentation to see what the default clip planes are set to. This might show up in the example above when you change the eye position along the z-axis. If you can see your objects for some values of z but not others then it's probably a clipping problem.

Let me know how you get on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文