当前位置:网站首页>Part 2: drawing a window

Part 2: drawing a window

2022-06-24 07:49:00 Code Knight

Catalog

         One 、 Instantiation GLFW window

        1、GLFW initialization

        2、 see GLFW Version information

        3、 Create window objects  

Two 、GLAD

        1、 initialization GLAD

        2、 viewport

        3、 Rendering  

        4、 Release resources

         3、 ... and 、 Experiment source code

        1、 Display window

        2、 Modify background color


One 、 Instantiation GLFW window

1、GLFW initialization

#include <glad/glad.h> 
#include <GLFW/glfw3.h>

#include <iostream>

int main() {

	glfwInit();// initialization GLFW
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);// Set the major version number 
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);// Set the minor version number 
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// Set the core mode 
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// Use MAC Add this code systematically 

	return 0;
}

If the following error occurs in the above code , Please check the solution link :(3 Bar message ) Solve mistakes : LNK2019 Unresolved external symbols _ Code Knight's blog -CSDN Blog

 2、 see GLFW Version information

void getVersion() {
	int Major, Minor, Rev;
	glfwGetVersion(&Major, &Minor, &Rev);
	printf("GLFW %d.%d.%d initialized\n", Major, Minor, Rev);
}

Call this function to output version information

3、 Create window objects  

void createWindow() {
	GLFWwindow* window = glfwCreateWindow(800, 600, "window", NULL, NULL);// Parameters : wide , high , name 
	if (window == NULL) {// Create failure 
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return;
	}
	glfwMakeContextCurrent(window);
}

 

Two 、GLAD

1、 initialization GLAD

glad Is to manage OpenGL Of the function pointer , So call any OpenGL We all need to initialize before the function of GLAD

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

2、 viewport

Before you start rendering , We need to set up OpenGL The size of the rendering window , That's the viewport (ViewPort). The aim is to make OpenGL Know the size and coordinates of the window . We can do it by calling glViewport Function to set the window dimension (Dimension):

glViewport(0, 0, 800, 600);// Parameters : The first two are the coordinates of the lower left corner of the window , The last two are the width and height of the rendering window ( Pixels )

Be careful :

Callback function :

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

 

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

 

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

 

3、 Rendering  

In order for our window to render continuously and receive user input , We will add a paragraph to the program while loop , It can also be called a render loop (Render Loop), It can make us GLF Keep running until you exit .

while (!glfwWindowShouldClose(window))
	{
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

Function description :

 

 

4、 Release resources

 

glfwTerminate();

 

3、 ... and 、 Experiment source code

1、 Display window

#include <glad/glad.h> 
#include <GLFW/glfw3.h>

#include <iostream>
#include<conio.h>

void getVersion() {
	int Major, Minor, Rev;
	glfwGetVersion(&Major, &Minor, &Rev);
	printf("GLFW %d.%d.%d initialized\n", Major, Minor, Rev);
}

GLFWwindow* createWindow() {
	GLFWwindow* window = glfwCreateWindow(800, 600, "window", NULL, NULL);// Parameters : wide , high , name 
	if (window == NULL) {// Create failure 
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return window;
	}
	glfwMakeContextCurrent(window);
	//_getch();
	return window;
}

// Callback function 
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

int main() {

	glfwInit();// initialization GLFW
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);// Set the major version number 
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);// Set the minor version number 
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// Set the core mode 
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// Use MAC Add this code systematically 
	
	getVersion();
	
	//createWindow();
	
	GLFWwindow* window = createWindow();

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

	//glViewport(0, 0, 800, 600);// Parameters : The first two are the coordinates of the lower left corner of the window , The last two are the width and height of the rendering window ( Pixels )
	framebuffer_size_callback(window, 800, 600);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

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

  Output results :

 

2、 Modify background color

Rendering instruction code structure :

// Render loop 
	while (!glfwWindowShouldClose(window))
	{
		// Input 
		processInput(window);

		// Rendering instructions 
		//……

		// Detect and invoke events , Exchange buffer 
		glfwPollEvents();
		glfwSwapBuffers(window);
	}

 

 

#include <glad/glad.h> 
#include <GLFW/glfw3.h>

#include <iostream>
#include<conio.h>

void getVersion() {
	int Major, Minor, Rev;
	glfwGetVersion(&Major, &Minor, &Rev);
	printf("GLFW %d.%d.%d initialized\n", Major, Minor, Rev);
}

GLFWwindow* createWindow() {
	GLFWwindow* window = glfwCreateWindow(800, 600, "window", NULL, NULL);// Parameters : wide , high , name 
	if (window == NULL) {// Create failure 
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return window;
	}
	glfwMakeContextCurrent(window);
	//_getch();
	return window;
}

// Callback function 
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

int main() {

	glfwInit();// initialization GLFW
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);// Set the major version number 
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);// Set the minor version number 
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// Set the core mode 
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// Use MAC Add this code systematically 
	
	getVersion();
	
	//createWindow();
	
	GLFWwindow* window = createWindow();

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

	//glViewport(0, 0, 800, 600);// Parameters : The first two are the coordinates of the lower left corner of the window , The last two are the width and height of the rendering window ( Pixels )
	framebuffer_size_callback(window, 800, 600);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	// Render loop 
	while (!glfwWindowShouldClose(window))
	{
		// Input 
		//processInput(window);
		// Rendering instructions 
		glClearColor(0.0f, 0.34f, 0.57f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		// Detect and invoke events , Exchange buffer 
		glfwPollEvents();
		glfwSwapBuffers(window);
	}
	glfwTerminate();
	return 0;
}

 

 

原网站

版权声明
本文为[Code Knight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240351284007.html