且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

win10 环境下配置 openGL的freeglut、glew等库,使用openGL

更新时间:2022-05-15 00:05:45

win10 环境下配置 openGL的freeglut、glew等库,使用openGL

  • 笔者环境 Visual Studio 2019

一.安装freeglut

网上的教程大都是项目配置,就是每次建一个新项目还得重新配置,我们来个一步到位

请注意,使用此方式配置时,创建一个新项目时无需重新配置。
同时以下教程仅适用于64位系统。

  • 找到Visual Studio的编译器目录,以我的VS2019默认安装路径为例,编译器目录为(其他版本VS或者非默认安装路径可以以此类推)

    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314
  • 把freeglut源码解压出来的include文件夹下的GL文件夹复制到编译器目录下的Include目录下:

win10 环境下配置 openGL的freeglut、glew等库,使用openGL
win10 环境下配置 openGL的freeglut、glew等库,使用openGL

win10 环境下配置 openGL的freeglut、glew等库,使用openGL

  • 把freeglut的静态数据链接库复制到VS的库目录下
    lib\x64\freeglut.lib复制到”<编译器目录>\lib\x64
    lib\ freeglut.lib复制到”<编译器目录>\ lib
  • freeglut的动态链接库dll文件freeglut.dll复制到系统目录:
    bin\x64\freeglut.dll复制C:\Windows\System32**
    bin\freeglut.dll复制到C:\Windows\SysWOW64
  • 配置完成,启动VS,新建空项目,无需其他额外配置,即可使用。

  • 新建一个项目输入以下代码测试,如果无误,即为配置成功

    #include <GL/glut.h>
    
    void Print()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
        glFlush();
    }
    
    int main(int argc, char* argv[])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
        glutInitWindowPosition(0, 100);
        glutInitWindowSize(800, 600);
        glutCreateWindow("Hello World!");
        glutDisplayFunc(Print);
        glutMainLoop();
        return 0;
    }
    
  • 对了在编译的时候注意配置

win10 环境下配置 openGL的freeglut、glew等库,使用openGL

二.安装glew

  • 下载glew源码
  • 链接:https://pan.baidu.com/s/1uMN5ALlnEmuH_to75Mufrg
    提取码:uwng
  • 源码路径\glew-2.1.0\bin\Release\x64\glew32.dll 复制到c:/windows/system32
  • 将glew-2.1.0-win32\glew-2.1.0\lib\Release\x64 下的glew32.lib,glew32s.lib 复制到C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\lib 保险起见也将上述两份文件拷贝到C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\lib\x64
  • glew-2.1.0-win32\glew-2.1.0\include\GL 下面所有文件 拷贝到 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\GL

之后要按照这个格式来调用 glutglew

#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment (lib, "glew32.lib")

void Print()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(0, 100);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Hello World!");
    glutDisplayFunc(Print);
    glutMainLoop();
    return 0;
}

这个项目也能顺利跑起来的话那么总的配置就已经完成了,感谢你的阅读------中北大学张建宏

  • 对了在编译的时候注意配置

win10 环境下配置 openGL的freeglut、glew等库,使用openGL

再加一个有趣的小项目,做最终的环境测试

#include <iostream>
#include <fstream>
#include <vector>
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment (lib, "glew32.lib")
using namespace std;

class MapPoint
{
public:
    double longitude;
    double latitude;
};
class Polygona
{
public:
    vector<MapPoint> points;  //多边形的顶点序列 
};
vector<Polygona*> polys;   //多边形集合

vector <Polygona*> ReadMapData(char* filename)
{
    int PointCount;
    vector <Polygona*> polygonas;
    ifstream fs(filename);    //从指定路径中读取double类型的数据
    while (fs.eof() != true)
    {
        Polygona* poly = new Polygona;
        fs >> PointCount;
        cout << PointCount << endl;
        for (int i = 0; i < PointCount; i++)
        {
            MapPoint p;
            fs >> p.longitude >> p.latitude;
            poly->points.push_back(p);    //在尾部加入一个数据
        }
        polygonas.push_back(poly);

    }
    return polygonas;
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);    //用蓝色色绘制各省边界
    glColor3f(0.0, 0.0, 1.0);
    glPolygonMode(GL_BACK, GL_LINE);
    for (int i = 0; i < polys.size(); i++)
    {
        vector<MapPoint> points = polys[i]->points;
        glBegin(GL_LINE_LOOP);
        for (int j = 0; j < points.size(); j++)
        {
            glVertex3f(points[j].longitude, points[j].latitude, 0.0);
        }
        glEnd();
    }
    glFlush();
}
void init(void)
{
    //设置背景颜色
    glClearColor(1.0, 1.0, 1.0, 0.0);
    //初始化观察值
    glMatrixMode(GL_PROJECTION);    //将矩阵模式设为投影
    glLoadIdentity();                 //对矩阵进行单位化
    glOrtho(110.0, 118.0, 30.0, 38.0, -1.0, 1.0);   //构造平行投影矩阵
}
int main(int argc, char** argv)
{
    // 数据文件目录
    char* filename = (char*)"D:/2633716446/FileRecv/河南地图经纬度数据.txt";
    polys = ReadMapData(filename);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    //单缓存和RGB
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("地图绘制");
    init();
    glutDisplayFunc(display);     //显示回调函数
    glutMainLoop();
    return 0;
}

win10 环境下配置 openGL的freeglut、glew等库,使用openGL