macOS下使用Clion写OpenGL

参考:https://www.jianshu.com/p/cf82ef5ff98a及https://www.jianshu.com/p/00642d9e83c5

网上参考的资料有很多,但是多数没有涉及到GLtools,现在终于弄好啦。 开心,可以进一步学习啦。

工具环境:macOS Mojave    Clion

配置过程:

1.安装 homebrew(已安装可跳过)

homebrew 的官方网站: http://brew.sh/

在 Mac 中打开 Termal 并输入以下命令进行安装 homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.利用homebrew安装glew和glfw3

brew install glew

brew install glfw3

3.利用homebrew安装cmake

brew install cmake

4. 编译 gltools 

依次执行以下命令:

$ git clonehttps://github.com/HazimGazov/GLTools

$ cd GLTools

$ cmake .

$ make

 $ sudo make install

5. 配置Clion的CMakeLists.txt

我的配置为:


macOS下使用Clion写OpenGL_第1张图片
CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

project(untitled1)

set(CMAKE_CXX_STANDARD 11)

# 添加头文件

set(GLEW_H /usr/local/Cellar/glew/2.1.0/include/GL)

set(GLFW_H /usr/local/Cellar/glfw/3.3/include/GLFW)

set(GLTOOLS_H /usr/local/include)

include_directories(${GLEW_H} ${GLFW_H} ${GLTOOLS_H})

# 添加目标链接

set(GLEW_LINK /usr/local/Cellar/glew/2.1.0/lib/libGLEW.2.1.dylib)

set(GLFW_LINK /usr/local/Cellar/glfw/3.3/lib/libglfw.3.dylib)

set(GLTOOLS_LINK /usr/local/lib/libgltools.dylib)

link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK} ${GLTOOLS_LINK})

#执行编译命令

set(SOURCE_FILES main.cpp)

add_executable(untitled1 ${SOURCE_FILES})

if (APPLE)

target_link_libraries(untitled1 "-framework OpenGL")

target_link_libraries(untitled1 "-framework GLUT")

endif()

注意,其中的untitled1是我的项目名,你需要改成自己的。Reload CMake后没报错即成功。

6. 测试

测试代码为:

#include

#include     // Shader Manager Class

#ifdef __APPLE__

#include           // OS X version of GLUT

#else 

#define FREEGLUT_STATIC

#include             // Windows FreeGlut equivalent

#endif

GLBatch triangleBatch;

GLShaderManager shaderManager;

void ChangeSize(int w, int h)

{

    glViewport(0,0,w,h);

}

void SetupRC()

{

    glClearColor(0.0f,0.0f,1.0f,1.0f);

    shaderManager.InitializeStockShaders();

    //Load up a triangle

    GLfloat vVerts[]={-0.5,0.0f,0.0f,

                                  0.5f,0.0f,0.0f,

                                     0.0f,0.5f,0.0f};

    triangleBatch.Begin(GL_TRIANGLES, 3);

    triangleBatch.CopyVertexData3f(vVerts);

    triangleBatch.End();

}

void RenderScene(void)

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

    GLfloat vRed[]={1.0f,0.0f,0.0f,1.0f};

    shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);

    triangleBatch.Draw();

    glutSwapBuffers();

}

int main(int argc, char* argv[])

{

    gltSetWorkingDirectory(argv[0]);

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL);

    glutInitWindowSize(800,600);

    glutCreateWindow("Triangle");

    glutReshapeFunc(ChangeSize);

    glutDisplayFunc(RenderScene);

    GLenum err=glewInit();

    if(GLEW_OK != err){

            fprintf(stderr,"GLEW ERROR: %s\n", glewGetErrorString(err));

        return 1;

    }

    SetupRC();

    glutMainLoop();

    return 0;

}

结果为:


macOS下使用Clion写OpenGL_第2张图片
测试结果

你可能感兴趣的:(macOS下使用Clion写OpenGL)