GLFW初體驗


GLFW - 很遺憾,沒有找到FW的確切含義,Wiki上沒有,GLFW主頁也沒有。猜測F表示for,W表示Window

GLFW是干啥用的?

一個輕量級的,開源的,跨平台的library。支持OpenGL及OpenGL ES,用來管理窗口,讀取輸入,處理事件等。因為OpenGL沒有窗口管理的功能,所以很多熱心的人寫了工具來支持這些功能,比如早期的glut,現在的freeglut等。那么GLFW有何優勢呢?glut太老了,最后一個版本還是90年代的。freeglut完全兼容glut,算是glut的代替品,功能齊全,但是bug太多。穩定性也不好(不是我說的啊),GLFW應運而生。

如何使用GLFW

直接使用庫文件

1. 到這里下載編譯好的庫文件

2. 解壓后直接使用即可,詳見后面配置。

自行編譯源碼

如果想提高B格,自行編譯的話,可以按如下步驟進行。

1. 到這里下載GLFW

2. 到這里下載CMake

3. 參考這個頁面進行編譯

配置GLFW編程環境

1. 打開Visual Studio 2012,新建一個Console程序

2. 右鍵單擊project選擇properties,打開屬性頁面

3. 在VC++Directories-Include Directories中寫入glfw的頭文件目錄,我這里是 ../glfw-3.0.4.bin.WIN32/include

4. 在VC++ Directories-Library Directory中寫入glfw的庫文件目錄,我這里是 ../glfw-3.0.4.bin.WIN32/lib-msvc110

5. 在Linker - Input - Additional Dependencies中填入glfw3dll.lib

注意:如果使用靜態鏈接,那么上面第五步中glfw3dll.lib應該換成glfw3.lib,並且在工程屬性頁面中C/C++ - Code Generation 將 Runtime Library 設置為 /Mt 或者 /Mtd

渲染三角形

配置好環境之后,開始進入正題,總得畫點什么吧。 渲染三角形幾乎是圖形程序中的Hello world。代碼如下,框架是從glfw首頁copy過來的,繪制三角形的代碼是我加上去的。

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Draw a triangle */
        glBegin(GL_TRIANGLES);

        glColor3f(1.0, 0.0, 0.0);    // Red
        glVertex3f(0.0, 1.0, 0.0);

        glColor3f(0.0, 1.0, 0.0);    // Green
        glVertex3f(-1.0, -1.0, 0.0);

        glColor3f(0.0, 0.0, 1.0);    // Blue
        glVertex3f(1.0, -1.0, 0.0);

        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

代碼很簡單,看注釋即可,無需多解釋。如果一切正常,屏幕上會出現一個窗口,里面有一個五顏六色的三角形。

常見錯誤及解決辦法

使用一個庫無非包含三個步驟:

  • 包含頭文件
  • 鏈接庫文件
  • 提供運行時dll文件

只要這三個步驟正確了,基本不會出問題。

錯誤一

1    error C1083: Cannot open include file: 'GLFW/glfw3.h': No such file or directory
2    IntelliSense: cannot open source file "GLFW/glfw3.h"
3    IntelliSense: identifier "GLFWwindow" is undefined
4    IntelliSense: identifier "window" is undefined
5    IntelliSense: identifier "glfwInit" is undefined
6    IntelliSense: identifier "glfwCreateWindow" is undefined
7    IntelliSense: identifier "NULL" is undefined
8    IntelliSense: identifier "glfwTerminate" is undefined
9    IntelliSense: identifier "glfwMakeContextCurrent" is undefined
10    IntelliSense: identifier "glfwWindowShouldClose" is undefined
11    IntelliSense: identifier "glfwSwapBuffers" is undefined
12    IntelliSense: identifier "glfwPollEvents" is undefined
13    IntelliSense: identifier "glfwTerminate" is undefine

這顯然是頭文件沒有找到,導致下面所有glfw相關的類型都找不到定義。需要正確設置頭文件的路徑,注意只要包含到include目錄一級即可,比如\glfw-3.0.4.bin.WIN32\include

錯誤二

Error    1    error LNK2019: unresolved external symbol _glfwInit referenced in function _main
Error    2    error LNK2019: unresolved external symbol _glfwTerminate referenced in function _main
Error    3    error LNK2019: unresolved external symbol _glfwCreateWindow referenced in function _main
Error    4    error LNK2019: unresolved external symbol _glfwWindowShouldClose referenced in function _main
Error    5    error LNK2019: unresolved external symbol _glfwPollEvents referenced in function _main
Error    6    error LNK2019: unresolved external symbol _glfwMakeContextCurrent referenced in function _main
Error    7    error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in function _main
Error    8    error LNK1120: 7 unresolved externals
這個錯誤發生在鏈接階段,說明是庫文件(.lib)文件找不到,需要在項目的屬性頁面中設置lib文件,在Visual Studio中右鍵單擊Project,選擇Properties - Configuration Propterties - Linker - Input - Additional Dependencies,單擊右側的下三角進入編輯頁面,將glfw3dll.lib加入其中即可。

注意:gflw提供了不同版本的庫文件,如果你使用的是Visual Studio 2012,請使用lib-msvc110下面的庫文件,如果是Visual Studio 2013,那么則需要使用lib-msvc120下面的庫文件。lib文件有兩個,一個是glfw3.lib,一個是glfw3dll.lib,前者是靜態鏈接用的(鏈接此文件后,運行時無需dll文件,但是程序體積會變大),后者是動態鏈接用的(配合dll使用),不要搞混。

錯誤三

dll缺失,如下。

編譯鏈接都沒問題,運行時出現這個錯誤,很簡單,使用的是動態鏈接,但是程序沒有找到對應的dll文件,將glfw3.dll復制到程序所在目錄即可,一般是Visual Studio的Debug或Release目錄。

錯誤四

Error    1    error LNK2005: _free already defined in LIBCMT.lib(free.obj)
Error    2    error LNK2005: _realloc already defined in LIBCMT.lib(realloc.obj)
Error    4    error LNK1169: one or more multiply defined symbols foun

這是由於VS默認鏈接了LIBCMT.lib,只要將它禁止即可,在工程屬性頁面選擇Linker - Input - Ignore Specific Library,點擊右側小箭頭進行編輯,加入libcmt.lib即可。

==


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM