Skip to main content
GameDev.net gamedev.net
🔒 Locked

Any good Obj Loaders out there for OpenGL?

Started by Astral0 Sep 18, 2005 at 6:53 PM 9 replies 17.2k views
Original Post
Astral0
Astral0
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.
anist
anist
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.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
Drew_Benton
Drew_Benton
Take a look at this reference page. Perhaps you could take any OBJ loader and add in the functionality to parse the MTL file format as well.
Astral0
Astral0
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.
anist
anist
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, &t_index, &n_index);				for(int v = 1; v != v_index; v++)			vcursor = vcursor->next;		temp->data.x = vcursor->data.x;		temp->data.y = vcursor->data.y;		temp->data.z = vcursor->data.z;		for(int k = 1; k != t_index; k++)			tcursor = tcursor->next;		temp->data.u = tcursor->data.u;		temp->data.v = tcursor->data.v;				for(int j = 1; j != n_index; j++)			ncursor = ncursor->next;		temp->data.a = ncursor->data.x;		temp->data.b = ncursor->data.y;		temp->data.c = 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]
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
Astral0
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
anist
anist
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.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
Drew_Benton
Drew_Benton
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!
Astral0
Astral0
I finally found one that uses mtl files. I found it on this page: http://isg.cs.tcd.ie/cosulliv/4BA6/Lab4.html

It was meant for milkshake so I just had to adjust the maya exported mtl file to look more like the one in the example. Otherwise it seems to be working great.
tejus
tejus
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.

www.objinopengl.blogspot.com


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

objload2.png

objload3.png

And this is also my first post here.
SimonForsman
SimonForsman

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.


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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.