[VS2013] build OpenGL and C + + console program environment and form drawing

  1. OpenGL learning reference website learnopengl.cn

  2. download GLFW . Establish the GLFW environment and ensure that it properly creates the OpenGL context and displays the window. GLFW already has a precompiled binary version and corresponding header file for Visual Studio 2013/2015... / 2019, but for completeness, we will start from compiling the source code. So we need to download the source code package. [32-bit is preferred, and there are some problems with 64 bit at present.]

  3. download GLAD open source library , GLAD uses an online service. Here, we can tell GLAD the OpenGL version that needs to be defined, and load all relevant opengl functions according to this version. Because OpenGL is only 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 run time. Therefore, the task falls on the developer. The developer needs to obtain the function address at runtime and save it in a function pointer for later use.

  4. Unzip the two downloaded files

  5. Add glfw-3.3 6.bin. Copy and paste the include and vc2013 under Win32 into the new glfw2013 folder

  6. The dynamic link library glfw3 in lib-vc2013 under glfw2013 folder Copy lib to C:\Windows\SysWOW64 directory (dynamic link library configuration completed)

  7. Create a new project TEST and copy glad and glfw2013 folders to the project directory

  8. Open the project and configure include. Project - attribute - C/C + +, and the include folders of glfw2013 and glad under TEST project are attached respectively.

  9. Configure static links. Linker - input - additional dependencies, manually enter glfw3 lib.

  10. Specifies the statically linked road stiffness. Linker - General - Additional Library Directory (specify to lib-vc2013 folder of glfw2013 under TEST project)

  11. Glad. Is included in the project C documents. Right click TEST - add - existing class - glad c

  12. This is the end of environment construction.

  13. Draw the window. Replace the following code with test CPP code, Ctrl+F5 to run the program.

#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "iostream"
 
//settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
 
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
void clearWindowByColor(GLFWwindow *window);
 
int main()
{
	/* initialization */
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//Major version number (Major)=3
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//Minor version number (Minor)=3
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//GLFW uses core mode
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//mac terminal settings
	
	/* Create form object */
	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);//★
 
	/* Register window size change callback function */
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
 
	/* Initialize GLAD */
	//Before calling any OpenGL function, we need to initialize GLAD
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}
 
	/* Render Loop  */
	while (!glfwWindowShouldClose(window))
	{
		clearWindowByColor(window);
		/* input */
		processInput(window);
 
		/* Rendering instructions */
 
		/* Check and invoke events, swap buffers */
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
 
	/* Correctly release / delete all previously allocated resources */
	glfwTerminate();
	return 0;
}
 
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	/* Set viewport size*/
	glViewport(0, 0, width, height);//The first two parameters control the position of the lower left corner of the window
}
 
void processInput(GLFWwindow *window)
{
	/* Press ESC to close the window */
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
}
 
void clearWindowByColor(GLFWwindow *window)
{
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
}
  1. Run results. The environment was built successfully.

Keywords: C++ OpenGL

Added by yaba on Wed, 29 Dec 2021 22:15:36 +0200