OpenGL 有好几种安装办法:下面列出二种,GLUT 和 GLFW+GLAD.
GLUT 方法
sudo apt-get install build-essential
sudo apt-get install libgl1-mesa-dev
sudo apt-get install libglu1-mesa-dev
sudo apt-get install libglut-dev
如果上一步出现报错:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libglut-dev
将上述
sudo apt-get install libglut-dev
命令改成:
sudo apt-get install freeglut3-dev
想测验是否成功,详见: Ubuntu 安装 openGL
GLFW + GLAD 方法
安装 GLFW
sudo apt-get install libglfw3-dev
git clone https://github.com/glfw/glfw
mkdir build && cd build && cmake ../glfw && make -j4 && sudo make install
安装 GLAD
打开GLAD的在线服务
生成zip压缩文件包含两个头文件目录,和一个glad.c文件。
将两个头文件目录(glad和KHR)复制到你的Include文件夹中(即/usr/local/include
),并添加glad.c文件到稍后的工程中。
测试
// System Headers
#include <glad/glad.h>
#include <GLFW/glfw3.h>
// Standard Headers
#include <cstdio>
#include <cstdlib>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);//回调函数原型声明
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main(int argc, char * argv[]) {
//初始化GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
//创建一个窗口对象
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "FirstGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
//通知GLFW将我们窗口的上下文设置为当前线程的主上下文
glfwMakeContextCurrent(window);
//对窗口注册一个回调函数,每当窗口改变大小,GLFW会调用这个函数并填充相应的参数供你处理
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//初始化GLAD用来管理OpenGL的函数指针
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
//渲染循环
while(!glfwWindowShouldClose(window))
{
// 输入
processInput(window);
// 渲染指令
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// 检查并调用事件,交换缓冲
glfwSwapBuffers(window);//检查触发事件
glfwPollEvents(); //交换颜色缓冲
}
//释放/删除之前的分配的所有资源
glfwTerminate();
return EXIT_SUCCESS;
}
//输入控制,检查用户是否按下了返回键(Esc)
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// 当用户改变窗口的大小的时候,视口也应该被调整
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// 注意:对于视网膜(Retina)显示屏,width和height都会明显比原输入值更高一点。
glViewport(0, 0, width, height);
}
将 glad.c
放在和该 main.cpp
在同一目录下, 编译:
g++ -o out main.cpp glad.c -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread -ldl -lXinerama -lXcursor
运行
./out
随即跳出一个窗口: