'glMakeContextCurrent / glViewport fatal error
I am following a book online on how to get started with GLFW. For reference, I am using Java.
I have written some code by following along with the book, but I am getting this error: FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. My code is below:
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryUtil;
public class Window {
private long window;
public Window()
{
}
public void init()
{
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
if(!GLFW.glfwInit())
{
throw new IllegalStateException("Could not initialize glfw");
}
window = GLFW.glfwCreateWindow(800, 600, "window", MemoryUtil.NULL, MemoryUtil.NULL);
if(window == MemoryUtil.NULL)
{
throw new IllegalStateException("Could not initialize window");
}
GLFW.glfwMakeContextCurrent(window);
GL11.glViewport(0, 0, 800, 600);
GLFW.glfwSetFramebufferSizeCallback(window, (window, width, height) -> {
GL11.glViewport(0, 0, width, height);
});
}
public void gameLoop()
{
while(!GLFW.glfwWindowShouldClose(window))
{
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
GLFW.glfwTerminate();
}
public void cleanup() {
GLFW.glfwDestroyWindow(window);
}
}
How should I fix this error? Also, if my code is not structured well, please let me know.
Thanks!
Solution 1:[1]
You need GL.createCapabilities() after GLFW.glfwMakeContextCurrent().
This is mentioned in the official sample code (https://www.lwjgl.org/guide).
// This line is critical for LWJGL's interoperation with GLFW's // OpenGL context, or any context that is managed externally. // LWJGL detects the context that is current in the current thread, // creates the GLCapabilities instance and makes the OpenGL // bindings available for use. GL.createCapabilities();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
