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

Drawing OBJ files using STL vector

Started by WillPash Sep 2, 2006 at 11:24 PM 3 replies 900+ views
Original Post
WillPash
WillPash
Hi I am starting to use OBJ, and I cam across some nifty code from Stompy9999's Blog and code archive on how to load .obj files using STL vectors and Vertex Arrays (glVertexPointer). I am using the cpp files Obj_loader.h and Obj_loader.cpp as loaders. I have rarely used STL or Vectors so i need some advice on how to use the vectors in OpenGL's vertex arrays... I am trying to render the vertices using vectors using this little snippets below Snippet to load OBJ into memory
// LOADING THE OBJ FILE INTO THE PROGRAM MEMORY
int init::LoadOBJ(string filename )
{

    model = new OBJ_MODEL;
    char some_char;

	    try
    {
       if(!model->m_Load(filename.c_str()))
          throw "Error loading model";
    }
    catch(string error)
    {
    out_file << error;
       
    return 0;
    }

      delete [] model;
return 0;
}
Snippet used during rendering
// TO RENDER THE OBJECT
glLoadIdentity();
glPushMatrix();
glTranslatef(1.0f,1.0f,80.0f);
vector<Vertex>	Vertices;
glColor4f(1.0f,1.0f,1.0f,1.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &Vertices[0]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, Vertices.size());
glDisableClientState( GL_VERTEX_ARRAY );
glPopMatrix();
When i compile, it seems fine, but when i run the program it load the file, but it doesnt show the object.... Does anybody know what is wrong with the code above as the object has definately loaded into memory from the .obj file.. I would be greatly appreciate it if someone can help... Regards Will
Dave 'Kit' Wilson - Reliant Code
Zakwayda
Zakwayda
I'm assuming there's more to the story here, as your rendering snippet simply creates an empty std::vector<> at local scope and renders from it; as such, you're unlikely to see anything meaningful onscreen. Somehow or another the vertex data needs to be drawn from the data loaded from the .obj file, that is, whatever is loaded by m_Load().

A couple of other notes. It's only a convention, but the m_ prefix is usually used for member variables, not member functions. Also, you should use std::auto_ptr<> or the boost equivalent for the OBJ_MODEL pointer so that it's cleaned up properly. Also, some might suggest that failure to load the .obj file isn't really an 'exceptional' situation per se, and that throwing might not be the best choice here.

On that topic, even if the model is loaded successfully, it is declared at local scope and is immediately deleted. I doubt this is what you intend.

Also, you seem to have an unused variable there (some_char), and in general std::string should be passed by constant reference rather than by value. That's probably more input than you asked for, but it might help you clean up the program a bit as you go about fixing the errors.

[Edit: To be more clear about the problem with the use of exceptions in your example, you're essentially using them to control the logical flow; in this case, a simple control structure such as if/else would probably be more appropriate.]

[Edit: Oh, and one more thing :) You return the same value from LoadOBJ() whether it succeeds or fails.]
LizardCPP
LizardCPP
You don't delete the model either if you catch an exception.

Also, model is not allocated as an array but it is deleted as one?

Lizard
WillPash
WillPash
Thanks to you both JYK and LizardCPP. Oh i can see... Now im wondering where would i put the std::auto_ptr<> into which code. Im still unaware of where to place the code

Does anyone know where i could place it as im extremely new at STL...
Thank you for your help....
Dave 'Kit' Wilson - Reliant Code
Zakwayda
Zakwayda
Quote:
Original post by WillPash
Thanks to you both JYK and LizardCPP. Oh i can see... Now im wondering where would i put the std::auto_ptr<> into which code. Im still unaware of where to place the code

Does anyone know where i could place it as im extremely new at STL...
Thank you for your help....
Actually I wouldn't worry about using auto_ptr<> at the moment, as there are some other things involved here (such as scope) which are more fundamental. Just remember that whatever you 'new' you also need to delete, so you need to arrange your code so that this is ensured. Also, dynamic allocation of a single object via new should be paired with a 'delete', while allocation of an array should be paired with a 'delete []' (as was mentioned).

Topic Locked

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

Sign in to reply to this topic.