执行以下指令
代码语言:javascript复制brew install glew
brew install glfw
下面是我的执行结果
代码语言:javascript复制16:45 Kobe-Wang:~$ brew install glew
==> Downloading https://homebrew.bintray.com/bottles/glew-2.1.0_1.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/59/590c10bf98e6263d8e573720e5521584d836f28c52f30f97840614e97c16ccfd?__gda__=exp=1592039864~hmac=7a7b8afc3de94c51f0e0532c8a2192efb4be6e944
######################################################################## 100.0%
==> Pouring glew-2.1.0_1.catalina.bottle.tar.gz
? /usr/local/Cellar/glew/2.1.0_1: 38 files, 3MB
17:05 Kobe-Wang:~$ brew install glfw
==> Downloading https://homebrew.bintray.com/bottles/glfw-3.3.2.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring glfw-3.3.2.catalina.bottle.tar.gz
? /usr/local/Cellar/glfw/3.3.2: 14 files, 493KB
记住关键两个路径,后面要用到。
代码语言:javascript复制/usr/local/Cellar/glew/2.1.0_1
/usr/local/Cellar/glfw/3.3.2
二、新建CLion项目
大家可以新建一个CLion项目,然后修改CMakeList.txt
新建生成的CMakeList.txt
代码语言:javascript复制cmake_minimum_required(VERSION 3.13)
project(StudyOpenGL) #这就是我的项目名,你们自己可以随意取
set(CMAKE_CXX_STANDARD 11)
add_executable(StudyOpenGL main.cpp)
修改之后的CMakeList.txt
代码语言:javascript复制cmake_minimum_required(VERSION 3.13)
project(StudyOpenGL) #这就是我的项目名,你们自己可以随意取
set(CMAKE_CXX_STANDARD 11)
# 添加头文件 就是前面记录的路径
set(GLEW_H /usr/local/Cellar/glew/2.1.0_1/include/GL)
set(GLFW_H /usr/local/Cellar/glfw/3.3.2/include/GLFW)
include_directories(${GLEW_H} ${GLFW_H})
# 添加目标链接 就是前面记录的路径
set(GLEW_LINK /usr/local/Cellar/glew/2.1.0_1/lib/libGLEW.2.1.dylib)
set(GLFW_LINK /usr/local/Cellar/glfw/3.3.2/lib/libglfw.3.dylib)
link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK})
add_executable(StudyOpenGL main.cpp)
if (APPLE)
target_link_libraries(StudyOpenGL "-framework OpenGL")
target_link_libraries(StudyOpenGL "-framework GLUT")
endif()
三、写OpenGL的hello world
以下就是main.cpp的代码,复制文章macOS下使用Clion写OpenGL的代码。
代码语言:javascript复制#include <iostream>
#include <glew.h>
#include <glfw3.h>
using namespace std;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
//如果按下ESC,把windowShouldClose设置为True,外面的循环会关闭应用
if(key==GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
std::cout<<"ESC"<<mode;
}
int main()
{
if(!glfwInit())
return -1;
//创建窗口以及上下文
GLFWwindow* window = glfwCreateWindow(640, 480, "hello world", NULL, NULL);
if(!window)
{
//创建失败会返回NULL
glfwTerminate();
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback); //注册回调函数
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2, 0.3, 0.3, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0); //Red
glVertex3f(0, 1, 1);
glColor3f(0, 1, 0); //Green
glVertex3f(-1, -1, 0);
glColor3f(0, 0, 1); //Blue
glVertex3f(1, -1, 0);
//结束一个画图步骤
glEnd();
glBegin(GL_POLYGON);
//再画个梯形,需要注意笔顺
glColor3f(0.5, 0.5, 0.5); //Grey
glVertex2d(0.5, 0.5);
glVertex2d(1, 1);
glVertex2d(1, 0);
glVertex2d(0.5, 0);
glEnd();
/******交换缓冲区,更新window上的内容******/
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
运行的效果
总结
俗话说的好,买了就会,希望以后有时间可以好好研究以下OpenGL,不要让买了就会这句话当做一句空话。
参考文章
macOS下使用Clion写OpenGL https://www.jianshu.com/p/cf82ef5ff98a