本文主要是介绍Ubuntu 搭建 GLFW 环境及其相关测试 demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1.系统信息:Ubuntu-22.04.1
- 2.依赖库安装
- 3.解压编译
- 4.测试 demo 1 窗体
- 5.测试 demo2 三角形
- 6.键盘事件、图像显示
GLFW (Graphics Library Framework)
是一个开源的跨平台库,专门用于创建和管理 OpenGL
或 Vulkan
上下文、窗口及其他与图形渲染相关的任务。它的主要用途包括创建窗口、处理用户输入(例如键盘和鼠标事件)、以及管理 OpenGL
或 Vulkan
的上下文。
glfw 渲染图片效果
1.系统信息:Ubuntu-22.04.1
[jn@jn glfw-3.4]$ uname -a
Linux jn 6.5.0-28-generic #29~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr 4 14:39:20 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
[jn@jn glfw-3.4]$
2.依赖库安装
# GLFW依赖
sudo apt-get install -y libx11-dev
sudo apt-get install -y libxrandr-dev
sudo apt-get install -y libxinerama-dev
sudo apt-get install -y libxcursor-dev
sudo apt-get install -y libxi-dev
# 测试程序编译依赖
sudo apt-get install -y libglu1-mesa-dev freeglut3-dev mesa-common-dev
3.解压编译
①下载GLFW库:
https://github.com/glfw/glfw/releases
https://github.com/glfw/glfw/archive/refs/tags/3.4.tar.gz
②编译:
[jn@jn GLFW]$ unzip glfw-3.4.zip
Archive: glfw-3.4.zipcreating: glfw-3.4/creating: glfw-3.4/CMake/
...
[jn@jn GLFW]$ cd glfw-3.4/
[jn@jn GLFW]$ cmake -S . -B build -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON -DCMAKE_INSTALL_PREFIX=$PWD/../glfw #生成cmake编译过程文件
-- Including X11 support
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Documentation generation requires Doxygen 1.9.8 or later
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jn/Desktop/GLFW/glfw-3.4/build
[jn@jn GLFW]$
Doxygen
:接口文档生成,不重要
cmake -S . -B build
一些错误解决办法:
-
Failed to find wayland-scanner
使用cmake -S . -B build -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
-
带了 wayland关闭和X11开启还是报错
CMake Error at CMakeLists.txt:31 (message):GLFW_USE_WAYLAND has been removed; delete the CMake cache and setGLFW_BUILD_WAYLAND and GLFW_BUILD_X11 instead
之前没参数-DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON,会有残留,需要清理重编译:
[jn@jn glfw-3.4]$ cmake -S . -B build -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
CMake Error at CMakeLists.txt:31 (message):GLFW_USE_WAYLAND has been removed; delete the CMake cache and setGLFW_BUILD_WAYLAND and GLFW_BUILD_X11 instead-- Configuring incomplete, errors occurred!
See also "/home/jn/Desktop/GLFW/glfw-3.4/build/CMakeFiles/CMakeOutput.log".
[jn@jn glfw-3.4]$ rm -rf build CMakeCache.txt
3.Could NOT find X11 (missing: X11_X11_INCLUDE_PATH X11_X11_LIB)
sudo apt-get install -y libx11-dev
4.RandR headers not found; install libxrandr development package
sudo apt-get install libxrandr-dev
5.Xinerama headers not found; install libxinerama development package
sudo apt-get install -y libxinerama-dev
6.Xcursor headers not found; install libxcursor development package
sudo apt-get install libxcursor-dev
7.XInput headers not found; install libxi development package
sudo apt-get install libxi-dev
4.测试 demo 1 窗体
①目录:
[jn@jn GLFW]$ tree -L 2
.
├── glfw
│ ├── include
│ └── lib
└── test├── CMakeLists.txt└── main.cpp
[jn@jn GLFW]$
②CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(glfwTest)# 设置C++标准
set(CMAKE_CXX_STANDARD 11)# 查找GLFW库
find_package(glfw3 3.3 REQUIRED PATHS ../glfw)
find_package(OpenGL REQUIRED)# 添加可执行文件
add_executable(${PROJECT_NAME} main.cpp)# 链接GLFW和OpenGL
target_link_libraries(${PROJECT_NAME} glfw OpenGL::GL)
③main.cpp
#include <GLFW/glfw3.h>
#include <iostream>int main() {// 初始化GLFW库if (!glfwInit()) {std::cerr << "Failed to initialize GLFW" << std::endl;return -1;}// 创建窗口GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW Window For Jn", nullptr, nullptr);if (!window) {std::cerr << "Failed to create GLFW window" << std::endl;glfwTerminate();return -1;}// 设置上下文为当前窗口glfwMakeContextCurrent(window);// 主循环while (!glfwWindowShouldClose(window)) {// 清空颜色缓冲glClear(GL_COLOR_BUFFER_BIT);// 交换前后缓冲glfwSwapBuffers(window);// 处理事件glfwPollEvents();}// 清理并退出glfwDestroyWindow(window);glfwTerminate();return 0;
}
④编译:
[jn@jn test]$ cmake -S . -B build
...
[jn@jn test]$ cd build;make
Consolidate compiler generated dependencies of target glfwTest
[100%] Built target glfwTest
[jn@jn build]$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake glfwTest Makefile
[jn@jn build]$ ./glfwTest
其他问题:
注
:cmake 找不到 OpenGL
Could NOT find OpenGL (missing: OPENGL_opengl_LIBRARY OPENGL_glx_LIBRARYOPENGL_INCLUDE_DIR)
安装依赖:
sudo apt-get install -y libglu1-mesa-dev freeglut3-dev mesa-common-dev
5.测试 demo2 三角形
①依赖glad
在官网根据需求生成源码:https://glad.dav1d.de/
可以查看本机 OpenGL 版本:
[jn@jn glad-2.0.6]$ glxinfo|grep version
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4Max core profile version: 4.5Max compat profile version: 4.5Max GLES1 profile version: 1.1Max GLES[23] profile version: 3.2
OpenGL core profile version string: 4.5 (Core Profile) Mesa 23.2.1-1ubuntu3.1~22.04.2
OpenGL core profile shading language version string: 4.50
OpenGL version string: 4.5 (Compatibility Profile) Mesa 23.2.1-1ubuntu3.1~22.04.2
OpenGL shading language version string: 4.50
OpenGL ES profile version string: OpenGL ES 3.2 Mesa 23.2.1-1ubuntu3.1~22.04.2
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20GL_EXT_shader_implicit_conversions, GL_EXT_shader_integer_mix,
[jn@jn glad-2.0.6]$
根据版本选择,点击右下角 GENENATE 会生成源码,下载集成到自己源码中即可
②代码
我是这么放的:
[jn@jn triangle]$ tree
.
├── CMakeLists.txt
├── glad.zip
├── include
│ ├── glad
│ │ └── glad.h
│ └── KHR
│ └── khrplatform.h
├── main.cpp
└── src└── glad.c
[jn@jn triangle]$
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(glfwTest)# 设置C++标准
set(CMAKE_CXX_STANDARD 11)INCLUDE_DIRECTORIES(
./include
)# 查找GLFW库
find_package(glfw3 3.3 REQUIRED PATHS ../glfw)
find_package(OpenGL REQUIRED)# 添加可执行文件
add_executable(${PROJECT_NAME} main.cpp src/glad.c)# 链接GLFW和OpenGL
target_link_libraries(${PROJECT_NAME} glfw OpenGL::GL)
main.cpp
#include <glad/glad.h> // 需要使用GLAD加载OpenGL函数
#include <GLFW/glfw3.h>
#include <iostream>const char* vertexShaderSource = R"(
#version 330 core
layout(location = 0) in vec3 aPos;
void main() {gl_Position = vec4(aPos, 1.0);
}
)";const char* fragmentShaderSource = R"(
#version 330 core
out vec4 FragColor;
void main() {FragColor = vec4(1.0, 0.0, 0.0, 1.0); // 红色
}
)";int main() {if (!glfwInit()) {std::cerr << "Failed to initialize GLFW" << std::endl;return -1;}GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Triangle", nullptr, nullptr);if (!window) {std::cerr << "Failed to create GLFW window" << std::endl;glfwTerminate();return -1;}glfwMakeContextCurrent(window);if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {std::cerr << "Failed to initialize GLAD" << std::endl;return -1;}unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);glCompileShader(vertexShader);unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);glCompileShader(fragmentShader);unsigned int shaderProgram = glCreateProgram();glAttachShader(shaderProgram, vertexShader);glAttachShader(shaderProgram, fragmentShader);glLinkProgram(shaderProgram);glDeleteShader(vertexShader);glDeleteShader(fragmentShader);float vertices[] = {0.0f, 0.5f, 0.0f,-0.5f, -0.5f, 0.0f,0.5f, -0.5f, 0.0f};unsigned int VAO, VBO;glGenVertexArrays(1, &VAO);glGenBuffers(1, &VBO);glBindVertexArray(VAO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);glEnableVertexAttribArray(0);glBindBuffer(GL_ARRAY_BUFFER, 0);glBindVertexArray(0);while (!glfwWindowShouldClose(window)) {glClear(GL_COLOR_BUFFER_BIT);glUseProgram(shaderProgram);glBindVertexArray(VAO);glDrawArrays(GL_TRIANGLES, 0, 3);glfwSwapBuffers(window);glfwPollEvents();}glDeleteVertexArrays(1, &VAO);glDeleteBuffers(1, &VBO);glfwDestroyWindow(window);glfwTerminate();return 0;
}
注意:这里有个坑,需要将glad/glad.h头文件放在glfw前面,先包含glad头文件,否则glad报错
[jn@jn build]$ make
Consolidate compiler generated dependencies of target glfwTest
[ 33%] Building CXX object CMakeFiles/glfwTest.dir/main.cpp.o
In file included from /home/jn/Desktop/GLFW/triangle/main.cpp:4:
/home/jn/Desktop/GLFW/triangle/./include/glad/glad.h:27:2: error: #error OpenGL header already included, remove this include, glad already provides it27 | #error OpenGL header already included, remove this include, glad already provides it| ^~~~~
/home/jn/Desktop/GLFW/triangle/./include/glad/glad.h:1321: warning: "GL_INVALID_INDEX" redefined1321 | #define GL_INVALID_INDEX 0xFFFFFFFF|
In file included from /usr/include/GL/gl.h:2050,from /home/jn/Desktop/GLFW/glfw/include/GLFW/glfw3.h:241,from /home/jn/Desktop/GLFW/triangle/main.cpp:1:
/usr/include/GL/glext.h:1355: note: this is the location of the previous definition1355 | #define GL_INVALID_INDEX 0xFFFFFFFFu
③编译:
[jn@jn triangle]$ cmake -S . -B build
[jn@jn triangle]$ cd build;make
[ 33%] Building CXX object CMakeFiles/glfwTest.dir/main.cpp.o
[ 66%] Building C object CMakeFiles/glfwTest.dir/src/glad.c.o
[100%] Linking CXX executable glfwTest
[100%] Built target glfwTest
[jn@jn build]$ ./glfwTest
④结果:
6.键盘事件、图像显示
贴代码太多,不再一一列出代码:
① 键盘事件监控:
图像显示:
这篇关于Ubuntu 搭建 GLFW 环境及其相关测试 demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!