JOGL使用NEWT创建窗口 - 示例?

时间:2010-12-28 14:31:56

标签: java opengl jogl

我一直在寻找使用JOGL创建一些东西,并一直在查看我能找到的文档。

简要教程他们都提到如何使用JOGL版本的画布可能会出现性能问题,而应该使用NEWT。然而,每个教程/ FAQ然后继续使用画布!或者只是使用NEWT指定一些小方法来创建一个窗口但是(至少在我的机器上)我无法正确运行。

有没有人有一个很好的例子来说明如何使用NEWT方法正确地在JOGL中创建和渲染窗口?我甚至不确定它与Canvas相比如何起作用,所以解释两者之间的差异以及创建/管理/渲染到窗口的典型方法布局将是理想的。

只是有点迷失,找不到任何有用的东西。希望有人以前遇到过某些事情!

2 个答案:

答案 0 :(得分:0)

看看JOGL的junit测试,它们涵盖了NEWT API的大部分内容。

答案 1 :(得分:0)

本教程对我帮助很大,参见第3.9章 - Yet Another Tutorial on JOGLdocumentation也很有用。请看附带的例子。

<强> 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's 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's 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 ...*/
}