Opengl绘制地图
生活随笔
收集整理的這篇文章主要介紹了
Opengl绘制地图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#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;
}
#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;
}
總結
以上是生活随笔為你收集整理的Opengl绘制地图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qt制作一个画板_基于Qt的画图板的设计
- 下一篇: 用CPU-Z查看内存插槽个数与频率