Sign in to follow this  

Any good Obj Loaders out there for OpenGL?

This topic is 2428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been in search for an obj loader that actually works, preferably for Wavefront obj files that support .mtl files for texture. Any help in finding one would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
the forum faq lists a loader thread by Ranger_One. i have my own that is modified version of some older code (and a really slow one that i wrote myself, but doesn't support textures well). i'll release the slow loading one to you if you don't feel Ranger_ones is any good.

Share this post


Link to post
Share on other sites
Anist, Ranger_one's code looks decent but doesnt have methods to draw the models. If I can check out your obj loader, that would be cool.

I'm surprised there arent any fully loaded libraries for making use of obj files. Everyone seems to have to resort to writing their own.

Share this post


Link to post
Share on other sites
ok, it does support texture mapping, but you have to load and bind the texture prior to drawing. normals also need supporting, these can be added to the draw finction fairly easily, they are loaded, but not used:


#include <stdio.h>
#include <string.h>

#define Debug(x){MessageBox(NULL, x, "Debug:", MB_OK);}
struct vertex
{
float x, y, z;
unsigned int index;
};
typedef vertex normal;
struct texcoord
{
float u, v;
unsigned int index;
};

struct face
{
float x[3], y[3], z[3]; //vertices
float u[3], v[3]; //text coords
float a[3], b[3], c[3]; //normals
};

struct material
{
char name[32];
int illum;
char map_Kd[32];
float Ni;
float Kd[3];
float Ka[3];
float Tf[3];
unsigned int *texture;
};

class model
{
private:
int faces;
int coords;
int normals;
bool loaded;
char mtllib[256];
char filename[256];
char directory[256];
unsigned int vindex;
unsigned int tindex;
unsigned int nindex;
struct vnode
{
vertex data;
vnode * next;
};
struct tnode
{
texcoord data;
tnode * next;
};

struct mnode
{
material data;
mnode * next;
};
struct fnode
{
face data;
mnode * mat;
fnode * next;
};

vnode * vfirst;
vnode * vcurrent;
tnode * tfirst;
tnode * tcurrent;
vnode * nfirst;
vnode * ncurrent;
fnode * ffirst;
fnode * fcurrent;
mnode * mfirst;
mnode * mcurrent;
void skipComment(FILE *);

bool loadVertex(FILE *);
bool loadTexCoord(FILE *);
bool loadNormal(FILE *);
bool loadFace(FILE *);
bool loadMaterialLib(FILE *);
void loadMaterials(FILE *);
void useMaterial(FILE *);

public:
bool Load(char * objfile);
model()
{
vfirst=vcurrent= NULL;
tfirst=tcurrent= NULL;
nfirst=ncurrent= NULL;
ffirst=fcurrent= NULL;
mfirst=mcurrent= NULL;
vindex=tindex=nindex= 1;
}
void draw();
float Y(float, float){return 0.0;}
bool Collide(float, float, float,float, float, float){return false;}
};

bool model::Load(char * objfile)
{
char buffer[256];
strcpy(filename, objfile);
FILE * file = fopen(filename, "r");

if(file == NULL)
{
MessageBox(NULL, objfile, "Model file not found:", MB_OK);
return false;
}
while(fscanf(file, "%s", buffer) != EOF)
{
if(!strcmp("#", buffer))skipComment(file);
if(!strcmp("mtllib", buffer))loadMaterialLib(file);
if(!strcmp("v", buffer))loadVertex(file);
if(!strcmp("vt", buffer))loadTexCoord(file);
if(!strcmp("vn", buffer))loadNormal(file);
if(!strcmp("f", buffer))loadFace(file);
if(!strcmp("s", buffer))fscanf(file, "%s", buffer);
if(!strcmp("usemtl", buffer))useMaterial(file);

}
fclose(file);
loaded = true;
return true;
}

void model::useMaterial(FILE * file)
{
char buffer[256];
mnode * cursor = mfirst;
fscanf(file, "%s", buffer);
while(strcmp(buffer, cursor->data.name))
cursor= cursor->next;
mcurrent = cursor;
}

void model::skipComment(FILE * file)
{
char buffer[256];
fgets(buffer, 256, file);
}

bool model::loadMaterialLib(FILE * file)
{
char * wd = strtok(filename, "/");
char buffer[256];
fscanf(file, "%s", buffer);
sprintf(mtllib, "%s/%s", wd, buffer);
strcpy(directory, wd);
FILE * lib = fopen(mtllib, "r");
if(lib == NULL)
{
MessageBox(NULL, mtllib, "Material library not found:", MB_OK);
return false;
}
else loadMaterials(lib);
fclose(lib);
return true;
}

void model::loadMaterials(FILE * file)
{

char parameter[32];
mnode * temp;
while(fscanf(file, "%s", parameter) != EOF)
{

if(!strcmp("newmtl", parameter))
{
temp = new mnode();
fscanf(file, "%s", temp->data.name);
}
if(!strcmp("illum", parameter))
fscanf(file, "%i", &temp->data.illum);
if(!strcmp("map_Kd", parameter))
{
fscanf(file, "%s", temp->data.map_Kd);
if(strstr(temp->data.map_Kd, ".bmp") != NULL)
{
char buffer[256];
sprintf(buffer, "%s/%s", directory, temp->data.map_Kd);
//#ifdef __GL_H__
//temp->data.texture = new unsigned int();
//loadTexture(buffer, temp->data.texture, 1);
//#endif
}
}
if(!strcmp("Ni", parameter))
fscanf(file, "%f", &temp->data.Ni);
if(!strcmp("Kd", parameter))
fscanf(file, "%f %f %f", &temp->data.Kd[0],&temp->data.Kd[1],&temp->data.Kd[2]);
if(!strcmp("Ka", parameter))
fscanf(file, "%f %f %f", &temp->data.Ka[0],&temp->data.Ka[1],&temp->data.Ka[2]);
if(!strcmp("Tf", parameter))
{
fscanf(file, "%f %f %f", &temp->data.Tf[0],&temp->data.Tf[1],&temp->data.Tf[2]);
if(mfirst == NULL)
{
mfirst = temp;
mcurrent = temp;
mfirst->next = NULL;
}
else
{
mcurrent->next = temp;
mcurrent = mcurrent->next;
mcurrent->next = NULL;
}

}
}

}



bool model::loadVertex(FILE * file)
{
vnode * temp = new vnode();
fscanf(file, "%f %f %f", &temp->data.x, &temp->data.y, &temp->data.z);
temp->data.index = vindex;
if(vfirst == NULL)
{
vfirst = temp;
vcurrent = temp;
vfirst->next = NULL;
}
else
{
vcurrent->next = temp;
vcurrent = vcurrent->next;
vcurrent->next = NULL;
}
vindex++;
return true;
}

bool model::loadTexCoord(FILE * file)
{
tnode * temp = new tnode();
fscanf(file, "%f %f", &temp->data.u, &temp->data.v);
temp->data.index = tindex;
temp->next = NULL;
if(tfirst == NULL)
{
tfirst = temp;
tcurrent = temp;
}
else
{
tcurrent->next = temp;
tcurrent = tcurrent->next;
}
tindex++;
return true;
}

bool model::loadNormal(FILE * file)
{
vnode * temp = new vnode();
fscanf(file, "%f %f %f", &temp->data.x, &temp->data.y, &temp->data.z);
temp->data.index = nindex;
temp->next = NULL;
if(nfirst == NULL)
{
nfirst = temp;
ncurrent = temp;
}
else
{
ncurrent->next = temp;
ncurrent = ncurrent->next;
}
nindex++;
return true;
}

bool model::loadFace(FILE * file)
{
fnode * temp = new fnode();
temp->mat = mcurrent;
vnode * vcursor = vfirst;
tnode * tcursor = tfirst;
vnode * ncursor = nfirst;
unsigned int v_index[3], t_index[3], n_index[3];
for(int i = 0; i < 3; i++)
{
vcursor = vfirst;
tcursor = tfirst;
ncursor = nfirst;
fscanf(file, "%i/%i/%i", &v_index[i], &t_index[i], &n_index[i]);

for(int v = 1; v != v_index[i]; v++)
vcursor = vcursor->next;
temp->data.x[i] = vcursor->data.x;
temp->data.y[i] = vcursor->data.y;
temp->data.z[i] = vcursor->data.z;

for(int k = 1; k != t_index[i]; k++)
tcursor = tcursor->next;
temp->data.u[i] = tcursor->data.u;
temp->data.v[i] = tcursor->data.v;

for(int j = 1; j != n_index[i]; j++)
ncursor = ncursor->next;
temp->data.a[i] = ncursor->data.x;
temp->data.b[i] = ncursor->data.y;
temp->data.c[i] = ncursor->data.z;
}

temp->next = NULL;
if(ffirst == NULL)
{
ffirst = temp;
fcurrent = temp;
ffirst->next = NULL;
}
else
{
fcurrent->next = temp;
fcurrent = fcurrent->next;
fcurrent->next = NULL;
}
faces++;
return true;
}

void model::draw()
{
#ifdef __GL_H__
if(loaded)
{
fnode * fcursor = ffirst;
glBindTexture(GL_TEXTURE_2D, *ffirst->mat->data.texture);
glBegin(GL_TRIANGLES);
while(fcursor != NULL)
{
glColor3f(1.0f, 1.0f, 1.0f);

glTexCoord2f(fcursor->data.u[0], fcursor->data.v[0]);
glNormal3f(fcursor->data.a[0], fcursor->data.b[0], fcursor->data.c[0]);
glVertex3f(fcursor->data.x[0], fcursor->data.y[0], fcursor->data.z[0]);

glTexCoord2f(fcursor->data.u[1], fcursor->data.v[1]);
glNormal3f(fcursor->data.a[1], fcursor->data.b[1], fcursor->data.c[1]);
glVertex3f(fcursor->data.x[1], fcursor->data.y[1], fcursor->data.z[1]);

glTexCoord2f(fcursor->data.u[2], fcursor->data.v[2]);
glNormal3f(fcursor->data.a[2], fcursor->data.b[2], fcursor->data.c[2]);
glVertex3f(fcursor->data.x[2], fcursor->data.y[2], fcursor->data.z[2]);
fcursor = fcursor->next;
}
glEnd();
}
#endif
}





EDIT: added the normal code. should work now.

[Edited by - anist on September 18, 2005 11:39:45 PM]

Share this post


Link to post
Share on other sites
Anist, I attempted compiling your code and get the following errors. Thanks in advance.

error C3861: 'loadTexture': identifier not found, even with argument-dependent lookup
Error refers to line 194 containing: loadTexture(buffer, temp->data.texture, 1);

The message box lines create errors too but should work with them commented out, I'm assuming

Share this post


Link to post
Share on other sites
Quote:
Original post by Astral0
Anist, I attempted compiling your code and get the following errors. Thanks in advance.

error C3861: 'loadTexture': identifier not found, even with argument-dependent lookup
Error refers to line 194 containing: loadTexture(buffer, temp->data.texture, 1);

The message box lines create errors too but should work with them commented out, I'm assuming


ok, i commented out the originals lines that were doing that. textures never had full support in this implementations and neither do materials. and i hate posting this code, becuase it doesn't have a deconstructor that reclaims memory (this is a bad idea in reality, but it was never production level). but it does work fine for loading geometry and displaying it. i use it for design of basic scenes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Astral0
I'm surprised there arent any fully loaded libraries for making use of obj files. Everyone seems to have to resort to writing their own.


Well there's a lot of tutorials on them, just not using that texture format you have described. Take a look at UGP (Tutorials->OpenGL->Page6). As for a library, this looks like one that you could try out. Good luck!

Share this post


Link to post
Share on other sites
Hi here is a code I wrote. There is also a ready made c program which is fully executable. The program can load any .obj file but is limited to reading only the vertices. I have tried many .obj like cars, elephants, buildings etc. It loads nicely. Maybe you can improve the program and keep us updated.
[url="http://www.objinopengl.blogspot.com"]
www.objinopengl.blogspot.com[/url]

Here are screen shots. You can download the source code from the above site.

[img]http://2.bp.blogspot.com/-3zE0iDTz9Rg/Td1EoiSMhMI/AAAAAAAAAbw/Cdrv5ZpayQM/s400/objload2.png[/img]

[img]http://2.bp.blogspot.com/-tYetAhqr3CM/Td1FHjbA2qI/AAAAAAAAAb4/IiSmhKfPI1I/s400/objload3.png[/img]

And this is also my first post here.

Share this post


Link to post
Share on other sites
[quote name='Astral0' timestamp='1127078868' post='3264112']
Anist, Ranger_one's code looks decent but doesnt have methods to draw the models. If I can check out your obj loader, that would be cool.

I'm surprised there arent any fully loaded libraries for making use of obj files. Everyone seems to have to resort to writing their own.
[/quote]

Most good object loaders are API agnostic (Why should a model loader care about how a model is rendered ?, its job is to load the model, nothing else)

Share this post


Link to post
Share on other sites
Sign in to follow this