java display使用方法,Java:openGL:JOGL:调用display()方法在幕后会发生什么?

I have this line of code:

renderableObject.renderObject(gl, glu);

This leads to a large list of objects being rendered by openGL, however it only works when used as follows:

@Override

public void display(GLAutoDrawable drawable)

{

renderableObject.renderObject(gl, glu);

}

If I call the line outside the overridden display method I get an Exception saying there is no glContext on the current thread, actually if I call any gl draw commands outside this method I get the same exception

now ideally I want to create a lot of display lists once, then render them every frame with the odd display list to be recreated periodically. However I have to go through this single display() method which means I would have to test every frame if the display list has been created, or is in need of change etc... 60 times a second! what a waste of processing power when I could handle them separately once when needed.

So whatever calling the display() method does, I would like to be able to replicate it allowing me to create a plethora of my own custom display methods, without going through this one method for everything!

So is there a simple gl call I can make myself?

解决方案

Odd as it may seem, this is the way it is supposed to work.

Behind the scenes what is going on is that when the GLCanvas you have created comes to be drawn, behind the scenes JOGL is doing a whole pile of work. It is creating a GLContext, and making it current for the GLCanvas for the current thread. Only when that is done can you make rendering calls. A GLContext that has not been made current, or a GL object that derives from it, is no use to you. Additionally the GLContext is made current only for that thread, and is made non-current as soon as the display call is finished, so hanging on to a reference to it or the GL for later use won't work.

Almost all JOGL applications work that way. You create a GLEventListener, and implement display() in it, extract a GL from the GLAutoDrawable and use it to make rendering calls. You don't want to make rendering calls in any other place, any more than you want to make Graphics2D calls outside of the paint() method. Most beginner Java programmers try to paint from outside the paint method; this is similar. If you need to trigger a repaint then you do it the same way you would with Java2D: use invalidate(). (You can of course write submethods that are called from the display() method, and which take the GL or GLAutoDrawable as an argument).

There are ways for you to specifically create a GLContext and make it current yourself, but they are rarely necessary. It is almost always better to use the approach here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Swing 是一个基于 Java 的图形用户界面(GUI)工具包,主要用于开发桌面应用程序。而 OpenGL 是一个跨平台的 3D 图形库,通常用于游戏开发和科学可视化等领域。 虽然 Java Swing 自带一些绘图功能,但是如果需要实现复杂的 3D 图形渲染效果,还是需要使用 OpenGL 这样的专业图形库。在 Java Swing 中使用 OpenGL 可以通过 JOGLJava OpenGL)库来实现。 以下是使用 JOGL 实现在 Java Swing 中使用 OpenGL 渲染的基本步骤: 1. 引入 JOGL 库:在项目中引入 JOGL 库,可以通过 Maven 或手动下载并添加到项目中来。 2. 创建 OpenGL 帧缓冲区:在 Java Swing 中创建一个 JPanel,用于在其中显示 OpenGL 渲染的图像。在 JPanel 中创建一个 GLJPanel,作为 OpenGL 帧缓冲区。 3. 实现 OpenGL 渲染器:创建一个实现 GLEventListener 接口的类,用于实现 OpenGL 渲染器。在其中实现 OpenGL 渲染所需的初始化、绘制等方法。 4. 在 JPanel 中添加 GLJPanel:将 GLJPanel 添加到 JPanel 中,以便在其中进行 OpenGL 渲染。 下面是一个简单的示例代码,演示如何在 Java Swing 中使用 JOGL 库实现 OpenGL 渲染: ```java import javax.swing.JFrame; import javax.swing.JPanel; import com.jogamp.opengl.GL; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.awt.GLJPanel; public class OpenGLDemo extends JPanel implements GLEventListener { private static final long serialVersionUID = 1L; private GLJPanel panel; public OpenGLDemo() { // 创建 OpenGL 帧缓冲区 GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); panel = new GLJPanel(caps); panel.addGLEventListener(this); // 将 GLJPanel 添加到 JPanel 中 add(panel); } @Override public void init(GLAutoDrawable drawable) { // 初始化 OpenGL 渲染器 GL gl = drawable.getGL(); gl.glClearColor(1f, 1f, 1f, 1f); } @Override public void display(GLAutoDrawable drawable) { // 实现 OpenGL 渲染 GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glBegin(GL.GL_TRIANGLES); gl.glColor3f(1f, 0f, 0f); gl.glVertex2f(-1f, -1f); gl.glColor3f(0f, 1f, 0f); gl.glVertex2f(0f, 1f); gl.glColor3f(0f, 0f, 1f); gl.glVertex2f(1f, -1f); gl.glEnd(); } @Override public void dispose(GLAutoDrawable drawable) { // 释放 OpenGL 资源 } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { // 重新设置 OpenGL 视口 GL gl = drawable.getGL(); gl.glViewport(0, 0, width, height); } public static void main(String[] args) { JFrame frame = new JFrame("OpenGL Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLocationRelativeTo(null); // 添加 OpenGLDemo 到 JFrame 中 OpenGLDemo demo = new OpenGLDemo(); frame.add(demo); frame.setVisible(true); } } ``` 这个示例代码创建了一个 JFrame,其中包含一个 JPanel,在其中使用 JOGL 库创建了一个 GLJPanel 作为 OpenGL 帧缓冲区。在 GLEventListener 中实现了初始化、绘制等方法,并将 GLJPanel 添加到 JPanel 中。最后将 JPanel 添加到了 JFrame 中并显示出来。 运行这个程序,就可以看到一个简单的 OpenGL 渲染效果。这个示例只是一个简单的演示,实际上使用 JOGL 库实现更复杂的 OpenGL 渲染还需要更多的代码和技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值