Mac平臺Clion配置GLFW+GLAD的項目
前期的準備工作詳見LearnOpenGL CN
看這篇教程的前提是假設你已經編譯好了GLFW文件夾以及下載好了GLAD,不會的話可以看我的另一篇
文章的前部分: 配置
Clion新建一個項目,CMakeLists.txt請按照下面的填寫:
1 cmake_minimum_required(VERSION 3.12) 2 project(OpenGL) #OpenGL是我的項目名稱 3 4 set(CMAKE_CXX_STANDARD 11) 5 set(HEADERS /Users/1kasshole/Documents/libs/include) #只有這兩個路徑需要 6set(GLFW_LINK /Users/1kasshole/Documents/libs/lib/libglfw3.a) #你自己設置 7 set(FRAMEWORKS_1 /System/Library/Frameworks/Cocoa.framework) 8 set(FRAMEWORKS_2 /System/Library/Frameworks/OpenGL.framework) 9 set(FRAMEWORKS_3 /System/Library/Frameworks/GLUT.framework) 10 set(FRAMEWORKS_4 /System/Library/Frameworks/CoreVideo.framework)11 set(FRAMEWORKS_5 /System/Library/Frameworks/IOKit.framework) 12 13 include_directories(${HEADERS}) 14 15 link_libraries(${GLFW_LINK} ${FRAMEWORKS_1} ${FRAMEWORKS_2} ${FRAMEWORKS_3} ${FRAMEWORKS_4} ${FRAMEWORKS_5}) 16 17 add_executable(OpenGL main.cpp glad.c)
註意,HEADERS的路徑include文件夾裏請包含GLFW文件夾和glad文件夾
記得把你的GLAD文件夾裏的glad.c放進你的項目目錄中
測試代碼:
// System Headers
#include <glad/glad.h>
#include <GLFW/glfw3.h>
// Standard Headers
#include <cstdio>
#include <cstdlib>
#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(int argc, char * argv[]) {
//初始化GLFW
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
//創建一個窗口對象
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "FirstGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
//通知GLFW將我們窗口的上下文設置為當前線程的主上下文
glfwMakeContextCurrent(window);
//對窗口註冊一個回調函數,每當窗口改變大小,GLFW會調用這個函數並填充相應的參數供你處理
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//初始化GLAD用來管理OpenGL的函數指針
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
//渲染循環
while(!glfwWindowShouldClose(window))
{
// 輸入
processInput(window);
// 渲染指令
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// 檢查並調用事件,交換緩沖
glfwSwapBuffers(window);//檢查觸發事件
glfwPollEvents(); //交換顏色緩沖
}
//釋放/刪除之前的分配的所有資源
glfwTerminate();
return EXIT_SUCCESS;
}
//輸入控制,檢查用戶是否按下了返回鍵(Esc)
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// 當用戶改變窗口的大小的時候,視口也應該被調整
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// 註意:對於視網膜(Retina)顯示屏,width和height都會明顯比原輸入值更高一點。
glViewport(0, 0, width, height);
}
---------------------
作者:milletluo
來源:CSDN
原文:https://blog.csdn.net/lm409/article/details/78420044
運行效果:
Mac平臺Clion配置GLFW+GLAD的項目