Java3d光照问题
从事编码工作已有 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;
}
Been coding for 20+ years. I'm new to Java3d but I am really impressed by it - retained mode is so much easier than direct mode!
Anyway I'm having a problem with lighting. When I create my own geometry I cannot get lighting to work; when I use geometry created by java3d utils (such as Sphere()) the lighting works fine. The issue I'm seeing is that my object is white from all angles. I've tried adding ambient and directional lights and my objects are always white.
The docs say I'm not supposed to supply color appearance attributes on my objects if I want to use lighting and I'm not doing that. It also says I should supply normals and I'm doing that. I've tried both creating normals by hand and using NormalGenerator. I've tried Java3d 1.5.1 and the pre release 1.6.0 without success. I'm using Java 1.6.0_18 on Windows 7 64 bit.
There's not much to this code. The problem must be something really basic but I can't see it. I've pasted 2 of my geometry creation functions here where the lighting doesn't work. Please take a look and let me know what I'm doing wrong:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让着色光照在 Java 3D 中工作至少需要法线和材质节点组件。您忘记为 Shape3D 实例设置外观。因此,采用默认值。这会产生一个没有阴影的白色几何体。
将以下外观添加到 Shap3D 的构造函数中,它们将/应该呈现为红色:
推荐的 Java 3D 稳定版本是 1.5.2,可以在此处下载:https://java3d.dev.java.net/binary-builds.html
八月
getting shaded lighting to work in Java 3D needs at least normals and a Material node component. You forgot to set an Appearance for your Shape3D instances. So, the default is taken. That results in a not shaded white geometry.
Add following Appearance to your Shap3Ds' constructor and they will/should be rendered in red color:
The recommended stable version of Java 3D is 1.5.2 and it can be downloaded here: https://java3d.dev.java.net/binary-builds.html
August