18
$\begingroup$

I am pretty confused about that function. GLFW Context reference says as follow:

This function makes the OpenGL or OpenGL ES context of the specified window current on the calling thread. A context can only be made current on a single thread at a time and each thread can have only a single current context at a time.

But it does not help me much.
Can it work like glBind- methods? (glBindBuffer, glBindVertexArray and so on)

$\endgroup$

1 Answer 1

30
$\begingroup$

OpenGL is a state machine. An OpenGL context holds that state. The state contains information such as which textures are bound to which texture units, which attachments the current FBO has, and things like that.

When you set the current context, you are switching all the state from the old context to the new context. Here's an example:

glfwMakeContextCurrent(window1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture1);

glfwMakeContextCurrent(window2);

What this does is makes the context of window1 current. It then enables the 2D texture unit and binds texture1 to it.

Next it makes the context of window2 current. If you were to check the enabled state of GL_TEXTURE_2D it would be false (unless you had previously enabled it on the window2 context). Likewise, if you check which texture is bound to the 2D texture unit, you would likely find no texture was bound, or some texture other than texture1 was bound.

Basically all drawing happens in the current context. If you make a different context current, then all drawing will now happen in that context.

It is common to have a single context per thread in a multi-threaded OpenGL application. That way each thread is drawing into its own context and doesn't mess up the state of contexts on other threads.

$\endgroup$
3
  • 2
    $\begingroup$ I have not more than 15 score points so I cannot vote up your awesome answer. Thanks!!! $\endgroup$
    – Adrian
    Jan 17, 2017 at 0:26
  • 1
    $\begingroup$ No worries. Glad I could help. $\endgroup$ Jan 17, 2017 at 0:27
  • 1
    $\begingroup$ I read this comment so I joined computergraphics and upvoted this answer. $\endgroup$
    – KANJICODER
    Feb 27, 2018 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.