當前位置:網站首頁>第 2 篇:繪制一個窗口

第 2 篇:繪制一個窗口

2022-06-24 07:49:01代碼騎士

目錄

        一、實例化GLFW窗口

        1、GLFW初始化

        2、查看GLFW版本信息

        3、創建窗口對象 

二、GLAD

        1、初始化GLAD

        2、視口

        3、渲染 

        4、釋放資源

        三、實驗源碼

        1、顯示窗口

        2、修改背景色


一、實例化GLFW窗口

1、GLFW初始化

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

#include <iostream>

int main() {

	glfwInit();//初始化GLFW
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//設置主版本號
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//設置次版本號
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//設置核心模式
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//使用MAC系統的加上這句代碼

	return 0;
}

上面的代碼如果發生如下報錯,請查看解决辦法鏈接:(3條消息) 解决錯誤: LNK2019 無法解析的外部符號_代碼騎士的博客-CSDN博客

 2、查看GLFW版本信息

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

調用這個函數輸出版本信息

3、創建窗口對象 

void createWindow() {
	GLFWwindow* window = glfwCreateWindow(800, 600, "window", NULL, NULL);//參數:寬,高,名稱
	if (window == NULL) {//創建失敗
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return;
	}
	glfwMakeContextCurrent(window);
}

 

二、GLAD

1、初始化GLAD

glad是用來管理OpenGL的函數指針的,所以在調用任何OpenGL的函數之前我們都需要初始化GLAD

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

2、視口

在開始渲染之前,我們要先設置OpenGL渲染窗口的尺寸大小,即視口(ViewPort)。目的是使OpenGL知道窗口的大小和坐標。我們可以通過調用glViewport函數設置窗口維度(Dimension):

glViewport(0, 0, 800, 600);//參數:前兩個是窗口左下角坐標,後兩個是渲染窗口的寬和高(像素)

注意:

回調函數:

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、渲染 

為了使我們的窗口能够持續渲染並且可以接收到用戶輸入,我們要在程序中添加一段while循環,也可稱之為渲染循環(Render Loop),它能够在我們讓GLF退出前一直保持運行。

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

函數說明:

 

 

4、釋放資源

 

glfwTerminate();

 

三、實驗源碼

1、顯示窗口

#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);//參數:寬,高,名稱
	if (window == NULL) {//創建失敗
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return window;
	}
	glfwMakeContextCurrent(window);
	//_getch();
	return window;
}

//回調函數
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

int main() {

	glfwInit();//初始化GLFW
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//設置主版本號
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//設置次版本號
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//設置核心模式
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//使用MAC系統的加上這句代碼
	
	getVersion();
	
	//createWindow();
	
	GLFWwindow* window = createWindow();

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

	//glViewport(0, 0, 800, 600);//參數:前兩個是窗口左下角坐標,後兩個是渲染窗口的寬和高(像素)
	framebuffer_size_callback(window, 800, 600);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

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

 輸出結果:

 

2、修改背景色

渲染指令代碼結構:

//渲染循環
	while (!glfwWindowShouldClose(window))
	{
		//輸入
		processInput(window);

		//渲染指令
		//……

		//檢測並調用事件,交換緩沖
		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);//參數:寬,高,名稱
	if (window == NULL) {//創建失敗
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return window;
	}
	glfwMakeContextCurrent(window);
	//_getch();
	return window;
}

//回調函數
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

int main() {

	glfwInit();//初始化GLFW
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//設置主版本號
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//設置次版本號
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//設置核心模式
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//使用MAC系統的加上這句代碼
	
	getVersion();
	
	//createWindow();
	
	GLFWwindow* window = createWindow();

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

	//glViewport(0, 0, 800, 600);//參數:前兩個是窗口左下角坐標,後兩個是渲染窗口的寬和高(像素)
	framebuffer_size_callback(window, 800, 600);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	//渲染循環
	while (!glfwWindowShouldClose(window))
	{
		//輸入
		//processInput(window);
		//渲染指令
		glClearColor(0.0f, 0.34f, 0.57f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		//檢測並調用事件,交換緩沖
		glfwPollEvents();
		glfwSwapBuffers(window);
	}
	glfwTerminate();
	return 0;
}

 

 

版權聲明
本文為[代碼騎士]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/175/202206240351284007.html

隨機推薦