dimanche 28 avril 2013

LWJGL, Maven and Eclipse

Hellow fellow developpers,

After struggling between outdated tutorials and uncomplete documentations, I will describe how to setup an OpenGL project using LWJGL, Maven and Eclipse (Juno SR2).

You need a working Eclipse and m2e plugin.


Creating a new project

  1. File/New/Other...
  2. Filter Eclipse projects using Maven Project
  3. Select it and press Next
  4. Configure workspace location
  5. Press Next
  6. Filter Maven archetypes using maven-archetype-quickstart
  7. Select the last version and press Next
  8. Configure Group Id (com.devstuffs) and Artifact Id (lwjglandmaven)
  9. Press Finish


 

LWJGL program

package com.devstuffs.lwjglandmaven;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class App
{
  public static void main( String[] args )
  {
    try
    {
      Display.setTitle( "Desktop" );
      Display.setDisplayMode( new DisplayMode( 640, 480 ) );
      Display.create();
    }
    catch( LWJGLException exception )
    {
      exception.printStackTrace();
      System.exit( 0 );
    }
    
    while( !Display.isCloseRequested() )
    {
      GL11.glClearColor( 0.f, 0.f, 0.f, 0.f );
      GL11.glClear( GL11.GL_COLOR_BUFFER_BIT );
      
      Display.update();
      Display.sync( 60 );
    }
    
    Display.destroy();
  }
}


 

Adding LWJGL dependency

  1. Edit pom.xml using the Maven POM editor
  2. Open the Dependencies tab
  3. Press Add... in the Dependencies list
  4. Filter available artifacts with org.lwjgl.lwjgl
  5. Pick lwjgl 2.8.5
  6. Valid and save pom.xml


No error anymore, but we can't launch it because LWJGL requires some native libraries.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
 at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
 at java.lang.Runtime.loadLibrary0(Runtime.java:845)
 at java.lang.System.loadLibrary(System.java:1084)
 at org.lwjgl.Sys$1.run(Sys.java:73)
 at java.security.AccessController.doPrivileged(Native Method)
 at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
 at org.lwjgl.Sys.loadLibrary(Sys.java:95)
 at org.lwjgl.Sys.(Sys.java:112)
 at org.lwjgl.opengl.Display.(Display.java:132)
 at com.devstuffs.lwjglandmaven.App.main(App.java:14)

 

Managing natives libraries

  1. Edit pom.xml
  2. Open the Dependencies tab again
  3. Press Add... in the Dependency Management list
  4. Filter available artifacts with com.googlecode.mavennatives
  5. Pick mavennatives 0.0.7
  6. Valid
  7. Open the pom.xml tab
  8. Add the following snippet
  9. Save
<build>
  <plugins>
    <plugin>
      <groupid>com.googlecode.mavennatives</groupid>
      <artifactid>maven-nativedependencies-plugin</artifactid>
      <version>0.0.7</version>
      <executions>
        <execution>
          <id>unpacknatives</id>
          <phase>generate-resources</phase>
          <goals>
            <goal>copy</goal>
          <goals>
          <configuration>
            <nativestargetdir>target/natives</nativestargetdir>
            <separatedirs>false</separatedirs>
          <configuration>
        <execution>
      <executions>
    <plugin>
  <plugins>
<build>

Now pom.xml looks like :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.devstuffs</groupId>
  <artifactId>lwjglandmaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>lwjglandmaven</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
     <groupId>org.lwjgl.lwjgl</groupId>
     <artifactId>lwjgl</artifactId>
     <version>2.8.5</version>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.googlecode.mavennatives</groupId>
        <artifactId>maven-nativedependencies-plugin</artifactId>
        <version>0.0.7</version>
        <type>maven-plugin</type>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <plugins>
      <plugin>
        <groupId>com.googlecode.mavennatives</groupId>
        <artifactId>maven-nativedependencies-plugin</artifactId>
        <version>0.0.7</version>
        <executions>
          <execution>
            <id>unpacknatives</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <nativesTargetDir>target/natives</nativesTargetDir>
              <separateDirs>false</separateDirs>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

 

Running the application

  1. Right click on the project
  2. Maven/Update Project...
  3. Select lwjglandmaven
  4. Valid
  5. Right click on the project
  6. Run as/Maven clean
  7. Run as/Maven install
  8. Right click on App.java
  9. Run as/Java application
If natives libraries can't be found edit App running configuration and check VM arguments is set to -Djava.library.path=target/natives.

Feel free to contribute !