Java3d照明问题

时间:2011-01-11 12:16:24

标签: java 3d java-3d lighting

已编码20多年。我是Java3d的新手,但我对它印象非常深刻 - 保留模式比直接模式更容易!

无论如何,我遇到了照明问题。当我创建自己的几何体时,我无法让光照工作;当我使用java3d utils创建的几何体(例如Sphere())时,照明工作正常。我看到的问题是我的物体从各个角度都是白色的。我尝试添加环境光和方向灯,我的物体总是白色。

文档说我不应该在我的对象上提供颜色外观属性,如果我想使用光照而我不这样做。它还说我应该提供法线,而我正在这样做。我已尝试手动创建法线并使用NormalGenerator。我已经尝试过Java3d 1.5.1和预发布1.6.0但没有成功。我在Windows 7 64位上使用Java 1.6.0_18。

这段代码并不多。问题必须是非常基本的问题,但我看不到它。我在这里粘贴了两个几何创建函数,其中光照不起作用。请看一看,让我知道我做错了什么:

protected BranchGroup createTriangle() {
  Point3f[] vertices = { new Point3f(-1, 0, 0), new Point3f(1, 0, 0),
    new Point3f(0, 1, 0), };
  int indices[] = { 0, 1, 2, };

  GeometryInfo geometryInfo = new GeometryInfo(
    GeometryInfo.TRIANGLE_ARRAY);
  geometryInfo.setCoordinates(vertices);
  geometryInfo.setCoordinateIndices(indices);

 // NormalGenerator normalGenerator = new NormalGenerator();
 // normalGenerator.generateNormals(geometryInfo);

   Vector3f[] normals = { new Vector3f(0.0f, 0.0f, 1.0f), };
   int normalIndices[] = { 0, 0, 0, };
   geometryInfo.setNormals(normals);
   geometryInfo.setNormalIndices(normalIndices);

  Shape3D shape = new Shape3D(geometryInfo.getIndexedGeometryArray());

  BranchGroup group = new BranchGroup();
  group.addChild(shape);

  return group;
 }

 protected BranchGroup createBox() {

  Point3d[] vertices = { new Point3d(-1, 1, -1), new Point3d(1, 1, -1),
    new Point3d(1, 1, 1), new Point3d(-1, 1, 1),
    new Point3d(-1, -1, -1), new Point3d(1, -1, -1),
    new Point3d(1, -1, 1), new Point3d(-1, -1, 1), };

  int[] indices = { 0, 1, 5, 0, 5, 4, 1, 2, 6, 1, 6, 5, 2, 3, 7, 2, 7, 6,
    3, 0, 4, 3, 4, 7, 0, 3, 2, 0, 2, 1, 4, 5, 3, 2, 3, 5, };

  GeometryInfo geometryInfo = new GeometryInfo(
    GeometryInfo.TRIANGLE_ARRAY);
  geometryInfo.setCoordinates(vertices);
  geometryInfo.setCoordinateIndices(indices);

  NormalGenerator normalGenerator = new NormalGenerator();
  normalGenerator.generateNormals(geometryInfo);

  // Vector3f[] normals = { new Vector3f(0, 0, -1), new Vector3f(1, 0, 0),
  // new Vector3f(0, 0, 1), new Vector3f(-1, 0, 0),
  // new Vector3f(0, 1, 0), new Vector3f(0, -1, 0), };
  // int[] normalIndices = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  // 2,
  // 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, };
  // geometryInfo.setNormals(normals);
  // geometryInfo.setNormalIndices(normalIndices);

  Shape3D shape = new Shape3D(geometryInfo.getIndexedGeometryArray());

  BranchGroup group = new BranchGroup();
  group.addChild(shape);

  return group;
 }

1 个答案:

答案 0 :(得分:4)

让阴影照明在Java 3D中工作至少需要法线和材质节点组件。您忘记为Shape3D实例设置外观。所以,默认是采取的。这导致了没有阴影的白色几何体。

将以下外观添加到Shap3Ds的构造函数中,它们将/应呈现为红色:

Appearance appearance = new Appearance();
Material material = new Material(); 
material.setDiffuseColor(1.0f, 0.0f, 0.0f);   // red
material.setSpecularColor(0.2f, 0.2f, 0.2f);  // reduce default values
appearance.setMaterial(material);

推荐的稳定版Java 3D是1.5.2,可以在这里下载:https://java3d.dev.java.net/binary-builds.html

八月