Advertisement
DexterAndre

Read file type: SOSI (map format; Norwegian)

Mar 14th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. void Renderer::initializeSOSI(QString filePath)
  2. {
  3.     // Open a file from filepath
  4.     QFile mFile(filePath);
  5.     if (!mFile.open(QFile::ReadOnly | QFile::Text))
  6.     {
  7.         qDebug() << "Could not read SOSI file.";
  8.         return;
  9.     }
  10.  
  11.     int currentRow = 0;
  12.     int vertexCounter = 0;
  13.     int curveCounter = 0;
  14.  
  15.     QTextStream in(&mFile);
  16.     std::string text;
  17.  
  18.     Vector2* subtract {nullptr};
  19.     Vector3 divide(10000, 100, 10000);
  20.     Vector3 color = Vector3(0.5f, 0.5f, 0.5f);
  21.  
  22.     while (!in.atEnd())
  23.     {
  24.         std::stringstream sStream;
  25.         text = in.readLine().toStdString();
  26.         sStream << text;
  27.         std::string lineHeader;
  28.         sStream >> lineHeader;
  29.         currentRow++;
  30.  
  31.         if (lineHeader == ".PUNKT")
  32.         {
  33.             float y;
  34.             vertexCounter = 0;
  35.             curveCounter = mTriangles.size();
  36.  
  37.             while (true)
  38.             {
  39.                 std::stringstream pointStream;
  40.                 text = in.readLine().toStdString();
  41.                 pointStream << text;
  42.                 lineHeader = "";
  43.                 pointStream >> lineHeader;
  44.                 currentRow++;
  45.  
  46.                 if (lineHeader == "..HØYDE")
  47.                 {
  48.                     pointStream >> y;
  49.                     if ((int)y % 100 == 0)
  50.                         color = Vector3(1.0f, 0.0f, 0.0f);
  51.                     else
  52.                         color = Vector3(0.5f, 0.5f, 0.5f);
  53.                     y = y / divide.y;
  54.                 }
  55.                 else if (lineHeader == "..NØ")
  56.                 {
  57.                     text = in.readLine().toStdString();
  58.                     std::stringstream points;
  59.                     points << text;
  60.  
  61.                     // Get coordinate
  62.                     float x, z;
  63.                     points >> x >> z;
  64.  
  65.                     if (subtract == nullptr)
  66.                     {
  67.                         subtract = new Vector2(x, z);
  68.                     }
  69.  
  70.                     x = (x - subtract->x) / divide.x;
  71.                     z = (z - subtract->y) / divide.z;
  72.  
  73.                     // Add vertex to vertex array
  74.                     mVertices.push_back(Vertex(Vector3(x, y, z), color, Vector2(0, 0)));
  75.                     mTriangles.push_back(vertexCounter + curveCounter);
  76.                     mTriangles.push_back(++vertexCounter + curveCounter);
  77.  
  78.                     // Points only have one line of coordinates, so we break out of the loop
  79.                     break;
  80.                 }
  81.             }
  82.         }
  83.         else if (lineHeader == ".KURVE")
  84.         {
  85.             float y = 0;
  86.             vertexCounter = 0;
  87.             curveCounter = mTriangles.size();
  88.  
  89.             // End of SOSI-file has a ".SLUTT"
  90.             while (lineHeader != ".SLUTT")
  91.             {
  92.                 std::stringstream pointStream;
  93.                 text = in.readLine().toStdString();
  94.                 pointStream << text;
  95.                 lineHeader = "";
  96.                 pointStream >> lineHeader;
  97.                 currentRow++;
  98.  
  99.                 if (lineHeader == "..HØYDE")
  100.                 {
  101.                     pointStream >> y;
  102.                     if ((int)y % 100 == 0)
  103.                         color = Vector3(1.0f, 0.0f, 0.0f);
  104.                     else
  105.                         color = Vector3(0.5f, 0.5f, 0.5f);
  106.                     y = y / divide.y;
  107.                 }
  108.                 else if (lineHeader == "..NØ")
  109.                 {
  110.                     while (true)
  111.                     {
  112.                         text = in.readLine().toStdString();
  113.                         std::stringstream points;
  114.                         points << text;
  115.                         lineHeader = "";
  116.                         points >> lineHeader;
  117.                         currentRow++;
  118.  
  119.                         //First point in a curve is always separate, so a ..NØ will show up before the second curve point
  120.                         if(lineHeader == "..NØ")
  121.                         {
  122.                             continue; //Skip this loop
  123.                         }
  124.                         else if(lineHeader == ".KURVE" || lineHeader == ".SLUTT")
  125.                         {
  126.                             break; //New curve starts
  127.                         }
  128.  
  129.                         // Get coordinate
  130.                         float x, z;
  131.                         x = stof(lineHeader);
  132.                         points >> z;
  133.  
  134.                         if(subtract == nullptr)
  135.                         {
  136.                             subtract = new Vector2(x, z);
  137.                         }
  138.  
  139.                         x = (x - subtract->x)/divide.x;
  140.                         z = (z - subtract->y)/divide.z;
  141.  
  142.                         //qDebug() << x/divide << ", " << z/divide;
  143.                         // Add vertex to vertices array
  144.                         mVertices.push_back(Vertex(Vector3(x, y, z), color, Vector2(0,0)));
  145.                         mTriangles.push_back(vertexCounter + curveCounter);
  146.                         mTriangles.push_back(++vertexCounter + curveCounter);
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.         else
  152.             continue;
  153.     }
  154.  
  155.     // Buffers
  156.     initializeBuffers();
  157.  
  158.     // Draw mode
  159.     setDrawMode(GL_POINTS);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement