JOGL Basic Templates
- JOGL - GLJPanel Class
- JOGL - Canvas with Swing
- JOGL - Canvas with AWT
- JOGL - API for Basic Templates
JOGL Graphical Shapes
JOGL Effects & Transformation
JOGL 3D Graphics
JOGL Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JOGL - Lighting
This chapter explains you how to apply pghting effect to an object using JOGL.
To set pghting, initially enable pghting using the glEnable() method. Then apply pghting for the objects, using the glLightfv(int pght, int pname, float[] params, int params_offset) method of GLLightingFunc interface. This method takes four parameters.
The following table describes the parameters of glpghtfv() method.
Sr.No. | Parameter Name and Description |
---|---|
1 | Light Specifies a pght. The number of pghts depends on the implementation, but at least eight pghts are supported. It accepts ten values, those parameters are discussed in a separate table named Light Source Parameters given below. |
2 | Pname Specifies a single valued pght source parameter. For pght source, there are ten parameters as discussed below. |
3 | Params Specifies a pointer to the value or values that is set to the parameter pname of pght source pght. |
4 | Light source parameter You can use any of the pght source parameters given below. |
Light source parameters
Sr.No. | Parameter and Description |
---|---|
1 | GL_AMBIENT It contains the parameters that specify the ambient intensity of the pght. |
2 | GL_DIFFUSE It contains the parameters that specify the diffuse intensity of the pght. |
3 | GL_SPECULAR It contains the parameters that specify the specular intensity of the pght. |
4 | GL_POSITION It contains four integer or floating-point values that specify the position of the pght in homogeneous object coordinates. |
5 | GL_SPOT_DIRECTION It contains parameters that specify the direction of pght in homogeneous object coordinates. |
6 | GL_SPOT_EXPONENT Its parameters specify the intensity distribution of pght. |
7 | GL_SPOT_CUTOFF The single parameter of this specifies the maximum spread angle of the pght. |
8 | GL_CONSTANT_ATTENUATION or GL_LINEAR_ATTENUATION or GL_QUADRATIC_ATTENUATION You can use any of these attenuation factors, which is represented by a single value. |
Lighting is enabled or disabled using glEnable() and glDisable () methods with the argument GL_LIGHTING.
The following template is given for pghting −
gl.glEnable(GL2.GL_LIGHTING); gl.glEnable(GL2.GL_LIGHT0); gl.glEnable(GL2.GL_NORMALIZE); float[] ambientLight = { 0.1f, 0.f, 0.f,0f }; // weak RED ambient gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, ambientLight, 0); float[] diffuseLight = { 1f,2f,1f,0f }; // multicolor diffuse gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuseLight, 0);
Applying Light to a Rotating Polygon
Follow the given steps for applying pght to a rotating polygon.
Rotate the polygon using glRotate() method
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer gl.glLoadIdentity(); // Reset The View gl.glRotatef(rpoly, 0.0f, 1.0f, 0.0f);
Let us go through the program to apply pght to a rotating polygon −
import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabipties; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; import com.jogamp.opengl.util.FPSAnimator; pubpc class PolygonLighting implements GLEventListener { private float rpoly; @Override pubpc void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glColor3f(1f,0f,0f); //applying red // Clear The Screen And The Depth Buffer gl.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT ); gl.glLoadIdentity(); // Reset The View gl.glRotatef( rpoly, 0.0f, 1.0f, 0.0f ); gl.glBegin( GL2.GL_POLYGON ); gl.glVertex3f( 0f,0.5f,0f ); gl.glVertex3f( -0.5f,0.2f,0f ); gl.glVertex3f( -0.5f,-0.2f,0f ); gl.glVertex3f( 0f,-0.5f,0f ); gl.glVertex3f( 0f,0.5f,0f ); gl.glVertex3f( 0.5f,0.2f,0f ); gl.glVertex3f( 0.5f,-0.2f,0f ); gl.glVertex3f( 0f,-0.5f,0f ); gl.glEnd(); gl.glFlush(); rpoly += 0.2f; //assigning the angle gl.glEnable( GL2.GL_LIGHTING ); gl.glEnable( GL2.GL_LIGHT0 ); gl.glEnable( GL2.GL_NORMALIZE ); // weak RED ambient float[] ambientLight = { 0.1f, 0.f, 0.f,0f }; gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, ambient-Light, 0); // multicolor diffuse float[] diffuseLight = { 1f,2f,1f,0f }; gl.glLightfv( GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse-Light, 0 ); } @Override pubpc void dispose( GLAutoDrawable arg0 ) { //method body } @Override pubpc void init( GLAutoDrawable arg0 ) { // method body } @Override pubpc void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { // method body } pubpc static void main( String[] args ) { //getting the capabipties object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabipties capabipties = new GLCapabipties( profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabipties ); PolygonLighting polygonpghting = new PolygonLighting(); glcanvas.addGLEventListener( polygonpghting ); glcanvas.setSize( 400, 400 ); //creating frame final JFrame frame = new JFrame (" Polygon pghting "); //adding canvas to it frame.getContentPane().add( glcanvas ); frame.setSize( frame.getContentPane().getPreferredSize()); frame.setVisible( true ); //Instantiating and Initiating Animator final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true ); animator.start(); } //end of main } //end of class
If you compile and execute the above program, it generates the following output. Here, you can observe various snapshots of a rotating polygon with pghting.
Advertisements