JOGL

0
66

JOGL – Oversee

This chapter introduces OpenGL, it’s 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 lacheck binding for OpenGL graphics API in Java. It is a wrapper library, which can access OpenGL API, and it is styleed to develop 2D and 3D graphics applications coded in Java. JOGL is an open-source library preliminaryly developed simply by previous MIT graduate pupils Ken Russell and Chris Krange. Later, it was adopted simply by the gaming group at Sun Microsystems, and now it is maintained simply by Java on Graphics Audio and Procesperform (JOGAMP). JOGL functions on various operating systems such as Windows, Solaris, Mac OS X, and Linux (on x86).

Whead wear is OpenGL?

OpenGL stands for Open Graphics Library, which is a collection of commands to develop 2D and 3D graphics. With OpenGL, you can develop complicated 3D forms uperform very basic primitives such as stages, ranges, polygons, bitmaps, and images.

Here are a couple of features of OpenGL −

  • It can work on multiple platforms.

  • It has bindings in a few languages such as C++, Python, etc

  • It can render 2D and 3D vector graphics.

  • It interbehaves with Graphical Procesperform 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 indusattempt 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) thead wear provides various modeling features, such as quadric surfaces and NURBS curves. GLU is a standard component of OpenGL.

  • The style of OpenGL is focused on efficiency, effectiveness, and it’s implementation on multiple platforms uperform multiple languages. To maintain simplicity of an OpenGL API, earndoearng tasks are not included.

Therefore, OpenGL depends on other programming languages for earndoearng tasks.

Java Binding for OpenGL API

It is a Java Specification Request (JSR) API specification, which permit’s to use
OpenGL on Java platform.

Specifications Details
JSR 231 This Java binding package supinterfaces Java SE platform.
JSR 239 This Java binding package supinterfaces Java ME platform.

There are various OpenGL bindings in Java. They are discussed below

GL4java

It is belowstandn as OpenGL for Java technology. It has links to OpenGL 1.3 and to nearly all vendor extensions. Also, it can be used with Abstrbehave Window Toolkit (AWT) and Searngs. It is a game focused OpenGL binding, which is a performle earndow thead wear displays compenablee screen applications.

LWJGL

  • Light Weight Java Game Library (LWJGL), uses OpenGL 1.5 and works with lacheck version of java.

  • It can use compenablee screen capabiliconnect ups of JSE 1.4. It has limited supinterface for AWT/ Searngs.

  • 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 inplace-awayplace are not included in JOGL.

  • It includes Graphics Utility Library (GLU), GL Utility toolkit (GLUT), and it’s own API – Native Windoearng Toolkit (NEWT).

Why JOGL?

  • It provides compenablee 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, Searng, and Standard Widget Toolkit (SWT).
    It also includes it’s own Native Windoearng Toolkit (NEWT). Hence, it provides
    comppermite supinterface for earndoearng.

History of JOGL

  • 1992 − Silicon Graphics Inc. relreare locatedved the preliminary OpenGL specification.

  • 2003 − Java.net website was launched with new features and JOGL was published for the preliminary time on the exact same website.

  • 2010 − Since yr 2010, it has been independent open source project below BSD license, which is a liberal license for complaceer smoothbattlee.

JOGL – Installation

This chapter covers setting up of the environment to use JOGL on your own own system
uperform various Integrated Development Environments (IDEs).

Installing JOGL

For JOGL Installation, you need to have folloearng system requirements −

System Requirements

The preliminary requirement is to have the Java Development Kit (JDK) instalimmediateed on your own own 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 own own environment to start with JOGL
application development −

Step 1 – Verifying Java Installation on Your Machine

Open console of your own own system and execute the folloearng 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 awayplace on the respective operating system.

Platform Outplace
Windows

Java “1.6.0.21”

java(TM) SE Runtime Environment(construct 1..6.0_21-b07)Java HotSpot(TM) Care locatednt VM(construct 17.0-b7, mixed mode, sharing)

Linux

Java “1.6.0.21”

java(TM) SE Runtime Environment(construct 1..6.0_21-b07)Java HotSpot(TM) Care locatednt VM(construct 17.0-b7, mixed mode, sharing)

MAC

Java “1.6.0.21”

java(TM) SE Runtime Environment(construct 1..6.0_21-b07)Java HotSpot(TM) Care locatednt VM(construct 17.0-b7, mixed mode, sharing)

Step 2 – Setting up Java Development Kit (JDK)

If Java is not instalimmediateed on your own own 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 immediateory thead wear contains java.exe and javac.exe files, typically java_install_dir/bin and java_install_dir respectively.

Set Java-home environment variable to stage to the base immediateory location on the exact same rawaye, where Java is wislimstalimmediateed on your own own machine.

Platform Command
Windows Set the environment variable JAVA_HOME to C:ProgramFilesJavaJdk1.6.0_21
Linux Exinterface JAVA_HOME=/usr/local/java-current
MAC Exinterface 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 rawaye
Linux Exinterface PATH=$PATH:$JAVA_HOME/bin/
MAC Not requireddish coloured

Step 3 – Downloading JOGL

  • You can download lacheck version of JOGL from the website www.jogamp.org

  • Go to the home page of www.jogamp.org

  • Click on Builds/Downloads > Current (zip).

Jogamp Home Page

This gets you to the list of .jar files for all APIs maintained simply by the website.

JAR Files List

  • Download the library .jar file jogamp-all-platforms.7z, java documentations for OpenGL native library glugen-javadoc.7z, and JOGL jogl-javadocs.7z.

  • Extrbehave the downloaded .jar files uperform any kind of zip extrbehaveing smoothbattlee.

When you open the extrbehaveed folder, you will find jar folder, source-codes, and
other files.

Source Code Folder

Get the source codes gluegen-java-src.zip and jogl-java-src.zip for supinterfaceing IDE. This is optional.

  • Inaspect the jar folder, there are multiple .jar files. This collection of files besizeys to Glugen and JOGL.

  • JOAMP provides native libraries thead wear supinterface various operating systems such as Windows, Solaris, Linux and Android. Hence, you need to get appropriate jar files which can execute on your own own desireddish coloured platform. For example, if you are uperform Windows 64-bit operating system, then get the folloearng .jar files from the jar folder −

    • gluegenrt.jar
    • jogl-all.jar
    • gluegen-rt-natives-earndows-amd64.jar
    • jogl-all-natives-earndowsamd64.jar

JAR Files

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-earndows-amd64.jar, gluegenrt.jar, jogl-all-natives-earndowsamd64.jar and jogl-all.jar into the lib folder.

Eclipse1

Step 5 − Now select these files and appropriate click your own own mouse button. A shortcut menu is displayed, which contains Build Path > Add to Build Path.

Eclipse2

Step 6 − To develop all .jar files available to other projects, go to main menu. Select Window > Preferences. The Preferences earndow appears.

Eclipse3
Eclipse4

  • In preferences earndow, in the drop down menu on the left hand aspect, follow the hierarchy- Java → Build Path → User Libraries.

  • Click on “New…” button.

  • It opens up a dialog package. Enter the library name as jogl2.1.

  • Add jar files glugen-rt.jar and jogl-all.jar uperform button “Add External JARs…”.

  • It develops a new user library named jogl2.1.

In the exact 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 package where you need to select the location of JOGL Java documentation, which we already have downloaded earare locatedr.

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 rawaye where native library files ('gluegen-rt-natives-earndows-amd64.jar and joglall-natives-earndows-amd64.jar') are located.

Step 6 − Repeat the exact same procedure for source code.

Step 7 − We can set the locations for Javadoc, source code and jar files in the exact same way as given above for both native library files glegen-rt.jar and glugen-natives-earndows-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.

NetBeans1

Step 2 − It leads you to Ant Library Manager.

NetBeans2

Step 3 − Under the Clbumrawaye tab, click New Library button located on the left lower corner. It opens a small dialog package.

Step 4 − Enter Library name as JoGl2.0.

Step 5 − Click on “OK” button.

NetBeans3

Step 6 − Click on “Add JAR/Folder…” button.

Step 7 − Select the rawaye where .jar files jogl.all.jar and gluegen-rt.jar are located.

To include JOGL library into every project, follow the steps given below −

Step 1 − Right-click on the project name. It shows a short-cut menu.

NetBeans4

Step 2 − Select Properconnect ups. It opens a earndow named Project properconnect ups.

NetBeans7

Step 3 − Select Libraries from Categories on the left hand aspect.

Step 4 − Select Compile tab and click on “Add Library…” button. Add library dialog package comes up.

Step 5 − Now add JOGL2.0 library, which you developd earare locatedr.

Including Native Library in Each Project

Follow the given steps to include native library in every project −

Step 1 − Right-click the project.

Step 2 − Select Set Configuration > Customize…

NetBeans8

It leads you to the Project Properconnect ups earndow.

NetBeans9

Step 3 − On the appropriate hand aspect, in VM options, click on “Customize” button.

Step 4 − Browse for the rawaye thead wear contains JOGL native libraries gluegen-rtnatives-earndows-amd64.jar'' and 'jogl-all-natives-earndowsamd64.jar.

Adding Java Documentation of Native Libraries

You need to open Ant library manager again to develop sources and Javadoc available for every project. Follow the given procedure −

Step 1 − Open main menu.

Step 2 − Select Tools > Libraries. This leads you to Library manager.

Step 3 − Under the JavaDoc tab, click on “New Library…” button.

Step 4 − Enter JOGLJavadoc name. (You can enter any kind of desireddish coloured name.)

Step 5 − Click on “Add jars/libraries…” button.

Step 6 − Select the rawaye where unzipped JOGL documentation code is located.

Adding Source Code of Native Libraries

Step 1 − Under Sources tab, click on “New Library…” button. Enter JOGLsources name.

Step 2 − Click on “Add jars/libraries…” button. Select the rawaye where unzipped source code is located.

Customizing the JDK Editor

Step 1 − Set Clbumrawaye for files jogl.all.jar and gluegen-rt.jar.

Step 2 − Set rawaye to native libraries gluegen-rt-natives-earndows-amd64.jar and joglall-natives-earndowsamd64.jar or duplicate all the jar files from the folder where you have downloaded them and paste them into the jse lib folder.

JOGL – API for Basic Template

Uperform JOGL programming, it is feasible to draw various graphical forms such as straight ranges, triangles, 3D forms including special effects such as rotation, lighting, colours, etc. To draw objects in JOGL preliminary of all we have to construct a basic JOGL frame. Below given are the coursees requireddish coloured to construct a basic frame.

GLEventListener Interface

To develop your own own program capable of uperform JOGL graphical API, you need to implement GLEventListener interface. You can find the GLEventListener interface in the javax.media.opengl package.

The folloearng table provides the details of various methods and descriptions of GLEventListener interface −

Sr.No. Methods and Descriptions
1

Void display(GLAutoDrawable drawable)

It is calimmediateed simply by the object of GLAutoDrawable interface to initiate OpenGL rendering simply by the care locatednt. i.e., this method contains the logic used to draw graphical elements uperform OpenGL API.

2

Void dispose(GLAutoDrawable drawable)

This method signals the listener to perform the relreare locatedve of all OpenGL resources per every GLContext, such as memory buffers and GLSL programs.

3

Void init(GLAutoDrawble drawable)

It is calimmediateed simply by the object of GLAutoDrawable interface immediately after the OpenGL context is wislimitialized.

4

Void reform(GLAutoDrawble drawble, int x, int y, int width, int height)

It is calimmediateed simply by the object of GLAutoDrawable interface during the preliminary repaint after the component has been redimensiond. It is also calimmediateed whenever the position of the component on the earndow, is modifyd.

All the methods of GLEventListener require object of GLAutoDrawable interface as a parameter.

GLAutoDrawable Interface

This wislimterface suppare locateds an actuallyt-based mechanism (GLEventListener) for performing OpenGL rendering. GLAutoDrawable automatically develops a primary rendering context which is bumociated with GLAutoDrawable for the lifetime of the object.

The folloearng table provides the details of various methods and descriptions of GLAutoDrawable interface −

Sr.No Methods and Descriptions
1

GL getGL()

Returns the GL piperange object thead wear is used simply 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 bumociated with this object of GLAutoDrawable interface, including the GLContext.

Note − There are other methods in this package. Only a couple of iminterfaceant methods pertaining to template are discussed in this wislimterface.

GLCanvas Clbum

GLCanvas and GLJpanel are the 2 main coursees of JOGL GUI thead wear implement GLAutoDrawable interface, which can be utilized as draearng surfaces for OpenGL commands.

GLCanvas is a hugeweight AWT component which provides OpenGL rendering supinterface. This is the primary implementation of an AWTAutoGLDrawable interface. It also inherit’s java.awt.Canvas course. Since it is a hugeweight component, in specific cases, GLJCanvas may not integrate with searng component properly. Therefore, care must be getn while uperform it with Searng. Whenever you face issues with GLJCanvas, then you must use GLJPanel course.

The hierarchical diagram of course GLCanvas can be as shown below −

Canvas

  • GLEventistener interface works asizey with GLCanvas course. It responds to the modifys in GLCanvas course and to the draearng requests made simply by them.

  • Whenever GLCanvas course is wislimstantiated, the init() method of GLEventListener is wislimvoked. You can override this method to preliminaryize the OpenGL state.

  • Whenever GLCanvas is drawn preliminaryly (immediateiated) or redimensiond, the reform() method of GLEventListener is executed. It is used to preliminaryize the OpenGL seeinterface and projection matrix. It is also calimmediateed whenever the component's location is modifyd.

  • The display() method of GLEventListener contains the code for rendering 3D scene. It is wislimvoked whenever display() method of GLCanvas is wislimvoked.

Below given are the constructors requireddish coloured to immediateiate GLCanvas course.

Sr.No Constructor and Description
1

GLCanvas()

It develops a new GLCanvas component with a default set of OpenGL capabiliconnect ups, uperform the default OpenGL capabiliconnect ups selection mechanism, on the default screen device.

2

GLCanvas(GLCapabiliconnect upsImmutable)

It develops a new GLCanvas component with the requested set of OpenGL capabiliconnect ups uperform the default OpenGL capabiliconnect ups selection mechanism on the default screen device.

Below given are the methods used for actuallyt handling of GLCanvas course.

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 immediateiate GLCanvas course, you need the object of GLCapabiliconnect upsImmutable interface, which specifies an immutable set of OpenGL capabiliconnect ups.

One of the ways to get an object of Capabiliconnect upsImmutable interface is to immediateiate GLCapabiliconnect ups course, which implements the interface. An instance of GLCapabiliconnect ups course can be used to serve the purpose.

GLCapabiliconnect ups Clbum

This course specifies a set of OpenGL capabiliconnect ups. It gets GLCapabiliconnect ups object as a parameter. The GLCapabiliconnect ups course describes the desireddish coloured capabiliconnect ups thead wear a rendering context must supinterface, such as the OpenGL profile.

Below given is a constructor to immediateiate GLCapabiliconnect ups course

Sr. No. Methods and Description
1

GLCapabiliconnect ups(GLProfile glprofile)

It develops a GLCapabiliconnect ups object.

To immediateiate GLCanvas course, you need an object of GLCapabiliconnect upsImmutable interface, which specifies an immutable set of OpenGL capabiliconnect ups.

One of the ways to get an object of Capabiliconnect upsImmutable interface is to immediateiate GLCapabiliconnect ups course, which implements the interface. The instance of GLCapabiliconnect ups course can be used to serve the purpose.

The GLCapabiliconnect ups course in turn requires a GLProfile object.

GLProfile Clbum

Since a few versions of OpenGL API were relreare locatedved; you need to specify the exbehave version of OpenGL API being used in your own own program to your own own Java Virtual Machine (JVM). This is done uperform the GLProfile course.

The get() method of this course accepts various preddish colouredegreatd String objects as parameters. Each String object is a name of an interface and every interface supinterfaces specific versions of OpenGL. If you preliminaryize this course as static and performpermiton, it gives you performpermiton GLProfile objects for every available JOGL profile.

Below given is the protokind of the get method of GLProfile course.

Sr.No. Method and Description
1

Static GLProfile get(String profile)

Uses the default device.

As this is a static method, you need to invoke it uperform the course name, and it
requires a preddish colouredegreatd static string variable as parameter. There are 12 such variables in this course, every represents an individual implementation of GL interface.

GLProfile.get(GLProfile.GL2);

Parameters of get() method

Sr.No Preddish colouredegreatd String value (Interface name) and Description
1

GL2

This wislimterface contains all OpenGL [1.0 … 3.0] methods as well as many kind of of it’s extensions degreatd at the time of this specification.

2

GLES1

This wislimterface contains all OpenGL ES [1.0 … 1.1] methods as well as many kind of of it’s extensions degreatd at the time of this specification.

3

GLES2

This wislimterface contains all OpenGL ES 2.0 methods as well as many kind of of it’s extensions degreatd at the time of this specification.

4

GLES3

This wislimterface contains all OpenGL ES 3.0 methods as well as many kind of of it’s extensions degreatd at the time of this specification.

5

GL2ES1

This Interface contains the common subset of GL2 and GLES1.

6

GL2ES2

This Interface contains the common subset of GL3, GL2, and GLES2.

7

GL2GL3

This Interface contains the common subset of core GL3 (OpenGL 3.1+) and GL2.

8

GL3

This wislimterface contains all OpenGL [3.1 … 3.3] core methods as well as many kind of of it’s extensions degreatd at the time of this specification.

9

GL3bc

This wislimterface contains all OpenGL [3.1 … 3.3] compatibility methods, as well as many kind of of it’s extensions degreatd at the time of this specification.

10

GL3ES3

This wislimterface contains the common subset of core GL3 (OpenGL 3.1+) and GLES3 (OpenGL ES 3.0).

11

GL4

This wislimterface contains all OpenGL [4.0 … 4.3] core methods, as well as many kind of of it’s extensions degreatd at the time of this specification.

12

GL4bc

This wislimterface contains all OpenGL [4.0 … 4.3] compatibility profile, as well as many kind of of it’s extensions degreatd at the time of this specification.

13

GL4ES3

Interface containing the common subset of core GL4 (OpenGL 4.0+) and GLES3 (OpenGL ES 3.0).

GLJPanel Clbum

It is a lightweight Searng component which provides OpenGL rendering supinterface. It
is provided for compatibility with Searng.

GLJPanel Clbum Hierarchy

Below given is diagram represents the course hierarchey of GLJPanel course.

GJPanel

Below given are various constructors GLJPanel course.

Sr. No. Constructors and Description
1

GJPanel()

It develops a new GLJPanel component with a default set of OpenGL capabiliconnect ups.

2

(GLCapabiliconnect upsImmutable)

It develops a new GLJPanel component with the requested set of OpenGL capabiliconnect ups.

3

GLJPanel(GLCapabiliconnect upsImmutable userCapsRequest, GLCapabiliconnect upsChooser selectr)

It develops a new GLJPanel component.

Below given are the methods of GLJPanel course.

Sr.No. Methods and Description
1

void addGLEventListener(GLEventListener listener)

This method adds the given listener to the end of this drawable queue.

2

void addGLEventListener(int indexGLEventListener listener)

This method adds the given listener at the given index of this drawable queue.

JOGL – Canvas with AWT

This chapter exsimples you how to draw a JOGL basic frame uperform Canvas with AWT frame. In here we will construct a AWT Frame and add the canvas object to the AWT frame uperform the add() method of the frame course.

Below given are the steps to write a program which develops a JOGL basic frame with the combination of JOGL's Canvas course and AWT's Frame course.

Step1: Creating the Clbum

Initially develop a course thead wear implements GlEventListener interface and iminterface the package javax.media.opengl. Implement all four methods display(), dispose(), reform(), init(). Since this is the basic frame, primitive tasks such as creating canvas course, adding it to frame were discussed. All the GLEVentListener interface methods were left unimplemented.

Step2: Preparing the Canvas

(a) Constructing the GLCanvas course object

final GLCanvas glcanvas = new GLCanvas( xxxxxxx );

//here capabiliconnect ups obj need to be pbumed as parameter

(b) Instantiating the GLCapabiliconnect ups course

GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups( xxxxx );

//here profile obj need to be pbumed as parameter

(c) Generating GLProfile object

As it is the static method, it is wislimvoked uperform course name. Since this tutorial is abaway JOGL2, permit us generate GL2 interface object.

final GLProfile profile = GLProfile.get( GLProfile.GL2 );

// both, variable and method are static hence both are calimmediateed uperform course name.

Let us see the code snippet for canvas.

//getting the capabiliconnect ups object of GL2 profile

final GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);

// The canvas
final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);

(d) Now add GLEventListener to the canvas uperform the method addGLEventListener(). This method needs object of GLEventListener interface as parameter. Hence, pbum object of a course thead wear implements GLEventListener.

BasicFrame basicframe = newBasic Frame( );// course which implements
GLEventListener interface
glcanvas.addGLEventListener( basicframe );

(e) Set dimension of the frame uperform setSize() method inherited simply by GLCanvas from javax.media.opengl.awt.AWTGLAutoDrawable.

glcanvas.setSize( 400, 400 );

Now you are ready with GLCanvas.

Step3: Creating the Frame

Create the frame simply by immediateiating the Frame course Object of JSE AWT frame component.

Add canvas to it and develop the frame noticeable.

//creating frame
final Frame frame = new frame( " Basic Frame" );

//adding canvas to frame
frame.add( glcanvas );
frame.setVisible( true ); 

Step 4: Vieearng the Frame in Full Screen

To see the frame in compenablee screen, get the default screen dimension uperform java.awt.Toolkit course. Now, uperform those default screen dimension dimensions, set the frame dimension uperform setSize() method.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width, screenSize.height);

Let us go through the program to generate the basic frame uperform AWT −

iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course 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 reform(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
      // method body
   }
	
   public static void main(String[] args) {
   
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
        
      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      BasicFrame b = new BasicFrame();
      glcanvas.addGLEventListener(b);        
      glcanvas.setSize(400, 400);
        
      //creating frame
      final Frame frame = new Frame (" Basic Frame");
        
      //adding canvas to frame
      frame.add(glcanvas);
      frame.setSize( 640, 480 );
      frame.setVisible(true);
   }
	
}

If you compile and execute the above program, the folloearng awayplace is generated.
It shows a basic frame formed when we use GLCanvas course with AWT −

Basic Frame

JOGL – Canvas with Searng

This chapter exsimples you how to draw a JOGL basic frame uperform Canvas, and JFrame course of javax.searng package. In here we will immediateiate a JFrame and add the canvas object to the instance of JFrame uperform the add() method.

Uperform Canvas with AWT gives you a graphical frame with hugeweight features. For having a lightweight graphical frame, you need to use GLCanvas with Searng. While uperform GLCanvas with Searng, you can place GLCanvas in the JFrame earndow immediately, or you can add it to JPanel.

Below given is the program which develops a JOGL basic frame with the combination of JOGL's GLCanvas course and JFrame course of the javax.searng package.

iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course 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 reform(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
      // method body
   }
	
   public static void main(String[] args) {
   
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
               
      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      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().getPreferreddish colouredSize());
      frame.setVisible(true);
      
   }//end of main
	
}//end of courseiminterface          

If you compile and execute the above program, the folloearng awayplace is generated.
It shows a basic frame formed when we use GLCanvas with Searng earndow.

Basic Frame

JOGL – GLJPanel Clbum

This chapter exsimples you how to draw a JOGL basic frame uperform GLJpanel course. It is a lightweight Searng component which provides OpenGL rendering supinterface. It is provided for compatibility with Searng. In here we will immediateiate a JFrame and add the GLJpanel object to the instance of JFrame uperform the add() method.

The folloearng program generates a basic frame uperform GLJPanel with Searng earndow −

iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course 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 reform(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
      // method body
   }
   
   public static void main(String[] args) {
   
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
      
      // The GLJpanel course
      GLJPanel gljpanel = new GLJPanel( glcapabiliconnect ups ); 
      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().getPreferreddish colouredSize());
      frame.setVisible(true);
      
   }//end of main
	
}//end of courseiminterface

If you compile and execute the above program, the folloearng awayplace is generated.
It shows a basic frame formed when we use GLJPanel with searng earndow −

Basic Frame

JOGL – Draearng Basics

OpenGL API has provided primitive methods for draearng basic graphical elements such as stage, vertex, range etc. Uperform these methods, you can develop forms such as triangle, polygon and group. In both, 2D and 3D dimensions. This chapter teveryes you how to draw a basic range uperform JOGL in a Java program.

Draearng Objects

To access programs which are specific to a hardbattlee and operating system platforms and where the libraries are developd in other languages such as C and C++ (native applications), Java uses a programming framework calimmediateed Java Native Interface (JNI). JOGL uses this wislimterface internally to access OpenGL functions as shown in the folloearng diagram.

JNI

All the four methods of GLEventListener interface have the code (java JOGL methods) to call OpenGL functions internally. Naming of those JOGL methods is also similar to the naming conventions of OpenGL. If the function name in OpenGL is glBegin(), it is used as gl.glBegin().

Whenever the gl.glBegin() method of java JOGL is calimmediateed, it internally invokes the glBegin() method of OpenGL. This is the reason for installing native library files on the user system at the time of installing JOGL.

The Display() Method

This is an iminterfaceant method which holds the code for developing graphics. It
requires the GLAutoDrawable interface object as it’s parameter.

The display() method preliminaryly gets OpenGL context uperform the object of GL interface (GL inherit’s GLBase interface which contains methods to generate all OpenGL context objects). Since this tutorial is abaway JOGL2, permit us generate a GL2 object.

The folloearng code snippet shows how to generate a GL2 Object −

//Generating GL object
GL gl = drawable.getGL();
GL gl = drawable.getGL();

//Uperform this Getting the Gl2 Object
//this can be developd in a performle range like
final GL2 gl = drawable.getGL().getGL2();

Uperform the object of GL2 interface, one can access the members of this wislimterface,
which in turn provide access to OpenGL [1.0… 3.0] functions.

Draearng a Line

GL2 interface contains a huge list of methods but here 3 main iminterfaceant
methods are discussed namely glBegin(), glVertex(), and glEnd().

Sr.No. Methods and Description
1

glBegin()

This method starts the process of draearng a range. It gets preddish colouredegreatd string integer “GL_LINES” as a parameter, which is wislimherited from GL interface.

2

glVertex3f()/glVertex2f()

This method develops the vertex and we have to pbum coordinates as parameters 3f and 2f, which denote 3-dimensional floating stage coordinates and 2-dimensional floating stage coordinates respectively.

3

glEnd()

ends the range

Below given is the program to draws a basic range uperform JOGL −

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course Line implements GLEventListener{

   @Override
   public void display(GLAutoDrawable drawable) {
      final GL2 gl = drawable.getGL().getGL2();
            
      gl.glBegin (GL2.GL_LINES);//static field
      gl.glVertex3f(0.50f,-0.50f,0);
      gl.glVertex3f(-0.50f,0.50f,0);
      gl.glEnd();

   }
            
   @Override
   public void dispose(GLAutoDrawable arg0) {
      //method body
   }
            
   @Override
   public void init(GLAutoDrawable arg0) {
      // method body
   }
	
   public static void main(String[] args) {

      //getting the capabiliconnect ups object of GL2 profile        
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
   
      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      Line l = new Line();
      glcanvas.addGLEventListener(l);
      glcanvas.setSize(400, 400);
   
      //creating frame
      final JFrame frame = new JFrame ("straight Line");
   
      //adding canvas to frame
      frame.getContentPane().add(glcanvas);
                 
      frame.setSize(frame.getContentPane().getPreferreddish colouredSize());
      frame.setVisible(true);
      
   }//end of main
	
}//end of courseiminterface javax.media.opengl.GL2;

Line

JOGL – Draearng with GL Lines

In the Previous chapter we have belowstanded how draw a basic range uperform JOGL. We draw ranges simply by pasperform a preddish colouredegreatd field, Gl_ranges to glBegin() method.

This chapter provides examples to draw forms like triangle, rhombus and a house, uperform glBegin() method and GL_Lines.

Let us go through a program to draw a triangle uperform GL_LINES −

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course Triangle implements GLEventListener{

   @Override
   public void display(GLAutoDrawable drawable) {
      final GL2 gl = drawable.getGL().getGL2();
      gl.glBegin (GL2.GL_LINES);
   
      //draearng the base
      gl.glBegin (GL2.GL_LINES);
      gl.glVertex3f(-0.50f, -0.50f, 0);
      gl.glVertex3f(0.50f, -0.50f, 0);
      gl.glEnd();
   
      //draearng the appropriate advantage
      gl.glBegin (GL2.GL_LINES);
      gl.glVertex3f(0f, 0.50f, 0);
      gl.glVertex3f(-0.50f, -0.50f, 0);
      gl.glEnd();
   
      //draearng the lft advantage
      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 reform(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
      // method body
   }
   
   public static void main(String[] args) {
      
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
   
      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      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().getPreferreddish colouredSize());
      frame.setVisible(true);
      
   }//end of main
	
}//end of courseiminterface javax.media.opengl.GL2;

If you compile and execute the above program, the folloearng awayplace is generated.
It shows a triangle drawn uperform GL_LINES of glBegin() method.

Triangle

Let us go through a program to draw a rhombus uperform GL_LINES −

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course Rhombus implements GLEventListener{

   @Override
   public void display( GLAutoDrawable drawable ) {
      final GL2 gl = drawable.getGL().getGL2();
      
      //advantage1  
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( 0.0f,0.75f,0 );
      gl.glVertex3f( -0.75f,0f,0 );
      gl.glEnd();
      
      //advantage2
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( -0.75f,0f,0 );
      gl.glVertex3f( 0f,-0.75f, 0 );
      gl.glEnd();
      
      //advantage3
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( 0f,-0.75f, 0 );
      gl.glVertex3f( 0.75f,0f, 0 );
      gl.glEnd();
      
      //advantage4
      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 reform( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) {
      // method body
   }
	
   public static void main( String[] args ) {
	
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get( GLProfile.GL2 );
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
      
      // The canvas
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups );
      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().getPreferreddish colouredSize() );
      frame.setVisible( true );
   }
	
}

If you compile and execute the above program, you get the folloearng awayplace. It shows a rhombus generated uperform GL_LINES of glBegin() method.

Rhombus

Let us go through a program to draw a house uperform GL_LINES −

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course House implements GLEventListener{

   @Override
   public void display( GLAutoDrawable drawable ) {
      final GL2 gl = drawable.getGL().getGL2();
      
      //draearng top
      gl.glBegin ( GL2.GL_LINES );
      gl.glVertex3f( -0.3f, 0.3f, 0 );
      gl.glVertex3f( 0.3f,0.3f, 0 );
      gl.glEnd();
      
      //draearng underside
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( -0.3f,-0.3f, 0 );
      gl.glVertex3f( 0.3f,-0.3f, 0 );
      gl.glEnd();
      
      //draearng the appropriate advantage
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( -0.3f,0.3f, 0 );
      gl.glVertex3f( -0.3f,-0.3f, 0 );
      gl.glEnd();
      
      //draearng the left advantage
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( 0.3f,0.3f,0 );
      gl.glVertex3f( 0.3f,-0.3f,0 );
      gl.glEnd();
      
      //constructing roof
      //constructing lft dia
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( 0f,0.6f, 0 );
      gl.glVertex3f( -0.3f,0.3f, 0 );
      gl.glEnd();
      
      //constructing rt dia
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( 0f,0.6f, 0 );
      gl.glVertex3f( 0.3f,0.3f, 0 );
      gl.glEnd();
      
      //constructing door
      //draearng top
      gl.glBegin ( GL2.GL_LINES );
      gl.glVertex3f( -0.05f, 0.05f, 0 );
      gl.glVertex3f( 0.05f, 0.05f, 0 );
      gl.glEnd();
      
      //draearng the left advantage
      gl.glBegin ( GL2.GL_LINES );
      gl.glVertex3f( -0.05f, 0.05f, 0 );
      gl.glVertex3f( -0.05f, -0.3f, 0 );
      gl.glEnd();
      
      //draearng the appropriate advantage
      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 reform( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) {
      // method body
   }
	
   public static void main( String[] args ) {
   
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get( GLProfile.GL2 );
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
   
      // The canvas
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups );
      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().getPreferreddish colouredSize() );
      frame.setVisible( true );
      
   }//end of main
	
}//end of course

If you compile and execute the above program, you get the folloearng awayplace. It shows a house diagram generated uperform GL_LINES() method.

House

JOGL – Pre Degreatd Shapes

In the Previous chapters we have belowstanded how draw a forms such as range, triangle, rhombus uperform JOGL. We draw ranges simply by pasperform a preddish colouredegreatd field, Gl_ranges to glBegin() method.

Other than GL_LINES, the glBegin() method accepts eight more parameters. You can use them to draw various forms. These are used the exact same way as GL_LINES.

The folloearng table shows the glBegin() method parameters asizey with their own description −

Sr.No Parameters and Description
1

GL_LINES

Creates every pair of vertices as an independent range segment.

2

GL_LINE_STRIP

Draws a connected group of range segments from the preliminary vertex to the final.

3

GL_LINE_LOOP

Draws a connected group of range segments from the preliminary vertex to the final, again back to the preliminary.

4

GL_TRIANGLES

Treats every trippermit of vertices as an independent triangle.

5

GL_TRIANGLE_STRIP

Draws a connected group of triangles. One triangle is degreatd for every vertex presented after the preliminary 2 vertices.

6

GL_TRIANGLE_FAN

Draws a connected group of triangles. One triangle is degreatd for every vertex presented after the preliminary 2 vertices.

7

GL_QUADS

Treats every group of four vertices as an independent quadrilateral.

8

GL_QUAD_STRIP

Draws a connected group of quadrilaterals. One quadrilateral is degreatd for every pair of vertices presented after the preliminary pair.

9

GL_POLYGON

Draws a performle, convex polygon. Vertices 1,…,n degreat this polygon.

Let us see a few examples uperform glBegin() parameters.

Program to draw a Line Strip

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;

iminterface javax.searng.JFrame;

public course 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 reform(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
      // method body
   }
   
   public static void main(String[] args) {
   
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
      
      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      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().getPreferreddish colouredSize());
      frame.setVisible(true);
      
   }//end of main
	
}//end of courseiminterface javax.media.opengl.GL2;

If you compile and execute the above code, the folloearng awayplace is generated −

LineStrip

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 kind of of the basic template programs with the above code, compile, and execute it, the folloearng awayplace is generated −

Line Loop

Code snippet for display() method to draw a triangle uperform GL_TRIANGLES

public void display(GLAutoDrawable drawable) {

   final GL2 gl = drawable.getGL().getGL2();
   
   gl.glBegin(GL2.GL_TRIANGLES);        // Draearng Uperform 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 kind of of the basic template programs with the above code, compile, and execute it, the folloearng awayplace is generated −

Triangles

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 kind of of the basic template programs with the above code, compile and execute it, the folloearng awayplace is generated −

Triangle Strip

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 kind of of the basic template programs with the above code, compile, and execute it, the folloearng awayplace is generated −

Quads

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 kind of of the basic template programs with the above code, compile, and execute it, the folloearng awayplace is generated −

Polygon

JOGL – Transformation

OpenGL provides more features such as applying colours to an object, scaling, lighting, rotating an object, etc. This chapter describes a few of the transformations on objects uperform JOGL.

Moving an Object on the Window

In earare locatedr chapters, we discussed the programs for draearng a range and draearng various forms uperform easy ranges. The forms developd in this way can be displayed on any kind of location wislim the earndow. It is done uperform the method glTranslatef (float x, float y, float z).

This method besizeys to the GLMatrixFunc interface, which is wislim the javax.media.opengl.fixedfunc package.

GLMatrixFunc Interface

interface − GLMatrixFunc

package − javax.media.opengl.fixedfunc

The folloearng table lists a few iminterfaceant methods of this wislimterface −

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 dimension 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 stage specified simply by the parameters (x,y,z), pbumed 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 modifys the position of the component on the screen. Hence, the reform() method of GLEventListener interface need to be overridden and OpenGL seeinterface and projection matrix need to be preliminaryized.

The folloearng code shows the template to preliminaryize a see interface and projection matrix −

public void reform(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; 
       
   //practuallyting devided simply by 0 exception height = 1; 
   final float h = (float) width / (float) height; 
            
   // display area to cover the entire earndow 
   gl.glVieearnterface(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 see gl.glLoadIdentity(); 
   gl.glMatrixMode(GL2.GL_MODELVIEW); 
   gl.glLoadIdentity(); 
}

JOGL – Coloring

This chapter teveryes you how to apply colours to the objects uperform JOGL. To apply colour to an object, use the method glColor() of GL2. Below given is the syntax for uperform glColor method.

Syntax

gl.glColorXY(1f,0f,0f); 

where,

  • X denotes the number of colours used, 3 (reddish coloured, green, blue) or 4(reddish coloured, green, blue, alpha). To get various colour combinations, the values of these colours are pbumed as parameters. The sequence of the colour parameters must be maintained in thead wear order.

    Example

    If you pbum colour values as (1, 0, 0), then you get reddish coloured colour. Similarly, (1, 1, 0) gives you yellow colour.

  • Y denotes the data kind which accepts parameters such as simply byte(b), double(d), float(f), int(i), short(s), usimply byte(ub), uint(ui), and ushort(us).

gl.glColor3f(1f,0f,0f);   //gives us reddish coloured          
gl.glColor3f(0f,1f,0f);   //gives us green            
gl.glColor3f(0f,0f,1f);   //gives us blue

In case of triangle, you can apply various colours for every vertex.

Let us go through the program to apply colours to a triangle −

iminterface javax.media.opengl.GL2; 
iminterface javax.media.opengl.GLAutoDrawable; 
iminterface javax.media.opengl.GLCapabiliconnect ups; 
iminterface javax.media.opengl.GLEventListener; 
iminterface javax.media.opengl.GLProfile; 
iminterface javax.media.opengl.awt.GLCanvas; 

iminterface javax.searng.JFrame; 
 
public course TriangleColor implements GLEventListener { 

   @Override 
   public void display( GLAutoDrawable drawable ) { 
   
      final GL2 gl = drawable.getGL().getGL2(); 
      gl.glBegin( GL2.GL_TRIANGLES );  
      
      // Draearng Uperform 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 reform( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { 
      // method body 
   } 
   
   public static void main( String[] args ) {    
   
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get( GLProfile.GL2 ); 
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
          
      // The canvas  
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups ); 
      TriangleColor triangle = new TriangleColor(); 
      glcanvas.addGLEventListener( triangle ); 
      glcanvas.setSize( 400, 400 );   
      
      //creating frame 
      final JFrame frame = new JFrame (" Coloreddish coloured Triangle"); 
          
      //adding canvas to it 
      frame.getContentPane().add( glcanvas ); 
      frame.setSize( frame.getContentPane().getPreferreddish colouredSize()); 
      frame.setVisible( true );   
      
   } //end of main
	
} //end of course 

When you compile and execute the above program, you get the folloearng coloureddish coloured triangle −

Triangle Color

Applying Color to a Polygon

Let us go through the program to apply colours to a polygon −

iminterface javax.media.opengl.GL2; 
iminterface javax.media.opengl.GLAutoDrawable; 
iminterface javax.media.opengl.GLCapabiliconnect ups; 
iminterface javax.media.opengl.GLEventListener; 
iminterface javax.media.opengl.GLProfile; 
iminterface javax.media.opengl.awt.GLCanvas; 

iminterface javax.searng.JFrame; 

public course PolygonColor implements GLEventListener { 

   @Override 
   public void display( GLAutoDrawable drawable ) { 
   
      final GL2 gl = drawable.getGL().getGL2(); 
      gl.glColor3f( 1f,0f,0f ); //applying reddish coloured  
  
      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 reform( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) {    
      // method body 
   } 
   
   public static void main( String[] args ) { 
   
      //getting the capabiliconnect ups object of GL2 profile  
      final GLProfile profile = GLProfile.get( GLProfile.GL2 ); 
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile); 
      
      // The canvas  
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups ); 
      PolygonColor polygon = new PolygonColor(); 
      glcanvas.addGLEventListener( polygon ); 
      glcanvas.setSize( 400, 400 ); 
      
      //creating frame 
      final JFrame frame = new JFrame ( "Coloreddish coloured Polygon" ); 
      
      //adding canvas to frame 
      frame.getContentPane().add( glcanvas ); 
      frame.setSize(frame.getContentPane().getPreferreddish colouredSize() ); 
      frame.setVisible( true );    
      
   } //end of main 
	
 } //end of course 

