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
相关的头文件与库实现;
glfw/glew
首先通过homebrew
安装这两个工具,
brew install glew
brew install glfw
安装完成后,库文件会在你的Mac系统目录下,
/usr/local/Cellar
接下来就是将这两个库的目录添加到Xcode
工具设置里;
点击Command+.
,在设置中Locations->Custom Path
中添加,
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
以及头文件引入工程文件中,并配置头文件搜索目录:
.c
文件则直接引入工程中(这里也可以将.c
编译成动态库或者静态库,我们这里不多作讨论);
接下来配置glfw
和glew
搜索,Header search paths
中添加:
$(glew_header)
$(glfw_header)
Library search paths
中添加:
$(glew_lib)
$(glfw_lib)
配置测试
我们用gl3w
官网用例来进行测试,注意官网给出了glfw
和glut
的版本,由于在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
网友评论