在 PC 平臺上開發 OpenGL 能夠使用的輔助工具備不少選擇,這裏我主要參考了 learnopengl 的配置,使用 GLFW 和 GLAD.php
基本編譯庫html
sudo apt-get install build-essential
須要 OpenGL 庫linux
sudo apt-get install libgl1-mesa-dev
操做指南:http://www.glfw.org/docs/latest/compile_guide.htmlios
下載源碼,運行 cmake
ubuntu
正式編譯:ide
make sudo make install
在這個網站上生成合適的庫,我選擇了 C/C++ OpenGL Core Version 4.6 而後點擊 GENERATE 便可獲得壓縮包。把壓縮包內 include 目錄的東西放到本機 include 目錄下(/usr/include), 把 src 下的源碼拷貝到工程內。網站
編碼,示範代碼以下:ui
#include <glad/glad.h> #include <GLFW/glfw3.h> #include <iostream> int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } glViewport(0, 0, 800, 600); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); while(!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; }
編譯方式:編碼
g++ -o run.out GlWindow.cpp glad.c -lglfw3 -lGL -lX11 -lpthread -lXrandr -lXxf86vm -lXinerama -lXcursor -lXi -ldl
能夠出現一個暗綠色的窗口。
https://learnopengl.com/Getting-started/Creating-a-window
Howto Install OpenGL Development Environment
Undefined References Compiling OpenGL/glfw/glew on Ubuntu(g++) [duplicate]