When you compile and execute the above program, you get the folloearng coloureddish coloured Polygon −

Polygon Color

JOGL – Scaling

This chapter teveryes you how to dimension an object ie., incrreare locatedve or decrreare locatedve the dimension of an object uperform JOGL.

Scaling an object is done simply by uperform the glScalef(float x, float y, float z) method of GLMatrixFunc interface. This method accepts 3 floating stage parameters, uperform which we specify the dimension fbehaveors asizey the x, y, and z axes respectively.

For example, in the folloearng program, a triangle is diminished to 50%. Here, the value 50 is pbumed as parameter asizey all the axes.

Let us go through the program to dimension a triangle −

iminterface javax.media.opengl.GL2; 
iminterface javax.media.opengl.GLAutoDrawable; 
iminterface javax.media.opengl.GLCapabiliconnect ups; 
iminterface javax.media.opengl.GLEventListener; 
iminterface javax.media.opengl.GLProfile; 
iminterface javax.media.opengl.awt.GLCanvas; 

iminterface javax.searng.JFrame;
 
public course Scaling implements GLEventListener {
 
   @Override 
	
   public void display( GLAutoDrawable drawable ) { 
   
      final GL2 gl = drawable.getGL().getGL2(); 
      gl.glScalef( 0.50f,0.25f,0.50f ); 
      gl.glBegin( GL2.GL_TRIANGLES );
		
      // Draearng Uperform 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 );     // blue 
      gl.glVertex3f( -0.2f,-0.50f,0.0f ); // Bottom Left 
		
      gl.glColor3f( 0.0f,0.0f,1.0f );     // green 
      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 reform( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { 
      // method body 
   }
   
   public static void main( String[] args ) {   
   
      //getting the capabiliconnect ups object of GL2 profile 
      final GLProfile profile = GLProfile.get( GLProfile.GL2 ); 
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
        
      // The canvas  
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups ); 
      Scaling scaling = new Scaling(); 
      glcanvas.addGLEventListener( scaling ); 
      glcanvas.setSize( 400, 400 );  

      //creating frame 
      final JFrame frame  = new JFrame (" Dimnished Triangle (Scaling )");   

      //adding canvas to it 
      frame.getContentPane().add(glcanvas); 
      frame.setSize(frame.getContentPane().getPreferreddish colouredSize()); 
      frame.setVisible(true);         
      
   } //end of main 
	
} //end of courseiminterface javax.media.opengl.GL2; 

On compiling and executing the above program, we get the folloearng awayplace. Here, you can observe a diminished triangle as compareddish coloured to the authentic triangle produced simply by TriangleColor.java −

Scaling

JOGL – Rotation

In this chapter we exsimpleed you how to rotate an object uperform JOGL. Rotation of objects can be done asizey any kind of of the 3 axes, uperform the glRotatef(float angle, float x, float y, float z) method of GLMatrixFunc interface. You need to pbum an angle of rotation and x, y, z axes as parameters to this method.

The folloearng steps guide you to rotate an object successcompenableey −

  • Clear the colour buffer and depth buffer preliminaryly uperform gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT) method. This method erases the previous state of the object and develops the see clear.

  • Reset the projection matrix uperform the glLoadIdentity() method.

Instantiate the animator course and start the animator uperform the start() method.

FPSAnimator Clbum

Below given ar the various constructors of FPSAnimator course.

Sr.No. Methods and Descriptions
1

FPSAnimator(GLAutoDrawable drawable, int fps)

It develops an FPSAnimator with a given target frames-per-2nd value and an preliminary drawable to animate.

2

FPSAnimator(GLAutoDrawable drawable, int fps, boolean cheduleAtFixedRate)

It develops an FPSAnimator with a given target frames-per-2nd value, an preliminary drawable to animate, and a flag indicating whether to use fixed-rate scheduling.

3

FPSAnimator(int fps)

It develops an FPSAnimator with a given target frames-per-2nd value.

4 It develops an FPSAnimator with a given target frames-per-2nd value and a flag indicating whether to use fixed rate scheduling.

It develops an FPSAnimator with a given target frames-per-2nd value and a flag indicating whether to use fixed rate scheduling.

start() and practuallyt() are the 2 iminterfaceant methods in this course. The folloearng program shows how to rotate a triangle uperform FPSAnimator course −

iminterface javax.media.opengl.GL2; 
iminterface javax.media.opengl.GLAutoDrawable; 
iminterface javax.media.opengl.GLCapabiliconnect ups; 
iminterface javax.media.opengl.GLEventListener; 
iminterface javax.media.opengl.GLProfile; 
iminterface javax.media.opengl.awt.GLCanvas; 

iminterface javax.searng.JFrame; 

iminterface com.jogamp.opengl.util.FPSAnimator; 

public course TriangleRotation implements GLEventListener { 
   private float rtri;  //for angle of rotation
      
   @Override 
   public void display( GLAutoDrawable drawable ) {
   
      final GL2 gl = drawable.getGL().getGL2(); 
      gl.glClear (GL2.GL_COLOR_BUFFER_BIT |  GL2.GL_DEPTH_BUFFER_BIT );  
      
      // Clear The Screen And The Depth Buffer 
      gl.glLoadIdentity();  // Reset The View     
              
      //triangle rotation      
      gl.glRotatef( rtri, 0.0f, 1.0f, 0.0f );  
              
      // Draearng Uperform Triangles 
      gl.glBegin( GL2.GL_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 );     // blue 
      gl.glVertex3f( -0.2f,-0.50f,0.0f ); // Bottom Left 
      gl.glColor3f( 0.0f,0.0f,1.0f );     // green 
      gl.glVertex3f( 0.5f,-0.5f,0.0f );   // Bottom Right 
      
      gl.glEnd();    
      gl.glFlush(); 
      
      rtri +=0.2f;  //bumigning the angle               
   } 
	
   @Override 
   public void dispose( GLAutoDrawable arg0 ) { 
      //method body 
   } 
   
   @Override 
   public void init( GLAutoDrawable arg0 ) { 
      // method body 
   }
   
   @Override 
   public void reform( GLAutoDrawable drawable, int x, int y, int width, int height ) { 
   
      public static void main( String[] args ) {
		
         //getting the capabiliconnect ups object of GL2 profile
         final GLProfile profile  = GLProfile.get(GLProfile.GL2 ); 
         GLCapabiliconnect ups capabiliconnect ups  = new GLCapabiliconnect ups( profile );

         // The canvas  
         final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups); 
         TriangleRotation triangle = new TriangleRotation(); 
         glcanvas.addGLEventListener( triangle ); 
         glcanvas.setSize( 400, 400 );  

         // creating frame 
         final JFrame frame = new JFrame ("Rotating Triangle");

         // adding canvas to it 
         frame.getContentPane().add( glcanvas ); 
         frame.setSize(frame.getContentPane() .getPreferreddish colouredSize());                 
         frame.setVisible( true ); 
                
         //Instantiating and Initiating Animator 
         final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true); 
         animator.start(); 
      }
		
   } //end of main
	
} //end of course 

If you compile and execute the above program, it generates the folloearng awayplace. Here, you can observe various snapshots of a rotating the coloureddish coloured triangle around the x-axis.

Triangle Rotation

JOGL – Lighting

This chapter exsimples you how to apply lighting effect to an object uperform JOGL.

To set lighting, preliminaryly enable lighting uperform the glEnable() method. Then apply lighting for the objects, uperform the glLightfv(int light, int pname, float[] params, int params_awayset) method of GLLightingFunc interface. This method gets four parameters.

The folloearng table describes the parameters of gllightfv() method.

Sr.No. Parameter Name and Description
1

Light

Specifies a light. The number of lights depends on the implementation, but at minimum eight lights are supinterfaceed. It accepts ten values, those parameters are discussed in a separate table named Light Source Parameters given below.

2

Pname

Specifies a performle valued light source parameter. For light source, there are ten parameters as discussed below.

