OpenGL learning notes: GLAD and first window

Keywords: Windows github

Environmental Science

System: Windows 10 64 bit home Chinese version
IDE: Visual Studio 2017 professional

Reference tutorial: https://learnopengl-cn.github.io/01 Getting started/03 Hello Window/

step

1. Get GLAD:
a. Open the online service
b. Set Language to C/C + +, Specification to OpenGL, gl option in API to version 3.3 (or above), Profile to Core, and select Generate a loader to ignore other contents temporarily, as shown in the figure below. Finally, click GENERATE to GENERATE library files;

c. Select glad.zip from the webpage after jump to download it, as shown in the following figure:

d. Extract the downloaded glad.zip file, and you will get the three files we need: include\glad\glad.h, include\KHR\khrplatform.h and src\glad.c.
2. Create a new Visual Studio project, add the three files from step 1 to the project, and add a main.cpp file to the project, and then copy the following code to the main.cpp file:

#Include < glad \ glad. H > / / this must be placed before GLFW
#include <GLFW\glfw3.h>

#Include < iostream > / / this is to use print statements

//Function declaration
void framebuffer_size_callback(GLFWwindow* window, int width, int height);   //Window size setting callback function
void processInput(GLFWwindow* window);										 //Input processing function

//Constant definition
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

//Main function
int main()
{
	glfwInit();	 //glfw initialization

	//Set OpenGL version and other information
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	//Create GLFW window
	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;
	}
	//Set current context
	glfwMakeContextCurrent(window);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	//Import function pointer of OpenGL to GLAD
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	//Loop rendering
	while (!glfwWindowShouldClose(window))
	{
		processInput(window);

		//Swap cache
		glfwSwapBuffers(window);
		//event processing 
		glfwPollEvents();
	}

	//Stop it
	glfwTerminate();

	return 0;
}

//Window size setting callback function
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	glViewport(0, 0, width, height);
}

//Input processing function
void processInput(GLFWwindow* window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
	{
		glfwSetWindowShouldClose(window, true);
	}
}

3. Compile and run the code to get a window, as shown below:

Posted by brob on Sat, 21 Dec 2019 13:35:17 -0800