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
- File/New/Other...
- Filter Eclipse projects using Maven Project
- Select it and press Next
- Configure workspace location
- Press Next
- Filter Maven archetypes using maven-archetype-quickstart
- Select the last version and press Next
- Configure Group Id (com.devstuffs) and Artifact Id (lwjglandmaven)
- 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
- Edit pom.xml using the Maven POM editor
- Open the Dependencies tab
- Press Add... in the Dependencies list
- Filter available artifacts with org.lwjgl.lwjgl
- Pick lwjgl 2.8.5
- 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
- Edit pom.xml
- Open the Dependencies tab again
- Press Add... in the Dependency Management list
- Filter available artifacts with com.googlecode.mavennatives
- Pick mavennatives 0.0.7
- Valid
- Open the pom.xml tab
- Add the following snippet
- 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
- Right click on the project
- Maven/Update Project...
- Select lwjglandmaven
- Valid
- Right click on the project
- Run as/Maven clean
- Run as/Maven install
- Right click on App.java
- Run as/Java application
Feel free to contribute !



Aucun commentaire:
Enregistrer un commentaire