Advertisement
thecplusplusguy

Simple obj loader 2 (OpenGL,SDL,C++) - objloader.h

Aug 26th, 2011
10,793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. //This example program is created by thecplusplusuy for demonstration purposes. It's a simple 3D model loader (wavefront (.obj)), which is capable to load materials and UV textures:
  2. //http://www.youtube.com/user/thecplusplusguy
  3. //Free source, modify if you want, LGPL licence (I guess), I would be happy, if you would not delete the link
  4. //so other people can see the tutorial
  5. //this file is the objloader.h
  6. #include <SDL/SDL.h>
  7. #include <GL/gl.h>
  8. #include <GL/glu.h>
  9. #include <cstdlib>
  10. #include <vector>
  11. #include <string>
  12. #include <algorithm>
  13. #include <fstream>
  14. #include <cstdio>
  15. #include <iostream>
  16. #ifndef OBJLOADER_H
  17. #define OBJLOADER_H
  18.  
  19. //This struct contain 3 floats and a constructor, it's used for vertexes and normal vectors
  20. struct coordinate{
  21.     float x,y,z;
  22.     coordinate(float a,float b,float c);
  23. };
  24.  
  25. //This structure is store every property of a face
  26. struct face{
  27.     int facenum;    //the number of the face (it's start from 1 not 0, so if you use it as an index, subtract 1 from it), it's used for the normal vectors
  28.     bool four;      //if true, than it's a quad else it's a triangle
  29.     int faces[4];   //indexes for every vertex, which makes the face (it's start from 1 not 0, so if you use it as an index, subtract 1 from it)
  30.     int texcoord[4];    //indexes for every texture coorinate that is in the face (it's start from 1 not 0, so if you use it as an index, subtract 1 from it)
  31.     int mat;                    //the index for the material, which is used by the face
  32.     face(int facen,int f1,int f2,int f3,int t1,int t2,int t3,int m);    //constuctor for triangle
  33.     face(int facen,int f1,int f2,int f3,int f4,int t1,int t2,int t3,int t4,int m);  //-"- for quad
  34. };
  35.  
  36. //this is a structure, which contain one material
  37. struct material{
  38.     std::string name;   //the name of the material
  39.     float alpha,ns,ni;  //some property, alpha, shininess, and some other, which we not used
  40.     float dif[3],amb[3],spec[3];    //the color property (diffuse, ambient, specular)
  41.     int illum;  //illum - we not use it
  42.     int texture;    //the id for the texture, if there is no texture than -1
  43.     material(const char* na,float al,float n,float ni2,float* d,float* a,float* s,int i,int t);
  44. };
  45.  
  46. //texture coorinate (UV coordinate), nothing to explain here
  47. struct texcoord{
  48.     float u,v;
  49.     texcoord(float a,float b);
  50. };
  51.  
  52. //the main class for the object loader
  53. class objloader{
  54.     std::vector<std::string*> coord;    //every line of code from the obj file
  55.     std::vector<coordinate*> vertex;    //all vertexes
  56.     std::vector<face*> faces;                   //all faces
  57.     std::vector<coordinate*> normals;   //all normal vectors
  58.     std::vector<unsigned int> texture;//the id for all the textures (so we can delete the textures after use it)
  59.     std::vector<unsigned int> lists;    //the id for all lists (so we can delete the lists after use it)
  60.     std::vector<material*> materials;   //all materials
  61.     std::vector<texcoord*> texturecoordinate;   //all texture coorinate (UV coordinate)
  62.     bool ismaterial,isnormals,istexture;    //obvious
  63.     unsigned int loadTexture(const char* filename); //private load texture function
  64.     void clean();   //free all of the used memory
  65.    
  66.     public:
  67.     objloader();   
  68.     ~objloader();   //free the textures and lists
  69.     int load(const char* filename); //the main model load function
  70. };
  71.  
  72. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement