JOGL – Pre-defined shapes

JOGL – Pre Defined Shapes ”; Previous Next In the Previous chapters we have learned how draw a shapes such as line, triangle, rhombus using JOGL. We draw lines by passing a predefined field, Gl_lines to glBegin() method. Other than GL_LINES, the glBegin() method accepts eight more parameters. You can use them to draw different shapes. These are used the same way as GL_LINES. The following table shows the glBegin() method parameters along with their description − Sr.No Parameters and Description 1 GL_LINES Creates each pair of vertices as an independent line segment. 2 GL_LINE_STRIP Draws a connected group of line segments from the first vertex to the last. 3 GL_LINE_LOOP Draws a connected group of line segments from the first vertex to the last, again back to the first. 4 GL_TRIANGLES Treats each triplet of vertices as an independent triangle. 5 GL_TRIANGLE_STRIP Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. 6 GL_TRIANGLE_FAN Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. 7 GL_QUADS Treats each group of four vertices as an independent quadrilateral. 8 GL_QUAD_STRIP Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. 9 GL_POLYGON Draws a single, convex polygon. Vertices 1,…,n define this polygon. Let us see some examples using glBegin() parameters. Program to draw a Line Strip import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class LineStrip implements GLEventListener { @Override public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin (GL2.GL_LINE_STRIP); gl.glVertex3f(-0.50f,-0.75f, 0); gl.glVertex3f(0.7f,0.5f, 0); gl.glVertex3f(0.70f,-0.70f, 0); gl.glVertex3f(0f,0.5f, 0); gl.glEnd(); } @Override public void dispose(GLAutoDrawable arg0) { //method body } @Override public void init(GLAutoDrawable arg0) { // method body } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { // method body } public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities); LineStrip r = new LineStrip(); glcanvas.addGLEventListener(r); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame (“LineStrip”); //adding canvas to frame frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); }//end of main }//end of classimport javax.media.opengl.GL2; If you compile and execute the above code, the following output is generated − Code snippet for display() method to draw a Line Loop public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin (GL2.GL_LINE_LOOP); gl.glVertex3f( -0.50f, -0.75f, 0); gl.glVertex3f(0.7f, .5f, 0); gl.glVertex3f(0.70f, -0.70f, 0); gl.glVertex3f(0f, 0.5f, 0); gl.glEnd(); } If you replace the display() method of any of the basic template programs with the above code, compile, and execute it, the following output is generated − Code snippet for display() method to draw a triangle using GL_TRIANGLES public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin(GL2.GL_TRIANGLES); // Drawing Using Triangles gl.glVertex3f(0.5f,0.7f,0.0f); // Top gl.glVertex3f(-0.2f,-0.50f,0.0f); // Bottom Left gl.glVertex3f(0.5f,-0.5f,0.0f); // Bottom Right gl.glEnd(); } If you replace the display() method of any of the basic template programs with the above code, compile, and execute it, the following output is generated − Code snippet for display() method to draw a Triangle Strip public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin (GL2.GL_TRIANGLE_STRIP); gl.glVertex3f(0f,0.5f,0); gl.glVertex3f(-0.50f,-0.75f,0); gl.glVertex3f(0.28f,0.06f,0); gl.glVertex3f(0.7f,0.5f,0); gl.glVertex3f(0.7f,-0.7f,0); gl.glEnd(); } If you replace the display() method of any of the basic template programs with the above code, compile and execute it, the following output is generated − Code snippet for display() method to draw a quadrilateral public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin(GL2.GL_QUADS); gl.glVertex3f( 0.0f,0.75f,0); gl.glVertex3f(-0.75f,0f,0); gl.glVertex3f(0f,-0.75f,0); gl.glVertex3f(0.75f,0f,0); gl.glEnd(); } If you replace the display() method of any of the basic template programs with the above code, compile, and execute it, the following output is generated − Code snippet for display() method to draw a polygon public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); 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(); } If you replace display() method of any of the basic template programs with the above code, compile, and execute it, the following output is generated − Print Page Previous Next Advertisements ”;

JOGL – Discussion

Discuss JOGL ”; Previous Next Java binding for OpenGL (JOGL) is an open source library for binding OpenGL graphics in Java. This tutorial provides a basic understanding of JOGL library and its features. It also explains how to develop 2D and 3D graphics applications using JOGL. Please enable JavaScript to view the comments powered by Disqus. Print Page Previous Next Advertisements ”;

JOGL – Drawing with GL_Lines

JOGL – Drawing with GL Lines ”; Previous Next In the Previous chapter we have learned how draw a basic line using JOGL. We draw lines by passing a predefined field, Gl_lines to glBegin() method. This chapter provides examples to draw shapes like triangle, rhombus and a house, using glBegin() method and GL_Lines. Let us go through a program to draw a triangle using GL_LINES − import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class Triangle implements GLEventListener { @Override public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin (GL2.GL_LINES); //drawing the base gl.glBegin (GL2.GL_LINES); gl.glVertex3f(-0.50f, -0.50f, 0); gl.glVertex3f(0.50f, -0.50f, 0); gl.glEnd(); //drawing the right edge gl.glBegin (GL2.GL_LINES); gl.glVertex3f(0f, 0.50f, 0); gl.glVertex3f(-0.50f, -0.50f, 0); gl.glEnd(); //drawing the lft edge gl.glBegin (GL2.GL_LINES); gl.glVertex3f(0f, 0.50f, 0); gl.glVertex3f(0.50f, -0.50f, 0); gl.glEnd(); gl.glFlush(); } @Override public void dispose(GLAutoDrawable arg0) { //method body } @Override public void init(GLAutoDrawable arg0) { // method body } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { // method body } public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities); Triangle l = new Triangle(); glcanvas.addGLEventListener(l); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame (“Triangle”); //adding canvas to frame frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); }//end of main }//end of classimport javax.media.opengl.GL2; If you compile and execute the above program, the following output is generated. It shows a triangle drawn using GL_LINES of glBegin() method. Let us go through a program to draw a rhombus using GL_LINES − import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class Rhombus implements GLEventListener { @Override public void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); //edge1 gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( 0.0f,0.75f,0 ); gl.glVertex3f( -0.75f,0f,0 ); gl.glEnd(); //edge2 gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( -0.75f,0f,0 ); gl.glVertex3f( 0f,-0.75f, 0 ); gl.glEnd(); //edge3 gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( 0f,-0.75f, 0 ); gl.glVertex3f( 0.75f,0f, 0 ); gl.glEnd(); //edge4 gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( 0.75f,0f, 0 ); gl.glVertex3f( 0.0f,0.75f,0 ); gl.glEnd(); gl.glFlush(); } @Override public void dispose( GLAutoDrawable arg0 ) { //method body } @Override public void init(GLAutoDrawable arg0 ) { // method body } @Override public void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { // method body } public static void main( String[] args ) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabilities ); Rhombus rhombus = new Rhombus(); glcanvas.addGLEventListener( rhombus ); glcanvas.setSize( 400, 400 ); //creating frame final JFrame frame = new JFrame ( “Rhombus” ); //adding canvas to frame frame.getContentPane().add( glcanvas ); frame.setSize(frame.getContentPane().getPreferredSize() ); frame.setVisible( true ); } } If you compile and execute the above program, you get the following output. It shows a rhombus generated using GL_LINES of glBegin() method. Let us go through a program to draw a house using GL_LINES − import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class House implements GLEventListener { @Override public void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); //drawing top gl.glBegin ( GL2.GL_LINES ); gl.glVertex3f( -0.3f, 0.3f, 0 ); gl.glVertex3f( 0.3f,0.3f, 0 ); gl.glEnd(); //drawing bottom gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( -0.3f,-0.3f, 0 ); gl.glVertex3f( 0.3f,-0.3f, 0 ); gl.glEnd(); //drawing the right edge gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( -0.3f,0.3f, 0 ); gl.glVertex3f( -0.3f,-0.3f, 0 ); gl.glEnd(); //drawing the left edge gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( 0.3f,0.3f,0 ); gl.glVertex3f( 0.3f,-0.3f,0 ); gl.glEnd(); //building roof //building lft dia gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( 0f,0.6f, 0 ); gl.glVertex3f( -0.3f,0.3f, 0 ); gl.glEnd(); //building rt dia gl.glBegin( GL2.GL_LINES ); gl.glVertex3f( 0f,0.6f, 0 ); gl.glVertex3f( 0.3f,0.3f, 0 ); gl.glEnd(); //building door //drawing top gl.glBegin ( GL2.GL_LINES ); gl.glVertex3f( -0.05f, 0.05f, 0 ); gl.glVertex3f( 0.05f, 0.05f, 0 ); gl.glEnd(); //drawing the left edge gl.glBegin ( GL2.GL_LINES ); gl.glVertex3f( -0.05f, 0.05f, 0 ); gl.glVertex3f( -0.05f, -0.3f, 0 ); gl.glEnd(); //drawing the right edge gl.glBegin ( GL2.GL_LINES ); gl.glVertex3f( 0.05f, 0.05f, 0 ); gl.glVertex3f( 0.05f, -0.3f, 0 ); gl.glEnd(); } @Override public void dispose( GLAutoDrawable arg0 ) { //method body } @Override public void init( GLAutoDrawable arg0 ) { // method body } @Override public void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { // method body } public static void main( String[] args ) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabilities ); House house = new House(); glcanvas.addGLEventListener( house ); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame( “House” ); //adding canvas to frame frame.getContentPane().add( glcanvas ); frame.setSize(frame.getContentPane().getPreferredSize() ); frame.setVisible( true ); }//end of main }//end of class If you compile and execute the above program, you get the following output. It shows a house diagram generated using GL_LINES() method. Print Page Previous Next Advertisements ”;

JOGL – Overview

JOGL – Overview ”; Previous Next This chapter introduces OpenGL, its functions, the OpenGL bindings in java (GL4java, LWJGL, JOGL), and the advantages of JOGL over other OpenGL bindings. Java binding for OpenGL (JOGL) is the recent binding for OpenGL graphics API in Java. It is a wrapper library, which can access OpenGL API, and it is designed to create 2D and 3D graphics applications coded in Java. JOGL is an open-source library initially developed by former MIT graduate students Ken Russell and Chris Kline. Later, it was adopted by the gaming group at Sun Microsystems, and now it is maintained by Java on Graphics Audio and Processing (JOGAMP). JOGL functions on various operating systems such as Windows, Solaris, Mac OS X, and Linux (on x86). What is OpenGL? OpenGL stands for Open Graphics Library, which is a collection of commands to create 2D and 3D graphics. With OpenGL, you can create complicated 3D shapes using very basic primitives such as points, lines, polygons, bitmaps, and images. Here are a few features of OpenGL − It can work on multiple platforms. It has bindings in several languages such as C++, Python, etc It can render 2D and 3D vector graphics. It interacts with Graphical Processing Unit (GPU) for achieving speedy and high quality rendering. Rendering is the process of creating an image from a 2D or 3D model. It is an industry standard API for writing 3D Graphics applications. For example, games, screensavers, etc. It contains around 150 commands, which programmers can use to specify objects and operations to develop applications. It contains OpenGL Utility Library (GLU) that provides various modeling features, such as quadric surfaces and NURBS curves. GLU is a standard component of OpenGL. The design of OpenGL is focused on efficiency, effectiveness, and its implementation on multiple platforms using multiple languages. To maintain simplicity of an OpenGL API, windowing tasks are not included. Therefore, OpenGL depends on other programming languages for windowing tasks. Java Binding for OpenGL API It is a Java Specification Request (JSR) API specification, which allows to use OpenGL on Java platform. Specifications Details JSR 231 This Java binding package supports Java SE platform. JSR 239 This Java binding package supports Java ME platform. There are various OpenGL bindings in Java. They are discussed below GL4java It is known as OpenGL for Java technology. It has links to OpenGL 1.3 and to nearly all vendor extensions. Also, it can be used with Abstract Window Toolkit (AWT) and Swings. It is a game focused OpenGL binding, which is a single window that displays full screen applications. LWJGL Light Weight Java Game Library (LWJGL), uses OpenGL 1.5 and works with latest version of java. It can use full screen capabilities of JSE 1.4. It has limited support for AWT/ Swings. It is suitable for lightweight devices such as mobile phones, embedded devices, etc. JOGL JOGL focuses only on 2D and 3D Rendering. The interfaces dealing with sound and input-output are not included in JOGL. It includes Graphics Utility Library (GLU), GL Utility toolkit (GLUT), and its own API – Native Windowing Toolkit (NEWT). Why JOGL? It provides full access to the OpenGL APIs (version 1.0, 4.3, ES 1, ES 2 and ES 3) as well as nearly all the vendor extensions. Hence, all the features in OpenGL are included in JOGL. JOGL integrates with the AWT, Swing, and Standard Widget Toolkit (SWT). It also includes its own Native Windowing Toolkit (NEWT). Hence, it provides complete support for windowing. History of JOGL 1992 − Silicon Graphics Inc. released the first OpenGL specification. 2003 − Java.net website was launched with new features and JOGL was published for the first time on the same website. 2010 − Since year 2010, it has been independent open source project under BSD license, which is a liberal license for computer software. Print Page Previous Next Advertisements ”;

JOGL – API for Basic Templates

JOGL – API for Basic Template ”; Previous Next Using JOGL programming, it is possible to draw various graphical shapes such as straight lines, triangles, 3D shapes including special effects such as rotation, lighting, colors, etc. To draw objects in JOGL first of all we have to construct a basic JOGL frame. Below given are the classes required to construct a basic frame. GLEventListener Interface To make your program capable of using JOGL graphical API, you need to implement GLEventListener interface. You can find the GLEventListener interface in the javax.media.opengl package. The following table provides the details of various methods and descriptions of GLEventListener interface − Sr.No. Methods and Descriptions 1 Void display(GLAutoDrawable drawable) It is called by the object of GLAutoDrawable interface to initiate OpenGL rendering by the client. i.e., this method contains the logic used to draw graphical elements using OpenGL API. 2 Void dispose(GLAutoDrawable drawable) This method signals the listener to perform the release of all OpenGL resources per each GLContext, such as memory buffers and GLSL programs. 3 Void init(GLAutoDrawble drawable) It is called by the object of GLAutoDrawable interface immediately after the OpenGL context is initialized. 4 Void reshape(GLAutoDrawble drawble, int x, int y, int width, int height) It is called by the object of GLAutoDrawable interface during the first repaint after the component has been resized. It is also called whenever the position of the component on the window, is changed. All the methods of GLEventListener require object of GLAutoDrawable interface as a parameter. GLAutoDrawable Interface This interface supplies an event-based mechanism (GLEventListener) for performing OpenGL rendering. GLAutoDrawable automatically creates a primary rendering context which is associated with GLAutoDrawable for the lifetime of the object. The following table provides the details of various methods and descriptions of GLAutoDrawable interface − Sr.No Methods and Descriptions 1 GL getGL() Returns the GL pipeline object that is used by the current object of GLAutoDrawable interface. 2 void addGLEventListener(GLEventListener Listener) Adds the given listener to the end of current drawable queue. 3 void addGLEventListener(int index, GLEventListener listener) Adds the given listener at the given index of this drawable queue. 4 void destroy() Destroys all resources associated with this object of GLAutoDrawable interface, including the GLContext. Note − There are other methods in this package. Only a few important methods pertaining to template are discussed in this interface. GLCanvas Class GLCanvas and GLJpanel are the two main classes of JOGL GUI that implement GLAutoDrawable interface, which can be utilized as drawing surfaces for OpenGL commands. GLCanvas is a heavyweight AWT component which provides OpenGL rendering support. This is the primary implementation of an AWTAutoGLDrawable interface. It also inherits java.awt.Canvas class. Since it is a heavyweight component, in certain cases, GLJCanvas may not integrate with swing component correctly. Therefore, care must be taken while using it with Swing. Whenever you face problems with GLJCanvas, then you must use GLJPanel class. The hierarchical diagram of class GLCanvas can be as shown below − GLEventistener interface works along with GLCanvas class. It responds to the changes in GLCanvas class and to the drawing requests made by them. Whenever GLCanvas class is instantiated, the init() method of GLEventListener is invoked. You can override this method to initialize the OpenGL state. Whenever GLCanvas is drawn initially (instantiated) or resized, the reshape() method of GLEventListener is executed. It is used to initialize the OpenGL viewport and projection matrix. It is also called whenever the component”s location is changed. The display() method of GLEventListener contains the code for rendering 3D scene. It is invoked whenever display() method of GLCanvas is invoked. Below given are the constructors required to instantiate GLCanvas class. Sr.No Constructor and Description 1 GLCanvas() It creates a new GLCanvas component with a default set of OpenGL capabilities, using the default OpenGL capabilities selection mechanism, on the default screen device. 2 GLCanvas(GLCapabilitiesImmutable) It creates a new GLCanvas component with the requested set of OpenGL capabilities using the default OpenGL capabilities selection mechanism on the default screen device. Below given are the methods used for event handling of GLCanvas class. Sr. No. Methods and Description 1 void addGLEventListener(GLEventListener listener) Adds the given listener to the end of this drawable queue. 2 void addGLEventListener(int indexGLEventListener listener) Adds the given listener at the given index of this drawable queue. To instantiate GLCanvas class, you need the object of GLCapabilitiesImmutable interface, which specifies an immutable set of OpenGL capabilities. One of the ways to get an object of CapabilitiesImmutable interface is to instantiate GLCapabilities class, which implements the interface. An instance of GLCapabilities class can be used to serve the purpose. GLCapabilities Class This class specifies a set of OpenGL capabilities. It takes GLCapabilities object as a parameter. The GLCapabilities class describes the desired capabilities that a rendering context must support, such as the OpenGL profile. Below given is a constructor to instantiate GLCapabilities class Sr. No. Methods and Description 1 GLCapabilities(GLProfile glprofile) It creates a GLCapabilities object. To instantiate GLCanvas class, you need an object of GLCapabilitiesImmutable interface, which specifies an immutable set of OpenGL capabilities. One of the ways to get an object of CapabilitiesImmutable interface is to instantiate GLCapabilities class, which implements the interface. The instance of GLCapabilities class can be used to serve the purpose. The GLCapabilities class in turn requires a GLProfile object. GLProfile Class Since several versions of OpenGL API were released; you need to specify the exact version of OpenGL API being used in your program to your Java Virtual Machine (JVM). This is done using the GLProfile class. The get() method of this class accepts different predefined String objects as parameters. Each String object is a name of an interface and each interface supports certain versions of OpenGL. If you initialize this class as static and singleton, it gives you singleton GLProfile objects for each available JOGL profile. Below given is the prototype of the get method of GLProfile class. Sr.No. Method and Description 1 Static GLProfile get(String profile) Uses the default device. As this is

JOGL – Transformation

JOGL – Transformation ”; Previous Next OpenGL provides more features such as applying colors to an object, scaling, lighting, rotating an object, etc. This chapter describes some of the transformations on objects using JOGL. Moving an Object on the Window In earlier chapters, we discussed the programs for drawing a line and drawing various shapes using simple lines. The shapes created in this way can be displayed on any location within the window. It is done using the method glTranslatef (float x, float y, float z). This method belongs to the GLMatrixFunc interface, which is in the javax.media.opengl.fixedfunc package. GLMatrixFunc Interface interface − GLMatrixFunc package − javax.media.opengl.fixedfunc The following table lists some important methods of this interface − Sr.No. Methods and Description 1 void glRotatef(float angle, float x, float y, float z) Rotates the current matrix. 2 void glScalef(float x, float y, float z) Used to scale the current matrix. 3 void glTranslatef(float x, float y,float z) Used to translate the current matrix. 4 void glLoadIdentity() Loads the current matrix with identity matrix. The glTranslate() method moves the origin of the coordinate system to the point specified by the parameters (x,y,z), passed to the glTranslate() method as argument. To save and restore the untranslated coordinate system, glPushMatrix() and glPopMatrix() methods are used. gl.glTranslatef(0f, 0f, -2.5f); Whenever glTranslate() is used, it changes the position of the component on the screen. Hence, the reshape() method of GLEventListener interface should be overridden and OpenGL viewport and projection matrix should be initialized. The following code shows the template to initialize a view port and projection matrix − public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { // TODO Auto-generated method stub final GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics object if(height <= 0) height = 1; //preventing devided by 0 exception height = 1; final float h = (float) width / (float) height; // display area to cover the entire window gl.glViewport(0, 0, width, height); //transforming projection matrix gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, h, 1.0, 20.0); //transforming model view gl.glLoadIdentity(); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } Print Page Previous Next Advertisements ”;

JOGL – Installation

JOGL – Installation ”; Previous Next This chapter covers setting up of the environment to use JOGL on your system using different Integrated Development Environments (IDEs). Installing JOGL For JOGL Installation, you need to have following system requirements − System Requirements The first requirement is to have the Java Development Kit (JDK) installed on your machine. Requirement Description JDK Version 1.4 or above Memory no minimum requirement Disk Space no minimum requirement Operating System no minimum requirement You need to follow the given steps to setup your environment to start with JOGL application development − Step 1 – Verifying Java Installation on Your Machine Open console of your system and execute the following java command − Platform TASK COMMAND Windows Open Command Console C:>java-version Linux Open Command terminal $ java -version MAC Open Terminal Machine:~ joseph$ java -version Verify the output on the respective operating system. Platform Output Windows Java “1.6.0.21” java(TM) SE Runtime Environment(build 1..6.0_21-b07)Java HotSpot(TM) Client VM(build 17.0-b7, mixed mode, sharing) Linux Java “1.6.0.21” java(TM) SE Runtime Environment(build 1..6.0_21-b07)Java HotSpot(TM) Client VM(build 17.0-b7, mixed mode, sharing) MAC Java “1.6.0.21” java(TM) SE Runtime Environment(build 1..6.0_21-b07)Java HotSpot(TM) Client VM(build 17.0-b7, mixed mode, sharing) Step 2 – Setting up Java Development Kit (JDK) If Java is not installed on your machine, then you need to install Java SDK from the Oracle website: Oracle. You can find instructions for installing the JDK from the downloaded files. You need to follow the given instructions to install and configure the setup. Finally, set PATH and JAVA_HOME environment variables to refer to the directory that contains java.exe and javac.exe files, typically java_install_dir/bin and java_install_dir respectively. Set Java-home environment variable to point to the base directory location on the same path, where Java is installed on your machine. Platform Command Windows Set the environment variable JAVA_HOME to C:ProgramFilesJavaJdk1.6.0_21 Linux Export JAVA_HOME=/usr/local/java-current MAC Export JAVA_HOME=/Library/Java/Home Append Java compiler location to System Path as follows − Platform Command Windows Append the string ;%JAVA_HOME% bin at the end of the system variable and path Linux Export PATH=$PATH:$JAVA_HOME/bin/ MAC Not required Step 3 – Downloading JOGL You can download latest version of JOGL from the website www.jogamp.org Go to the home page of www.jogamp.org Click on Builds/Downloads > Current (zip). This takes you to the list of .jar files for all APIs maintained by the website. Download the library .jar file jogamp-all-platforms.7z, java documentations for OpenGL native library glugen-javadoc.7z, and JOGL jogl-javadocs.7z. Extract the downloaded .jar files using any zip extracting software. When you open the extracted folder, you will find jar folder, source-codes, and other files. Get the source codes gluegen-java-src.zip and jogl-java-src.zip for supporting IDE. This is optional. Inside the jar folder, there are multiple .jar files. This collection of files belongs to Glugen and JOGL. JOAMP provides native libraries that support various operating systems such as Windows, Solaris, Linux and Android. Hence, you need to take appropriate jar files which can execute on your desired platform. For example, if you are using Windows 64-bit operating system, then get the following .jar files from the jar folder − gluegenrt.jar jogl-all.jar gluegen-rt-natives-windows-amd64.jar jogl-all-natives-windowsamd64.jar Setting up JOGL for Eclipse 4.4 Follow the given procedure for setting up JOGL − Adding Libraries Step 1 − Open Eclipse. Step 2 − Create a new project. Step 3 − Create a new folder named lib in the project folder. Step 4 − Copy the files gluegen-rt-natives-windows-amd64.jar, gluegenrt.jar, jogl-all-natives-windowsamd64.jar and jogl-all.jar into the lib folder. Step 5 − Now select these files and right click your mouse button. A shortcut menu is displayed, which contains Build Path > Add to Build Path. Step 6 − To make all .jar files available to other projects, go to main menu. Select Window > Preferences. The Preferences window appears. In preferences window, in the drop down menu on the left hand side, follow the hierarchy- Java → Build Path → User Libraries. Click on “New…” button. It opens up a dialog box. Enter the library name as jogl2.1. Add jar files glugen-rt.jar and jogl-all.jar using button “Add External JARs…”. It creates a new user library named jogl2.1. In the same way, we can add java documentation and source code for the added.jar files. Adding Native Libraries Step 1 − Expand the jogl-all.jar node, select Javadoc location (none). Step 2 − Click on “New…” button. Enter the name for JOGL Java Document. Step 3 − Click on “Add External JARs…” button. Step 4 − It opens a dialog box where you need to select the location of JOGL Java documentation, which we already have downloaded earlier. Adding source code Step 1 − Select the node Native library location: (None). Step 2 − Click on “New…” button. Step 3 − Enter name for native libraries and click “OK” button. Step 4 − Click on “Add External JARs…” button. Step 5 − Now select the path where native library files (”gluegen-rt-natives-windows-amd64.jar and joglall-natives-windows-amd64.jar”) are located. Step 6 − Repeat the same procedure for source code. Step 7 − We can set the locations for Javadoc, source code and jar files in the same way as given above for both native library files glegen-rt.jar and glugen-natives-windows-amd64.jar. Setting up JOGL for NetBeans 4.4 Let us go through the steps for setting up JOGL for NetBeans 4.4 − Adding Libraries Step 1 − In the main menu, select Tools > Libraries. Step 2 − It leads you to Ant Library Manager. Step 3 − Under the Classpath tab, click New Library button located on the left lower corner. It opens a small dialog box. Step 4 − Enter Library name as JoGl2.0. Step 5 − Click on “OK” button. Step 6 − Click on “Add JAR/Folder…” button. Step 7 − Select the path where .jar files jogl.all.jar and gluegen-rt.jar are located. To include JOGL library into each project, follow the steps given below − Step 1 − Right-click on the project name. It shows a short-cut menu. Step 2 − Select Properties. It opens a window named Project properties.

JOGL – Coloring

JOGL – Coloring ”; Previous Next This chapter teaches you how to apply colours to the objects using JOGL. To apply colour to an object, use the method glColor() of GL2. Below given is the syntax for using glColor method. Syntax gl.glColorXY(1f,0f,0f); where, X denotes the number of colours used, 3 (red, green, blue) or 4(red, green, blue, alpha). To get various colour combinations, the values of these colours are passed as parameters. The sequence of the colour parameters must be maintained in that order. Example If you pass colour values as (1, 0, 0), then you get red colour. Similarly, (1, 1, 0) gives you yellow colour. Y denotes the data type which accepts parameters such as byte(b), double(d), float(f), int(i), short(s), ubyte(ub), uint(ui), and ushort(us). gl.glColor3f(1f,0f,0f); //gives us red gl.glColor3f(0f,1f,0f); //gives us green gl.glColor3f(0f,0f,1f); //gives us blue In case of triangle, you can apply different colors for each vertex. Let us go through the program to apply colors to a triangle − import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class TriangleColor implements GLEventListener { @Override public void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin( GL2.GL_TRIANGLES ); // Drawing Using Triangles gl.glColor3f( 1.0f, 0.0f, 0.0f ); // Red gl.glVertex3f( 0.5f,0.7f,0.0f ); // Top gl.glColor3f( 0.0f,1.0f,0.0f ); // green gl.glVertex3f( -0.2f,-0.50f,0.0f ); // Bottom Left gl.glColor3f( 0.0f,0.0f,1.0f ); // blue gl.glVertex3f( 0.5f,-0.5f,0.0f ); // Bottom Right gl.glEnd(); } @Override public void dispose( GLAutoDrawable arg0 ) { //method body } @Override public void init( GLAutoDrawable arg0 ) { // method body } @Override public void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { // method body } public static void main( String[] args ) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabilities ); TriangleColor triangle = new TriangleColor(); glcanvas.addGLEventListener( triangle ); glcanvas.setSize( 400, 400 ); //creating frame final JFrame frame = new JFrame (” Colored Triangle”); //adding canvas to it frame.getContentPane().add( glcanvas ); frame.setSize( frame.getContentPane().getPreferredSize()); frame.setVisible( true ); } //end of main } //end of class When you compile and execute the above program, you get the following colored triangle − Applying Color to a Polygon Let us go through the program to apply colors to a polygon − import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class PolygonColor implements GLEventListener { @Override public void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glColor3f( 1f,0f,0f ); //applying red 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(); } @Override public void dispose( GLAutoDrawable arg0 ) { //method body } @Override public void init( GLAutoDrawable arg0 ) { // method body } @Override public void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { // method body } public static void main( String[] args ) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabilities ); PolygonColor polygon = new PolygonColor(); glcanvas.addGLEventListener( polygon ); glcanvas.setSize( 400, 400 ); //creating frame final JFrame frame = new JFrame ( “Colored Polygon” ); //adding canvas to frame frame.getContentPane().add( glcanvas ); frame.setSize(frame.getContentPane().getPreferredSize() ); frame.setVisible( true ); } //end of main } //end of class When you compile and execute the above program, you get the following coloured Polygon − Print Page Previous Next Advertisements ”;

JOGL – GLJPanel Class

JOGL – GLJPanel Class ”; Previous Next This chapter explains you how to draw a JOGL basic frame using GLJpanel class. It is a lightweight Swing component which provides OpenGL rendering support. It is provided for compatibility with Swing. In here we will instantiate a JFrame and add the GLJpanel object to the instance of JFrame using the add() method. The following program generates a basic frame using GLJPanel with Swing window − import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class BasicFrame implements GLEventListener { @Override public void display(GLAutoDrawable arg0) { // method body } @Override public void dispose(GLAutoDrawable arg0) { //method body } @Override public void init(GLAutoDrawable arg0) { // method body } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { // method body } public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The GLJpanel class GLJPanel gljpanel = new GLJPanel( glcapabilities ); BasicFrame b = new BasicFrame(); gljpanel.addGLEventListener(b); gljpanel.setSize(400, 400); //creating frame final JFrame frame = new JFrame (” Basic Frame”); //adding canvas to it frame.getContentPane().add( gljpanel); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); }//end of main }//end of classimport If you compile and execute the above program, the following output is generated. It shows a basic frame formed when we use GLJPanel with swing window − Print Page Previous Next Advertisements ”;

JOGL – Canvas with Swing

JOGL – Canvas with Swing ”; Previous Next This chapter explains you how to draw a JOGL basic frame using Canvas, and JFrame class of javax.swing package. In here we will instantiate a JFrame and add the canvas object to the instance of JFrame using the add() method. Using Canvas with AWT gives you a graphical frame with heavyweight features. For having a lightweight graphical frame, you need to use GLCanvas with Swing. While using GLCanvas with Swing, you can place GLCanvas in the JFrame window directly, or you can add it to JPanel. Below given is the program which creates a JOGL basic frame with the combination of JOGL”s GLCanvas class and JFrame class of the javax.swing package. import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; public class BasicFrame implements GLEventListener { @Override public void display(GLAutoDrawable arg0) { // method body } @Override public void dispose(GLAutoDrawable arg0) { //method body } @Override public void init(GLAutoDrawable arg0) { // method body } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { // method body } public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities); BasicFrame b = new BasicFrame(); glcanvas.addGLEventListener(b); glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame (” Basic Frame”); //adding canvas to it frame.getContentPane().add(glcanvas); frame.setSize(frame.getContentPane().getPreferredSize()); frame.setVisible(true); }//end of main }//end of classimport If you compile and execute the above program, the following output is generated. It shows a basic frame formed when we use GLCanvas with Swing window. Print Page Previous Next Advertisements ”;