Beginner OpenGL (1): Preparations

    Because the lab is going to do a 3D eyeball reconstruction project, the boss asked me to familiarize myself with OpenGL programming. The following content refers to LearnOpenGL CN, and the github address pokes me .

    1. Environment configuration

    GLFW+GLAD+VS2017, please refer to the above tutorial for the specific process.

    GLFW is a C library specific to OpenGL that provides some minimal interfaces required to render objects. It allows users to create OpenGL contexts, define window parameters, and handle user input.

    Because OpenGL is just a standard/specification, the specific implementation is implemented by the driver developer for a specific graphics card. Due to the large number of OpenGL driver versions, the location of most of its functions cannot be determined at compile time and needs to be queried at runtime. So the task falls to the developer, who needs to get the function address at runtime and save it in a function pointer for later use. Fortunately, there are libraries that simplify this process, with GLAD being the newest and most popular library out there.

    Configure project properties: New project - Solution right-click properties - VC++ directory include directory, library directory, add include and libc folders respectively

Linker--input--add glfw3.lib, opengl32.lib.

    In order to create a project in the future without further configuration, click View--Other Windows--Properties Manager to directly set the parent properties.

    Check the OpenGL version on your computer: Install the OpenGL extensions viewer and check the version number. Considering compatibility issues, follow the tutorial to use version 3.3.

    Need to add when using:

#include <glad\glad.h>  
#include <GLFW\glfw3.h>  
    Note that GLAD's headers include the correct OpenGL headers (eg GL/gl.h), so GLAD needs to be included before any other OpenGL-dependent headers.

    2. Create a window

    Instantiate a GLFW window:

// glfw: initialize and configure  
	// ------------------------------  
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    Set the version number to 3.3 and use kernel mode.

    Then create the window object:

	 // glfw window creation  
	// --------------------  
	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
	if (window == NULL)
	{
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);
     GLAD is used to manage OpenGL function pointers, so we need to initialize GLAD before calling any OpenGL functions:
	// glad: load all OpenGL function pointers  
	// ---------------------------------------  
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

    There is one more important thing to do before we start rendering, we must tell OpenGL the size of the rendering window, that is, the Viewport, so that OpenGL can only know how to display data and coordinates according to the size of the window. We can set the dimension of the window by calling the glViewport function. The first two parameters of the glViewport function control the position of the lower left corner of the window. The third and fourth parameters control the width and height (pixels) of the render window.

// glfw: whenever the window size changed (by OS or user resize) this callback function executes  
// ---------------------------------------------------------------------------------------------  
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	// make sure the viewport matches the new window dimensions; note that width and   
	// height will be significantly larger than specified on retina displays.  
	glViewport(0, 0, width, height);
}

    Here, a callback function (Callback Function) is registered for the window, which is called every time the window size changes.

    We also need to register this function to tell GLFW that we want this function to be called whenever the window is resized, and also when it is first displayed:

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    In order to keep it displayed until we actively close it, the image needs to be drawn continuously, for which we need to add a Render Loop:
        // render loop  
	// -----------  
	while (!glfwWindowShouldClose(window))
	{
		// input  
		// -----  
		processInput(window);

		// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)  
		// -------------------------------------------------------------------------------  
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

    The glfwWindowShouldClose function checks at the beginning of each loop to see if GLFW has been asked to exit, if so the function returns true and the rendering loop is over, after which we can close the application.

    ProcessInput checks if the user pressed the ese key:

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly  
// ---------------------------------------------------------------------------------------------------------  
void processInput(GLFWwindow *window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
}

    The glfwSwapBuffers function will swap the color buffer (a large buffer that stores the color value of each pixel in the GLFW window), which is used for drawing in this iteration, and will be displayed on the screen as output.

    The glfwPollEvents function checks whether any events are triggered (such as keyboard input, mouse movement, etc.), updates the window state, and calls the corresponding callback function (which can be manually set through the callback method).




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520632&siteId=291194637