Hi all,
Please consider this example:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <std_lib_facilities_4.h>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow*);
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
GLFWwindow* window = glfwCreateWindow(800, 600, "Hello OpenGL!", nullptr, nullptr);
if (window == nullptr)
{
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// glad: load all OpenGL function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
while (!glfwWindowShouldClose(window))
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
processInput(window);
glfwSwapBuffers(window);
//check and call events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
//*******************************************
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
//******************************************
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
1- Why do we need glfwPollEvents(); here? Apparently processInput(window); gets our Esc key when pressed.
2- This function:
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{ ** *
glViewport(0, 0, width, height);
}
is called by this statement: glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); Here the parameters width and height aren’t set, so how does the function above set its arguments to be used by glViewport?