In 3D Rendering, lighting is essential to give a better visual experience. Indeed, since the rendering is projected on a 2D plane, it is not always possible to sense the 3D relief. Lighting gives a better sense of the 3D space.
In JCarnac3D-OGL, the lighting is done by modifying the context in which the OpenGL shaders program are created. Indeed, the JCarnac-OGL provides utility methods to perform lighting in shaders. These function can vary from doing nothing if there is no light to performing complex lighting computing.
Same geometry renderer with and without lighting (directional light pointing to the screen).
JCarnac3D-OGL provides a default lighting implementation which supports a single light which type can be Directional, Spot, or Point. It also provides a composite node which automatically activates the lighting for all its children : cgJOGLSingleLightCompositeNode
Sample code
// Creating the light composite node. cgJOGLSingleLightCompositeNode<cgNode> lightNode = new cgJOGLSingleLightCompositeNode<cgNode>(); // Initiating light's components// The direction vector Vector3d direction = new Vector3d(0., 0., -1.); // Boolean which tells if it's in a view space boolean isInViewSpace = true; // The ambient light's color Color ambient = Color.WHITE; // The diffuse light's color Color diffuse = Color.WHITE; // The specular light's color Color specular = Color.WHITE; // Creating the light cgDirectionalLight light = new cgDirectionalLight(direction, isInViewSpace, ambient, diffuse, specular); // Add the light to the light composite node lightNode.setLight(light); // Set the light lighting enable lightNode.setLightEnabled(true); // Add the surface in the light composite node lightNode.addNode(surface); //assumes that surface is a valid shape node.// Add the light composite node in the scenegraph parent.addNode(lightNode);
By default, the material is set on the node which activates the lighting (cgJOGLSingleLightCompositeNode), but children can override the current material to modify the way they interact with the light. This behavior was done to be able to globally set the material since the object usually handled by JCarnac3D-OGL has no visual material that can be set to match their real state.
Note that the lighting algorithm uses the product of the light source colors and the material colors.
Same object renderer with different materials (shininess parameter is respectively 100, 5 and 0, Colors parameters are unchanged)
Sample code
//create a new material cgDefaultMaterial material = new cgDefaultMaterial( Color.DARK_GRAY, //ambiant color Color.LIGHT_GRAY, //diffuse color Color.LIGHT_GRAY, //specular color Color.black, //emissive color 50); //shininess (0 to 128).//set material on the light node. lightNode.setMaterial(material);