🔨学习OpenGL,在不同平台使用不同工具构建项目的方法。
Archlinux Archlinux 配置 OpenGL 开发环境 (glfw + glad)
安装glfw
库
在此网站选择需要的版本https://glad.dav1d.de ,点击GRNERATE
,下载生成的zip文件,解压后将其放到项目文件夹中。
文件目录为:
├── CMakeLists.txt ├── glad │ ├── include │ │ ├── glad │ │ │ └── glad.h │ │ └── KHR │ │ └── khrplatform.h │ └── src │ └── glad.c └── main.cpp
CMakeLists.txt
可以写成如下:
cmake_minimum_required (VERSION 3.14 )project (OpenglTest)set (CMAKE_EXPORT_COMPILE_COMMANDS ON ) set (CMAKE_CXX_STANDARD 11 )set (SOURCE_FILES main.cpp glad/src/glad.c) include_directories (glad/include ) add_executable (OpenglTest ${SOURCE_FILES} )target_link_libraries (OpenglTest glfw)
Archlinux 安装glew并配置CMake sudo pacman -S glewlwyd glew1.10 glew
在项目中的CMakeLists.txt
中配置:
cmake_minimum_required (VERSION 3.14 )project (OpenglTest)find_package (OpenGL REQUIRED)if (NOT OPENGL_FOUND) message ("ERROR: OpenGL not found" ) endif (NOT OPENGL_FOUND)set (CMAKE_EXPORT_COMPILE_COMMANDS ON )set (CMAKE_CXX_STANDARD 11 )set (SOURCE_FILES main.cpp)add_executable (OpenglTest ${SOURCE_FILES} )find_package (GLEW REQUIRED)target_link_libraries (OpenglTest glfw GLEW::GLEW libGL.so)
编写代码main.cpp
:
#include <GL/glew.h> #include <GLFW/glfw3.h> #include <iostream> int main (void ) { if (!glfwInit ()) { return -1 ; } GLFWwindow *window = glfwCreateWindow (800 , 600 , "LearnOpenGL" , NULL , NULL ); if (window == NULL ) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate (); return -1 ; } glfwMakeContextCurrent (window); if (GLEW_OK != glewInit ()) { std::cout << "Error!" << std::endl; } while (!glfwWindowShouldClose (window)) { glClear (GL_COLOR_BUFFER_BIT); glBegin (GL_TRIANGLES); glVertex2f (-0.5f , -0.5f ); glVertex2f (0.0f , 0.5f ); glVertex2f (0.5f , -0.5f ); glEnd (); glfwSwapBuffers (window); glfwPollEvents (); } glfwTerminate (); return 0 ; }
编译运行:
Windows 10/11 Visual Studio on Windows 配置 OpenGL 开发环境(使用解决方案链接库)
下载CMake(x64):https://github.com/Kitware/CMake/releases/download/v3.26.4/cmake-3.26.4-windows-x86_64.msi
下载GLFW源码:https://github.com/glfw/glfw/releases/download/3.3.8/glfw-3.3.8.zip,并解压
打开CMake-GUI,设置如下:
点击Configure,选择自己需要的VS版本和架构:
点击Generate,会源文件中生成一个build
文件夹,用 VS 打开其中的GLFW.sln
,生成,然后将生成的dll
文件放置好
直接下载Windows版本的预编译文件 ,其中有includes
和dll
文件,链接到项目即可使用
与Linux版本相同,均为将源代码文件包含到项目中
Visual Studio 使用 CMake 配置 glfw + glad 其他与上文大致相同,下载编译glfw,生成下载对应的glad,然后将编译好的glfw库文件和glad的源文件放到项目文件夹中,文件结构如下图:
然后在CMakeLists.txt
中配置:
cmake_minimum_required (VERSION 3.8 )if (POLICY CMP0141) cmake_policy (SET CMP0141 NEW) set (CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>" ) endif ()project ("opengl_test" )include_directories (${PROJECT_SOURCE_DIR} /include ) link_directories (lib) file (COPY "lib" DESTINATION ${CMAKE_BINARY_DIR} )file (GLOB_RECURSE src_dir "./src/*.c" "./src/*.h" "./src/*.cpp" )add_executable (opengl_test "main.cpp" ${src_dir} )if (CMAKE_VERSION VERSION_GREATER 3.12 ) set_property (TARGET opengl_test PROPERTY CXX_STANDARD 20 ) endif ()target_link_libraries (opengl_test glfw3 opengl32)
将OpenGL基础 | DIONYSEN BLOG 的源码添加到main.cpp
,编译运行: