当前位置:网站首页>第 2 篇:繪制一個窗口
第 2 篇:繪制一個窗口
2022-06-24 07:49:00 【代碼騎士】
目錄
一、實例化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;
}
边栏推荐
猜你喜欢

Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its

开放合作,共赢未来 | 福昕鲲鹏加入金兰组织

Maxcompute remote connection, uploading and downloading data files

Baidu map, coordinate inversion, picking coordinate position

面试中的最常被问到的两种锁

【Django中运行scrapy框架,并将数据存入数据库】

LeetCode 207:课程表(拓扑排序判断是否成环)

Moonwell Artemis现已上线Moonbeam Network

Hongmeng OS development III

本地备份和还原 SQL Server 数据库
随机推荐
opencvsharp二值图像反色
Global and Chinese market of inline drip irrigation 2022-2028: Research Report on technology, participants, trends, market size and share
How to realize high stability and high concurrency of live video streaming transmission and viewing?
Pair class notes
Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
闲谈:3AC到底发生了什么?
Win10 build webservice
《canvas》之第4章 线条操作
Reppoints: Microsoft skillfully uses deformation convolution to generate point sets for target detection, full of creativity | iccv 2019
any类备注
MySQL case: analysis of full-text indexing
IndexError: Target 7 is out of bounds.
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
Detailed explanation of C language compilation, link and operation
RDD的执行原理
后疫情时代下,家庭服务机器人行业才刚启航
Deploy L2TP in VPN (Part 1)
New ways to play web security [6] preventing repeated use of graphic verification codes
Shell script for MySQL real-time synchronization of binlog
Shader 常用函数