Loading wavefront obj file into an indexed vertex array...
Hi guys, i'm trying to load a .obj model into an indexed vertex array. I can read and parse all the data from the file just fine, however converting the faces into an indexed array seems to escape me. Can anyone shove me in the right direction? My code (This is only a spike solution):
.h
Code:
#ifndef _H_LOAD_
#define _H_LOAD_
#include <vector>
#include <string>
using std::vector;
using std::string;
struct xyz {
union {
struct {float x, y, z;};
float data[3];
};
};
struct ijk {
union {
struct {int i, j, k;};
int data[3];
};
};
struct st {
union {
struct {float s, t;};
float data[2];
};
};
struct face {
union {
struct {
int x, y, z;
int nx, ny, nz;
int u, t, p;
};
int data[9];
};
};
struct lookup {
};
static vector<xyz> g_vVerts;
static vector<st> g_vCords;
static vector<xyz> g_vNorms;
static vector<ijk> g_vLookupTable;
bool LoadWavefrontObject(const char* szName);
#endif
.cpp
Code:
#include "Load.h"
#include <algorithm>
#include <fstream>
using std::ifstream;
bool LoadWavefrontObject(const char* szName) {
vector<xyz> vBuildVerts;
vector<st> vBuildCords;
vector<xyz> vBuildNorms;
vector<face> vFaceLookup;
ifstream wave_object(szName);
if (!wave_object.is_open())
return false;
bool bContainsNorms = false;
bool bContainsCords = false;
char buffer[256];
char c;
while (!wave_object.eof()) {
wave_object.get(c);
if (c == 'v') {
wave_object.get(c);
if (c == ' ' || c == '\t') {
xyz temp;
wave_object.getline(buffer, 256);
sscanf(buffer, "%f %f %f", &temp.x, &temp.y, &temp.z);
vBuildVerts.push_back(temp);
} else if (c == 't') {
st temp;
wave_object.getline(buffer, 256);
sscanf(buffer, " %f %f", &temp.s, &temp.t);
vBuildCords.push_back(temp);
bContainsCords = true;
} else if (c == 'n') {
xyz temp;
wave_object.getline(buffer, 256);
sscanf(buffer, " %f %f %f", &temp.x, &temp.y, &temp.z);
vBuildNorms.push_back(temp);
bContainsNorms = true;
} else {
wave_object.getline(buffer, 256);
}
} else if (c == 'f') {
xyz zero_xyz = {0.0f, 0.0f, 0.0f};
st zero_st = {0.0f, 0.0f};
face temp = {0, 0, 0, 0, 0, 0, 0, 0, 0};
wave_object.getline(buffer, 256);
if (bContainsNorms && bContainsCords) {
sscanf(buffer, "%d/%d/%d %d/%d/%d %d/%d/%d", &temp.x, &temp.nx, &temp.u,
&temp.y, &temp.ny, &temp.t,
&temp.z, &temp.nz, &temp.p);
} else if (!bContainsNorms && bContainsCords) {
sscanf(buffer, "%d/%d %d/%d %d/%d", &temp.x, &temp.u,
&temp.y, &temp.t,
&temp.z, &temp.p);
} else if (bContainsNorms && !bContainsCords) {
sscanf(buffer, "%d//%d %d//%d %d//%d", &temp.x, &temp.nx,
&temp.y, &temp.ny,
&temp.z, &temp.nz);
} else {
sscanf(buffer, " %d %d %d", &temp.x, &temp.y, &temp.z);
}
vFaceLookup.push_back(temp);
} else {
wave_object.getline(buffer, 256);
}
}
wave_object.close();
// Do some magic to go from faces to indexed vertex arrays......
return true;
}
Thank you for your time
~Gabor
Re: Loading wavefront obj file into an indexed vertex array...
Quote:
struct face {
union {
struct {
int x, y, z;
int nx, ny, nz;
int u, t, p;
};
int data[9];
};
};
This isn't even a complete face since you only list one vertex.
Try this:
Code:
struct face {
int num_indices;
int *vertex_indices; // dynamically allocated (num_indices)
int *uv_indices; // dynamically allocated (num_indices)
int *normal_indices; // dynamically allocated (num_indices)
}
Or if you're using STL:
Code:
struct face {
vector<int> vertex_indices;
vector<int> uv_indices;
vector<int> normal_indices;
}