Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

31337

OBJ Loader Confusion!

This topic is 5799 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

Can anybody critique my obj loader? How could I make it better and faster, and also texturing it doesn''t work. It seems as though all the vertices use the same texture coordinate. I have spent endless hours it seems on this and some help would be great. Well enough talking here it is objLoader.cpp: #include <stdio.h> // Header File For Input/Output #include <string.h> // Header for working with strings #include <stdarg.h> #include <iostream.h> // Header for C++ console input/output #include <fstream.h> // Header for C++ file I/O #include <math.h> // Header for sine and cosine #include <gl/glfw.h> // Header for GLFW, OpenGL and GLU #include <gl/gl.h> // Header file for OpenGL #include <gl/glaux.h> // Header file for GLaux #include <gl/glu.h> // Header file for glu #include <gl/glut.h> // Header file for glut #include "objLoader.h" // Header file for the OBJ variables/class #include "loadTexture.h" void OBJ::displayModel() { for(int i = 0; i.texCords[0] ] << texcordy[ faces[i].texCords[0] ] << endl; cout << texcordx[ faces[i].texCords[1] ] << texcordy[ faces[i].texCords[1] ] << endl; cout << texcordx[ faces[i].texCords[2] ] << texcordy[ faces[i].texCords[2] ] << endl; } } void OBJ::readstr(FILE *f,char *string) // Reads A String From File (f) { do // Do This { fgets(string, 255, f); // Gets A String Of 255 Chars Max From f (File) } while ((string[0] == ''/'') || (string[0] == ''\n'')); // Until End Of Line Is Reached return; // Return } void OBJ::drawOBJ(int mode) { if(mode==0) { glColor4f(1, 1, 1, 1); for(int i = 0; i // Header File For Input/Output #include <string.h> // Header for working with strings #include <stdarg.h> #include <iostream.h> // Header for C++ console input/output #include <fstream.h> // Header for C++ file I/O class OBJ { public: void displayModel(); void readstr(FILE *f,char *string); void drawOBJ(int mode); int loadOBJ(const char *filename); private: int linenumber; struct polyface { int vertices[4]; int texCords[4]; int uvNormals[4]; int status; }; /* In reference to the status variable, if it is 0 it means its a triangle with 3 vertices, no tex cords, and no lighting normals. If it is 1 then it means that its a quad with 4 vertices, no tex cords, and no lighting normals. If it is 2 then it means that its a triangle with 3 vertices, 3 texture cordinates, and no lighting normals. If it is a 3 then it means its a quad with 4 vertices, 4 tex cords, and no lighting normals. If it is a 4 then it means that its a triangle with 3 tex cords, vertices, and lighting normals. If its a 5 then it means its a quad with 4 tex cords, vertices, and lighting normals. If its a 6 then it means its a triangle with 3 vertices and 3 lighting normals but if its a 7 its a quad. Sounds simple eh? */ /* Here''s all the memory used for storing the obj data. Please note the data is shared between loadOBJ and drawOBJ. Obviously this program needs a more object oriented structure however for now this gets the job done */ // Vertice memory float *vertx; float *verty; float *vertz; // Texture Coordinate memory float *texcordx; float *texcordy; // Lighting normal memory float *lightingx; float *lightingy; float *lightingz; int verts; // how many vertices int cords; // how many texture coordinates int normals; // how many normals int groups; // how many groups int facesn; // how many face lines // Pointer for f lines polyface *faces; }; #endif Thanks! My main question is - whats the deal with the texture coordinates? It looks as though the problem is in the draw function but I cant find out why.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You want people to read all that?! How bout pick ONE part that needs help and ask about.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Dang thats like going to your friend and asking him to read an artical about quantom physics and see if there is something wrong with it. (Considering that your friends has no idea what quantom physics is)

Share this post


Link to post
Share on other sites
Lol sorry about that guys. I thought I tipped you guys off when I said I thought the problem was in the drawing code

Share this post


Link to post
Share on other sites
some performance tips:

- use pointer arithmetic instead of indexed array access wherever possible (replacing faces[counter[3]] with a single pointer can be a good start).

- use memcpy instead of one-element float transfers (values[k]). you'll need to rearrange your values elements for this.

- use one big dynamic array for all file data. this will mainly affect loading speed at the expense of even more cryptic code.

- if you're using windows, use a file mapping and string manipulation routines instead of stdio ones. this should give you a noticeable decrease in loading time, although it won't affect rendering performance.

- to get better rendering performance, convert your model to a single vertex array-triangle list and use one gldrawarrays call to render it. or try putting it in a display list*.

* i'm not sure how display lists are implemented internally, and in particular, what their memory usage is. if they take significantly more memory than a vertex array, you should probably use the array.

depending on your experience and coding style, you may see these tweaks as making your code cleaner or uglier. keep in mind that they generally (file mapping optimization may actually make debugging easier) reduce your ability to debug this code, as well as tend to screw you up more in case you do put a bug in. so make sure you're not optimizing prematurely, that you know what you're doing, and that you optimize after coding required functionality and testing.

of course, for some people the above optimizations are natural, and they wouldn't write the kind of code you wrote at all. this doesn't mean that your code is bad, performance generally comes at the price of readability and sometimes portability.

[edited by - niyaw on December 5, 2002 3:53:41 AM]

[edited by - niyaw on December 5, 2002 3:29:47 PM]

Share this post


Link to post
Share on other sites
Thanks for the performance tips. Once I get the texture coordinates working I will probably optimize it. Do you have any idea why the texturing coordinates are not being done properly? Thanks a bunch

Share this post


Link to post
Share on other sites
not really, as i have no experience with .obj file format and don''t even know what the format is.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!