美文网首页
glfw/glew/gl3w与Mac的纠葛

glfw/glew/gl3w与Mac的纠葛

作者: 听风轻咛 | 来源:发表于2020-07-13 01:32 被阅读0次

gl3w

$ python gl3w_gen.py

<urlopen error [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590)>
openssl version
python -c "import ssl; print(ssl.OPENSSL_VERSION)"

得出openssl与python的版本不匹配,Mac上解决:

brew install python

中间如果出现权限报错的问题:

sudo chown $(whoami):admin /usr/local/Frameworks

之后关联python:

brew link python3

接下来运行python文件,

$ python gl3w_gen.py 

文件会自动下载gl3w相关的头文件与库实现;

gl3w01.png

glfw/glew

首先通过homebrew安装这两个工具,

brew install glew
brew install glfw

安装完成后,库文件会在你的Mac系统目录下,

/usr/local/Cellar

接下来就是将这两个库的目录添加到Xcode工具设置里;

点击Command+.,在设置中Locations->Custom Path中添加,

gl3w02.png
glew_header
/usr/local/Cellar/glew/2.1.0/include
glew_lib
/usr/local/Cellar/glew/2.1.0/lib
glfw_header
/usr/local/Cellar/glfw/3.3/include
glfw_lib
/usr/local/Cellar/glfw/3.3/lib

工程中头文件与库查找配置

将文章开头使用python下载的gl3w.c以及头文件引入工程文件中,并配置头文件搜索目录:

gl3w03.png

.c文件则直接引入工程中(这里也可以将.c编译成动态库或者静态库,我们这里不多作讨论);

gl3w04.png

接下来配置glfwglew搜索,Header search paths中添加:

$(glew_header)
$(glfw_header)

Library search paths中添加:

$(glew_lib)
$(glfw_lib)

配置测试

我们用gl3w官网用例来进行测试,注意官网给出了glfwglut的版本,由于在Mac系统下glut有对应的库GLUT/GLUT.h,所以我们不再去下载源码进行编译,且glut大部分库为deprecated,我们这里使用glfw测试版本:

main.cpp中:

#include <stdio.h>
#include <stdlib.h>

#include <GL/gl3w.h>
#include <GLFW/glfw3.h>

static int width = 600, height = 600;

static float randf()
{
    return (float) rand() / ((float) RAND_MAX + 1);
}

static void display(GLFWwindow *window)
{
    glClearColor(randf(), randf(), randf(), 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glfwSwapBuffers(window);
}

static void reshape(GLFWwindow *window, int w, int h)
{
    width = w > 1 ? w : 1;
    height = h > 1 ? h : 1;
    glViewport(0, 0, width, height);
    glClearDepth(1.0);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);
}

int main(int argc, char **argv)
{
    GLFWwindow *window;

    glfwInit();

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    window = glfwCreateWindow(width, height, "cookie", NULL, NULL);

    glfwSetFramebufferSizeCallback(window, reshape);
    glfwSetWindowRefreshCallback(window, display);

    glfwMakeContextCurrent(window);

    if (gl3wInit()) {
        fprintf(stderr, "failed to initialize OpenGL\n");
        return -1;
    }
    if (!gl3wIsSupported(3, 2)) {
        fprintf(stderr, "OpenGL 3.2 not supported\n");
        return -1;
    }
    printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION),
           glGetString(GL_SHADING_LANGUAGE_VERSION));

    while (!glfwWindowShouldClose(window)) {
        display(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

效果如下:

cookies.gif

相关文章

网友评论

      本文标题:glfw/glew/gl3w与Mac的纠葛

      本文链接:https://www.haomeiwen.com/subject/dbeocktx.html