1. 程式人生 > >ubuntu14.04 上使用glfw

ubuntu14.04 上使用glfw

第零步:下載glfw: 百度, 下載解壓好,暫且成目錄名字為 glfw3-3.x.x

第一步: 編譯glfw: 

               (1): 安裝依賴庫,sudo apt-get build-dep glfw,  sudo apt-get install cmake xorg-dev libglu1-mesa-dev

               (2): 進入 glfw3-3.x.x 目錄,建立build子目錄, 命令列執行 cmake-gui, 原始碼目錄選擇glfw3-3.x.x, 目標目錄選擇build。 configure,generate

               (3): 命令列模式,cd build,執行 make, sudo make install . 


第二步: 使用glfw(關鍵)

                建立main.cpp, 敲入下面原始碼: 

#include <iostream>

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

using namespace std;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

const GLuint WIDTH = 800, HEIGHT = 600;

int main()
{
    cout << "Starting GLFW context, OpenGL3.3" << endl;
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
    if(window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glViewport(0, 0, WIDTH, HEIGHT);

    while(!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
    }

    glfwTerminate();

    return 0;
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    std::cout << key << std::endl;
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

     編譯: g++  -c  main.cpp 

                 g++  mian.o -o main.exec -lGL  - lGLU  -lglfw3 -lX11 -lXxf86vm  -lXrandr -lpthread -lXi -lXcursor -lXinerama 

      編譯也可用CmakeLists.txt: 

project(OpenGL_1)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
LINK_LIBRARIES(glfw3)
LINK_LIBRARIES(X11)
LINK_LIBRARIES(Xxf86vm)
LINK_LIBRARIES(Xrandr)
LINK_LIBRARIES(pthread)
LINK_LIBRARIES(Xi)
LINK_LIBRARIES(Xcursor)
LINK_LIBRARIES(Xinerama)
LINK_LIBRARIES(GL)
LINK_LIBRARIES(GLU)
add_executable(${PROJECT_NAME} ${SRC_LIST})