之前介绍的 GLUT 只是很多不错的 OpenGL 库之一,另一个很好的OpenGL库是 GLFW.

在OS X中,GLUT库已经很久不更新了,而Apple更是从OS X 10.9就把GLUT的函数标记为deprecated了.比如你使用一个glutSwapBuffers(),你会收到一个warning

xxx.cc:72:5: warning: 'glutSwapBuffers' is deprecated: first deprecated in OS X 10.9 [-Wdeprecated-declarations]
    glutSwapBuffers();
    ^
/System/Library/Frameworks/Glut.framework/Headers/glut.h:448:22: note: 'glutSwapBuffers' has been explicitly marked deprecated here
extern void APIENTRY glutSwapBuffers(void) OPENGL_DEPRECATED(10_0, 10_9);
                     ^

当然,我们可以设置参数隐藏掉这些 warning, 但心中还是很不爽不是 .通过寻找, 我发现 GLFW 也是一个很不错的库.

GLFW 的可以参考官方的文档 quick start, 使用 cmake 的方法也被写得很清楚.

我同样也写了个 demo, 若有兴趣可以围观一下.

OS X 默认并没有安装 GLFW, 若打算用 GLFW, 我们可能要动手安装下.

brew install glfw

然后 find package

FIND_PACKAGE(OpenGL REQUIRED)
INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})

PKG_SEARCH_MODULE(GLFW REQUIRED glfw3)
INCLUDE_DIRECTORIES(${GLFW_INCLUDE_DIRS})
MESSAGE(STATUS "FIND GLFW_INCLUDE_DIRS " ${GLFW_INCLUDE_DIRS})

顺便一句, findGLFW.cmake 貌似要自己找个. 我使用的是这个版本.

调用可以参考这儿 有个例子.

简单说如下:

// 头文件
#ifdef __APPLE__
#include <GLFW/glfw3.h>
//#elif defined(_WIN32) || defined(_WIN64)
//#include <GLFW/glfw3.h> //I DON'T KNOW... NOT TESTED
#else // for linux : yum install glfw* :)
#include <GL/glfw3.h>
#endif

int main(int argc, char* argv[]) {
    GLFWwindow* window; // 声明window的描述符

    if (!glfwInit()) { // 初始化
        fprintf(stderr, "GLFW init failed\n");
        return -1;
    }

    window = glfwCreateWindow(640, 480, "Hello, OpenGL!", NULL, NULL); // 初始化window
    if (!window) {
        glfwTerminate();
        return -1;
    }

    // Make the window's context current
    // http://www.glfw.org/docs/latest/group__context.html#ga1c04dc242268f827290fe40aa1c91157
    glfwMakeContextCurrent(window);

    // http://www.glfw.org/docs/latest/group__context.html#ga6d4e0cdf151b5e579bd67f13202994ed
    glfwSwapInterval(1);

    // 以上一堆各种callback的设置

    // Loop until the user closes the window
    // http://www.glfw.org/docs/latest/group__window.html#ga24e02fbfefbb81fc45320989f8140ab5
    while (!glfwWindowShouldClose(window)) { // 自己玩主循环
        int rsec = (int)glfwGetTime(); // 可以获得当前时间

        // 对画面进行各种处理
      
        // Swap front and back buffers
        // http://www.glfw.org/docs/latest/group__window.html#ga15a5a1ee5b3c2ca6b15ca209a12efd14
        glfwSwapBuffers(window);

        // Poll for and process events
        // http://www.glfw.org/docs/latest/group__window.html#ga37bd57223967b4211d60ca1a0bf3c832
        glfwPollEvents();
    }
    printf("jump out of the loop\n");

    // http://www.glfw.org/docs/latest/group__window.html#gacdf43e51376051d2c091662e9fe3d7b2
    glfwDestroyWindow(window); // 关闭窗口

    // http://www.glfw.org/docs/latest/group__init.html#gaaae48c0a18607ea4a4ba951d939f0901
    glfwTerminate();
    return 0;
}

以上就是基本调用,然后完了 cmake 中 include 下 OpenGL 的 library 和 GLFW 的 Library 即可.

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published. Required fields are marked *