当前位置:网站首页>第 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;
}
边栏推荐
- Event related | reveal how Ti-One's support ability for large-scale events is developed
- C# Lambda
- Tidb operator source code reading (IV) control cycle of components
- 鸿蒙开发四
- 解决 These dependencies were not found: * core-js/modules/es6.array.fill in xxx 之类的问题
- Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
- Reppoints: Microsoft skillfully uses deformation convolution to generate point sets for target detection, full of creativity | iccv 2019
- 『C语言』系统日期&时间
- 免费ICP域名备案查接口
- Thread blocking
猜你喜欢
随机推荐
Take my brother to make a real-time Leaderboard
Terminal network in VPN client connection settings of router
L2tp/ipsec one click installation script
调用Feign接口时指定ip
鸿蒙os开发三
[special session] SME growth plan - ECS special session
【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA
简单的折射效果
The startup mode of cloudbase init is \Cloudbase init has hidden dangers
10 common malware detection and analysis platforms
使用 kubeconfig 文件组织集群访问
Super fast reading in OI
《canvas》之第2章 直线图形
A summary of the posture of bouncing and forwarding around the firewall
用Ngrok 配置属于自己的免费外网域名
POM configuration provided and test
Tidb operator source code reading (IV) control cycle of components
any类备注
Deploy L2TP in VPN (Part 2)
线程的支持








