Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Some questions about a first example of OpenGL

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    29

    Some questions about a first example of OpenGL

    Hi all,

    Please consider this example:
    Code :
    #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:
    Code :
    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?

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,728
    Quote Originally Posted by Tomyfr View Post
    1- Why do we need glfwPollEvents(); here? Apparently processInput(window); gets our Esc key when pressed.
    Because glfwPollEvents is for more than just keyboard input.

    Quote Originally Posted by Tomyfr View Post
    2- This function ... 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?
    The function is not called. All that this does is register a callback with GLFW. GLFW internals will call the function at an arbitrary later time, and will provide the missing arguments.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    29
    Because glfwPollEvents is for more than just keyboard input.
    But I thought the key used here Esc is of processInput(window); to deal with not that glfwPollEvents! So while processInput(window); is for that key why do we still need glfwPollEvents, please?

    GLFW internals will call the function at an arbitrary later time, and will provide the missing arguments.
    So should always the function framebuffer_size_callback have this sort/kind of arguments and that body code so that it is suitable for GLFW to call later?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •