在线程内执行OpenGL绘图 [英] Performing OpenGL Drawing from within a Thread

查看:460
本文介绍了在线程内执行OpenGL绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Delphi VCL自定义控件中包装核心的OpenGL功能。目前,我正在使用在此控件内创建的 TTimer 进行循环。我强烈地觉得使用定时器是完全错误的,因为绘图可能会重叠并有延迟。相反,我想把这个绘图循环放在一个线程中。请参阅我的原始问题,其中包含一些问题闪烁 - 我把责任归咎于我正在使用的 TTimer



问题



OpenGL是否可以安全地使用线程内部?我应该注意什么?我打算从这个线程的内部和外部进行OpenGL API调用。它将基本上是一个连续循环,当绘制完成后,它将立即开始绘制。目前,使用定时器,这取决于等待定时器的间隔时间。因此,例如,如果定时器的间隔设置为100毫秒,绘图只需要20毫秒,那么将再次等待80毫秒。使用线程将消除这种延迟。



背景



我基于我的所有API调用的原始示例项目都是使用应用程序的主线程,实际上绝对没有使用VCL。使用纯Windows API调用动态创建显示图像的窗口,并从应用程序主线程的连续循环中重复绘制。这就是为什么我相信使用线程会解决我的许多问题 - 特别是闪烁。

解决方案

一个OpenGL上下文只能是当前(活动)在一个给定的线程。因此,您必须在线程中正确解除/重新绑定当前上下文,或者必须使用两个上下文共享数据。我强烈建议您重新设计您的设计,只使用单线程进行OpenGL调用。 http://www.equalizergraphics.com/documentation/parallelOpenGLFAQ.html


I've been wrapping the core OpenGL functionality within a Delphi VCL custom control. Currently, I am doing a loop using a TTimer created within this control. I strongly feel using a timer is completely wrong, as drawing could both overlap and have delays. Instead, I would like to put this drawing loop within a thread. Please refer to my original question with some issues of flickering - which I'm putting the blame on the TTimer which I'm using.

Question

Is OpenGL safe to use inside of a thread? What should I be aware of? I plan on making OpenGL API calls from both inside and outside of this thread. It will essentially be a continuous loop, where as soon as it's finished drawing, it will immediately start drawing again. Currently, using a Timer, it depends on waiting for the Timer's interval to be reached. So, for example, if the Timer's interval is set to 100 msec, and the drawing only takes 20 msec, then there will be 80 msec of waiting until it draws again. Using a thread will eliminate this delay. How should I make such calls from inside this thread?

Background

The original sample project which I based all of my API calls off of was using the application's main thread, and in fact used absolutely no VCL at all. The Window which displayed the image was dynamically created using pure Windows API calls, and the drawing was repeated from within a continuous loop in the application's main thread. This is why I believe using a Thread will solve many of my issues - specifically the flickering.

解决方案

One OpenGL context can only be current (active) in one given thread. So either you have to unbind/rebind the current context correctly in your threads, or you have to use two context sharing data. I strongly recommend to restructure your design to only use a single thread doing OpenGL call. More background can be found at http://www.equalizergraphics.com/documentation/parallelOpenGLFAQ.html.

这篇关于在线程内执行OpenGL绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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