一、构建GLFW
1、下载GLFW源码:https://www.glfw.org/download.html
2、解压GLFW源码至指定目录中,如:glfw-3.2.1
3、用CMake生成XCode工程文件:
如下图所示:
我们将源码路径指定为: your_path/glfw-3.2.1,
将构建的目标位置指定为: your_path/glfw-3.2.1/build
然后,如图2所示,点击Configure按钮,在弹出的对话框中 我们选择XCode,然后点击Down:
CMake会显示可选的编译选项用来配置最终生成的库。如图3所示:
这里我们使用默认设置,并再次点击Configure(设置)按钮保存设置。保存之后,点击Generate(生成)按钮,生成的工程文件会在你的build文件夹中:
双击打开工程文件GLFW.scodeproj,点击run,即可开始编译,如图5所示:
默认情况下是Debug模式,编译完成后会在: your_path/glfw-3.2.1/build/src/Debug目录下生成相应的库文件:
如果想生成release版本,可以如图7所示,长按run按钮,在弹出的窗口中选择test:
然后再弹出的对话框中,点击:Edit Scheme...按钮,如图8所示:
然后在弹出的对话框中,现在左侧列表中选择RUN,灾后设置Build Configuration为Release,然后点击右下角的RUN按钮,如图9所示:
编译完成后会在: your_path/glfw-3.2.1/build/src/Release目录下生成相应的库文件:
4、把编译结果放在指定目录下:
4.1. 在你想要的位置创建3rds文件夹:path/3rds
然后再3rds目录下创建两个文件夹:include和libs
4.2. 把GLFW编译产生的库文件复制到path/3rds/libs下边:
完成后,其目录结构如下:
├── libs
│ ├── GLFW
│ │ ├── Debug
│ │ │ ├── libglfw3.a
│ │ ├── Release
│ │ │ ├── libglfw3.a
4.3 把GLFW的头文件复制到path/3rds/include下边:
完成后,其目录结构如下:
├── include
│ ├── GLFW
│ │ ├── glfw3.h
│ │ ├── glfw3native.h
│ │ ├── glfw_config.h
二、配置GLAD
2.1. GLAD是一个开源项目:https://github.com/Dav1dde/glad
同时它还提供了一个在线服务:https://glad.dav1d.de/
打开GLAD在线服务,如图11所示,我设置语言为C/C++,gl版本选择4.6:
点击GENERATE按钮,GLAD现在应该提供给你了一个zip压缩文件,包含两个头文件目录,和一个glad.c文件:
2.2. 把GLAD的相关文件放在指定目录:
把两个头文件放在path/3rds/include/glad目录下:
├── include
│ ├── glad
│ │ ├── glad.h
│ │ ├── khrplatform.h
三、创建XCode项目
3.1 新建XCode项目,如图13所示,选择”Command Line Tool“:
3.2 链接第三方库:
设置libs搜索路径:
设置头文件的搜索路径:
3.3 把glad.c加入到工程项目中:
3.4 示例代码:
#include <glad.h>
#include <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); // uncomment this statement to fix compilation on OS X
#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))
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// input
// -----
processInput(window);
// 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);
}
3.5 示例代码的运行结果