There is no OpenGL context current in the current thread

Started by silent_ptr, August 03, 2020, 13:28:14

Previous topic - Next topic

silent_ptr

For some reason I have this code:

GLFW.glfwMakeContextCurrent(gameInstance.window.windowHandle);
System.out.println("made context current");
GL.createCapabilities();


I still get an error when I create capabilities saying I do not have an OpenGL current context. All GLFW and GL functions are called in the same thread. Here is my full code:

private void gameThread()
{
	if (gameInstance == null)
	{
		System.err.println("game is null");
		return;
	}
	else if (!GLFW.glfwInit())
	{
		System.err.println("could not initialise GLFW");
		return;
	}
	
	GLFW.glfwWindowHint(GLFW.GLFW_VERSION_MAJOR, 3);
	GLFW.glfwWindowHint(GLFW.GLFW_VERSION_MINOR, 3);
	GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
	GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
	
	resolutions.clear();
	GLFWVidMode.Buffer b = GLFW.glfwGetVideoModes(GLFW.glfwGetPrimaryMonitor());
	
	while (b.hasRemaining())
	{
		GLFWVidMode vidMode = b.get();
		boolean alreadyIn = false;
		
		for (Resolution r : resolutions)
		{
			if (r.getWidth() == vidMode.width() && r.getHeight() == vidMode.height())
			{
				alreadyIn = true;
			}
		}
		
		if (!alreadyIn)
		{
			resolutions.add(new Resolution(vidMode.width(), vidMode.height()));
		}
	}
	
	gameInstance.initialise();
	
	if (gameInstance.window == null)
	{
		GLFW.glfwTerminate();
		System.err.println("window is null");
		return;
	}
	
	gameInstance.window.windowHandle = GLFW.glfwCreateWindow(
	gameInstance.window.resolution.getWidth(), gameInstance.window.resolution.getHeight(), gameInstance.window.title,
	gameInstance.window.fullscreen ? GLFW.glfwGetPrimaryMonitor() : 0, 0);
	
	if (gameInstance.window.verticalSync)
	{
		GLFW.glfwSwapInterval(1);
	}
	
	GLFW.glfwMakeContextCurrent(gameInstance.window.windowHandle);
	System.out.println("made context current");
	GL.createCapabilities();
	
	while (!GLFW.glfwWindowShouldClose(gameInstance.window.windowHandle))
	{
		GL33.glClear(GL33.GL_COLOR_BUFFER_BIT | GL33.GL_DEPTH_BUFFER_BIT);
		GLFW.glfwSwapBuffers(gameInstance.window.windowHandle);
		GLFW.glfwPollEvents();
	}
	
	gameRunning.set(false);
	GLFW.glfwDestroyWindow(gameInstance.window.windowHandle);
	GLFW.glfwTerminate();
}


What did I do wrong?

spasi

Please make sure that gameInstance.window.windowHandle is not NULL and that GLFW has not reported any errors (you need to register a GLFW error handler for that, try GLFWErrorCallback.createPrint(System.err).set()).

silent_ptr

I was doing GLFW_VERSION_MAJOR and GLFW_VERSION_MINOR when it should have been GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR (additionally glfwSwapInterval can only be done after a context is made current). Thanks for the help!