当前位置:网站首页>创建OpenGl窗口
创建OpenGl窗口
2022-06-26 00:24:00 【_Bruce】
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit(); //初始化GLFW
//使用glfwWindowHint配置GLFW
//版本号3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//选项的名称,选项名称值 主版本3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本3
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //监听窗口变化
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
// glfwPollEvents函数检查有没有触发什么事件(比如键盘输入、鼠标移动等)、更新窗口状态,并调用对应的回调函数(可以通过回调方法手动设置)。
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
边栏推荐
猜你喜欢

recv & send

Sweet girl lisixia was invited to be the little host of the global finals of the sixth season perfect child model

CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕

The 19th - 22nd week of scientific research training - about tnet and memnet

关于VS scanf出现‘scanf‘: This function or variable may be unsafe. Consider usi问题的解决方法

cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network

Redis7.0 installation steps

俏皮少女王艺璇 受邀担任第六季完美童模全球总决赛推广大使

Abnova anti GBA monoclonal antibody solution

论文阅读 Exploring Temporal Information for Dynamic Network Embedding
随机推荐
wifi 相关知识详解
V4L2+QT视频优化策略
通俗易懂C語言關鍵字static
Redis-SDS
memory leak check tools 详解
求n的乘阶
recv & send
Assertion of postman interface test
Reverse output an integer
Easy to understand C language keyword static
What happens from entering a web address in the browser's input box to seeing the contents of the web page?
Wechat circle of friends test point
Gun make (5) variables in makefile
记录一个诡异的图片上传问题
The role of xargs
热血男孩滕文泽 受邀担任第六季完美童模全球总决赛形象大使
GUN make (5) makefile中的变量
shell学习记录(三)
Interpretation of script corresponding to postman assertion
Dazhou suggested that we media bloggers do these four things in the early stage