当前位置:网站首页>第 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;
}
边栏推荐
- Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
- Phonics
- pair类备注
- [Lua language from bronze to king] Part 2: development environment construction +3 editor usage examples
- Take my brother to make a real-time Leaderboard
- Domain environment importing Tencent cloud considerations
- 位运算
- [special session] SME growth plan - ECS special session
- 【Django中运行scrapy框架,并将数据存入数据库】
- 云开发谁是卧底小程序源码
猜你喜欢

基于Distiller的模型压缩工具简介

GPU is not used when the code is running

鸿蒙os开发三

图形技术之管线概念

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

ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决

Blue Bridge Cup seven segment code (dfs/ shape pressing + parallel search)

Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible

闲谈:3AC到底发生了什么?

2022年PMP项目管理考试敏捷知识点(1)
随机推荐
[special session] SME growth plan - ECS special session
科一易错点
What should I pay attention to after the live broadcast system source code is set up?
.jar中没有主清单属性
C code writing specification
10. Tencent cloud IOT device side learning - firmware upgrade
【NILM】非入侵式负荷分解模块nilmtk安装教程
希尔伯特-黄变换
Counter attack from outsourcing to big factories! Android has been developed for 5 years, and after a year of dormancy, it has tried to become an offer harvester. Tencent has a fixed salary of 20*15
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
New features of PHP: bytecode cache and built-in server
Deploy L2TP in VPN (Part 1)
热赛道上的冷思考:乘数效应才是东数西算的根本要求
UE常用控制臺命令
Global and Chinese market of offshore furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Anaconda 中使用 You Get
位运算
Oracle-高级SQL限定查询
Commandes de console communes UE
Tidb operator source code reading (IV) control cycle of components