3

Params

Specifies a stageer to the value or values thead wear is set to the parameter pname of light source light.

4

Light source parameter

You can use any kind of of the light source parameters given below.

Light source parameters

Sr.No. Parameter and Description
1

GL_AMBIENT

It contains the parameters thead wear specify the ambient intensity of the light.

2

GL_DIFFUSE

It contains the parameters thead wear specify the diffuse intensity of the light.

3

GL_SPECULAR

It contains the parameters thead wear specify the specular intensity of the light.

4

GL_POSITION

It contains four integer or floating-stage values thead wear specify the position of the light in homogeneous object coordinates.

5

GL_SPOT_DIRECTION

It contains parameters thead wear specify the immediateion of light in homogeneous object coordinates.

6

GL_SPOT_EXPONENT

It’s parameters specify the intensity distribution of light.

7

GL_SPOT_CUTOFF

The performle parameter of this specifies the maximum spread angle of the light.

8

GL_CONSTANT_ATTENUATION or GL_LINEAR_ATTENUATION or GL_QUADRATIC_ATTENUATION

You can use any kind of of these attenuation fbehaveors, which is represented simply by a performle value.

Lighting is enabimmediateed or disabimmediateed uperform glEnable() and glDisable () methods with the argument GL_LIGHTING.

The folloearng template is given for lighting −

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 };  // multicolour diffuse 
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuseLight, 0); 

Applying Light to a Rotating Polygon

Follow the given steps for applying light to a rotating polygon.

Rotate the polygon uperform 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 light to a rotating polygon −

iminterface javax.media.opengl.GL2; 
iminterface javax.media.opengl.GLAutoDrawable; 
iminterface javax.media.opengl.GLCapabiliconnect ups; 
iminterface javax.media.opengl.GLEventListener; 
iminterface javax.media.opengl.GLProfile; 
iminterface javax.media.opengl.awt.GLCanvas; 

iminterface javax.searng.JFrame; 

iminterface com.jogamp.opengl.util.FPSAnimator; 
 
public course PolygonLighting implements GLEventListener { 
   private float rpoly;
	
   @Override 
	
   public void display( GLAutoDrawable drawable ) {
   
      final GL2 gl = drawable.getGL().getGL2(); 
      gl.glColor3f(1f,0f,0f); //applying reddish coloured
      
      // 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;  //bumigning 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);  

      // multicolour diffuse 
      float[] diffuseLight = { 1f,2f,1f,0f };  
      gl.glLightfv( GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse-Light, 0 ); 
   }  
      
   @Override 
   public void dispose( GLAutoDrawable arg0 ) { 
      //method body  
   } 
  
   @Override 
   public void init( GLAutoDrawable arg0 ) { 
      // method body     
   } 
	
   @Override 
   public void reform( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { 
      // method body 
   } 
	
   public static void main( String[] args ) { 
   
      //getting the capabiliconnect ups object of GL2 profile 
      final GLProfile profile = GLProfile.get( GLProfile.GL2 ); 
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups( profile);

      // The canvas  
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups ); 
      PolygonLighting polygonlighting = new PolygonLighting(); 
      glcanvas.addGLEventListener( polygonlighting ); 
      glcanvas.setSize( 400, 400 ); 

      //creating frame 
      final JFrame frame = new JFrame (" Polygon lighting ");  

      //adding canvas to it 
      frame.getContentPane().add( glcanvas ); 
      frame.setSize( frame.getContentPane().getPreferreddish colouredSize()); 
      frame.setVisible( true );  
                    
      //Instantiating and Initiating Animator 
      final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true ); 
      animator.start();                     
      
   } //end of main 
	
} //end of course 

If you compile and execute the above program, it generates the folloearng awayplace. Here, you can observe various snapshots of a rotating polygon with lighting.

Polygon Lighting

JOGL – 3D Basics

In previous chapters we have seen how to develop 2d objects, apply effects to it, and transform the object. This chapter teveryes you how to draw a range with 3rd dimension, and a few forms.

Let us draw a easy range with z-axis and see the difference between 2D and 3D ranges. Draw a easy range preliminary, then draw the 2nd range 3 devices into the earndow.

Let us go through the program to draw a 3D range −

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;
iminterface javax.media.opengl.glu.GLU;

iminterface javax.searng.JFrame;
   
public course Line3d implements GLEventListener {
   private GLU glu = new GLU();
	
   @Override
   
   public void display( GLAutoDrawable drawable ) {
      final GL2 gl = drawable.getGL().getGL2();
      gl.glTranslatef( 0f, 0f, -2.5f );
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( -0.75f,0f,0 );
      gl.glVertex3f( 0f,-0.75f, 0 );
      gl.glEnd();
      
      //3d range
      gl.glBegin( GL2.GL_LINES );
      gl.glVertex3f( -0.75f,0f,3f );// 3 devices into the earndow
      gl.glVertex3f( 0f,-0.75f,3f );
      gl.glEnd();
   }
   
   @Override
   public void dispose( GLAutoDrawable arg0 ) {
      //method body
   }
   
   @Override
   public void init( GLAutoDrawable arg0 ) {
      // method body
   }
   
   @Override
   public void reform( GLAutoDrawable drawable, int x, int y, int width, int height ) {
	
      GL2 gl = drawable.getGL().getGL2();
      
      if( height <= 0 )
         height = 1;
			
      final float h = ( float ) width / ( float ) height;
      gl.glVieearnterface( 0, 0, width, height );
      gl.glMatrixMode( GL2.GL_PROJECTION );
      gl.glLoadIdentity();
		
      glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
      gl.glMatrixMode( GL2.GL_MODELVIEW );
      gl.glLoadIdentity();
   }
   
   public static void main( String[] args ) {
	
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get( GLProfile.GL2 );
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
          
      // The canvas
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups );
      Line3d range3d = new Line3d();
      glcanvas.addGLEventListener( range3d );
      glcanvas.setSize( 400, 400 );
       
      //creating frame
      final JFrame frame = new JFrame (" 3d range");
          
      //adding canvas to it
      frame.getContentPane().add( glcanvas );
      frame.setSize(frame.getContentPane().getPreferreddish colouredSize() );
      frame.setVisible( true );
   }//end of main
	
}//end of course

When you compile and execute the above program, the folloearng awayplace is
generated −

3D Line

3D forms can be drawn simply by giving non-zero values to z quadrant of the glVertex3f() method, which generates the above see. Now joining the remaining ranges will lead to a 3D advantage.

Now in the exact same way permit us develop an advantage with 3rd dimension.

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;
iminterface javax.media.opengl.glu.GLU;

iminterface javax.searng.JFrame;

public course Edge1 implements GLEventListener {
   private GLU glu = new GLU();
	
   @Override
   public void display(GLAutoDrawable drawable) {
   
      // TODO Auto-generated method stub
      final GL2 gl = drawable.getGL().getGL2();
      gl.glTranslatef(0f, 0f, -2.5f);
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glEnd();

      //3d range
      gl.glBegin(GL2.GL_LINES);

      //3 devices in to the earndow
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();

      //top
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();

      //underside
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();
   }

   @Override
   public void dispose(GLAutoDrawable arg0) {
      //method body
   }
	
   @Override
   public void init(GLAutoDrawable arg0) {
      // method body
   }
   
   @Override
   public void reform(GLAutoDrawable drawable, int x, int y, int width, int height) {
	
      // TODO Auto-generated method stubfinal
      GL2 gl = drawable.getGL().getGL2();
      if(height <= 0)
         height = 1;
			
      final float h = (float) width / (float) height;
      gl.glVieearnterface(0, 0, width, height);
      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
		
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL2.GL_MODELVIEW);
      gl.glLoadIdentity();

   }
   
   public static void main(String[] args) {
   
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);

      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      Edge1 b = new Edge1();
      glcanvas.addGLEventListener(b);
      glcanvas.setSize(400, 400);

      //creating frame
      final JFrame frame = new JFrame (" 3d advantage");

      //adding canvas to it
      frame.getContentPane().add(glcanvas);
      frame.setSize(frame.getContentPane().getPreferreddish colouredSize());
      frame.setVisible(true);
   }//end of main
	
}//end of course

When you compile and execute the above program, the folloearng awayplace is generated −

3D Edge

In the exact same way, simply by developing 3D advantages to corresponding aspects of any kind of 2D quadrilateral and joining the adjacent vertices, you can get a 3D quadrilateral.

Below given is a program to draw a rhombus uperform JOGL.

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;
iminterface javax.media.opengl.glu.GLU;

iminterface javax.searng.JFrame;

public course Rhombus implements GLEventListener {
   private GLU glu = new GLU();
    
   @Override
   public void display(GLAutoDrawable drawable) {
	
      final GL2 gl = drawable.getGL().getGL2();
      gl.glTranslatef(0f, 0f, -2.5f);

      //draearng advantage1.....
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,3f); // 3 devices into the earndow
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();

      //top
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();

      // underside
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0f,-0.75f,3f);
      gl.glEnd();
   
      // advantage 2....
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 3f);
      gl.glVertex3f(0.75f,0f, 3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0f,-0.75f, 0);
      gl.glVertex3f(0f,-0.75f, 3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glVertex3f(0.75f,0f, 3f);
      gl.glEnd();

      //Edge 3.............
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(-0.75f,0f,0);
      gl.glVertex3f(-0.75f,0f,3f);
      gl.glEnd();

      //final advantage
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f,3f);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f(0.75f,0f, 0);
      gl.glVertex3f(0.75f,0f,3f);
      gl.glEnd();
		
      gl.glBegin(GL2.GL_LINES);
      gl.glVertex3f( 0.0f,0.75f,0);
      gl.glVertex3f( 0.0f,0.75f,3f);
      gl.glEnd();
   }
   
   @Override
   public void dispose(GLAutoDrawable arg0) {
      //method body
   }
	
   @Override
   public void init(GLAutoDrawable arg0) {
      // method body
   }
	
   @Override
   public void reform(GLAutoDrawable drawable, int x, int y, int width, int height) {
   
      // TODO Auto-generated method stub final
      GL2 gl = drawable.getGL().getGL2();
      if(height lt;= 0)
         height = 1;
			
      final float h = (float) width / (float) height;
      gl.glVieearnterface(3, 6, width, height);
      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
		
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL2.GL_MODELVIEW);
      gl.glLoadIdentity();
   }
   
   public static void main(String[] args) {
	
      //getting the capabiliconnect ups object of GL2 profile
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);

      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      Rhombus b = new Rhombus();
      glcanvas.addGLEventListener(b);
      glcanvas.setSize(400, 400);

      //creating frame
      final JFrame frame = new JFrame (" Rhombus 3d");

      //adding canvas to it
      frame.getContentPane().add(glcanvas);
      frame.setSize(frame.getContentPane().getPreferreddish colouredSize());
      frame.setVisible(true);
   }//end of main
	
}//end of courseiminterface javax.media.opengl.GL2;

