Opengl绘制地图

来源:互联网 发布:数据链路层和网络层 编辑:程序博客网 时间:2024/06/03 17:22
#include<iostream>
#include<fstream>//文本读写头文件
#include<vector>//在C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。
#include<GL/glut.h>
using namespace std;
class MapPoint//声明一个名为MapPoint的类
{
public:
double longitude;//数据成员 经度
double latitude;//纬度
};
class Polygon
{
public:
vector<MapPoint>points; //多边形的顶点序列
};
vector<Polygon*> polys;  //多边形集合
vector<Polygon*> ReadMapData(char* filename)//文件名
{
int PointCount;//计算点的个数
vector<Polygon*>polygons;
ifstream fs(filename);//寻找filename文件是否为空
while (fs.eof() != true)
{
Polygon* poly = new Polygon;//定点个数
fs >> PointCount;//定点个数赋值给fs
cout << PointCount << endl;
for (int i = 0; i < PointCount; i++)//做循环,定义p
{
MapPoint p;//地图名
fs >> p.longitude >> p.latitude;
poly->points.push_back(p);


}
polygons.push_back(poly);
}
return polygons;
}
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, 1.0);//设置背景颜色//初始化观察值
glMatrixMode(GL_PROJECTION);//将矩阵模式设为投影
glLoadIdentity();//对矩阵进行单位化
glOrtho(70.0, 140.0, 0.0, 60.0, -1.0, 1.0); //构造平行投影矩阵


}
int main(int argc,char *argv[])
{
char*filename = "D:HENG.txt";//文件名与文件目录对应
polys = ReadMapData(filename);//读定义的文件给polys
glutInit(&argc, argv);//初始化
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//单缓存和RGB
glutInitWindowSize(250, 250);//窗口大小
glutInitWindowPosition(100, 100);//窗口位置
glutCreateWindow("hello");//窗口标题
init();
glutDisplayFunc(display); //显示回调函数
glutMainLoop();//进行消息循环
return 0;


}
原创粉丝点击