gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:15:46 27 4
gpt4 key购买 nike

我一直在考虑使用 JOGL 来创建一些东西,并且一直在浏览我能找到的文档。

简短的教程他们都提到使用 JOGL 版本的 Canvas 可能会出现性能问题,而您应该使用 NEWT。然而,每个教程/常见问题解答都会继续使用 Canvas !或者简单地指定一些微小的方法片段来使用 NEWT 创建一个窗口,但(至少在我的机器上)我无法正确运行。

是否有人有关于如何使用 NEWT 方法在 JOGL 中正确实现创建和渲染窗口的良好示例来源?我什至不确定它与 Canvas 相比的功能如何,因此解释两者之间的差异以及创建/管理/渲染窗口的方法的典型布局将是理想的。

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

最佳答案

本教程对我帮助很大,请参阅第 3.9 章 - Yet Another Tutorial on JOGL 。另外documentation很有用。请查看随附的示例。

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

关于java - JOGL 使用 NEWT 创建窗口 - 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com