0. 前言
关于几种开发环境的组合,参考这篇博客:https://blog.csdn.net/libaineu2004/article/details/105879521
1. 安装 CLion
安装包下载链接:https://www.jetbrains.com/clion/download/#section=linux
cd /usr/local
sudo mkdir clion
将下载得到的压缩包解压到目标文件夹下:
sudo tar -zxvf CLion-2021.2.2.tar.gz -C /usr/local/clion
创建打开软件的快捷方式:
touch CLion.desktop
填入下面的内容:
[Desktop Entry]
Encoding=UTF-8
Name=CLion
Exec=/usr/local/clion/clion-2021.2.2/bin/clion.sh
Icon=/usr/local/clion/clion-2021.2.2/bin/clion.png
Terminal=False
Type=Application
StartupNotify=False
Categories=Application;Development
然后右键打开属性,修改文件的权限为可读可写,可作为可执行文件。 将其移动到指定文件夹中:
sudo mv CLion.desktop /usr/share/applications
然后就可以在启动台中找到该软件。
2. 安装相关包
sudo apt-get install build-essential libgl1-mesa-dev
sudo apt-get install freeglut3-dev
sudo apt-get install libglew-dev libsdl2-dev libsdl2-image-dev libglm-dev libfreetype6-dev
3. 编译测试
新建一个名为test 的C项目。
#include <GL/glut.h>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glFlush();
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("window name");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
cmake_minimum_required(VERSION 3.14)
project(test)
set(CMAKE_CXX_STANDARD 11)
add_executable(test main.c)
set(TARGET_LIB
"/usr/lib/x86_64-linux-gnu/libGL.so"
"/usr/lib/x86_64-linux-gnu/libGLU.so"
"/usr/lib/x86_64-linux-gnu/libglut.so"
)
target_link_libraries(test ${TARGET_LIB})
编译运行,效果如下:
4. glfw+glad 开发环境配置
sudo apt-get install libglfw3-dev
sudo apt install mesa-utils
DISPLAY=:0 glxgears -info | grep GL_VERSION
输出如下结果:
GL_VERSION = 4.6 (Compatibility Profile) Mesa 21.0.3
在该网站(https://glad.dav1d.de)进行选择,然后生成代码文件: 将zip文件解压后,将文件移动到指定路径下:
cd glad/include
sudo mv glad/ /usr/local/include
sudo mv KHR/ /usr/local/include
// main.cpp
#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() {
//初始化GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//创建一个窗口对象
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "GLFW 3.3.1", 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);
}
cmake_minimum_required(VERSION 2.8)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
project(1_helloWindow)
find_package(glfw3 REQUIRED)
file(GLOB project_file glad.c main.cpp)
add_executable(${PROJECT_NAME} ${project_file})
target_link_libraries(${PROJECT_NAME}
-lglfw
-lGL
-lm
-lXrandr
-lXi
-lX11
-lXxf86vm
-lpthread
-ldl
-lXinerama
-lXcursor
)
参考链接
[1] Ubuntu下搭建OpenGL开发环境(GLFW_3.3.1 + GLM_0.9.9 + GLAD)
|