Opengl for java tutorial

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

OpenGL tutorial by Joey De Vries ported to Java

License

Naitsirc98/LearnOpenGL-LWJGL3

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This is a Java adaptation of the great tutorial by Joey De Vries, available at his website.

All the examples are completely written in Java, using LWJGL3, library which provides bindings to GLFW and OpenGL functions, the possibility of natively allocating/freeing resources, debug options and much more.

Thus, the code is very similar to the original in C++. However, I have also modified some parts of the original code to make it more efficient, better-looking, and to follow the Java coding style, always keeping the same results as in the original examples.

About

OpenGL tutorial by Joey De Vries ported to Java

Источник

Getting started with JOGL (OpenGL bindings for Java) in Eclipse

Recently, a reader of my OpenGL 101 series emailed me about how to get started with OpenGL in Java. More specifically, he was interested in JOGL the Java bindings for OpenGL. I thought his question was general enough to write a small post about creating a Java OpenGL getting started project.

JOGL is a good fit for a Java programmer that wants to learn OpenGL, it is particularly useful if you follow some OpenGL intro book or article. If you are more interested in writing Java games, you should check a more game friendly library like LWJGL or, even better, libGDX.

Читайте также:  Python levenshtein не устанавливается

In the first four sections of this tutorial I will show you how to install Java and Eclipse on your computer, feel free to skip forward if you are an experienced Java developer. The fifth section shows you how to configure JOGL as a user defined library in *Eclipse. The last section of the tutorial will exemplify how to create an OpenGL context and attach it to a window.

If you know how to do some to these steps, you can use the next table of contents to go to the sections that present interest for you:

First thing that you need is to install JDK, the Java Development Kit, from Oracle. Use the above link to download the appropriate version of JDK for your machine:

Java SE

After you confirm that you accept the license agreement for JDK, you can select which version of Java you need, for this tutorial I will use Java SE 8 64 bits for Windows (you can follow along on OS X, the procedure is essentially the same):

Java SE 8 64 bits Windows

Once the download is completed, start the installer and accept all the default options.

It is advisable, but optional for the purposes of this tutorial, to create an environment variable named JAVA_HOME, where you store the location of the JDK. If you don’t want to setup JAVA_HOME, you can jump to the next section.

On Windows 8.1, right click on the Windows button and select System, after that chose Advanced system settings, you should now have the System Properties window open:

Windows System Properties

On older Windows systems, right click My Computer and select Properties, after that you should see the System Properties window, select the Advanced menu.

Select the Environment Variables button in the above window, this should open the Environment Variables editor:

Windows Environment Variables

Press New in the upper side of the above window, this will let you create a New User Variable, you should write JAVA_HOME in the name text box and the path to your JDK installation in the value text box, in my case this was C:\Program Files\Java\jdk1.8.0_25, be sure to replace this with the JDK path for your machine:

Windows JAVA_HOME Environment Variables

Repeat the above procedure and create a new environment variable named PATH, or, if this is already present, select Edit instead of New. For the value add %JAVA_HOME\bin% or ;%JAVA_HOME\bin% if the PATH variable was already defined:

Windows PATH Environment Variables

On OS X, you can define the JAVA_HOME environment variable by simply adding the next line at the end of your .bash_profile file:

Eclipse Luna for Java Developers installer

just ignore for now, the error about the play method from the Game class reported by Eclipse, we will correct this in few moments.

In order to create a Java window for our application, we will extend the JFrame class from Swing. We can access the OpenGL functionality from JOGL by implementing the GLEventListener interface.

Читайте также:  Python work with html

Open Game.java and copy the next piece of code:

  1 import com.jogamp.opengl.GLAutoDrawable; 2 import com.jogamp.opengl.GLEventListener; 3 import javax.swing.JFrame; 4 5 public class Game extends JFrame implements GLEventListener   6 private static final long serialVersionUID = 1L; 7 8 @Override 9 public void display(GLAutoDrawable drawable)  10 > 11 12 @Override 13 public void dispose(GLAutoDrawable drawable)  14 > 15 16 @Override 17 public void init(GLAutoDrawable drawable)  18 > 19 20 @Override 21 public void reshape(GLAutoDrawable drawable, int x, int y, int width, 22 int height)  23 > 24 >

