OpenGL笔记一之基础窗体搭建以及事件响应
总结自bilibili赵新政老师的教程
code review!
文章目录
- OpenGL笔记一之基础窗体搭建以及事件响应
- 1.运行
- 2.目录结构
- 3.main.cpp
- 4.CMakeList.txt
1.运行
2.目录结构
01_GLFW_WINDOW/
├── CMakeLists.txt
├── glad.c
├── main.cpp
└── thirdParty├── include│ ├── glad│ │ └── glad.h│ ├── GLFW│ │ ├── glfw3.h│ │ └── glfw3native.h│ └── KHR│ └── khrplatform.h└── lib└── glfw3.lib6 directories, 8 files
3.main.cpp
#include <iostream>//注意:glad头文件必须在glfw引用之前引用
#include<glad/glad.h>
#include <GLFW/glfw3.h>/*
* 目标:-创建glfw的窗体系统-加入窗体变化的事件回调-加入键盘消息事件回调
*
*///声明且实现一个响应窗体大小变化的函数
void frameBufferSizeCallBack(GLFWwindow* window, int width, int height) {std::cout << "窗体最新大小:" << width << " , " << height << std::endl;
}//声明且实现一个键盘消息回调函数
void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mods) {if (key == GLFW_KEY_W) {//触发了w}if (action == GLFW_PRESS) {//键位按下}if (action == GLFW_RELEASE) {//键位抬起}if (mods == GLFW_MOD_CONTROL) {//按下ctrl的同时,按下了key}if (mods == GLFW_MOD_SHIFT) {//按下shift的同时,按下了key}std::cout << "按下了:" << key << std::endl;std::cout << "action:" << action << std::endl;std::cout << "mods:" << mods << std::endl;
}int main() {
//1 初始化GLFW基本环境glfwInit();//1.1 设置OpenGL主版本号、次版本号glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//1.2 设置OpenGL启用核心模式(非立即渲染模式)glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//2 创建窗体对象GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGLStudy", NULL, NULL);//**设置当前窗体对象为OpenGL的绘制舞台glfwMakeContextCurrent(window);//设置监听,监听窗体大小变化的消息glfwSetFramebufferSizeCallback(window, frameBufferSizeCallBack);//设置监听,监听键盘消息glfwSetKeyCallback(window, keyCallBack);//3 执行窗体循环while (!glfwWindowShouldClose(window)) {//接收并分发窗体消息//检查消息队列是否有需要处理的鼠标、键盘等消息//如果有的话就将消息批量处理,清空队列glfwPollEvents();}//4 退出程序前做相关清理glfwTerminate();return 0;
}
4.CMakeList.txt
# 指定 CMake 最低版本
cmake_minimum_required(VERSION 3.12)# 项目名称
project(OpenGL_Lecture)# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)# 包含头文件目录
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/include/usr/include
)# 包含库文件目录
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/lib/usr/lib/x86_64-linux-gnu
)# 设置 CMAKE_PREFIX_PATH 以找到 GLFW
set(CMAKE_PREFIX_PATH "/usr/local/lib/cmake/glfw3")
set(GLFW_DIR "/usr/local/lib/cmake/glfw3")# 查找 GLFW3 库
find_package(glfw3 REQUIRED CONFIG)# 添加可执行文件
add_executable(openglStudy "main.cpp" "glad.c")# 链接库
target_link_libraries(openglStudy glfw)