Java 3d:对象显示不正确

发布于 2024-12-29 19:44:22 字数 6272 浏览 2 评论 0原文

我正在尝试使用 GeometryInfo 创建模型。我只是制作一个平面模型并将其附加到 y 轴的反射上,以保持对称。但运行时,以下代码仅显示我编程的模型的一半(它仅显示 y 轴左侧的一侧)。我做错了什么?

该程序是从 Stage 类运行的。我将不胜感激任何建议。

public class Head extends BranchGroup
{
    public Head()
{
    Shape3D shape = getShape();
    setAppearance(shape, 1.0f, 0.0f, 0.0f);
    this.addChild(shape);
}

private Shape3D getShape()
{
    float[] data = shapeCoord();
    int[] stripCount = {3,3,3,3,3,3,3,3,3,3,3,3};
    GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
    gi.setCoordinates(data);
    gi.setStripCounts(stripCount);

    Triangulator tr = new Triangulator();
    tr.triangulate(gi);
        gi.recomputeIndices();

    NormalGenerator ng = new NormalGenerator();
    ng.generateNormals(gi);
        gi.recomputeIndices();

    Stripifier st = new Stripifier();
    st.stripify(gi);
        gi.recomputeIndices();

    Shape3D part = new Shape3D();
    part.setGeometry(gi.getGeometryArray());

    return part;
}

    public static void setAppearance(Shape3D shape, float x, float y, float z)
    {
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(x, y, z);
        Appearance appear = new Appearance();
        Material material = new Material();
        material.setDiffuseColor(x, y, z);
        appear.setMaterial(material);
        appear.setColoringAttributes(ca);
        shape.setAppearance(appear);
    }



private float[] shapeCoord()
{
    float[] data = new float[36*3];
    int i=0; 

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = 0.2f; data[i++] = 0.0f;
    data[i++] = 0.09f; data[i++] = 0.18f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.09f; data[i++] = 0.18f; data[i++] = 0.0f;
    data[i++] = 0.14f; data[i++] = 0.1f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.14f; data[i++] = 0.1f; data[i++] = 0.0f;
    data[i++] = 0.16f; data[i++] = 0.0f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.16f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.15f; data[i++] = -0.09f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.15f; data[i++] = -0.09f; data[i++] = 0.0f;
    data[i++] = 0.06f; data[i++] = -0.2f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.06f; data[i++] = -0.2f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = -0.2f; data[i++] = 0.0f;

    //////////////////////////////////////////////////////////

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = 0.2f; data[i++] = 0.0f;
    data[i++] = -0.09f; data[i++] = 0.18f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.09f; data[i++] = 0.18f; data[i++] = 0.0f;
    data[i++] = -0.14f; data[i++] = 0.1f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.14f; data[i++] = 0.1f; data[i++] = 0.0f;
    data[i++] = -0.16f; data[i++] = 0.0f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.16f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.15f; data[i++] = -0.09f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.15f; data[i++] = -0.09f; data[i++] = 0.0f;
    data[i++] = -0.06f; data[i++] = -0.2f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.06f; data[i++] = -0.2f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = -0.2f; data[i++] = 0.0f;


    return data;
}
}

public class Stage extends Applet
{
private BranchGroup scene;

public Stage(BranchGroup scene)
{
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas3D = new Canvas3D(config);
    add("Center", canvas3D);

    this.scene = scene;
    scene.compile();    

    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

    simpleU.getViewingPlatform().setNominalViewingTransform();
    simpleU.addBranchGraph(scene);
}

    public static void main(String[] args) 
{
    Frame frame = new MainFrame(new Stage(new RotationTest()), 500, 500);
} 
}

public class RotationTest extends BranchGroup
{   

    public static final int X = 0;
public static final int Y = 1;
public static final int Z = 2;
public static final int CLOCKWISE = -1;
public static final int COUNTER_CLOCKWISE = 1;
private static long speed = 4000;

public RotationTest()
{
    TransformGroup rotate = new TransformGroup();
    setRotation(rotate, Tools.Y, -Tools.CLOCKWISE, 360d, 4000, -1);
    }

