} publicvoidrun() { // Initialize and configure GLFW init(); // Create the window and the OpenGL context createWindow(); // Load all OpenGL function pointers loadGL(); // Render loop loop(); // Terminate GLFW and free resources terminate(); }
privatevoidinit() { // Set up an error callback to print to System.err GLFWErrorCallback.createPrint(System.err).set();
// Initialize GLFW if (!glfwInit()) { thrownewIllegalStateException("Unable to initialize GLFW"); }
// Set window hints for OpenGL version and profile glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Uncomment this line for MacOS compatibility // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); }
privatevoidcreateWindow() { // Create the window window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { thrownewRuntimeException("Failed to create the GLFW window"); }
// Make the window's OpenGL context current glfwMakeContextCurrent(window);
// Set up a window size callback to adjust the viewport glfwSetWindowSizeCallback(window, newGLFWWindowSizeCallbackI() { @Override publicvoidinvoke(long window, int width, int height) { // Make sure the viewport matches the new window dimensions glViewport(0, 0, width, height); } }); }
privatevoidloadGL() { // Create the OpenGL capabilities object GLCapabilitiescaps= GL.createCapabilities();
// Check if the capabilities object is null if (caps == null) { thrownewIllegalStateException("Failed to create GL capabilities"); } }
privatevoidloop() { // Set the clear color to red glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
// Loop until the user closes the window while (!glfwWindowShouldClose(window)) { // Process input processInput();
// Clear the color buffer glClear(GL_COLOR_BUFFER_BIT);
// Swap the front and back buffers glfwSwapBuffers(window);
// Poll for window events glfwPollEvents(); } }
privatevoidprocessInput() { // Check if the user pressed the Esc key if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { // Set the window's close flag to true glfwSetWindowShouldClose(window, true); } }
privatevoidterminate() { // Free the window callbacks and destroy the window glfwFreeCallbacks(window); glfwDestroyWindow(window);
// Terminate GLFW and free the error callback glfwTerminate(); glfwSetErrorCallback(null).free(); }