This series of articles are based on their own understanding and practice, there may be wrong places, welcome to correct.

Introduction to the

I believe that friends have more or less heard of opengL this keyword, more related to it is the rendering of 2D, 3D model, can be used to do games and so on, a listen to more difficult complex, DO not know how to use.

When you look at it, it’s a little bit complicated, and the difficulty is

  • There are many functions, rendering a simple graph requires a lot of function calls, and the process is complicated
  • The GLSL shader language is hard to understand
  • The use of process-oriented thinking, the long-term use of Java and other object-oriented programming, feel the call is very awkward

On mobile phones, there are two components, one is CPU, the other is GPU. There are also two ways to display graphical interfaces on mobile phones. One is to use CPU to render, and the other is to use GPU to render. It can be said that GPU rendering is actually a kind of hardware acceleration.

The reason why GPU can greatly improve rendering speed is because GPU is best at parallel floating-point arithmetic, which can be used to do parallel arithmetic on many, many pixels.

OpenGL (Open Graphics Library) is a tool for indirectly operating GPU. It is a set of defined cross-platform and cross-language Graphics API, the underlying Graphics Library that can be used for 2D and 3D picture rendering, and the programming interface specifically implemented by various hardware manufacturers.

Core mode vs. Immediate render mode

Early Versions of OpenGL used Immediate mode (i.e., fixed rendering pipeline), which made it easy to draw graphics. Most of OpenGL’s functionality is hidden away from libraries, and developers have little freedom to control how OpenGL performs its calculations. Developers are desperate for more flexibility. Over time, as specifications became more flexible, developers gained more control over the details of their drawings. Render now mode is easy to use and understand, but inefficient. So starting with OpenGL3.2, the specification documentation began to scrap the render now mode and encouraged developers to develop under OpenGL’s Core-profile, a branch of the specification that completely removed the old features.

OpenGL forces us to use modern functions when using its core mode. When we try to use a deprecated function, OpenGL throws an error and terminates the drawing. Modern functions have the advantage of being more flexible and efficient, but they are also harder to learn. Render now abstracts a lot of detail from how OpenGL actually works, so while it’s easy to learn, it’s hard to grasp how OpenGL actually works. Modern functions require a real understanding of OpenGL and graphical programming, which is somewhat difficult, but provides more flexibility, greater efficiency, and more importantly, a deeper understanding of graphical programming.

The developer of the actual OpenGL library is usually the producer of the graphics card. The version of OpenGL supported by the graphics card you purchased was developed specifically for this series of graphics cards, and a bug can usually be resolved by upgrading the graphics card driver. These drivers will include the latest version of OpenGL that your graphics card can support, which is why it is always advisable to update your graphics card drivers occasionally.

The development environment

Since OpenGL is a graphics API and not a standalone platform, it needs a programming language to work, and we’re using C++ here. Therefore, proficiency in C++ is essential in this tutorial.

Tools: Visual Studio 2019

Environment development configuration: please refer to blog.csdn.net/qq_36696486…

And then write the code

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);

int main(a) {
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow *window = glfwCreateWindow(800.600."LearnOpenGL".NULL.NULL);
	if (window == NULL) {
		cout << "Failed to create GLFW window" << endl;
		glfwTerminate();
		return - 1;
	}
	glfwMakeContextCurrent(window);

	if(! gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {std: :cout << "Failed to initialize GLAD" << std: :endl;
		return - 1;
	}

	glViewport(0.0.800.600);

	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	while(! glfwWindowShouldClose(window)) { glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate();return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0.0, width, height);
}

Copy the code

If the configuration is successful and a black window is displayed, the configuration is successful. Congratulations on the first step