Linux 配置OpenGL环境
安装QT
sudo apt-get install qt5-default qtcreator
或者在应用管家中查找qt进行安装也可以.
安装glfw
1.安装cmake
sudo apt-get build-dep glfw
sudo apt-get install cmake xorg-dev libglu1-mesa-dev
安装中报了个无法解析域名archive.ubuntukylin.com 的错误,可以查看下自己的DNS Server:
cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0
search DHCP HOST
如果你的和上边一样,只有127.0.0.53这个地址,那可以通过下面的方式修改,再进行cmake的安装:
1. sudo vim /etc/resolv.conf # 添加如下内容
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 127.0.0.1
2. 输入Esc,:wq,保存并退出
3. sudo /etc/init.d/networking restart
2.在 https://www.glfw.org/ 下载GLFW压缩包到本地,找个文件集命名为GLFW,然后将压缩包内文件提取到该文件夹中,在该文件夹内打开终端窗窗口,执行
makedir build
cd build
cmake ../
make
sudo make install
安装glad
glad与glew作用相同,可以将其看作是glew的升级版
在下载页面选择版本和模式,版本选择最新,profile可以选择核心模式,还是Compatibility,如果不需要对老版本进行支持,建议选择核心模式.选好后点击generate, 跳转到下载页面,点击zip文件链接进行下载:
下载后,解压到本地,将include中的文件拷贝到/usr/local/include当中:
cd include
sudo mv * /usr/local/include
配置QT工程设置
考虑到跨平台,而且cmake使用较多,所以我们新建qt工程的时候使用的是camke作为工程配置的, 你可以根据需要,在最后的target_link_libraries中添加所需要的库即可
cmake_minimum_required(VERSION 2.8.12)
project(GLConsole)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(SOURCE_FILES main.cpp glad.c) # 源文件
find_package(Qt5Core)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
# 在这里添加lib库
target_link_libraries(${PROJECT_NAME} glfw3 GL m Xrandr Xi X11 Xxf86vm pthread dl Xinerama Xcursor)
配置好以后,我们就写一个Hello OpenGL吧,具体代码如下:
#include
#include
#include
#define GOUT std::cout
#define GENDL std::endl
enum GError{
GERROR_WINDOW_FAIL=-1,
GERROR_GLFW_FAIL = -2,
GERROR_GLAD_FAIL = -3
};
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
// system("pause");
// 初始化glfw
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,5);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
// Create GLWindow
GLFWwindow* window = glfwCreateWindow(800, 600,"Hello OpenGL", nullptr, nullptr);
if(nullptr == window)
{
GOUT<<"Failed to create OpenGL window."<
参考文章
Linux下OpenGL环境搭建(glfw)
Ubuntu18.04 sudo apt update无法解析域名的解决方案
使用GLFW与GLAD创建窗口并画出三角形