在Manjaro中配置OpenGL的环境,并且运行demo程序

配置环境为OpenGL+GLFW+GLEW

  1. 这套环境在ubuntu下面已经无数人写过了,但是我搜索了良久,并没有发现有在写过在manjaro下的配置过程,所以这篇随笔由此而生。

那么,接下来就是下载对应的包。

  1. OpenGL。
    这个部分可以通过“ glxinfo | grep OpenGL ”来查看本机上是否已经安装了OpenGL。大部分情况下是安装的,所以一般不用担心。
    如果,没有的话,请搜索“mesa”以及相关的包。
  2. GLFW。
    sudo pacman -S glfw-x11
    
    也可以通过
    yay -Ss glfw
    
    搜索其他的glfw包
  3. GLEW
    sudo pacman -S glew
    

同样的安装。

cmake配置

cmake_minimum_required(VERSION 3.0.0)
project(QEM VERSION 0.1.0)

enable_testing()

set (OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL REQUIRED)
if (OPENGL_FOUND)
    include_directories(${OPENGL_INCLUDE_DIRS})
    link_libraries(${OPENGL_LIBRARIES})
endif()

find_package(GLEW REQUIRED)
if (GLEW_FOUND)
    include_directories(${GLEW_INCLUDE_DIRS})
    link_libraries(${GLEW_LIBRARIES})
endif()
# find_package(X11 REQUIRED)

# include directories
include_directories()

add_executable(QEM "test_opengl.cpp")
target_link_libraries(${PROJECT_NAME} glfw)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

运行demo

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include <vector>

int main(){

    int WIDTH = 800, HEIGHT = 600;
    GLFWwindow* window;
    /* Initialize the library */
    if (!glfwInit())
        return -1;
    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(WIDTH, HEIGHT, "hello opengl test", NULL, NULL);
    if (!window){
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    // tell the GLFW to caputure mouse
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    /*glewInit();*/
    if (glewInit() != GLEW_OK)
        std::cout << "ERROR ON GLEWINIT" << std::endl;

    std::cout << glGetString(GL_VERSION) << std::endl;

    while( !glfwWindowShouldClose(window)){

        glClearColor(1.f, 1.f, 0.5f, 0.f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // draw something there

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

结果

如果配置正确的话,会显示一张黄色背景的图片。我这里没有上传成功。

posted @ 2022-04-20 21:56  bkctbkct  阅读(323)  评论(0编辑  收藏  举报