GLFW初体验

GLFW - 很遗憾,没有找到FW的确切含义,Wiki上没有,GLFW主页也没有。猜想F表示for,W表示Windowhtml

GLFW是干啥用的?

一个轻量级的,开源的,跨平台的library。支持OpenGL及OpenGL ES,用来管理窗口,读取输入,处理事件等。由于OpenGL没有窗口管理的功能,因此不少热心的人写了工具来支持这些功能,好比早期的glut,如今的freeglut等。那么GLFW有何优点呢?glut太老了,最后一个版本仍是90年代的。freeglut彻底兼容glut,算是glut的代替品,功能齐全,可是bug太多。稳定性也很差(不是我说的啊),GLFW应运而生。编程

如何使用GLFW

直接使用库文件

1. 到这里下载编译好的库文件框架

2. 解压后直接使用便可,详见后面配置。ide

自行编译源码

若是想提升B格,自行编译的话,能够按以下步骤进行。工具

1. 到这里下载GLFWoop

2. 到这里下载CMakespa

3. 参考这个页面进行编译3d

配置GLFW编程环境

1. 打开Visual Studio 2012,新建一个Console程序code

2. 右键单击project选择properties,打开属性页面htm

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便可。

==

相关文章
相关标签/搜索