Monday, 18 November 2013

Lighting QuadArrays

QuadArrays as created in previous tutorials, are not effected by lighting effects. To make the QuadArrays effected by light, the normals need to be specified.

To start, lets use the Material and lighting from the previous tutorial.
 Appearance app = new Appearance();
 Material mat = new Material();
 mat.setAmbientColor(new Color3f(0.0f,0.0f,1.0f));
 mat.setDiffuseColor(new Color3f(0.7f,0.7f,0.7f));
 mat.setSpecularColor(new Color3f(0.7f,0.7f,0.7f));
 app.setMaterial(mat);     

 BoundingSphere bounds = new BoundingSphere (new Point3d (0, 0.0, 5), 5.0);
     Color3f lightColor = new Color3f (1.0f, 1.0f, 1.0f);
     Vector3f light1Direction = new Vector3f (0.0f, 0.0f, -1f);
     DirectionalLight light1  = new DirectionalLight (lightColor, light1Direction);
     light1.setInfluencingBounds (bounds);
     objRoot.addChild (light1);

     AmbientLight ambientLightNode = new AmbientLight (lightColor);
     ambientLightNode.setInfluencingBounds (bounds);
     objRoot.addChild (ambientLightNode);


Next we will create a QuadArray with a new parameter QuadArray.NORMALS which will allow us to specify the Normal at each vertex.
 QuadArray polygon1 = new QuadArray (4, QuadArray.COORDINATES | QuadArray.NORMALS);
 polygon1.setCoordinate(0, new Point3f (-3f,-1f,0f));
 polygon1.setCoordinate(1, new Point3f (3f,-1f,0f));
 polygon1.setCoordinate(2, new Point3f (3f,1f,0f));
 polygon1.setCoordinate(3, new Point3f (-3f,1f,0f));
 objRoot.addChild(new Shape3D(polygon1,app);

In this example, each vertex on the polygon has the same normal, so we'll create a Vector3f for the normal and set it in the QuadArray
 Vector3f polygon1Normal = new Vector3f(0f,0f,1f);
 polygon1.setNormal(0,polygonNormal);
 polygon1.setNormal(1,polygonNormal);
 polygon1.setNormal(2,polygonNormal);
 polygon1.setNormal(3,polygonNormal);

The 2 Screenshots show the polygon with the Material, and one without material (but with the ColoringAttributes used)
With MaterialWithout Material
Post a Comment