Java:openGL:JOGL:调用display()方法在幕后会发生什么? [英] Java: openGL: JOGL: What happens behind the scenes when I call the display() method?

查看:139
本文介绍了Java:openGL:JOGL:调用display()方法在幕后会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行代码:

renderableObject.renderObject(gl, glu);

这会导致openGL渲染大量对象,但是仅在按以下方式使用时才起作用:

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);
    }

如果我在覆盖的显示方法之外调用该行,则会得到一个异常,表明当前线程上没有glContext,实际上,如果我在此方法之外调用任何gl draw命令,则会得到相同的异常

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

现在,理想情况下,我想一次创建很多显示列表,然后使用奇数显示列表在每个帧中渲染它们,以定期重新创建它们.但是,我必须通过这种display()方法,这意味着如果显示列表已创建,或者需要更改等,我将必须测试每一帧……每秒60次!当我在需要时可以一次单独处理它们时,会浪费处理能力.

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.

因此,无论调用display()方法做什么,我都希望能够对其进行复制,从而使我能够创建大量自己的自定义显示方法,而无需对所有方法都进行遍历!

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!

那么我能做一个简单的gl呼叫吗?

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.

在幕后发生的事情是,当绘制您创建的GLCanvas时,JOGL在幕后进行了大量工作.它正在创建一个GLContext,并将其作为当前线程的GLCanvas的当前对象.只有完成此操作后,您才能进行渲染调用.尚未成为最新的GLContext或从其衍生的GL对象对您没有用处.此外,仅将GLContext设为当前用于该线程的 ,并在显示调用结束后立即将其设为非当前的,因此,将不会对其进行引用或GL,以供以后使用工作.

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.

几乎所有的JOGL应用程序都以这种方式工作.您创建一个GLEventListener,并在其中实现display(),从GLAutoDrawable中提取一个GL并使用它进行渲染调用.您不想在任何其他地方进行渲染调用,而不是在paint()方法之外进行Graphics2D调用.大多数初学者Java程序员都尝试从paint方法之外进行绘制.这是相似的.如果需要触发重新绘制,则可以使用Java2D的相同方法进行操作:使用invalidate(). (当然,您可以编写从display()方法调用的子方法,这些子方法将GL或GLAutoDrawable作为参数.)

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).

您可以通过多种方法专门创建GLContext并将其设置为当前状态,但是很少有必要.在这里使用这种方法几乎总是更好.

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.

这篇关于Java:openGL:JOGL:调用display()方法在幕后会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