85 lines
2.0 KiB
C++
85 lines
2.0 KiB
C++
#include "FileReader.h"
|
|
#include "Renderer.h"
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
//Initialize the FileReader instance
|
|
FileReader FileReader::m_instance;
|
|
|
|
void FileReader::readFile(char* fileName) {
|
|
char input[81], *inputPtr; //temp string space
|
|
std::fstream fs; //File Stream
|
|
int numTris, numPts; //Number of objects
|
|
|
|
//Open file
|
|
fs.open(fileName, std::fstream::in);
|
|
|
|
if (!fs.is_open()) {
|
|
std::cout << "The file failed to open.\n";
|
|
exit(-1);
|
|
}
|
|
|
|
//Get the # of vertices and # of triangles
|
|
fs.getline(input,81);
|
|
|
|
//Number of Triangles
|
|
numTris = strtol(input,&inputPtr,0);
|
|
//Renderer::getInstance()->triangles->reserve(numTris);
|
|
|
|
//Number of points
|
|
numPts = strtol(inputPtr,NULL,0);
|
|
//Renderer::getInstance()->points->reserve(numPts);
|
|
|
|
//skip over the first newline
|
|
fs.getline(input,81);
|
|
|
|
//Read all triangles
|
|
for (int i = 0; i < numTris; i++) {
|
|
readTriangle(&fs, i);
|
|
}
|
|
|
|
//skip over the second newline
|
|
fs.getline(input,81);
|
|
|
|
//Read all Points
|
|
for (int i = 0; i < numPts; i++) {
|
|
readPoint(&fs, i);
|
|
}
|
|
|
|
}
|
|
|
|
void FileReader::readTriangle(std::fstream *fs, int index) {
|
|
char input[81], *inputPtr; //temp string space
|
|
Triangle t;
|
|
|
|
//Read next line
|
|
fs->getline(input,81);
|
|
|
|
t.a1 = strtol(input,&inputPtr,0);
|
|
t.a2 = strtol(inputPtr,&inputPtr,0);
|
|
t.a3 = strtol(inputPtr,NULL,0);
|
|
|
|
t.normal.i = t.normal.j = t.normal.k = 0;
|
|
|
|
Renderer::getInstance()->triangles->push_back(t);
|
|
}
|
|
|
|
void FileReader::readPoint(std::fstream *fs, int index) {
|
|
char input[81], *inputPtr; //temp string space
|
|
Point p;
|
|
|
|
//Read next line
|
|
fs->getline(input,81);
|
|
|
|
p.x = (float)strtod(input,&inputPtr);
|
|
p.y = (float)strtod(inputPtr,&inputPtr);
|
|
p.z = (float)strtod(inputPtr,NULL);
|
|
|
|
Renderer::getInstance()->points->push_back(p);
|
|
}
|
|
|
|
|
|
|
|
|