Preliminary knowledge
OpenGL: Open graphics library, or a graphics standard. GLFW: The Graphics Library For Windows is designed to create, manage Windows, increase interactivity (mouse, keyboard, and other devices), and do other things with the operating system. GLUT, FreeGLUT, GLAD GL ADdress, get the ADdress of OpenGL extension function, and bind to the corresponding function pointer, convenient to call the function GLSL OpenGL shader language, drawing image texture will be used
1. Import the relevant libraries
GLFW is used for window drawing and interaction, GLAD is used to load GLFW function pointer address
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
Copy the code
2. Initial GLFW
GlfwInit initial, version 3.3, mode for core mode, Mac extra configuration
glfwInit(a);glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
Copy the code
3. Create Windows
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL".NULL.NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(a);return - 1;
}
glfwMakeContextCurrent(window);
Copy the code
I am glad for you
Load the GLFW function pointer address
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return - 1;
}
Copy the code
5. Set the view
Framebuffer_size_callback is a callback function that modifies the window display whenever the window size changes
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0.0, width, height);
}
Copy the code
6. Keep rendering so as not to quit after rendering once
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents(a); }Copy the code
7. Set the background color
glClearColor(0.2 f.0.3 f.0.3 f.1.0 f);
glClear(GL_COLOR_BUFFER_BIT);
Copy the code
Additional features
Press esc to exit the screen
processInput(window);
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
Copy the code
Release/delete all previously allocated resources
glfwTerminate(a);Copy the code
The complete code
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main(a)
{
// glfw: initialize and configure
// ------------------------------
glfwInit(a);glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL".NULL.NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(a);return - 1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return - 1;
}
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.2 f.0.3 f.0.3 f.1.0 f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents(a); }glfwTerminate(a);return 0;
}
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0.0, width, height);
}
Copy the code