OpenGL开发环境搭建
Dionysen

🔨学习OpenGL,在不同平台使用不同工具构建项目的方法。

Archlinux

Archlinux 配置 OpenGL 开发环境 (glfw + glad)

  • GLFW

安装glfw

sudo pacman -S glfw-x11
  • GLAD
image

在此网站选择需要的版本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) # Make sure clang can find .h file

set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp glad/src/glad.c) # src files

include_directories(glad/include) # include files

add_executable(OpenglTest ${SOURCE_FILES})

target_link_libraries(OpenglTest glfw) # link the glfw library

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)

# include_directories(glad/include)

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)
{
// glfw: initialize and configure
// ------------------------------

if (!glfwInit())
{
return -1;
}
// glfw window creation
// --------------------
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();
}

// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}

编译运行:

image

Windows 10/11

Visual Studio on Windows 配置 OpenGL 开发环境(使用解决方案链接库)

  • GLFW(手动编译,没有必要)

下载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,设置如下:

image

点击Configure,选择自己需要的VS版本和架构:

image

点击Generate,会源文件中生成一个build文件夹,用 VS 打开其中的GLFW.sln,生成,然后将生成的dll文件放置好

  • GLFW

直接下载Windows版本的预编译文件,其中有includesdll文件,链接到项目即可使用

  • GLAD

与Linux版本相同,均为将源代码文件包含到项目中

Visual Studio 使用 CMake 配置 glfw + glad

其他与上文大致相同,下载编译glfw,生成下载对应的glad,然后将编译好的glfw库文件和glad的源文件放到项目文件夹中,文件结构如下图:

image

然后在CMakeLists.txt中配置:

# CMakeList.txt: opengl_test 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)

# Enable Hot Reload for MSVC compilers if supported.
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) #添加要链接的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()
# TODO: 如有需要,请添加测试并安装目标。

target_link_libraries(opengl_test glfw3 opengl32)

OpenGL基础 | DIONYSEN BLOG的源码添加到main.cpp,编译运行:

image

显示评论