PDA

View Full Version : .obj Loader -> drawing of object



djhabana
04-03-2012, 06:29 AM
Hey ok so I've been working on my .obj loader for 3 months now and I can't seem to get it working when it comes to the drawing. Soooo.... I decided to come to the forum. anyway here is my code
Objloader.h



#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

typedef struct _Position{
char * bone_id;
float X, Y, Z;
} Position;

typedef struct _Vertex {
float X, Y, Z;
} ObjVertex;

typedef struct _Normal {
float X, Y, Z;
} ObjNormal;

typedef struct _UV {
float U, V;
} ObjTexCoord;

typedef struct _Triangle {
unsigned int Vertex[3];
unsigned int Normal[3];
unsigned int TexCoord[3];
} ObjTriangle;


class ModelData{
private:
int vCount, nCount, tCount, TrCount;
//These are the data array's
ObjVertex * VertexArray;
ObjNormal * NormalArray;
ObjTexCoord * TextureCoordinates;
ObjTriangle * Triangles;

float * Indices;
public:

void LoadMesh(char *);
void DrawMesh(GLfloat,GLfloat,GLfloat);
~ModelData()
{
delete VertexArray;
delete NormalArray;
delete TextureCoordinates;
delete Triangles;
}

};

void ModelData::LoadMesh(char *filename)
{
vCount = nCount = tCount = TrCount = 0;
vector<string> VertexLines;
vector<string> NormalLines;
vector<string> TexCoordLines;
vector<string> FaceLines;
fstream TheFile(filename);


if (!TheFile)
{
MessageBox(NULL, "File Not Found", "Msg", MB_OK | MB_ICONERROR);
}
else
{
string Line;
while (!TheFile.eof())
{
//int Found;
getline(TheFile,Line);
stringstream Test(Line);
string TestValue;
getline(Test,TestValue,' '); //Get the first "descrtiptor" of each line
//Vertexes
if(TestValue == "v")
{
vCount++;
VertexLines.push_back(Line);
}
else
//Texture Coordinates
if(TestValue == "vt")
{
tCount++;
TexCoordLines.push_back(Line);
}
else
//Vertex Normals
if(TestValue == "vn")
{
nCount++;
NormalLines.push_back(Line);
}
else
//Faces
if(TestValue == "f")
{
TrCount++;
FaceLines.push_back(Line);
}
}
TheFile.close();
}
//Allocate Memory
VertexArray = new ObjVertex[vCount];
NormalArray = new ObjNormal[nCount];
TextureCoordinates = new ObjTexCoord[tCount];
Triangles = new ObjTriangle[TrCount];

int A=(TrCount*3)*3;
Indices = new float[A];
//Temprorary text holder used for manupilation
vector<string> Token;

if (vCount>0) //Insert Vertex points XYZ
{
for (int v=0; v<vCount; v++)
{
string A;
A=VertexLines[v];
stringstream ss(A);
while (ss>>VertexLines[v])
{
Token.push_back(VertexLines[v]);
};
VertexArray[v].X = atof( Token[1].c_str());
VertexArray[v].Y = atof( Token[2].c_str());
VertexArray[v].Z = atof( Token[3].c_str());
Token.clear();
}
};

if (nCount>0) //Insert Normal Points XYZ
{
for (int vn=0; vn<nCount; vn++)
{
string A;
A=NormalLines[vn];
stringstream ss(A);
while (ss>>NormalLines[vn])
{
Token.push_back(NormalLines[vn]);
};
NormalArray[vn].X = atof( Token[1].c_str());
NormalArray[vn].Y = atof( Token[2].c_str());
NormalArray[vn].Z = atof( Token[3].c_str());
Token.clear();
};
};

if (tCount>0) //Insert Texture Coordenates UV
{
for (int vt=0; vt<tCount; vt++)
{
string A;
A=TexCoordLines[vt];
stringstream ss(A);
while (ss>>TexCoordLines[vt])
{
Token.push_back(TexCoordLines[vt]);
};
TextureCoordinates[vt].U = atof( Token[1].c_str());
TextureCoordinates[vt].V = atof( Token[2].c_str());
Token.clear();
}
};

if (TrCount>0) //Insert Face Details
{
for (int f=0; f<TrCount; f++)
{
vector<string> newtoken;
string A;
A=FaceLines[f];
stringstream ss(A);

//Put each part of the line into an array
while (ss>>FaceLines[f])
{
Token.push_back(FaceLines[f]);
};

for (vector<string>::size_type i=1; i< Token.size(); i++)
{
string save;
stringstream FACE(Token[i]);
int TestForData =0;
while (getline(FACE,save,'/')) //Split each ellement of the array
{
TestForData+=1;
newtoken.push_back(save);
};
if (TestForData<3)
{
newtoken.push_back(""); //If there is no Normal in the object it adds an empty string
}
}
//Insert Vertex
if (newtoken[0]!="" &amp;&amp; newtoken[3]!="" &amp;&amp; newtoken[6]!="")
{
Triangles[f].Vertex[0]=atoi(newtoken[0].c_str());
Triangles[f].Vertex[1]=atoi(newtoken[3].c_str());
Triangles[f].Vertex[2]=atoi(newtoken[6].c_str());
}
//Insert Textures
if (newtoken[1]!="" &amp;&amp; newtoken[4]!="" &amp;&amp; newtoken[7]!="")
{
Triangles[f].TexCoord[0]=atoi(newtoken[1].c_str());
Triangles[f].TexCoord[1]=atoi(newtoken[4].c_str());
Triangles[f].TexCoord[2]=atoi(newtoken[7].c_str());
}
//Insert Normal
if (newtoken[2]!="" &amp;&amp; newtoken[5]!="" &amp;&amp; newtoken[8]!="")
{
Triangles[f].Normal[0]=atoi(newtoken[2].c_str());
Triangles[f].Normal[1]=atoi(newtoken[5].c_str());
Triangles[f].Normal[2]=atoi(newtoken[8].c_str());
}
newtoken.clear();
Token.clear();
}
}

int Coll=-1;
for (int i=0;i<TrCount;i++)
{
for (int j=0;j<3;j++)
{
int V=Triangles[i].Vertex[j];
Indices[Coll++] = VertexArray[V].X;
Indices[Coll++] = VertexArray[V].Y;
Indices[Coll++] = VertexArray[V].Z;
}
}
};

void ModelData::DrawMesh(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x,y,z);//Position

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0,VertexArray);
glNormalPointer(GL_FLOAT, 0, NormalArray);
glTexCoordPointer(2, GL_FLOAT, 0, TextureCoordinates);
// draw data
glDrawArrays(GL_TRIANGLES, 0, vCount);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glPopMatrix();
};

djhabana
04-03-2012, 06:32 AM
Im at the point of saying, I really don't know anymore how to fix it haha, so far (according to debug and breakpoints) the code loads the .obj model

BionicBytes
04-03-2012, 10:10 AM
Well, you have to debug the OBJ model loader and actually prove to your self that the data in each array is exactly what you were expecting. Nobody here can do that for you.
Start by loading a very simple OBJ model (like a cube with NO shared faces).
Then, it's just a case of proving your draw code works. That's the easy bit and can be forcibly tested by deleting the contents of the arrays created by the OBJ loader and inserting your own hand-coded values.