سؤال

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;
 }
هل كانت مفيدة؟

المحلول

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:

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);

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top