我有java3d的以下程序。我使用的是http://jogamp.org/wiki/index.php/Downloading_and_installing_Java3D
的最新版本当我渲染显示(justDisplay = true)时,它看起来像那样
Whan我渲染到文件(justDisplay = false)它看起来像
渲染到文件时,为什么侧面不是蓝色?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.LineArray;
import javax.media.j3d.Material;
import javax.media.j3d.PointLight;
import javax.media.j3d.Screen3D;
import javax.media.j3d.ShaderAppearance;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class HelloJava3Db {
public static void main(String[] args) {
boolean justDisplay = true;
// create canvas
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config, !justDisplay);
if (justDisplay) {
JFrame jFrame = new JFrame("huhu");
jFrame.setVisible(true);
jFrame.setSize(1000, 1000);
jFrame.add(canvas3D);
}
// create universe
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
// create scene
BranchGroup scene = new BranchGroup();
Transform3D drehungX = new Transform3D();
Transform3D drehungY = new Transform3D();
drehungX.rotX(Math.toRadians(30));
drehungY.rotY(Math.toRadians(20));
drehungX.mul(drehungY);
TransformGroup objDreh = new TransformGroup(drehungX);
Appearance a = new ShaderAppearance();
Material material = new Material(new Color3f(Color.BLUE), new Color3f(Color.RED), new Color3f(Color.YELLOW), new Color3f(Color.GREEN), 10f);
material.setLightingEnable(true);
material.setEmissiveColor(0, 0, 0);
a.setMaterial(material);
Box upperBox = new Box(0.1f, 0.1f, 0.1f, a);
objDreh.addChild(upperBox);
scene.addChild(objDreh);
Transform3D unten = new Transform3D();
unten.setTranslation(new Vector3f(0, -0.5f, 0));
TransformGroup untenGroup = new TransformGroup(unten);
untenGroup.addChild(new Box(0.5f, 0.01f, 0.5f, a));
scene.addChild(untenGroup);
PointLight pl = new PointLight(new Color3f(Color.WHITE), new Point3f(0, 0.5f, 0), new Point3f(0.0f, 0.0f, 0.2f));
pl.setInfluencingBounds(new BoundingSphere());
scene.addChild(pl);
Color3f light1Color = new Color3f(1.0f, 0.0f, 0.0f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100000.0);
Vector3f light1Direction = new Vector3f(0.0f, -1.0f, 0.0f);
DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
// scene.addChild(light1);
// AXIS
// X
LineArray lineX = new LineArray(2, LineArray.COORDINATES);
lineX.setCoordinate(0, new Point3f(-1000.0f, 0.0f, 0.0f));
lineX.setCoordinate(1, new Point3f(1000.0f, 0.0f, 0.0f));
Appearance appearanceGreen = new Appearance();
ColoringAttributes coloringAttributesGreen = new ColoringAttributes();
coloringAttributesGreen.setColor(new Color3f(Color.green));
appearanceGreen.setColoringAttributes(coloringAttributesGreen);
Shape3D shapeLine = new Shape3D(lineX, appearanceGreen);
scene.addChild(shapeLine);
// Y
LineArray lineY = new LineArray(2, LineArray.COORDINATES);
lineY.setCoordinate(0, new Point3f(0.0f, -1000.0f, 0.0f));
lineY.setCoordinate(1, new Point3f(0.0f, 1000.0f, 0.0f));
scene.addChild(new Shape3D(lineY));
// Z
LineArray lineZ = new LineArray(2, LineArray.COORDINATES);
lineZ.setCoordinate(0, new Point3f(0.0f, 0.0f, -1000.0f));
lineZ.setCoordinate(1, new Point3f(0.0f, 0.0f, 1000.0f));
scene.addChild(new Shape3D(lineZ));
scene.compile();
simpleU.addBranchGraph(scene);
if (!justDisplay) {
// set off screen size
Screen3D sOff = canvas3D.getScreen3D();
sOff.setSize(new Dimension(1000, 1000));
sOff.setPhysicalScreenWidth(3);
sOff.setPhysicalScreenHeight(3);
// render to off screen
BufferedImage bImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
ImageComponent2D buffer = new ImageComponent2D(ImageComponent.FORMAT_RGBA, bImage);
buffer.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
canvas3D.setOffScreenBuffer(buffer);
canvas3D.renderOffScreenBuffer();
canvas3D.waitForOffScreenRendering();
// write to file
File output = new File("img.png");
try {
ImageIO.write(canvas3D.getOffScreenBuffer().getRenderedImage(), "png", output);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(output.getAbsolutePath());
}
// System.exit(0);
}
}