当前位置:网站首页>第 2 篇:绘制一个窗口
第 2 篇:绘制一个窗口
2022-06-24 06:47: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;
}
边栏推荐
- LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
- Maxcompute remote connection, uploading and downloading data files
- UTC、GMT、CST
- 云开发谁是卧底小程序源码
- [Lua language from bronze to king] Part 2: development environment construction +3 editor usage examples
- tuple(元组)备注
- 用Ngrok 配置属于自己的免费外网域名
- 使用 kubeconfig 文件组织集群访问
- What should I pay attention to after the live broadcast system source code is set up?
- Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

爬虫基础B1——Scrapy(B站学习笔记)
![LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路](/img/16/011ba3aef1315c39526daac7e3ec89.png)
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路

Oracle-高级SQL限定查询

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

Free ICP domain name filing interface

保留一位小数和保留两位小数

OpenGauss数据库在 CentOS 上的实践,配置篇

屏幕截图推荐—Snipaste

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

GPU is not used when the code is running
随机推荐
Hongmeng OS development III
免费ICP域名备案查接口
C# Lambda
智能指针备注
tuple(元组)备注
用Ngrok 配置属于自己的免费外网域名
Super fast reading in OI
UTC、GMT、CST
. No main manifest attribute in jar
开放合作,共赢未来 | 福昕鲲鹏加入金兰组织
Alibaba cloud full link data governance
The describeregion interface of CVM is special and has two versions
Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
pair类备注
10. Tencent cloud IOT device side learning - firmware upgrade
位运算
Bit operation
Session & cookie details
L2tp/ipsec one click installation script
常见的数组封装