从事编码工作 20 多年。我是 Java3d 的新手,但我对它印象深刻——保留模式比直接模式容易得多!
无论如何,我的照明有问题。当我创建自己的几何图形时,我无法让照明工作;当我使用 java3d utils 创建的几何图形(例如 Sphere())时,照明效果很好。我看到的问题是我的对象从各个角度都是白色的。我试过添加环境光和定向光,我的物体总是白色的。
文档说如果我想使用照明,我不应该在我的对象上提供颜色外观属性,而我没有这样做。它还说我应该提供法线,我正在这样做。我尝试过手动创建法线和使用 NormalGenerator。我已经尝试过 Java3d 1.5.1 和预发行版 1.6.0,但没有成功。我在 Windows 7 64 位上使用 Java 1.6.0_18。
这段代码没有太多内容。问题一定是非常基本的,但我看不到。我在此处粘贴了 2 个我的几何创建功能,其中照明不起作用。请看一下,让我知道我做错了什么:
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;
}