1. 程式人生 > >vs2013及以上版本opengl環境搭建

vs2013及以上版本opengl環境搭建

一、更新電腦驅動

由於OpenGL的大多數實現都是由顯示卡廠商編寫的,當產生一個bug時通常可以通過升級顯示卡驅動來解決。這些驅動會包括你的顯示卡能支援的最新版本的OpenGL,這也是為什麼總是建議你偶爾更新一下顯示卡驅動。

 

二、GLFW+GLEW庫檔案下載

下載對應的庫資源,OpenGL庫集合,網上很多。

https://download.csdn.net/download/qq_27175513/10661153

這裡使用的是32位的vs2013庫。

 

三、vs建空白專案,新增專案檢索路徑

新增目錄(需要VS搜尋庫和include檔案的地方),我們首先進入Project Properties(工程屬性,在解決方案窗口裡右鍵專案),然後選擇VC++ Directories(VC++ 目錄)

選項卡(如下圖)。在下面的兩欄新增目錄:

GLFW+GLEW的標頭檔案 新增到 ‘include directories’

庫目錄(lib檔案) 新增到 ‘Library directories’

Linker(連結器)選項卡里的Input(輸入)選項卡里新增:

glfw3.lib

opengl32.lib

glew32sd.lib

Windows上的OpenGL庫

如果你是Windows平臺,opengl32.lib已經包含在Microsoft SDK裡了,它在Visual Studio安裝的時候就預設安裝了。由於這篇教程用的是VS編譯器,並且是在Windows作業系統上,我們只需將opengl32.lib

新增進聯結器設定裡就行了。

 

四、執行測試

新建cpp,程式碼如下:

#include <iostream>  

// GLEW  
#define GLEW_STATIC  
//#include "eglew.h" 
#include "glew.h" 
//#include "glxew.h" 
//#include "wglew.h" 

// GLFW  
#include "glfw3.h"
//#include "glfw3native.h"


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

// Window dimensions  
const GLuint WIDTH = 800, HEIGHT = 600;

// The MAIN function, from here we start the application and run the game loop  
int main()
{
    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
    // Init GLFW  
    glfwInit();
    // Set all the required options for GLFW  
    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);

    // Create a GLFWwindow object that we can use for GLFW's functions  
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
    if (window == nullptr)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    // Set the required callback functions  
    glfwSetKeyCallback(window, key_callback);

    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions  
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers  
    if (glewInit() != GLEW_OK)
    {
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }

    // Define the viewport dimensions  
    glViewport(0, 0, WIDTH, HEIGHT);

    // Game loop  
    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions  
        glfwPollEvents();

        // Render  
        // Clear the colorbuffer  
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Swap the screen buffers  
        glfwSwapBuffers(window);
    }

    // Terminate GLFW, clearing any resources allocated by GLFW.  
    glfwTerminate();
    return 0;
}

// Is called whenever a key is pressed/released via GLFW  
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);
}

效果圖:

五、打完收工