Ubuntu 搭建 GLFW 环境及其相关测试 demo

2024-09-01 07:28

本文主要是介绍Ubuntu 搭建 GLFW 环境及其相关测试 demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

      • 1.系统信息:Ubuntu-22.04.1
      • 2.依赖库安装
      • 3.解压编译
      • 4.测试 demo 1 窗体
      • 5.测试 demo2 三角形
      • 6.键盘事件、图像显示

GLFW (Graphics Library Framework) 是一个开源的跨平台库,专门用于创建和管理 OpenGLVulkan 上下文、窗口及其他与图形渲染相关的任务。它的主要用途包括创建窗口、处理用户输入(例如键盘和鼠标事件)、以及管理 OpenGLVulkan 的上下文。

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 一些错误解决办法:
  1. Failed to find wayland-scanner
    使用cmake -S . -B build -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON

  2. 带了 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1126308

相关文章

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

新特性抢先看! Ubuntu 25.04 Beta 发布:Linux 6.14 内核

《新特性抢先看!Ubuntu25.04Beta发布:Linux6.14内核》Canonical公司近日发布了Ubuntu25.04Beta版,这一版本被赋予了一个活泼的代号——“Plu... Canonical 昨日(3 月 27 日)放出了 Beta 版 Ubuntu 25.04 系统镜像,代号“Pluc

JavaScript Array.from及其相关用法详解(示例演示)

《JavaScriptArray.from及其相关用法详解(示例演示)》Array.from方法是ES6引入的一个静态方法,用于从类数组对象或可迭代对象创建一个新的数组实例,本文将详细介绍Array... 目录一、Array.from 方法概述1. 方法介绍2. 示例演示二、结合实际场景的使用1. 初始化二

Centos环境下Tomcat虚拟主机配置详细教程

《Centos环境下Tomcat虚拟主机配置详细教程》这篇文章主要讲的是在CentOS系统上,如何一步步配置Tomcat的虚拟主机,内容很简单,从目录准备到配置文件修改,再到重启和测试,手把手带你搞定... 目录1. 准备虚拟主机的目录和内容创建目录添加测试文件2. 修改 Tomcat 的 server.X

VSCode配置Anaconda Python环境的实现

《VSCode配置AnacondaPython环境的实现》VisualStudioCode中可以使用Anaconda环境进行Python开发,本文主要介绍了VSCode配置AnacondaPytho... 目录前言一、安装 Visual Studio Code 和 Anaconda二、创建或激活 conda

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4

pytorch+torchvision+python版本对应及环境安装

《pytorch+torchvision+python版本对应及环境安装》本文主要介绍了pytorch+torchvision+python版本对应及环境安装,安装过程中需要注意Numpy版本的降级,... 目录一、版本对应二、安装命令(pip)1. 版本2. 安装全过程3. 命令相关解释参考文章一、版本对

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总