简体   繁体   English

渲染时加载纹理(OpenGL)

[英]Load Textures while rendering (OpenGL)

I want to load some meshes including OpenGL-textures while the screen shows an animated loading screen (rendered with OpenGL). 我想在屏幕上显示一个动画加载屏幕(使用OpenGL渲染)时加载一些网格物体,包括OpenGL纹理。

So I do my loading stuff in a second thread which of course does not work, because I can't access the same GL-context from two threads. 因此,我在第二个线程中进行加载,这当然是行不通的,因为我无法从两个线程中访问相同的GL上下文。

I am using SDL 1.2 and the whole thing has to be platform independent, so I can't use wgl-stuff. 我正在使用SDL 1.2,并且整个过程必须独立于平台,因此不能使用wgl-stuff。

Which possibilities do I have? 我有哪些可能性?

Use Pixel Buffer Objects to create address space mappings for image data ( glMapBuffer ), the mapped OpenGL memory region can be accessed from all threads of the process. 使用像素缓冲区对象创建图像数据的地址空间映射( glMapBuffer ),可以从进程的所有线程访问映射的OpenGL内存区域。 Copy the image data into this using the secondary thread, then glUnmapBuffer and glTexImage from the PBO in the main thread. 使用辅助线程将图像数据复制到其中,然后从主线程中的PBO glUnmapBufferglTexImage Both unmapping and texture upload happen asynchronous so that you can already process the next image while the previous is still transferred to fast memory. 取消映射和纹理上载都是异步发生的,因此您可以在将下一个图像传输到快速内存的同时处理下一个图像。

All you need then is just a way to communicate between the threads which task to perform next and when this task has been finished. 然后,您所需要的只是在线程之间进行通信的一种方式,该线程将执行下一个任务以及该任务何时完成。

It's surely not the most elegant solution, but you can just draw your loading screen with an empty bar, load some assets and increase the bar and then draw it again. 当然,这不是最优雅的解决方案,但是您可以使用一个空的条形绘制加载屏幕,加载一些资产并增加该条形然后重新绘制。 repeat until you have loaded everything. 重复进行,直到您加载了所有内容。
in pseudo: 伪:

loadgraphics(){
  int progress=0;
  glClear(GL_COLOR_BUFFER_BIT);
  //draw you loading bar from 0 to progress
  //load something
  ++progress;
  SwapBuffers(hdc);
  glClear(GL_COLOR_BUFFER_BIT);
  //draw you loading bar from 0 to progress
  //load something
  ++progress;
  SwapBuffers(hdc);
  glClear(GL_COLOR_BUFFER_BIT);
  //draw you loading bar from 0 to progress
  //load something
  ++progress;
  SwapBuffers(hdc);
  glClear(GL_COLOR_BUFFER_BIT);
  //draw you loading bar from 0 to progress
  //load something
  ++progress;
  SwapBuffers(hdc);
  glClear(GL_COLOR_BUFFER_BIT);
  //draw you loading bar from 0 to progress
  //load something
  ++progress;
  SwapBuffers(hdc);
}  

This really is not the way to go, but it works if you just want a quick n dirty way. 这确实不是要走的路,但如果您只想快速找到n条肮脏的路,它就可以工作。

Yes you can access resources created on another thread . 是的,您可以访问在另一个线程上创建的资源。 what you need to do is create a secondary opengl context on the secondary thread and use wglShareLists to share the data between the 2 contexts. 您需要做的是在辅助线程上创建一个辅助opengl上下文,并使用wglShareLists在两个上下文之间共享数据。

Of course before using it on the main thread you need to bind it again . 当然,在主线程上使用它之前,您需要再次绑定它。

also check this question SDL / OpenGL: Implementing a "Loading thread" 还检查此问题SDL / OpenGL:实现“加载线程”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM