Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Java programming with OpenGL #1 - Introduction

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 05-27-12, 04:25 PM Thread Starter   #1
ssjwizard
Member

 
ssjwizard's Avatar 

Join Date: Mar 2002
Location: Rio Rancho, NM

10 Year Badge
 
Java programming with OpenGL #1 - Introduction


Java programming with OpenGL #1 - Introduction

Welcome to the first installment of my Java programming tutorial series. I will begin from the position that you have some basic understanding of programming. I'd like to introduce you to my latest project: building a game engine using the highly portable Java for logic handling. As many of you already know, Java graphics rendering is horrifically slow and bulky on system resources. Now for any of us here at OCF this really isn't an issue as our machines are very capable; However, as an independent developer in today's market portability and uniqueness reign supreme. So I have selected OpenGL as a candidate to replace Java's graphics rendering.

To access OpenGL through Java we will be working with LWJGL. You may know a bit about LWJGL, and may also know that Slick 2D is a game library built on top of LWJGL, so why not start there? If all we wanted to do was produce a simple game then that would be fine, but we're here to learn the fundamentals of building a game library.

Ok for our projects we will be needing a few tools:

1. Java SE 1.6 Developers Kit or higher found HERE
2. Eclipse for Java Developers found HERE.
3. LWJGL found HERE.

Installing LWJGL

LWJGL is a library and isn't "installed" but rather needs to be included in our project. Detailed instructions can be found HERE.

I recommend placing the LWJGL .jar files Slick_Util.jar and the appropriate natives for your OS(I use the Windows natives but will include the OSX and Linux natives in my finished game) into a folder inside your eclipse workspace before creating any projects.

You should begin by opening eclipse and creating a new Project.

Name:  new_project.png
Views: 111
Size:  9.8 KB

Select new Java Project.

Name:  new_Java_project1.png
Views: 111
Size:  7.5 KB

Enter a project name and select a developer environment. For compatibility reasons I will be using JavaSE 1.6.

Name:  new_Java_project2.png
Views: 111
Size:  9.7 KB

Now right click on your project and select properties.

Name:  project_properties.png
Views: 113
Size:  7.6 KB

Select Java Build Path, click the libraries tab, and the click Add External JARs.

Name:  add_external_jar1.png
Views: 109
Size:  9.8 KB

Navigate to the folder where you put the LWJGL .jar files. Select all 3 files and click Open.

Name:  add_external_jar2.png
Views: 110
Size:  17.2 KB

Expand the lwjgl.jar options and select Native library location, and then click Edit.

Name:  add_native1.png
Views: 110
Size:  12.5 KB

Click on External Folder, and navigate to the folder where you put LWJGL then select the natives folder and click Ok.

Name:  add_native2.png
Views: 110
Size:  10.8 KB

Click OK to close the properties window. Now expand your project folder in the explorer, right click on the src folder and create a new Class.

Name:  new_class.png
Views: 110
Size:  9.7 KB

Enter a name for your class I will use HelloWorld, and then check the box for public static void main..

Name:  new_class_HelloWorld.png
Views: 111
Size:  10.2 KB

Now open up your new class into the Java editor. We are going to need a few imports to get started and they are:
Code:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
Now as java is an Object oriented language were going to want to get out of the static main method ASAP to do this we will simply call the constructor of the class.

Code:
public static void main(String[] args) {
  new HelloWorld();
}
All were going to do with this example now is create a basic window which we can draw content onto later. We're going to be calling a few of LWJGL Display's methods to do this:

1. Display.setDisplayMode
2. Display.setTitle
3. Display.create

The LWJGL library throws a LWJGLException so these calls must be nested within a try catch block.

Code:
try {
  Display.setDisplayMode(new DisplayMode(800,500));
  Display.setTitle("Hello from LWJGL!");
  Display.create();
} 
 catch (LWJGLException e) {
    e.printStackTrace();
 }
We have the display now, but it isn't rendering yet. The next step is create a simple display loop. For this, we will use a while statement attached to the Display.isCloseRequested method. To force a draw to the screen, we will need to use the Display.update method. Next we will limit the games system resources by assigning a frame rate cap using the Display.sync method.

Code:
    //program starts here
  while(!Display.isCloseRequested()){
    Display.update();
    Display.sync(60);
  }
Finally we will call the Display.destroy method to close the window once display requests to be closed

Code:
Display.destroy();
Bringing it all together

Name:  HelloWorld_running.png
Views: 110
Size:  11.6 KB

Code:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class HelloWorld {

  public static void main(String[] args) {
    new HelloWorld();
  }

  public HelloWorld(){
    try {
      Display.setDisplayMode(new DisplayMode(800,500));
      Display.setTitle("Hello from LWJGL!");
      Display.create();
      
    } 
    catch (LWJGLException e) {
      e.printStackTrace();
    }
    
      //program starts here
    while(!Display.isCloseRequested()){
      Display.update();
      Display.sync(60);
    }
    
    Display.destroy();
  }

}
That's pretty much it for this installment. We have setup eclipse, started a project, linked to our OpenGL library, and created a Display. We are now ready to start coding our game engine. I look forward to bringing the second installment of this series on basic shapes and colors!

__________________
Life is what you make of it, we exist for the sole purpose of existing. Dont waste the universe efforts!
All statements, imagery, and ideas contained within any posts are merely retold tales of fictional acts by a figment of the universe imagination.

So your looking for an AMD FX motherboard, things you need to know.
Whats the max safe temps/volts for an FX?

Green Gorilla, CM 690-II nVidia, FX 8320, GTX 670, liquid cooled
Liquid Fusion, CM 690-II, A8+6670 Dual GFX, liquid cooled
Extreme evaporative water loop
450mm^3 water cooled beast - Incomplete

Last edited by ssjwizard; 05-31-12 at 03:13 PM.
ssjwizard is offline Benching Profile Folding Profile Heatware Profile   QUOTE Thanks
Old 05-28-12, 12:52 PM   #2
Badbonji

 
Badbonji's Avatar 

Join Date: Apr 2008
Location: Birmingham, UK

 
This looks great, been planning on learning some Java alongside practicing C++ this summer whilst I am free from uni.

__________________
i7 3770K 4.5GHz 1.2V | Phanteks PH-TC14PE | 16GB TeamGroup Elite 1600MHz | GIGABYTE G1.Sniper M3 | EVGA GTX680 SC 1254/1500MHz | M4 256GB + 150GB Raptor | Corsair HX+ 850W | Antec 1200
i7 965 | 8GB OCZ Gold 1600MHz | GIGABYTE Extreme X58 | HD5450 | M4 64GB + 7TB | OCZ 500W
P9500 3.13GHz | 4GB 1000MHz DDR2 | Mobility HD4850 550/900MHz | 500GB
Badbonji is offline Benching Profile SETI Profile Heatware Profile   QUOTE Thanks
Old 05-28-12, 01:16 PM Thread Starter   #3
ssjwizard
Member

 
ssjwizard's Avatar 

Join Date: Mar 2002
Location: Rio Rancho, NM

10 Year Badge
 
Thought Id point out I posted a Snake game that I developed my framework that we will be going through with these articles over in the GAMES forum.

__________________
Life is what you make of it, we exist for the sole purpose of existing. Dont waste the universe efforts!
All statements, imagery, and ideas contained within any posts are merely retold tales of fictional acts by a figment of the universe imagination.

So your looking for an AMD FX motherboard, things you need to know.
Whats the max safe temps/volts for an FX?

Green Gorilla, CM 690-II nVidia, FX 8320, GTX 670, liquid cooled
Liquid Fusion, CM 690-II, A8+6670 Dual GFX, liquid cooled
Extreme evaporative water loop
450mm^3 water cooled beast - Incomplete
ssjwizard is offline Benching Profile Folding Profile Heatware Profile   QUOTE Thanks
Old 05-28-12, 04:17 PM   #4
dropadrop
Member

 
dropadrop's Avatar 

Join Date: Mar 2001
Location: Finland

10 Year Badge
 
Great idea, I'm looking forward to this.
dropadrop is offline   QUOTE Thanks
Old 05-29-12, 12:44 PM Thread Starter   #5
ssjwizard
Member

 
ssjwizard's Avatar 

Join Date: Mar 2002
Location: Rio Rancho, NM

10 Year Badge
 
Second issue has been posted HERE

__________________
Life is what you make of it, we exist for the sole purpose of existing. Dont waste the universe efforts!
All statements, imagery, and ideas contained within any posts are merely retold tales of fictional acts by a figment of the universe imagination.

So your looking for an AMD FX motherboard, things you need to know.
Whats the max safe temps/volts for an FX?

Green Gorilla, CM 690-II nVidia, FX 8320, GTX 670, liquid cooled
Liquid Fusion, CM 690-II, A8+6670 Dual GFX, liquid cooled
Extreme evaporative water loop
450mm^3 water cooled beast - Incomplete
ssjwizard is offline Benching Profile Folding Profile Heatware Profile   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 06:23 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?