    private static void setRotation(TransformGroup objSpin, int line, int direction, double angle, long rate, int repeat)
{
    speed = rate;
    if((direction != CLOCKWISE) && (direction != COUNTER_CLOCKWISE))
    {
    direction = -1;
    }
    objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Transform3D axis = new Transform3D();   // rotates around positive y-axis by default
        if(line == X)
    {
    axis.rotZ(Math.toRadians(direction * 90)); // rotates around positive x-axis
    }
    else if(line == Z)
    {
    axis.rotX( Math.toRadians(direction * 90) ); // rotates about the positive z-axis
    }
    else
    {
        axis.rotY( Math.toRadians(direction * 90) ); // rotates about the positive y-axis
    }
    Alpha rotationAlpha = new Alpha(repeat, speed);
    //rotationAlpha.setPhaseDelayDuration(6000);
    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin, axis, 0.0f, (float) Math.toRadians(angle));
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    objSpin.addChild(rotator);
}

    public static void setLighting(BranchGroup objRoot, float x, float y, float z)
{
    Color3f color = new Color3f(x, y, z);
    Vector3f direction = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight light = new DirectionalLight(color, direction);
    //AmbientLight light = new AmbientLight(color);
    light.setInfluencingBounds(new BoundingSphere());
    objRoot.addChild(light);
}
}

I am trying to create a model using GeometryInfo. I am simply making a flat model and attaching it to its reflection in the y-axis, for symmetry. But when ran, the following code only shows half of the model that I programmed (It only shows the side on the left of the y-axis). What am I doing wrong?

The program is ran from the Stage class. I would appreciate any suggestions.

public class Head extends BranchGroup
{
    public Head()
{
    Shape3D shape = getShape();
    setAppearance(shape, 1.0f, 0.0f, 0.0f);
    this.addChild(shape);
}

private Shape3D getShape()
{
    float[] data = shapeCoord();
    int[] stripCount = {3,3,3,3,3,3,3,3,3,3,3,3};
    GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
    gi.setCoordinates(data);
    gi.setStripCounts(stripCount);

    Triangulator tr = new Triangulator();
    tr.triangulate(gi);
        gi.recomputeIndices();

    NormalGenerator ng = new NormalGenerator();
    ng.generateNormals(gi);
        gi.recomputeIndices();

    Stripifier st = new Stripifier();
    st.stripify(gi);
        gi.recomputeIndices();

    Shape3D part = new Shape3D();
    part.setGeometry(gi.getGeometryArray());

    return part;
}

    public static void setAppearance(Shape3D shape, float x, float y, float z)
    {
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(x, y, z);
        Appearance appear = new Appearance();
        Material material = new Material();
        material.setDiffuseColor(x, y, z);
        appear.setMaterial(material);
        appear.setColoringAttributes(ca);
        shape.setAppearance(appear);
    }



private float[] shapeCoord()
{
    float[] data = new float[36*3];
    int i=0; 

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = 0.2f; data[i++] = 0.0f;
    data[i++] = 0.09f; data[i++] = 0.18f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.09f; data[i++] = 0.18f; data[i++] = 0.0f;
    data[i++] = 0.14f; data[i++] = 0.1f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.14f; data[i++] = 0.1f; data[i++] = 0.0f;
    data[i++] = 0.16f; data[i++] = 0.0f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.16f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.15f; data[i++] = -0.09f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.15f; data[i++] = -0.09f; data[i++] = 0.0f;
    data[i++] = 0.06f; data[i++] = -0.2f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.06f; data[i++] = -0.2f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = -0.2f; data[i++] = 0.0f;

    //////////////////////////////////////////////////////////

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = 0.2f; data[i++] = 0.0f;
    data[i++] = -0.09f; data[i++] = 0.18f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.09f; data[i++] = 0.18f; data[i++] = 0.0f;
    data[i++] = -0.14f; data[i++] = 0.1f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.14f; data[i++] = 0.1f; data[i++] = 0.0f;
    data[i++] = -0.16f; data[i++] = 0.0f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.16f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.15f; data[i++] = -0.09f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.15f; data[i++] = -0.09f; data[i++] = 0.0f;
    data[i++] = -0.06f; data[i++] = -0.2f; data[i++] = 0.0f;

    data[i++] = 0.0f; data[i++] = 0.0f; data[i++] = 0.0f;
    data[i++] = -0.06f; data[i++] = -0.2f; data[i++] = 0.0f;
    data[i++] = 0.0f; data[i++] = -0.2f; data[i++] = 0.0f;


    return data;
}
}

