Creating a JOGL window using NEWT - examples?

I was looking for using JOGL to create some things and browsing through the documentation I can find.

In the quick tutorials, they all mention how using the JOGL canvas version can have performance issues, and you should use NEWT instead. However, each tutorial / FAQ then continues to use the canvas! Or just specify a few small fragments of methods for creating a window using NEWT, but which (at least on my machine) I can not start correctly.

Does anyone have a good example of how to properly create and render in a window in JOGL using the NEWT method? I'm not even sure how it works compared to Canvas, so it would be an ideal explanation of the differences between them and the typical layout of the methods for creating / managing / rendering in a window.

Just a little lost and can't find anything useful. Hope someone has met something before!

+3
source share
2 answers

take a look at JOGL junit tests, they cover most of the NEWT API.

0
source

, . 3.9 - JOGL. . .

JOGL2NewtDemo.java

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.FPSAnimator;

/**
 * A program that draws with JOGL in a NEWT GLWindow.
 *
 */
public class JOGL2NewtDemo {
    private static String TITLE = "JOGL 2 with NEWT";  // window title
    private static final int WINDOW_WIDTH = 640;  // width of the drawable
    private static final int WINDOW_HEIGHT = 480; // height of the drawable
    private static final int FPS = 60; // animator target frames per second

    static {
        GLProfile.initSingleton();  // The method allows JOGL to prepare some Linux-specific locking optimizations
    }

    /**
     * The entry main() method.
     */
    public static void main(String[] args) {
        // Get the default OpenGL profile, reflecting the best for your running platform
        GLProfile glp = GLProfile.getDefault();
        // Specifies a set of OpenGL capabilities, based on your profile.
        GLCapabilities caps = new GLCapabilities(glp);
        // Create the OpenGL rendering canvas
        GLWindow window = GLWindow.create(caps);

        // Create a animator that drives canvas' display() at the specified FPS.
        final FPSAnimator animator = new FPSAnimator(window, FPS, true);

        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDestroyNotify(WindowEvent arg0) {
                // Use a dedicate thread to run the stop() to ensure that the
                // animator stops before program exits.
                new Thread() {
                    @Override
                    public void run() {
                        if (animator.isStarted())
                            animator.stop();    // stop the animator loop
                        System.exit(0);
                    }
                }.start();
            }
        });

        window.addGLEventListener(new JOGL2Renderer());
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setTitle(TITLE);
        window.setVisible(true);
        animator.start();  // start the animator loop
    }
}

JOGL2Renderer.java

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;

/**
 * Class handles the OpenGL events to render graphics.
 *
 */
public class JOGL2Renderer implements GLEventListener {
    private double theta = 0.0f;  // rotational angle

    /** 
     * Called back by the drawable to render OpenGL graphics 
     */
    @Override
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();   // get the OpenGL graphics context

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);    // clear background
        gl.glLoadIdentity();                   // reset the model-view matrix    

          // Rendering code - draw a triangle
        float sine = (float)Math.sin(theta);
        float cosine = (float)Math.cos(theta);
        gl.glBegin(GL.GL_TRIANGLES);
        gl.glColor3f(1, 0, 0);
        gl.glVertex2d(-cosine, -cosine);
        gl.glColor3f(0, 1, 0);
        gl.glVertex2d(0, cosine);
        gl.glColor3f(0, 0, 1);
        gl.glVertex2d(sine, -sine);
        gl.glEnd();

        update();
    }

    /** 
     * Update the rotation angle after each frame refresh 
     */
    private void update() {
        theta += 0.01;
    }

    /*... Other methods leave blank ...*/
}
0

Source: https://habr.com/ru/post/1782451/


All Articles