1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
//reader.cpp
#include <GL/freeglut.h>
#include <fstream>
#include "reader.h"
using namespace std;
Reader::Reader()
{
for(int i = 0; i < 256; i++ ) {
vertex[i].x = 0;
vertex[i].y = 0;
vertex[i].z = 0;
}
}
Reader::~Reader()
{
}
void Reader::load(char* filename)
{
ifstream file;
file.open(filename);
string str;
while(!file.eof()) //while we are still in the file
{
getline(file,str); //move one line down
if(str[0] == 'v') break; //if we have a vertex line, stop
}
int v = 0;
while(str[0] == 'v') {
int i = 0;
while(true)
{
while(str[i] == ' ' )
{
i++; //move a space over
}
i++;
i++;
int j = i, k = i;
while(str[i] != ' ') {
i++;
k = i;
}
vertex[v].x = atof(str.substr(j, k-j).c_str());
//moving on to the y coord
while(str[i] == ' ' ) {
i++;
}
int q = i, w = i;
while(str[i] != ' ' ) {
i++;
w = i;
}
vertex[v].y = atof(str.substr(q, w-q).c_str());
while(str[i] == ' ' ) {
i++;
}
int a = i, s = i;
while(str[i] != ' ' ) {
i++;
s = i;
}
vertex[v].z = atof(str.substr(a, s-a).c_str());
break;
}
v++;
getline(file, str);
}
}
void Reader::draw(char *filename)
{
ifstream file;
file.open(filename);
string str;
while(true)
{
getline(file, str);
if(str[0] == 'f' ) {
break;
}
}
int i = 0;
while(str[0] == 'f')
{
while(str[i] == 'f') i++;
while(str[i] == ' ') i++;
int j = i, k = i;
while(str[i] != ' ') {
i++;
k = i;
}
int one = atof(str.substr(j, k - j).c_str());
i +=1;
j = i;
k = i;
while(str[i] != ' ') {
i++;
k = i;
}
int two = atof(str.substr(j, k - j).c_str());
i+=1;
j = i;
k = i;
while(str[i] != ' ') {
i++;
k = i;
}
int three = atof(str.substr(j, k - j).c_str());
i+=1;
j = i;
k = i;
while(str[i] != ' ') {
i++;
k = i;
}
int four = atof(str.substr(j, k - j).c_str());
glBegin(GL_POLYGON);
glVertex3d(vertex[one - 1].x, vertex[one - 1].y, vertex[one - 1].z);
glVertex3d(vertex[two - 1].x, vertex[two - 1].y, vertex[two - 1].z);
glVertex3d(vertex[three - 1].x, vertex[three - 1].y, vertex[three - 1].z);
glVertex3d(vertex[four - 1].x, vertex[four - 1].y, vertex[four - 1].z);
glEnd();
getline(file, str);
i = 0;
}
}
|