天天看點

glfw3的使用

glfw3是glut之後的比較流行的opengl的渲染架構,同樣他支援雙緩沖等,具體看glfw3的doc

1.編寫makefile

CC      = g++
# -O2       : optimization option
# -s        : build small binary
# -mwindows : use this option to remove the popping cmd window
CCFLAGS = -O2 -Wall
BIN     = Demo.exe
WINDRES = windres
COMMONC = 
RES     = 
OBJ     = 

# MinGWLib路徑
MGBPATH = 'D:\ProgramFiles\MinGW\lib'
# 項目Lib路徑
CUBPATH = lib
# 項目頭檔案路徑
CUCPATH = include
# 連接配接庫檔案
LIBS	=-L$(CUBPATH) -lglfw3 -lgdi32
INCLU	=-I$(CUCPATH)
RM      = -del

# 編譯源碼
$(BIN): Demo.cpp
	$(CC) $(CCFLAGS) -o $(BIN) -c $^ $(INCLU) $(LIBS)  

# 清理,PHONY避免無依賴時被看成最終目标
.PHONY:clean
clean:
	$(RM) $(BIN) $(OBJ)

# 直接運作生成的檔案
# .PHONY:run
run:$(BIN)
	$(BIN)
           

記住這裡我們使用mingw中的g++是4.0+的,而官方的提供glfw3的mingw版式3.+的版本的,是以需要-c指定源檔案,否則會__chkstk_ms的失敗

2.官方的例子

#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(640, 480, "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))
    {
        /* Render here */

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

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

    glfwTerminate();
    return 0;
}           

3.接着在cmd下set你的minwg的bin路徑(個人不喜歡把bin加入系統path,隻是臨時使用,因為當你的開發工具太多時經常出現版本沖突問題等)

F:\work\opengl\_draw_triangle>make
g++ -O2 -Wall -o Demo.exe -c Demo.cpp -Iinclude -Llib -lglfw3 -lgdi32

F:\work\opengl\_draw_triangle>           

4.

glfw3的使用

繼續閱讀