初学opengl 的人和我有一样的感受,看了那么多OPENGL的书 只是让我添加GLU GLUT  GLEW GLFW这些东西到底是什么? 我们学opengl 而opengl竟然什么也没有.大家一定很纳闷.

  当初学三个月opengl,也没有理解清楚.opengl到底是什么?如果我打个浅显比喻:opengl就c++ 就是语法,在绘图里不是c++语法 这个语法就是opengl ,就是绘图描述 渲染 处理 ,他有自己的语法,类型和自己的操作命令,就如同你学会了c++ 你能做什么 什么也做不了,必须靠工具,靠外部框架,GLUT GLEW GLFW QT 都是.我们在windows 下绘图使用graphics.h  在VS下使用easyx.h  我们要包含glut.h 也就是说他是一个封装了opengl图形操作函数的图形库 只要调用GLUT里的函数就可以绘图,

//------------------GLEW  opengl 的扩展库 安装使用--------------------------------------------------------

 OpenGL扩展库是一个简单的工具,有助于C / C++开发者编写可移植的应用程序扩展和初始化。目前支持多种操作系统,包括Windows,Linux和Solaris,达尔文,IRIX。

OpenGL扩展

  1. https://sourceforge.net/project/downloading.php?group_id=67586&filename=glew-1.5.1-win32.zip  

  2. 点击上面的链接下载最新的GLEW(支持OpenGL 3.0),解压,将 /bin/glew32.dll 拷贝到 c:/windows/system32 下面,将 /lib/glew32.lib 拷贝到VC安装目录下的 lib 目录下(如:/Microsoft Visual Studio 9.0/VC/lib/下),将 /include/glew.h 和 /include/wglew.h 拷贝到 VC 安装目录下的 /include/gl/ 目录下(如:/Microsoft Visual Studio 9.0/VC/include/gl/下)。在程序中我们只需要在包含gl,glu 或 glut.h 之前包含 glew.h就可以了(注意:一定要先包含 glew.h),在在代码中加上这么一句:   

  3. #pragma comment (lib, "glew32.lib")    

  4. 示例:   

  5. #include <GL/glew.h>  

  6. #include <GL/glut.h>   

  7. #progrma comment(lib, "glew32.lib")   

  8. 在创建OpenGL渲染context之后,调用 glewInit(); 初始化glew就可以了。

//---------------------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应运而生。

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


#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;
}

opengl 程序到底是如何写的?_程序

代码很简单,看注释即可,无需多解释。如果一切正常,屏幕上会出现一个窗口,里面有一个五颜六色的三角形。

opengl 程序到底是如何写的?_如何_02

常见错误及解决办法

使用一个库无非包含三个步骤:

  • 包含头文件

  • 链接库文件

  • 提供运行时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使用),不要搞混