C++ OpenGL显示地图

这是一次opengl 加载地图资源显示学习过程

使用

使用glfw而不是使用glut方式

glfw-3.4.0 Download | GLFW

glad2.0 在https://glad.dav1d.de/网站上,通过配置定制源码

包含路径

glfw-3.4\include\

glfw-3.4\deps\

将下面代码依次复制到一个cpp文件中

cpp 复制代码
#include <iostream>
#include <fstream>
#include <vector>
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>   // GLAD2 头文件,必须在 GLFW 前面
#include <GLFW/glfw3.h>
cpp 复制代码
class 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);
    while(fs.eof()!=true)
    {
        Polygon* poly=new Polygon;
        fs>>PointCount;
        cout<<PointCount<<endl;
        for(int i=0;i<PointCount;i++)
        {
            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_STRIP);
        for(int j=0;j<points.size();j++)
        {           
            glVertex3f (points[j].longitude, points[j].latitude, 0.0);       
        }
        glEnd();
    }   
    glFlush();
}
cpp 复制代码
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);
}

// 窗口大小改变时自动调整视口和投影
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height); // 自动适配新窗口大小

    // 重新设置投影矩阵,保证地图不变形
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(110.0, 118.0, 30.0, 38.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
}
cpp 复制代码
int main(int argc, char** argv)
{
    char* filename = "D:/HenanCounty.txt";
    polys = ReadMapData(filename);

    if (!glfwInit()) return -1;

    // 单缓冲
    glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_FALSE);

    GLFWwindow* window = glfwCreateWindow(500, 500, "地图绘制", NULL, NULL);
    if (!window) { glfwTerminate(); return -1; }

    glfwSetWindowPos(window, 100, 100);

    // 1. 先把上下文设为当前
    glfwMakeContextCurrent(window);

    // 2. 注册窗口大小变化回调(自适应关键)
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // 3. GLAD2 加载 OpenGL 函数
    int version = gladLoadGL(glfwGetProcAddress);
    if (version == 0) {
        // 加载失败
        glfwTerminate();
        return -1;
    }

    //
    init();

    while (!glfwWindowShouldClose(window))
    {
        display();
        glFlush();
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

D:/HenanCounty.txt 网上搜索

参考资料网址:

Draw_extend使用OpenGL显示数据点 - mememagic - 博客园

相关推荐
I Promise341 小时前
C++ 多线程编程:从入门到实战
开发语言·c++
邪修king1 小时前
C++map_set封装 : 红黑树底层迭代器以及仿函数的运用
android·c语言·数据结构·c++·b树
牟师傅敲代码1 小时前
第2章:底层时间驱动机制
c++
并不喜欢吃鱼2 小时前
从零开始 C++------ 十四【C++ 数据结构】unordered_map/unordered_set 全解析:从使用到底层模拟实现
开发语言·数据结构·c++
小欣加油2 小时前
leetcode3633 最早完成陆地和水上游乐设施的时间I
数据结构·c++·算法·leetcode
啦啦啦啦啦zzzz2 小时前
数据结构:二叉排序树(递归与非递归函数的全部实现)
数据结构·c++·二叉排序树
£suPerpanda2 小时前
AtCoder Beginner Contest 453
c++·算法
郝学胜-神的一滴2 小时前
Qt 高级开发 022:栅格布局深度实战
开发语言·c++·qt·软件构建·用户界面
basketball6162 小时前
设计模式入门:3. 装饰器模式详解 C++实现
c++·设计模式·装饰器模式