首页 > 解决方案 > OpenGL - 从外部文件绘制点

问题描述

AM 试图从 CSV 文件中绘制一些点。由于文件很大(> 2GB),将文件内容加载到向量会std::vector<std::vector<std::string> >parsedCsv引发内存不足异常。

所以我想,不是将文件读取到向量然后绘制它,而是可以直接从 CSV 绘制它。我正在寻找下面的一些修改glVertex3f(x,y,z)

    std::ifstream  data("D:\\Files\\Dummy2.csv");
    std::string line;
    while (std::getline(data, line))
    {
        std::stringstream lineStream(line);
        std::string cell;
        std::vector<std::string> parsedRow;
        while (std::getline(lineStream, cell, ','))
        {
            glBegin(GL_POINTS);
            glColor3f(0.0f, 1.0f, 1.0f);
            glVertex3f(----how to represent the points--?)
            glEnd();
        }

CSV 文件已经是所需的格式:

x1,y1,z1    
x2,y2,z2    
x3,y3,z3    
-------
----
--  

有什么建议么 ?

标签: c++openglvisual-c++freeglutopengl-compat

解决方案


您可以使用stof将字符串值转换为浮点数。将单元格的数量推到vector. 顶点顶点坐标的分量存储在 中vector,可以通过 绘制glVertex3fv

std::ifstream data("D:\\Files\\Dummy2.csv");
std::string line;
while (std::getline(data, line))
{
    std::stringstream lineStream(line);

    std::string cell;
    std::vector<float> parsedRow;
    while (std::getline(lineStream, cell, ','))
        parsedRow.push_back(std::stof(cell));

    if (parsedRow.size() == 3)
    {
        glBegin(GL_POINTS);
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3fv(parsedRow.data());
        glEnd();
    }
}

注意,如果stof不能执行转换,则抛出无效参数异常。


推荐阅读