• 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.WIN32include

    错误二

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

    ==

  • 相关阅读:
    MERGE INTO
    StringBuffer 去掉最后一个字符
    spring boot 在线项目创建
    centos rpm包下载地址
    maven 添加jdbc6
    初识算法----二分查找
    初识递归
    爬虫----抽屉新热榜
    python基础 字典
    0002 两数相加
  • 原文地址:https://www.cnblogs.com/graphics/p/3661500.html
Copyright © 2020-2023  润新知