Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

c++ - OpenGL multiple rendering contexts in one window

Ok I have a program with a number of threads (loaded from DLLs at run-time). Each thread has a separate OpenGL rendering context. What I want to do is, from the main thread, be able to draw the frame buffers of each opengl contexts and draw them where I choose in the main window (if that makes any sense). I have it set up multi-threaded because, when single threaded the "run-time processes"(just glorified functions), made the hole program hang while loading resources (run-time processes, can be created and terminated any time). Basicly I want to be able to "trick" opengl into drawing into a random buffer, take that buffer and in the main thread turn it into a opengl texture and draw it where it needs to be (the position is also determined during runtime and it subject to change). My question is this, is there any way to get a rendering context to draw to some allocated memory without drawing it in a window? Also is there any way to get the frame buffer from that context. If so i would just call "glTexImage2D" and in the "bytes" argument have the pointer to the memory. Don't worry about the thread syncing, i've gotten rather good at it...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My question is this, is there any way to get a rendering context to draw to some allocated memory without drawing it in a window?

Yes. Those off-screen areas are called Framebuffer Objects (FBO).

Also is there any way to get the frame buffer from that context. If so i would just call "glTexImage2D" and in the "bytes" argument have the pointer to the memory.

In fact using FBOs you can render directly to textures. You can then use the texture to draw to a regular window.

I recommend you do not use multiple OpenGL rendering threads. The main reason is, that the GPU is a shared resource and having multiple OpenGL contexts/threads access it concurrently impairs performance. Do the renderings in sequence. You can use a single, shared context for this.

Multiple OpenGL contexts can share their data objects (textures, vertex buffer objects, pixel buffer objects); the exact API used for this depends on the operating sytem through. On Windows you use wglShareLists to make the connection. Abstract state carrier objects (FBOs, VAOs) can not be shared though; as a rule of thumb, everthing you can upload bulk data to, can be shared between contexts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...