程式語言 - Java Binding for the OpenGL API (JOGL) - OpenGL 1.0 - Draw Triangle



參考資訊:
https://jogamp.org/
https://github.com/sgothel/jogl
https://jogamp.org/deployment/maven/org/jogamp/jogl/jogl-all/2.6.0/

Main.java

import com.jogamp.opengl.*;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.Animator;

public class Main {
    static final int w = 320;
    static final int h = 240;

    public static void main(String[] args) throws Exception {
        GLWindow win = GLWindow.create(new GLCapabilities(GLProfile.get(GLProfile.GL2)));

        win.setSize(w, h);
        win.addGLEventListener(new GLEventListener() {
            public void init(GLAutoDrawable d) {}
            public void dispose(GLAutoDrawable d) {}
            public void reshape(GLAutoDrawable d, int x, int y, int w, int h) {
                GL2 gl = d.getGL().getGL2();

                gl.glViewport(0, 0, w, h);
            }

            public void display(GLAutoDrawable d) {
                GL2 gl = d.getGL().getGL2();

                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                gl.glBegin(GL2.GL_TRIANGLES);
                    gl.glVertex2f(0, 0.5f);
                    gl.glVertex2f(-0.5f, -0.5f);
                    gl.glVertex2f(0.5f, -0.5f);
                gl.glEnd();
            }
        });

        Animator a = new Animator(w);
        win.setVisible(true);
        a.start();

        Thread.sleep(3000);

        a.stop();
        win.destroy();
    }
}

編譯、執行

$ export JAR="$HOME/jogamp-all-platforms/jar"
$ export CP="$JAR/jogl-all.jar:$JAR/gluegen-rt.jar"
$ export CP="$CP:$JAR/jogl-all-natives-linux-amd64.jar"
$ export CP="$CP:$JAR/gluegen-rt-natives-linux-amd64.jar"

$ javac -cp "$CP" Main.java
$ java --enable-native-access=ALL-UNNAMED -cp ".:$CP" Main