1. 程式人生 > 其它 >在Manjaro中配置OpenGL的環境,並且執行demo程式

在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;
}

結果

如果配置正確的話,會顯示一張黃色背景的圖片。我這裡沒有上傳成功。