系统信息
-
macOS Catalina 10.15.7
-
CMake 3.19.0(通过Homebrew安装)
部署
-
将依赖部署到系统目录的操作通常需要更高的权限,如果你不想在命令前加
sudo
,可以选择关闭SIP保护来取得完全的系统目录读写权限 -
前往 GLFW官网 下载Source Package,手动编译源码的理由是可以用到
make install
命令部署依赖,方便CMakeLists.txt编写。 -
前往 GLAD官网,按照下图勾选(gl版本可以选3.3以上),点击Generate按钮
之后下载生成的压缩包
- 我个人用一个单独的文件夹存放第三方库,我们将GLFW和GLAD的源码解压到这里:
- 打开终端,依次输入以下指令进行编译
-
cd glfw目录
-
mkdir build
-
cd build
-
cmake ..
-
make -j4
- 结束后,继续输入
sudo make install
部署依赖至系统目录。
使用
- 在一个空目录新建一个CMakeLists.txt,复制并保存以下内容:
cmake_minimum_required(VERSION 3.10)
project(OpenGL)
set(CMAKE_CXX_STANDARD 14)
#这里GLAD_INCLUDE目录填你自己存放的路径
set(GLAD_INCLUDE /Users/fijiisland/Documents/Dependencies/glad/include)
find_package(glfw3 REQUIRED)
include_directories(${GLAD_INCLUDE})
add_executable(OpenGL main.cpp glad.c)
target_link_libraries(OpenGL glfw)
-
将glad/src下的glad.c复制到此目录中
-
新建一个main.cpp,复制以下内容并保存:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
- 终端cd进此项目目录,并依次执行:
-
mkdir build
-
cd build
-
cmake ..
-
make
-
./OpenGL
成功的话,你会看到一个绿色的窗口:
CLion可以直接打开此目录并运行: