簡體   English   中英

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

[英]JOGL creating window using NEWT - examples?

我一直在考慮使用 JOGL 來創建一些東西,並且一直在查看我能找到的文檔。

簡短的教程他們都提到使用 JOGL 版本的畫布可能會出現性能問題,而您應該使用 NEWT。 但是,每個教程/常見問題解答都將繼續使用畫布! 或者簡單地指定一些小的方法片段來使用 NEWT 創建一個窗口,但是(至少在我的機器上)我無法正確運行。

有沒有人有關於如何使用 NEWT 方法在 JOGL 中正確實現創建和渲染窗口的示例的良好來源? 我什至不確定與 Canvas 相比它的功能如何,因此對兩者之間的差異的解釋以及創建/管理/渲染到窗口的方法的典型布局將是理想的。

只是有點迷失,找不到任何有用的東西。 希望有人之前遇到過一些事情!

本教程對我幫助很大,請參閱第 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'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 ...*/
}

看看 JOGL 的 junit 測試,它們涵蓋了 NEWT API 的大部分內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM