当前位置:网站首页>Part 2: drawing a window
Part 2: drawing a window
2022-06-24 07:49:00 【Code Knight】
Catalog
One 、 Instantiation GLFW window
2、 see GLFW Version information
3、 ... and 、 Experiment source code
One 、 Instantiation GLFW window
1、GLFW initialization
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main() {
glfwInit();// initialization GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);// Set the major version number
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);// Set the minor version number
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// Set the core mode
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// Use MAC Add this code systematically
return 0;
}If the following error occurs in the above code , Please check the solution link :(3 Bar message ) Solve mistakes : LNK2019 Unresolved external symbols _ Code Knight's blog -CSDN Blog

2、 see GLFW Version information
void getVersion() {
int Major, Minor, Rev;
glfwGetVersion(&Major, &Minor, &Rev);
printf("GLFW %d.%d.%d initialized\n", Major, Minor, Rev);
}
Call this function to output version information

3、 Create window objects
void createWindow() {
GLFWwindow* window = glfwCreateWindow(800, 600, "window", NULL, NULL);// Parameters : wide , high , name
if (window == NULL) {// Create failure
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
}
Two 、GLAD
1、 initialization GLAD
glad Is to manage OpenGL Of the function pointer , So call any OpenGL We all need to initialize before the function of GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}2、 viewport
Before you start rendering , We need to set up OpenGL The size of the rendering window , That's the viewport (ViewPort). The aim is to make OpenGL Know the size and coordinates of the window . We can do it by calling glViewport Function to set the window dimension (Dimension):
glViewport(0, 0, 800, 600);// Parameters : The first two are the coordinates of the lower left corner of the window , The last two are the width and height of the rendering window ( Pixels )Be careful :

Callback function :

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、 Rendering
In order for our window to render continuously and receive user input , We will add a paragraph to the program while loop , It can also be called a render loop (Render Loop), It can make us GLF Keep running until you exit .
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}Function description :



4、 Release resources

glfwTerminate();3、 ... and 、 Experiment source code
1、 Display 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);// Parameters : wide , high , name
if (window == NULL) {// Create failure
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return window;
}
glfwMakeContextCurrent(window);
//_getch();
return window;
}
// Callback function
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
int main() {
glfwInit();// initialization GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);// Set the major version number
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);// Set the minor version number
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// Set the core mode
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// Use MAC Add this code systematically
getVersion();
//createWindow();
GLFWwindow* window = createWindow();
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
//glViewport(0, 0, 800, 600);// Parameters : The first two are the coordinates of the lower left corner of the window , The last two are the width and height of the rendering window ( Pixels )
framebuffer_size_callback(window, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}Output results :

2、 Modify background color
Rendering instruction code structure :
// Render loop
while (!glfwWindowShouldClose(window))
{
// Input
processInput(window);
// Rendering instructions
//……
// Detect and invoke events , Exchange buffer
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);// Parameters : wide , high , name
if (window == NULL) {// Create failure
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return window;
}
glfwMakeContextCurrent(window);
//_getch();
return window;
}
// Callback function
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
int main() {
glfwInit();// initialization GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);// Set the major version number
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);// Set the minor version number
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// Set the core mode
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// Use MAC Add this code systematically
getVersion();
//createWindow();
GLFWwindow* window = createWindow();
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
//glViewport(0, 0, 800, 600);// Parameters : The first two are the coordinates of the lower left corner of the window , The last two are the width and height of the rendering window ( Pixels )
framebuffer_size_callback(window, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Render loop
while (!glfwWindowShouldClose(window))
{
// Input
//processInput(window);
// Rendering instructions
glClearColor(0.0f, 0.34f, 0.57f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Detect and invoke events , Exchange buffer
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
边栏推荐
- Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
- Hongmeng OS development III
- Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
- 希尔伯特-黄变换
- 《canvas》之第4章 线条操作
- Los Angeles p1051 who won the most Scholarships
- opencvsharp二值图像反色
- 火线,零线,地线,你知道这三根线的作用是什么吗?
- Opencvsharp binary image anti color
- exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
猜你喜欢

语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)

光照使用的简单总结

What kind of experience is it when the Institute earns 20000 yuan a month!

【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA

Cloud development who is the source code of undercover applet

RDD的执行原理

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

Moonwell Artemis现已上线Moonbeam Network

图形技术之管线概念

『C语言』系统日期&时间
随机推荐
Domain environment importing Tencent cloud considerations
New features of PHP: bytecode cache and built-in server
Opencvsharp binary image anti color
timer使用备注
Thread considerations
C code writing specification
Global and Chinese market of bed former 2022-2028: Research Report on technology, participants, trends, market size and share
Cloud development who is the source code of undercover applet
js实现查看一个数组对象中是否包含另一个数组对象中的值
IndexError: Target 7 is out of bounds.
Pair class notes
pair类备注
关于h5页面苹果手机使用fixed定位tabbar最底部时遮挡内容问题
Jenkins 太老了 试试它?云原生 CI/CD Tekton
Thread support
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
UE常用控制臺命令
POM configuration provided and test
开放合作,共赢未来 | 福昕鲲鹏加入金兰组织
Shader 常用函数