All the @Override methods from above were generated by Eclipse based on the declaration of the GLEventListener interface. If you’ve configured the JOGL documentation as advised earlier, you can hover with the mouse over a particular function and see a short description. For the purposes of this tutorial we will use only the display and the init methods.

Let’s make peace with Eclipse and add an empty play function to the above class, this is were I usually prefer to put the code that starts the game animation:

  1 . 2 3 public class Game extends JFrame implements GLEventListener   4 private static final long serialVersionUID = 1L; 5 6 public void play()   7 > 8 9 . 10 >

If you wish, you can run the above code, but you will see nothing, we first need to create a window, this can be done in the constructor of the Game class:

  1 . 2 3 final private int width = 800; 4 final private int height = 600; 5 6 public Game()   7 super("Minimal OpenGL"); 8 9 this.setName("Minimal OpenGL"); 10 11 this.setSize(width, height); 12 this.setLocationRelativeTo(null); 13 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 14 this.setVisible(true); 15 this.setResizable(false); 16 > 17 . 

In the above code we’ve defined two variables that will store the width and height of our window and we’ve created a Swing window named Minimal OpenGL. If you run the application you should see an empty gray window.

First step to enable OpenGL in our Java application is to define an OpenGL profile, e.g. we can enable all the OpenGL functions from OpenGL 1.x to 3.0 with:

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

this will let you use the fixed function pipeline from OpenGL which normally shouldn’t be used by any modern application, but it is still used by a large number of OpenGL courses usually for legacy and commodity reasons.

My recommendation, if you are interested in modern OpenGL for desktop, is to use GL4 or, if you have an older GPU, at least GL3. Another interesting profile is GL2ES2 that allows us to use the common functions available in both OpenGL 2.x desktop and OpenGL ES 2. For OpenGL ES 3 there is, of course, GL2ES3 or GL4ES3. For the purposes of this tutorial I will use GL4.

In order to enable the OpenGL 4.x on our machine, we can modify the constructor of the Game class:

  1 import com.jogamp.opengl.GL4; 2 import com.jogamp.opengl.GLAutoDrawable; 3 import com.jogamp.opengl.GLCapabilities; 4 import com.jogamp.opengl.GLEventListener; 5 import com.jogamp.opengl.GLProfile; 6 import com.jogamp.opengl.awt.GLCanvas; 7 import javax.swing.JFrame; 8 9 public class Game extends JFrame implements GLEventListener  10 private static final long serialVersionUID = 1L; 11 12 final private int width = 800; 13 final private int height = 600; 14 15 public Game()  16 super("Minimal OpenGL"); 17 GLProfile profile = GLProfile.get(GLProfile.GL4); 18 GLCapabilities capabilities = new GLCapabilities(profile); 19 20 GLCanvas canvas = new GLCanvas(capabilities); 21 canvas.addGLEventListener(this); 22 23 this.setName("Minimal OpenGL"); 24 this.getContentPane().add(canvas); 25 26 this.setSize(width, height); 27 this.setLocationRelativeTo(null); 28 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 29 this.setVisible(true); 30 this.setResizable(false); 31 canvas.requestFocusInWindow(); 32 > 33 34 . 35 >

Now, we have an OpenGL context attached to our window.

The actual draw commands needs to be called from the display function:

  1 . 2 3 public void display(GLAutoDrawable drawable)   4 GL4 gl = drawable.getGL().getGL4(); 5 gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT); 6 7 // call your draw code here 8 9 gl.glFlush(); 10 > 11 . 12 >

If you run the above code, you should see a nice black window, OpenGL uses black as the default color for clearing the color buffer. We can change this, if necessary, in the init function:

 1 . 2 3 public void init(GLAutoDrawable drawable)  4 GL4 gl = drawable.getGL().getGL4(); 5 gl.glClearColor(0.392f, 0.584f, 0.929f, 1.0f); 6 > 7 . 8 >

This is what you should see, if you run the above code:

OpenGL Window filled with blue

it may not look like much, but you now have a minimal OpenGL 4.x application.

A modern book about using Java with OpenGL is Computer Graphics Programming in OpenGL with Java by V. S. Gordon and J.Clevenger:

Disclaimer:
All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.

Copyright © 2023 — Paul Silisteanu

Источник

Оцените статью