public class Stage extends Applet
{
private BranchGroup scene;

public Stage(BranchGroup scene)
{
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas3D = new Canvas3D(config);
    add("Center", canvas3D);

    this.scene = scene;
    scene.compile();    

    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

    simpleU.getViewingPlatform().setNominalViewingTransform();
    simpleU.addBranchGraph(scene);
}

    public static void main(String[] args) 
{
    Frame frame = new MainFrame(new Stage(new RotationTest()), 500, 500);
} 
}

public class RotationTest extends BranchGroup
{   

    public static final int X = 0;
public static final int Y = 1;
public static final int Z = 2;
public static final int CLOCKWISE = -1;
public static final int COUNTER_CLOCKWISE = 1;
private static long speed = 4000;

public RotationTest()
{
    TransformGroup rotate = new TransformGroup();
    setRotation(rotate, Tools.Y, -Tools.CLOCKWISE, 360d, 4000, -1);
    }

    private static void setRotation(TransformGroup objSpin, int line, int direction, double angle, long rate, int repeat)
{
    speed = rate;
    if((direction != CLOCKWISE) && (direction != COUNTER_CLOCKWISE))
    {
    direction = -1;
    }
    objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Transform3D axis = new Transform3D();   // rotates around positive y-axis by default
        if(line == X)
    {
    axis.rotZ(Math.toRadians(direction * 90)); // rotates around positive x-axis
    }
    else if(line == Z)
    {
    axis.rotX( Math.toRadians(direction * 90) ); // rotates about the positive z-axis
    }
    else
    {
        axis.rotY( Math.toRadians(direction * 90) ); // rotates about the positive y-axis
    }
    Alpha rotationAlpha = new Alpha(repeat, speed);
    //rotationAlpha.setPhaseDelayDuration(6000);
    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin, axis, 0.0f, (float) Math.toRadians(angle));
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    objSpin.addChild(rotator);
}

    public static void setLighting(BranchGroup objRoot, float x, float y, float z)
{
    Color3f color = new Color3f(x, y, z);
    Vector3f direction = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight light = new DirectionalLight(color, direction);
    //AmbientLight light = new AmbientLight(color);
    light.setInfluencingBounds(new BoundingSphere());
    objRoot.addChild(light);
}
}

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

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

发布评论

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

评论(1

我不吻晚风 2025-01-05 19:44:22

狂野刺击。定义三角形的方式意味着一个“面向”相机,另一个“面向”相机。

  *1

*2   *2'

  *3

根据右手定则,1,2,3 面向摄像机,1,2',3 背向摄像机。如果启用背面剔除,则会假设 1,2',3 位于相机的另一侧。如果你翻转相机位置/矢量并且基本上得到相同的东西,那么几乎肯定是这个。

Wild stab. The way you're defining your triangles means one "faces" towards the camera, and one away.

  *1

*2   *2'

  *3

Following the right hand rule, 1,2,3 would face toward the camera, 1,2',3 faces away. If back-face culling is enabled, it would assume that 1,2',3 is on the opposite side of the camera. If you flip the camera position/vector and you basically get the same thing, it's almost definitely this.

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