When you compile and execute the above program, the folloearng awayplace is
generated. It shows a rhombus drawn uperform 3D ranges.

Rhombus 3D

The preddish colouredegreatd parameters of glBegin() method can be used for draearng 3D forms.

JOGL – 3D Triangle

In previous chapter we have seen how to draw 3d forms, this chapter teveryes you how to draw 3d triangle and rotate it.

Below given is the program to draw a 3d triangle an rotate it.

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;
iminterface javax.media.opengl.glu.GLU;

iminterface javax.searng.JFrame;

iminterface com.jogamp.opengl.util.FPSAnimator;

public course Triangle3d implements GLEventListener {

   private GLU glu = new GLU();
   private float rtri = 0.0f;
      
   @Override
   public void display(GLAutoDrawable drawable) {
      final GL2 gl = drawable.getGL().getGL2();

      // 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.glTranslatef( -0.5f, 0.0f, -6.0f ); // Move the triangle
      gl.glRotatef( rtri, 0.0f, 1.0f, 0.0f );
      gl.glBegin( GL2.GL_TRIANGLES ); 
        
      //draearng triangle in all dimensions
      // Front
      gl.glColor3f( 1.0f, 0.0f, 0.0f ); // Red
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top Of Triangle (Front)
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
      gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Left Of Triangle (Front)
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
      gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Right Of Triangle (Front)
        
      // Right
      gl.glColor3f( 1.0f, 0.0f, 0.0f ); // Red
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top Of Triangle (Right)
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
      gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Left Of Triangle (Right)
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
      gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Right Of Triangle (Right)
        
      // Left
      gl.glColor3f( 1.0f, 0.0f, 0.0f ); // Red
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top Of Triangle (Back)
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
      gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Left Of Triangle (Back)
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
      gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Right Of Triangle (Back)
        
      //left
      gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Red
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top Of Triangle (Left)
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
      gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Left Of Triangle (Left)
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
      gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Right Of Triangle (Left)
		
      gl.glEnd(); // Done Draearng 3d triangle (Pyramid)
      gl.glFlush();
      rtri += 0.2f;
   }
   
   @Override
   public void dispose( GLAutoDrawable drawable ) {
      //method body
   }
   
   @Override
   public void init( GLAutoDrawable drawable ) {
      //method body
   }
   
   @Override
   public void reform( GLAutoDrawable drawable, int x, int y, int width, int height ) {
	
      // TODO Auto-generated method stub
      final GL2 gl = drawable.getGL().getGL2();
      if(height lt;=;)
         height = 1;
			
      final float h = ( float ) width / ( float ) height;
      gl.glVieearnterface( 0, 0, width, height );
      gl.glMatrixMode( GL2.GL_PROJECTION );
      gl.glLoadIdentity();
		
      glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
      gl.glMatrixMode( GL2.GL_MODELVIEW );
      gl.glLoadIdentity();
   }
   
   public static void main( String[] args ) {
   
      // TODO Auto-generated method stub
      final GLProfile profile = GLProfile.get( GLProfile.GL2 );
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups( profile );
         
      // The canvas
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups );
      Triangle3d triangle = new Triangle3d();
		
      glcanvas.addGLEventListener( triangle );
      glcanvas.setSize( 400, 400 );
		
      final JFrame frame = new JFrame ( "3d Triangle (shpermit)" );
		
      frame.getContentPane().add( glcanvas );
      frame.setSize( frame.getContentPane().getPreferreddish colouredSize() );
      frame.setVisible( true );
		
      final FPSAnimator animator = new FPSAnimator(glcanvas,300,true);
      animator.start();
   }
	
}

When you compile and execute the above program, the folloearng awayplace is generated. Here, you have the snapshots of rotating 3D triangle. Since this program does not includes depth check, the triangle is generated hollow.

Triangle 3D

To develop the triangle strong, you need to enable depth check simply by uperform glEnable(GL_DEPTH_TEST). Enabling the depth buffer gives you a blank
screen. This can be cleareddish coloured simply by clearing the colour uperform glClear(GL_COLOR_BUFFERBIT | GL_DEPTH_BUFFER_BIT) method. To enable depth check in the init() method or in the glDisplay() method, write the folloearng code −

public void init(GLAutoDrawable drawable) {
   final GL2 gl = drawable.getGL().getGL2();
	
   gl.glShadeModel(GL2.GL_SMOOTH);
   gl.glClearColor(0f, 0f, 0f, 0f);
   gl.glClearDepth(1.0f);
   gl.glEnable(GL2.GL_DEPTH_TEST);
   gl.glDepthFunc(GL2.GL_LEQUAL);
   gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
 }

Below given is the Program to draw a 3D triangle with depth check.

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;
iminterface javax.media.opengl.glu.GLU;

iminterface javax.searng.JFrame;

iminterface com.jogamp.opengl.util.FPSAnimator;

public course Triangimmediateedepthcheck implements GLEventListener {

   private GLU glu = new GLU();
   private float rtri = 0.0f; 
	
   @Override
   public void display( GLAutoDrawable drawable ) {
	
      final GL2 gl = drawable.getGL().getGL2();
		
      gl.glShadeModel( GL2.GL_SMOOTH );
      gl.glClearColor( 0f, 0f, 0f, 0f );
      gl.glClearDepth( 1.0f );
      gl.glEnable( GL2.GL_DEPTH_TEST );
      gl.glDepthFunc( GL2.GL_LEQUAL );
      gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);

      // 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.glTranslatef( -0.5f,0.0f,-6.0f ); // Move the triangle
      gl.glRotatef( rtri, 0.0f, 1.0f, 0.0f );
      gl.glBegin( GL2.GL_TRIANGLES ); 

      //draearng triangle in all dimensions
      //front part
      gl.glColor3f( 1.0f, 0.0f, 0.0f ); // Red
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f ); // Green
      gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Left
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
      gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Right)

      //appropriate
      gl.glColor3f( 1.0f, 0.0f, 0.0f );
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f );
      gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Left
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f );
      gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Right

      //left
      gl.glColor3f( 1.0f, 0.0f, 0.0f );
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f );
      gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Left 
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f );
      gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Right 

      //top
      gl.glColor3f( 0.0f, 1.0f, 0.0f );
      gl.glVertex3f( 1.0f, 2.0f, 0.0f ); // Top
		
      gl.glColor3f( 0.0f, 0.0f, 1.0f );
      gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Left
		
      gl.glColor3f( 0.0f, 1.0f, 0.0f );
      gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Right
		
      gl.glEnd(); // Done Draearng 3d triangle (Pyramid)

      gl.glFlush();
      rtri += 0.2f;
   }
      
   @Override
   public void dispose( GLAutoDrawable drawable ) {
   }
   
   @Override
   public void init( GLAutoDrawable drawable ) {
	
      final GL2 gl = drawable.getGL().getGL2();
		
      gl.glShadeModel( GL2.GL_SMOOTH );
      gl.glClearColor( 0f, 0f, 0f, 0f );
      gl.glClearDepth( 1.0f );
      gl.glEnable( GL2.GL_DEPTH_TEST );
      gl.glDepthFunc( GL2.GL_LEQUAL );
      gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST );
   }
   
   @Override
   public void reform(GLAutoDrawable drawable, int x, int y, int width, int height ) {
	
      // TODO Auto-generated method stub
      final GL2 gl = drawable.getGL().getGL2();
      if( height <= 0 ) 
         height = 1;
			
      final float h = ( float ) width / ( float ) height;
      gl.glVieearnterface( 0, 0, width, height );
      gl.glMatrixMode( GL2.GL_PROJECTION );
      gl.glLoadIdentity();
		
      glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
      gl.glMatrixMode( GL2.GL_MODELVIEW );
      gl.glLoadIdentity();
   }
      
   public static void main( String[] args ) {
	
      // TODO Auto-generated method stub
      final GLProfile profile = GLProfile.get( GLProfile.GL2 );
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups( profile );
		
      // The canvas
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups );
      Triangimmediateedepthcheck triangimmediateedepthcheck = new Triangimmediateedepthcheck();
		
      glcanvas.addGLEventListener( triangimmediateedepthcheck );
      glcanvas.setSize( 400, 400 );
		
      final JFrame frame = new JFrame ( "3d Triangle (strong)" );
      frame.getContentPane().add(glcanvas);
      frame.setSize( frame.getContentPane().getPreferreddish colouredSize() );
      frame.setVisible( true );
      final FPSAnimator animator = new FPSAnimator( glcanvas, 300,true);
		
      animator.start();
   }
	
}

When you compile and execute the above program, the folloearng awayplace is generated.

Here, you can see the snapshots of a rotating 3D triangle. Since this program includes code for depth check, the triangle is generated strong.

Triangle Depth Test

