3DS Loader & Viewer
by Matt Fairfax · 02/17/2001 (11:18 am) · 25 comments
Download Code File
This is a simple 3D studio model loader/viewer class that can be easily used with your projects. It isn't the most efficient way to load a 3DS model but I find it a lot easier to read and debug. I have included my simple Texture class to support texturing of the models. I know it is crude and doesn't port well to non-Windows platforms so just replace it with your favorite texture class and have fun! Feel free to use this code in any project. I just ask that you let me know if you decide to use it. This code stands as is but I would like to know of any bugs so that I can address them in later releases. I have a mostly working Direct3D 8.0 port of this code (haven't finished the texture part) if anyone is interested.
Description from file:
// This is a simple class for loading and viewing
// 3D Studio model files (.3ds). It supports models
// with multiple objects. It also supports multiple
// textures per object. It does not support the animation
// for 3D Studio models b/c there are simply too many
// ways for an artist to animate a 3D Studio model and
// I didn't want to impose huge limitations on the artists.
// However, I have imposed a limitation on how the models are
// textured:
// 1) Every faces must be assigned a material
// 2) If you want the face to be textured assign the
// texture to the Diffuse Color map
// 3) The texture must be supported by the GLTexture class
// which only supports bitmap and targa right now
// 4) The texture must be located in the same directory as
// the model
// Support for non-textured faces is done by reading the color
// from the material's diffuse color.
// Some models have problems loading even if you follow all of
// the restrictions I have stated and I don't know why. If you
// can import the 3D Studio file into Milkshape 3D
// (http://www.swissquake.ch/chumbalum-soft) and then export it
// to a new 3D Studio file, it seems to fix many of the problems
// but there is a limit on the number of faces and vertices Milkshape 3D
// can read.
Usage:
Model_3DS m;
m.Load("model.3ds"); // Load the model
m.Draw(); // Renders the model to the screen
// If you want to show the model's normals
m.shownormals = true;
// If the model is not going to be lit then set the lit
// variable to false. It defaults to true.
m.lit = false;
// You can disable the rendering of the model
m.visible = false;
// You can move and rotate the model like this:
m.rot.x = 90.0f;
m.rot.y = 30.0f;
m.rot.z = 0.0f;
m.pos.x = 10.0f;
m.pos.y = 0.0f;
m.pos.z = 0.0f;
// If you want to move or rotate individual objects
m.Objects[0].rot.x = 90.0f;
m.Objects[0].rot.y = 30.0f;
m.Objects[0].rot.z = 0.0f;
m.Objects[0].pos.x = 10.0f;
m.Objects[0].pos.y = 0.0f;
m.Objects[0].pos.z = 0.0f;
This is a simple 3D studio model loader/viewer class that can be easily used with your projects. It isn't the most efficient way to load a 3DS model but I find it a lot easier to read and debug. I have included my simple Texture class to support texturing of the models. I know it is crude and doesn't port well to non-Windows platforms so just replace it with your favorite texture class and have fun! Feel free to use this code in any project. I just ask that you let me know if you decide to use it. This code stands as is but I would like to know of any bugs so that I can address them in later releases. I have a mostly working Direct3D 8.0 port of this code (haven't finished the texture part) if anyone is interested.
Description from file:
// This is a simple class for loading and viewing
// 3D Studio model files (.3ds). It supports models
// with multiple objects. It also supports multiple
// textures per object. It does not support the animation
// for 3D Studio models b/c there are simply too many
// ways for an artist to animate a 3D Studio model and
// I didn't want to impose huge limitations on the artists.
// However, I have imposed a limitation on how the models are
// textured:
// 1) Every faces must be assigned a material
// 2) If you want the face to be textured assign the
// texture to the Diffuse Color map
// 3) The texture must be supported by the GLTexture class
// which only supports bitmap and targa right now
// 4) The texture must be located in the same directory as
// the model
// Support for non-textured faces is done by reading the color
// from the material's diffuse color.
// Some models have problems loading even if you follow all of
// the restrictions I have stated and I don't know why. If you
// can import the 3D Studio file into Milkshape 3D
// (http://www.swissquake.ch/chumbalum-soft) and then export it
// to a new 3D Studio file, it seems to fix many of the problems
// but there is a limit on the number of faces and vertices Milkshape 3D
// can read.
Usage:
Model_3DS m;
m.Load("model.3ds"); // Load the model
m.Draw(); // Renders the model to the screen
// If you want to show the model's normals
m.shownormals = true;
// If the model is not going to be lit then set the lit
// variable to false. It defaults to true.
m.lit = false;
// You can disable the rendering of the model
m.visible = false;
// You can move and rotate the model like this:
m.rot.x = 90.0f;
m.rot.y = 30.0f;
m.rot.z = 0.0f;
m.pos.x = 10.0f;
m.pos.y = 0.0f;
m.pos.z = 0.0f;
// If you want to move or rotate individual objects
m.Objects[0].rot.x = 90.0f;
m.Objects[0].rot.y = 30.0f;
m.Objects[0].rot.z = 0.0f;
m.Objects[0].pos.x = 10.0f;
m.Objects[0].pos.y = 0.0f;
m.Objects[0].pos.z = 0.0f;
About the author
I am a Game Designer at PopCap who has worked on PvZ Adventures, PvZ2, Peggle Blast, and Bejeweled Skies. I am an ex-GarageGames employee who helped ship TGE, TGEA, Torque 3D, and Constructor.
#2
Warning: If someone else wants to use this code on Linux -> remember to rename all used texture files to UPPERCASE ... *gnarf!*
04/12/2004 (1:25 pm)
You saved my day =)Warning: If someone else wants to use this code on Linux -> remember to rename all used texture files to UPPERCASE ... *gnarf!*
#3
10/12/2004 (9:58 am)
The only 3DS loader/renderer I have found that can handle all textures of even the most complex models properly ... And I have acquired and used a lot of them.
#4
Linking...
GLTexture.obj : error LNK2019: unresolved external symbol _auxDIBImageLoadA@4 referenced in function "public: void __thiscall GLTexture::LoadBMP(char *)" (?LoadBMP@GLTexture@@QAEXPAD@Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals
What should I do with it?
Thank you
11/24/2005 (12:23 pm)
I have problem with this 3DS loader. I'm using MS Visual Studio .NET 2003.When I add this files to my project (and include "Model_3DS.h", linker find this error error:Linking...
GLTexture.obj : error LNK2019: unresolved external symbol _auxDIBImageLoadA@4 referenced in function "public: void __thiscall GLTexture::LoadBMP(char *)" (?LoadBMP@GLTexture@@QAEXPAD@Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals
What should I do with it?
Thank you
#5
If you dont have the glaux library installed, (and cant find it, like me) you must write
the bitmap loader function yourself.
Here is the code, if you dont want to write it yourself:
http://www.gamedev.net/community/forums/topic.asp?topic_id=275238
12/12/2005 (9:53 am)
You need to link the glaux library.If you dont have the glaux library installed, (and cant find it, like me) you must write
the bitmap loader function yourself.
Here is the code, if you dont want to write it yourself:
http://www.gamedev.net/community/forums/topic.asp?topic_id=275238
#6
Model_3DS syr;
syr.Load("models/SYR.3DS");
it gives me the following errors:
error C2143: syntax error : missing ';' before '.'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'syr' : redefinition; different basic types
for the second line...
More: after having declared it and after having written the second line, when I put my mouse pointer on 'syr', it shows a message with 'int syr' !!!!!
Any Ideas?
Thank you
03/16/2006 (5:48 am)
I have Visual Studio 2005 and when I type the following code:Model_3DS syr;
syr.Load("models/SYR.3DS");
it gives me the following errors:
error C2143: syntax error : missing ';' before '.'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'syr' : redefinition; different basic types
for the second line...
More: after having declared it and after having written the second line, when I put my mouse pointer on 'syr', it shows a message with 'int syr' !!!!!
Any Ideas?
Thank you
#7
but still i got an essential problem with it:
the FSEEK and FREAD functions wont work! i even tried to include a bunch of extra libraries(like fstream) but still i get an error while including "cruntime.h", "mtdll.h", "file2.h",... (yes i know these are not included in the original code but i get the "cant include" error while fseek.h or fread.h tries to include them)
and when i copy these files (cruntime.h, file2.h, ...) into the include directories of the vs 6.0 include directories, i get the fatal error: "Use of C runtime library internal header file."
this is my first post on these kind of forums, and i'd be really grateful to find some help here.. thanx..
03/27/2006 (4:51 am)
yes it really is the most easy-to-debug 3ds loader i have ever found (and i have found 4 different of 'em) and it's even like a tutorial for the 3ds format for those who are not so familiar with it (like me!)but still i got an essential problem with it:
the FSEEK and FREAD functions wont work! i even tried to include a bunch of extra libraries(like fstream) but still i get an error while including "cruntime.h", "mtdll.h", "file2.h",... (yes i know these are not included in the original code but i get the "cant include" error while fseek.h or fread.h tries to include them)
and when i copy these files (cruntime.h, file2.h, ...) into the include directories of the vs 6.0 include directories, i get the fatal error: "Use of C runtime library internal header file."
this is my first post on these kind of forums, and i'd be really grateful to find some help here.. thanx..
#8
Does anyone else have the same problem?
thanks for reply Luke
05/26/2006 (3:37 am)
My Compliments! This is really the easiest 3ds-loader i have ever seen. It works almost fine for me.....except for one little problem - memory usage is increasing steadily. i dont find the bug, but there must somewhere be a little memory leek in your code.Does anyone else have the same problem?
thanks for reply Luke
#9
It sounds like your compiler dont recognize the type name Model_3DS.
Luba McLean:
There is several memory leaks in the GLTexture module.
The author uses "delete pointer_type" rather than "delete [] pointer_type".
There is also some inconsistencies in the use of "free" and "delete".
06/07/2006 (10:54 am)
Alberto Bietti:It sounds like your compiler dont recognize the type name Model_3DS.
Luba McLean:
There is several memory leaks in the GLTexture module.
The author uses "delete pointer_type" rather than "delete [] pointer_type".
There is also some inconsistencies in the use of "free" and "delete".
#10
08/15/2006 (8:37 am)
Ok, so im gonna seem like a real n00b, but could someone tell me how to use this? Do I need to make an openGL window before using the load and draw? Plus, i get a whole heap of errors when i try to compile this.
#11
09/29/2006 (3:12 pm)
So I downloaded the code. I had no errors. But when I try to run it I get the following error! Debug assertion failed! File: fseek.c line 101! Any ideas why. All I need to do once I have the files in place is to m.Load("Model.3ds"); to load the model, correct? Please help!
#12
EDIT - Problem solved. Fseek error occurs when the .3ds file you're trying to open isn't located in the exact project directory. Drop a copy into your project directory and it will solve your problems :)
12/05/2006 (8:19 am)
The fseek problem, which I just encountered as well, is because the .3ds file isn't being opened properly, and instead the pointer is set to NULL, as fopen does when a file cannot be opened properly. I wrote a small test code and opened a .3ds file in binary mode no problem. I'm still not sure why this code doesn't work, but I'm exploring and I'll report back once I get it figured out.EDIT - Problem solved. Fseek error occurs when the .3ds file you're trying to open isn't located in the exact project directory. Drop a copy into your project directory and it will solve your problems :)
#13
I am a new programer on OpenGL. These days I do some research on .3ds files
I try to display my model.3ds like the guide above
Model_3DS m;
m.Load("model.3ds"); // Load the model
m.Draw(); // Renders the model to the screen
However I only got a black screen. Is there anyone could help me with this problem? I don't know where am I wrong?
the following code is my code.
[file: main.cpp]
#include
#include
#include "GLTexture.h"
#include "Model_3DS.h"
Model_3DS m;
void display(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
m.Draw();
glutSwapBuffers();
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
m.Load("test/011.3DS");
m.visible = true;
printf("numObject=%d\n",m.numObjects);
}
int main(int argc, char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("demo2do");
init();
glutDisplayFunc(display);
glutMainLoop();
return(0);
}
Thank you very much indeed~
03/28/2007 (12:24 am)
Hi Everyone,I am a new programer on OpenGL. These days I do some research on .3ds files
I try to display my model.3ds like the guide above
Model_3DS m;
m.Load("model.3ds"); // Load the model
m.Draw(); // Renders the model to the screen
However I only got a black screen. Is there anyone could help me with this problem? I don't know where am I wrong?
the following code is my code.
[file: main.cpp]
#include
#include
#include "GLTexture.h"
#include "Model_3DS.h"
Model_3DS m;
void display(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
m.Draw();
glutSwapBuffers();
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
m.Load("test/011.3DS");
m.visible = true;
printf("numObject=%d\n",m.numObjects);
}
int main(int argc, char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("demo2do");
init();
glutDisplayFunc(display);
glutMainLoop();
return(0);
}
Thank you very much indeed~
#14
ARE YOU ALL BLIND?!?
This code causes horrible MEMORY LEAKS! How can the destructor be empty after a lot of "new" calls? I suggest to implement a suitable destructor or to search a different loader, if you are not familiar with standard c++.
About "However I only got a black screen." (if you are still interested after months): The model might be placed out of the scene, add a glTranslate3f(dx, dy, dz) just before m.Draw() trying different values of dx, dy and dz to see if something changes (you can debug your application to see where the model is placed after m.Load(...)). Try with glColor3f(0.0,0.0,1.0); instead of glColor3f(0.0,0.0,0.0); (draw transparent objects is not that great idea).
06/07/2007 (2:32 pm)
Thanks a lot, that was very useful, but... ARE YOU ALL BLIND?!?
This code causes horrible MEMORY LEAKS! How can the destructor be empty after a lot of "new" calls? I suggest to implement a suitable destructor or to search a different loader, if you are not familiar with standard c++.
About "However I only got a black screen." (if you are still interested after months): The model might be placed out of the scene, add a glTranslate3f(dx, dy, dz) just before m.Draw() trying different values of dx, dy and dz to see if something changes (you can debug your application to see where the model is placed after m.Load(...)). Try with glColor3f(0.0,0.0,1.0); instead of glColor3f(0.0,0.0,0.0); (draw transparent objects is not that great idea).
#15
Quick correction for memory leaks :
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
Model_3DS::~Model_3DS()
{
SAFE_DELETE_ARRAY( path );
SAFE_DELETE_ARRAY( Materials );
for (int k = 0; k < numObjects; k++) {
SAFE_DELETE_ARRAY( Objects[k].TexCoords );
SAFE_DELETE_ARRAY( Objects[k].TexCoords );
SAFE_DELETE_ARRAY( Objects[k].Vertexes );
SAFE_DELETE_ARRAY( Objects[k].Normals );
SAFE_DELETE_ARRAY( Objects[k].Faces );
for( int l=0; l
SAFE_DELETE_ARRAY( Objects[k].MatFaces[l].subFaces );
SAFE_DELETE_ARRAY( Objects[k].MatFaces );
}
SAFE_DELETE_ARRAY( Objects );
}
if someone is intersted.
(not tested but it should be almost that)
futhrp
11/20/2007 (4:50 am)
Hi great loader/viewer, BUT the memory management is awful.Quick correction for memory leaks :
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
Model_3DS::~Model_3DS()
{
SAFE_DELETE_ARRAY( path );
SAFE_DELETE_ARRAY( Materials );
for (int k = 0; k < numObjects; k++) {
SAFE_DELETE_ARRAY( Objects[k].TexCoords );
SAFE_DELETE_ARRAY( Objects[k].TexCoords );
SAFE_DELETE_ARRAY( Objects[k].Vertexes );
SAFE_DELETE_ARRAY( Objects[k].Normals );
SAFE_DELETE_ARRAY( Objects[k].Faces );
for( int l=0; l
SAFE_DELETE_ARRAY( Objects[k].MatFaces );
}
SAFE_DELETE_ARRAY( Objects );
}
if someone is intersted.
(not tested but it should be almost that)
futhrp
#16
I have written a litte game in C++/OpenGL using this loader, in Windows it works flawless.
Compiling in Linux worked after i included in the cpp file, but it won't run.
Everytime i try to load my models (the same one as in windows), i got an "segmentation fault"
error at runtime.
Is there any problem if using this under Linux ?
Thanks
01/11/2008 (6:47 am)
Hi i could use some help with this loader.I have written a litte game in C++/OpenGL using this loader, in Windows it works flawless.
Compiling in Linux worked after i included
Everytime i try to load my models (the same one as in windows), i got an "segmentation fault"
error at runtime.
Is there any problem if using this under Linux ?
Thanks
#17
04/11/2008 (5:13 am)
Helped me pull in some nice looking models quickly and painlessly. Hopefully no problems with Linux--I should be finding out soon. Can't offhand imagine what there would be... maybe the memory issues above?
#18
it was a little error, but not easy to find.
The problem was: in my code is used this filename BARREL.3DS, but the files had the name format
BARREL.3ds.
that was all ;)
the error message was not really clear though
04/11/2008 (8:40 am)
I managed to solve this problem. it was a little error, but not easy to find.
The problem was: in my code is used this filename BARREL.3DS, but the files had the name format
BARREL.3ds.
that was all ;)
the error message was not really clear though
#19
04/11/2008 (3:32 pm)
:hehe: Death by Windows :)
#20
For some reason, when I use it, it shows the model with no shadows.
Does anyone have a solution for this problem?
(I've tried 2 models from different sources)
Edit: Nevermind, I found a way to do it.
If you also have problems with this, enable lighting, or take a lighting tutorial.
07/19/2008 (12:00 pm)
hello, I was just wondering if you guys managed to get your models with the shadows.For some reason, when I use it, it shows the model with no shadows.
Does anyone have a solution for this problem?
(I've tried 2 models from different sources)
Edit: Nevermind, I found a way to do it.
If you also have problems with this, enable lighting, or take a lighting tutorial.

Torque Owner David M. Byttow