Finish the face and texture coordinates parsing

This commit is contained in:
Skia 2016-05-23 16:37:43 +02:00
parent 96a0e8add8
commit af57d02814
6 changed files with 30289 additions and 26 deletions

1
.gitignore vendored
View file

@ -3,7 +3,6 @@
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch

View file

@ -1,4 +1,4 @@
CC=g++
CC=clang++
FLAGS=-Wall -g
INCLUDE=-I.

View file

@ -14,34 +14,86 @@
using namespace std;
/* Define split functions */
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
return split(s, delim, elems);
}
/* >> operator for class FaceVertex */
std::istream& operator>>(std::istream& is, FaceVertex& obj) {
string s;
is >> s;
vector<string> tokens = split(s, '/');
for (unsigned int i = 0; i < tokens.size(); i++)
{
std::istringstream ss(tokens[i]);
ss >> obj.v[i];
if(ss.bad())
is.setstate(std::ios::badbit);
}
return is;
}
/* Model methods */
/* Main parsing function */
void Model::loadFile(const char *filename) {
ifstream fileStream(filename);
if (fileStream.is_open()) {
while (!fileStream.eof()) {
if (fileStream.is_open())
{
while (!fileStream.eof())
{
char buf[256];
fileStream.getline(buf, 256, '\n');
istringstream bufStream(buf);
string type;
bufStream >> type;
// cout << "Type: " << type << "\n";
if (type == "v") {
/* If needed, add a material variable, that can be added to a non existent vertex class, and that would be a
* pointer to the currently used material...
*/
if (type == "v")
{
float x, y, z;
bufStream >> x >> y >> z;
Vec3 v(x, y, z);
this->vertex_list.push_back(v); // save the values into the vector vertex_list of
// cout << "Vertex: " << v << "\n";
this->vertex_list.push_back(v);
}
else if (type == "vt")
{
float x, y;
bufStream >> x >> y;
Vec2 v(x, y);
//cout << this->vertex_list.size() << endl;
} else if (type == "f") {
int f[3] = {0};
bufStream >> f[0] >> f[1] >> f[2];
this->face_index_list.push_back(f[0]);
this->face_index_list.push_back(f[1]);
this->face_index_list.push_back(f[2]);
} else {
this->texture_list.push_back(v);
}
else if (type == "f")
{
FaceVertex v1, v2, v3;
bufStream >> v1 >> v2 >> v3;
this->face_list.push_back(Face(v1, v2, v3));
}
else if (type == "#" || type == "") // "#" defines a comment in obj files. Also skip empty lines
{
continue;
}
else
{
cout << "Unknown type: " << type << endl;
}
}
@ -49,17 +101,17 @@ void Model::loadFile(const char *filename) {
}
void Model::printVertex() {
for (unsigned int i = 0; i < this->vertex_list.size(); i++) {
for (unsigned int i = 0; i < this->vertex_list.size(); i++)
{
cout << "Vertex: " << this->vertex_list[i] << "\n";
}
for (unsigned int i = 0; i < this->face_index_list.size(); i+=3) {
cout << "Face: "
<< this->face_index_list[i]
<< " "
<< this->face_index_list[i+1]
<< " "
<< this->face_index_list[i+2]
<< "\n";
for (unsigned int i = 0; i < this->texture_list.size(); i++)
{
cout << "Texture: " << this->texture_list[i] << "\n";
}
for (unsigned int i = 0; i < this->face_list.size(); i+=3)
{
cout << "Face: " << this->face_list[i] << "\n";
}
}

30
model.h
View file

@ -11,6 +11,33 @@
#include <vectors.h>
class FaceVertex
{
private:
int v[3];
public:
FaceVertex() { v[0] = 0; v[1] = 0; v[2] = 0; }
friend std::ostream& operator<<(std::ostream& os, const FaceVertex& v) {
return os << "(" << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ")";
}
friend std::istream& operator>>(std::istream& is, FaceVertex& obj);
};
class Face
{
private:
FaceVertex v1, v2, v3;
public:
Face(FaceVertex V1, FaceVertex V2, FaceVertex V3) : v1(V1), v2(V2), v3(V3) {}
friend std::ostream& operator<<(std::ostream& os, const Face& f) {
return os << f.v1 << f.v2 << f.v3;
}
};
class Model
{
public:
@ -20,7 +47,8 @@ class Model
private:
std::vector<Vec3> vertex_list;
std::vector<int> face_index_list;
std::vector<Vec2> texture_list;
std::vector<Face> face_list;
float max_x;
float min_x;

30180
res/ALDUIN/alduin.obj Normal file

File diff suppressed because it is too large Load diff

View file

@ -19,6 +19,10 @@ class AbstractVec2
AbstractVec2( T X, T Y ) : x( X ), y( Y ) {
}
friend std::ostream& operator<<(std::ostream& os, const AbstractVec2<T>& v) {
return os << "(" << v.x << ", " << v.y << ")";
}
bool operator==( const AbstractVec2 & v ) {
return (x == v.x && y == v.y);
}