JOGL – 3D Cube

In the previous chapters we have seen how to draw 3d triangle and rotate it. Now in this chapter you can belowstand how to a 3d cube, how to rotate it, how to attach an image on it. In the exact same way, This chapter provides examples to draw a 3D cube and apply colours to it and attach image to it.

Below given is the program to draw a 3d cube and apply colours to it.

iminterface java.awt.DisplayMode;
iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;
iminterface javax.media.opengl.glu.GLU;

iminterface javax.searng.JFrame;

iminterface com.jogamp.opengl.util.FPSAnimator;

public course Cube implements GLEventListener {

   public static DisplayMode dm, dm_old;
   private GLU glu = new GLU();
   private float rquad = 0.0f;
      
   @Override
   public void display( GLAutoDrawable drawable ) {
	
      final GL2 gl = drawable.getGL().getGL2();
      gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
      gl.glLoadIdentity();
      gl.glTranslatef( 0f, 0f, -5.0f ); 

      // Rotate The Cube On X, Y & Z
      gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f); 
 
      //giving various colours to various aspects
      gl.glBegin(GL2.GL_QUADS); // Start Draearng The Cube
      gl.glColor3f(1f,0f,0f); //reddish coloured colour
      gl.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
      gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
      gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top)
      gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom Right Of The Quad (Top)
		
      gl.glColor3f( 0f,1f,0f ); //green colour
      gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Top Right Of The Quad
      gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Top Left Of The Quad
      gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
      gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad 

      gl.glColor3f( 0f,0f,1f ); //blue colour
      gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Front)
      gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front)
      gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad
      gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad 

      gl.glColor3f( 1f,1f,0f ); //yellow (reddish coloured + green)
      gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
      gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
      gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back)
      gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Back)

      gl.glColor3f( 1f,0f,1f ); //purple (reddish coloured + green)
      gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Left)
      gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Left)
      gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
      gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad 

      gl.glColor3f( 0f,1f, 1f ); //sky blue (blue +green)
      gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right)
      gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Left Of The Quad
      gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad
      gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
      gl.glEnd(); // Done Draearng The Quad
      gl.glFlush();
      rquad -= 0.15f;
   }
   
   @Override
   public void dispose( GLAutoDrawable drawable ) {
      // TODO Auto-generated method stub
   }
   
   @Override
   public void init( GLAutoDrawable drawable ) {
	
      final GL2 gl = drawable.getGL().getGL2();
      gl.glShadeModel( GL2.GL_SMOOTH );
      gl.glClearColor( 0f, 0f, 0f, 0f );
      gl.glClearDepth( 1.0f );
      gl.glEnable( GL2.GL_DEPTH_TEST );
      gl.glDepthFunc( GL2.GL_LEQUAL );
      gl.glHint( GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST );
   }
      
   @Override
   public void reform( GLAutoDrawable drawable, int x, int y, int width, int height ) {
	
      // TODO Auto-generated method stub
      final GL2 gl = drawable.getGL().getGL2();
      if( height lt;= 0 )
         height = 1;
			
      final float h = ( float ) width / ( float ) height;
      gl.glVieearnterface( 0, 0, width, height );
      gl.glMatrixMode( GL2.GL_PROJECTION );
      gl.glLoadIdentity();
		
      glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
      gl.glMatrixMode( GL2.GL_MODELVIEW );
      gl.glLoadIdentity();
   }
      
   public static void main( String[] args ) {
	
      final GLProfile profile = GLProfile.get( GLProfile.GL2 );
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups( profile );
      
      // The canvas
      final GLCanvas glcanvas = new GLCanvas( capabiliconnect ups );
      Cube cube = new Cube();
		
      glcanvas.addGLEventListener( cube );
      glcanvas.setSize( 400, 400 );
		
      final JFrame frame = new JFrame ( " Multicoloureddish coloured cube" );
      frame.getContentPane().add( glcanvas );
      frame.setSize( frame.getContentPane().getPreferreddish colouredSize() );
      frame.setVisible( true );
      final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true);
		
      animator.start();
   }
	
}

When you compile and execute the above program, the folloearng awayplace is
generated. It shows a coloureddish coloured 3D cube.

Multicoloureddish coloured Cube

Applying Texture to the Cube

The folloearng steps are given to apply texture to a cube −

  • You can bind requireddish coloured texture to the cube uperform the
    gl.glBindTexture(GL2.GL_TEXTURE_2D.texture) method of the Drawable interface.

  • This method requires texture (int) argument asizey with GL2.GL_TEXTURE_2D(int).

  • Before you execute Display(), you need to develop texture variable

  • In the init() method or in the starting ranges of glDisplay() method, enable the texture uperform gl.glEnable(GL2.GL_TEXTURE_2D) method.

  • Create the texture object, which needs a file object as a parameter, which
    in turn needs the rawaye of the image used as the texture to the object.

File file = new File(“c:\pictures\boy.jpg”);
Texture t = textureIO.newTexture(file, true);
texture = t.getTextureObject(gl);
  • Handle the ‘file not found’ exception

Below given is the program to attach image on a cube.

iminterface java.awt.DisplayMode;

iminterface java.io.File;
iminterface java.io.IOException;

iminterface javax.media.opengl.GL2;
iminterface javax.media.opengl.GLAutoDrawable;
iminterface javax.media.opengl.GLCapabiliconnect ups;
iminterface javax.media.opengl.GLEventListener;
iminterface javax.media.opengl.GLProfile;
iminterface javax.media.opengl.awt.GLCanvas;
iminterface javax.media.opengl.glu.GLU;

iminterface javax.searng.JFrame;

iminterface com.jogamp.opengl.util.FPSAnimator;
iminterface com.jogamp.opengl.util.texture.Texture;
iminterface com.jogamp.opengl.util.texture.TextureIO;

public course CubeTexture implements GLEventListener {

   public static DisplayMode dm, dm_old;
   private GLU glu = new GLU();
   private float xrot,yrot,zrot;
   private int texture;
   
   @Override
   public void display(GLAutoDrawable drawable) {
   
      // TODO Auto-generated method stub
      final GL2 gl = drawable.getGL().getGL2();
      gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity(); // Reset The View
      gl.glTranslatef(0f, 0f, -5.0f);
		
      gl.glRotatef(xrot, 1.0f, 1.0f, 1.0f);
      gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
      gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
		
      gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
      gl.glBegin(GL2.GL_QUADS);

      // Front Face
      gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
      gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
      gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
      gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);

      // Back Face
      gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
      gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
      gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
      gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);

      // Top Face
      gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
      gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
      gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
      gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);

      // Bottom Face
      gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
      gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
      gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
      gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);

      // Right face
      gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
      gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
      gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
      gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);

      // Left Face
      gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
      gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
      gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
      gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
      gl.glEnd();
      gl.glFlush();

      //modify the speeds here
      xrot += .1f;
      yrot += .1f;
      zrot += .1f;
   }
   
   @Override
   public void dispose(GLAutoDrawable drawable) {
      // method body
   }
   
   @Override
   public void init(GLAutoDrawable drawable) {
	
      final GL2 gl = drawable.getGL().getGL2();
		
      gl.glShadeModel(GL2.GL_SMOOTH);
      gl.glClearColor(0f, 0f, 0f, 0f);
      gl.glClearDepth(1.0f);
      gl.glEnable(GL2.GL_DEPTH_TEST);
      gl.glDepthFunc(GL2.GL_LEQUAL);
      gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
      
      //
      gl.glEnable(GL2.GL_TEXTURE_2D);
      attempt{
		
         File im = new File("E:\awayice\boy.jpg ");
         Texture t = TextureIO.newTexture(im, true);
         texture= t.getTextureObject(gl);
          
      }catch(IOException e){
         e.printStackTrace();
      }
   }
      
   @Override
   public void reform(GLAutoDrawable drawable, int x, int y, int width, int height) {
   
      // TODO Auto-generated method stub
      final GL2 gl = drawable.getGL().getGL2();
      if(height lt;= 0)
         height = 1;
			
      final float h = (float) width / (float) height;
      gl.glVieearnterface(0, 0, width, height);
      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
		
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL2.GL_MODELVIEW);
      gl.glLoadIdentity();
   }
   
   public static void main(String[] args) {
   
      // TODO Auto-generated method stub
      final GLProfile profile = GLProfile.get(GLProfile.GL2);
      GLCapabiliconnect ups capabiliconnect ups = new GLCapabiliconnect ups(profile);
      
      // The canvas
      final GLCanvas glcanvas = new GLCanvas(capabiliconnect ups);
      CubeTexture r = new CubeTexture();
		
      glcanvas.addGLEventListener(r);
      glcanvas.setSize(400, 400);
		
      final JFrame frame = new JFrame (" Textureddish coloured Cube");
      frame.getContentPane().add(glcanvas);
      frame.setSize(frame.getContentPane().getPreferreddish colouredSize());
      frame.setVisible(true);
      final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);
		
      animator.start();
   }
	
}

When you compile and execute the above program, the folloearng awayplace is generated. You can see a 3D cube with desireddish coloured texture appare locatedd on it.

Texture Cube

JOGL – Appendix

GPU − Graphical procesperform device, it is a special electronic device thead wear accelerates the rendering of images.

JNI − Java Native Interface. Uperform which, java access native methods.

Model − They are the objects constructed from basic graphics primitives such as stages, ranges and polygons.

Pixel − Smallest device of display seen on the screen.

Projection − The method of mapping the coordinates of an object to a 2-dimensional plane is calimmediateed projection.

Projection matrix − It is a rangear transformation of an object on the 2D surface.

Rendering − A process simply by which complaceer develops images from models.

Vieearnterface − A seeinterface is a seeing region on the screen in complaceer graphics.

SHARE
Previous articleMagento
Next articleCOBOL

NO COMMENTS

LEAVE A REPLY