• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Astral0

Any good Obj Loaders out there for OpenGL?

10 posts in this topic

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.
0

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.
0

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.
0

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]
0

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
0

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.
0

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!
0

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.
0

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)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0