James Williams
LinkedInMastodonGithub

Java3DBuilder - Textures and Lighting

Tags: General

I was hacking a little bit on Java3DBuilder when I was trying to kill time in VA the day before Groovy/Grails Experience. Textures and lighting are the fruit of that labor. I'm still figuring out some of the details of having lighting affect textures(TextureAttributes class) but basic texturing works and provided there are normals present, lighting works.

For a cube with texturing normals, the following code is how to add a texture to that cube:

appearance {
    texture2D(image: new File("images/test.png"), magFilter: Texture.BASE_LEVEL_LINEAR,
 minFilter: Texture.BASE_LEVEL_LINEAR)
}

That call replaces the following code:

def file = map.remove("image")
def loader = new TextureLoader(file.toURL(), TextureLoader.ALLOW_NON_POWER_OF_TWO, null)
def image = loader.getImage()
texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image.getWidth(), image.getHeight())
texture.setImage(0, image)
...
//call all the setters

In the lighting example, we draw a sphere and a cone, each with a directional light to the side of it. There is a bit of convergence of the the light sources on the surface of the sphere.

Here's the important code that's at work.

transformGroup(translation: new Vector3f(3.0f, 0.0f, -2.0f), capability: TransformGroup.ALLOW_TRANSFORM_WRITE) {
    sphere() {
      appearance {
          material()
      }
    }
    directionalLight(influencingBounds: new BoundingSphere(new Point3d(-5.0d,0.0d,0.0d), 4), color:new Color3f(1.0f, 1.0f, 0.0f))
   }
   transformGroup(translation: new Vector3f(0.0f, 0.0f, -2.0f), capability: TransformGroup.ALLOW_TRANSFORM_WRITE) {
      cone() {
          appearance {
              material()
          }
      }
      directionalLight(influencingBounds: new BoundingSphere(new Point3d(5.0d,0.0d,0.0d), 4), color:new Color3f(0.0f, 1.0f, 0.0f))
}