Lwjgl

Lwjgl - LightWeight Java game Library é uma APi que implementa o suporte a diversas bibliotecas nativas tais como OpenGL e OpenAL em código Java.

Tutorial do Básico em LWJGL

Retirado de: http://www.lwjgl.org/wiki/index.php?title=LWJGL_Basics_1_%28The_Display%29

Exemplo básico

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
 
public class DisplayExample {
 
    public static void main(String[] args) {
        DisplayExample displayExample = new DisplayExample();
        displayExample.start();
    }
 
    private void start() {
        try {
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
 
        while( !Display.isCloseRequested() ){
            // render OpenGL here
            Display.update();
        }
 
        Display.destroy();
    }
 
}

Exemplo básico com comandos GL super básicos

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
 
public class DisplayExample {
 
    public void start() {
        try {
            //Display.setFullscreen(true);
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.create();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
 
        while( !Display.isCloseRequested() ){
            int x = Mouse.getX();
            int y = Mouse.getY();
 
            Display.setTitle( "(" + x + ", " + y + ")" );
 
            // init OpenGL here
            GL11.glClearColor(0, x / 800.0f, y / 600.0f, 0.3f);
 
            // render OpenGL here
            GL11.glClear( GL11.GL_COLOR_BUFFER_BIT );
 
            Display.update();
        }
 
        Display.destroy();
    }
 
    public static void main(String[] args) {
        DisplayExample displayExample = new DisplayExample();
        displayExample.start();
    }
 
}

Um exemplo com ligeiramente mais funcionalidades

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
 
public class DisplayExample {
 
    public void start() {
        try {
 
            for (DisplayMode displayMode : Display.getAvailableDisplayModes()) {
                if( displayMode.isFullscreenCapable() ){
                    Display.setDisplayModeAndFullscreen(displayMode);
                    break;
                }
            }
            Display.create();
 
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
 
        boolean escape = false;
 
        while( !Display.isCloseRequested() && !escape ){
            int x = Mouse.getX();
            int y = Mouse.getY();
 
            String additional = "";
 
            while( Keyboard.next() ){
                int key = Keyboard.getEventKey();
                additional += " key " + key;
 
                if( key == Keyboard.KEY_ESCAPE){
                    escape = true;
                }
            }
 
            if( Keyboard.isKeyDown( Keyboard.KEY_SPACE )){
                additional+= " space";
            }
 
            additional += GL11.glGetString( GL11.GL_VERSION );
 
            additional += (Display.getDisplayMode().isFullscreenCapable() ? "" : "not ") +
                    "capable of fullscreen";
 
            Display.setTitle( "(" + x + ", " + y + ")"  + additional );
 
            // init OpenGL here
            GL11.glClearColor(0, x / 800.0f, y / 600.0f, 0.3f);
 
            // render OpenGL here
            GL11.glClear( GL11.GL_COLOR_BUFFER_BIT );
 
            Display.update();
        }
 
        Display.destroy();
    }
 
    public static void main(String[] args) {
        DisplayExample displayExample = new DisplayExample();
        displayExample.start();
    }
 
}

Selecionando a versão OpenGL3+ com LWJGL

import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
 
public class OpenGL3Plus {
 
    public OpenGL3Plus() {
        boolean select = true;
 
        if( select ) this.specifiedCreate();
        else this.normalCreate();
 
        String version = GL11.glGetString( GL11.GL_VERSION );
        Display.setTitle(version);
 
        while( !Display.isCloseRequested() ){
            Display.sync(60);
            Display.update();
        }
 
        Display.destroy();
    }
 
    // Let Display manage our OpenGL version
    private void normalCreate(){
        try {
            Display.setDisplayMode(new DisplayMode(320, 240));
            Display.setTitle( "Version selection" );
            Display.create();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
 
    // Use a specified version of OpenGL - namely version 3.2
    private void specifiedCreate(){
        PixelFormat pixelFormat = new PixelFormat();
        ContextAttribs contextAttributes = new ContextAttribs(3,2)
            .withForwardCompatible(true)
            .withProfileCore(true);
 
        try {
            Display.setDisplayMode( new DisplayMode(320, 240) );
            Display.setTitle( "Version selection" );
            Display.create(pixelFormat, contextAttributes);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
 
    public static void main(String[] args) {
        new OpenGL3Plus();
    }
 
}

Links

http://www.lwjgl.org

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.