0

TL;DR:在屏幕外绘制Canvas3D图像,然后在屏幕上绘制图像。View例如应该不断增加它到立方体的距离;而是仅在调整窗口大小时才这样做。

在以下示例代码中,当我更改相机变换时,Canvas3D 对象不会更新场景。

在我调整窗口大小之前它会保持原样,此时它会更新一次,并且在我再次调整大小之前不会再次这样。所以它只在调整大小事件时更新。

解决此问题的任何帮助将不胜感激!

编辑:最小示例:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Transform3D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.vecmath.Vector3d;

import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class eg extends JComponent implements ActionListener {
    SimpleUniverse m_universe = new SimpleUniverse(new Canvas3D(SimpleUniverse.getPreferredConfiguration(), true));
    JFrame frame = new JFrame();

    @Override
    public void actionPerformed(ActionEvent arg0) {
        repaint();
    }

    eg() {
        // setup Screen3D
        m_universe.getCanvas().getScreen3D().setSize(600, 600);
        m_universe.getCanvas().getScreen3D().setPhysicalScreenHeight(0.0254/90.0 * 600);
        m_universe.getCanvas().getScreen3D().setPhysicalScreenWidth(0.0254/90.0 * 600);

        // setup scene
        BranchGroup group = new BranchGroup();
        group.addChild(new ColorCube(0.5f));        
        m_universe.addBranchGraph(group);
        m_universe.getViewingPlatform().setNominalViewingTransform();

        // setup JFrame
        frame.add(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setVisible(true);

        // repaint() every half second
        Timer timer = new Timer(500, this);
        timer.start();
    }

    BufferedImage render(int width, int height) {
        BufferedImage returnValue = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        ImageComponent2D buffer = new ImageComponent2D(ImageComponent.FORMAT_RGBA, returnValue);
        buffer.setCapability(ImageComponent.ALLOW_IMAGE_WRITE);

        m_universe.getCanvas().setOffScreenBuffer(buffer);
        m_universe.getCanvas().renderOffScreenBuffer();
        m_universe.getCanvas().waitForOffScreenRendering();
        returnValue = m_universe.getCanvas().getOffScreenBuffer().getImage();

        return returnValue;
    }

    private Transform3D m_cameraTransform = new Transform3D();
    private Vector3d m_cameraPosition = new Vector3d();

    @Override
    public void paintComponent(Graphics target) {
        // Move camera back along z.
        m_universe.getViewingPlatform().getViewPlatformTransform().getTransform(m_cameraTransform);
        m_cameraTransform.get(m_cameraPosition);
        m_cameraPosition.z += 0.05;
        m_cameraTransform.set(m_cameraPosition);
        m_universe.getViewingPlatform().getViewPlatformTransform().setTransform(m_cameraTransform);

        System.out.println(m_cameraPosition.z);

        // Draw canvas.
        Graphics2D buffer = (Graphics2D) target;
        buffer.drawImage(render(getWidth(), getHeight()), null, 0, 0);
    }

    public static void main(String[] args) {
        eg test = new eg();
    }
}
4

0 回答 0