Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 12th, 2012  |  syntax: C++  |  size: 22.01 KB  |  views: 55  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //////////////////////////////
  2. //Main Libary
  3. /////////////////////////////
  4. #define NO_SDL_GLEXT
  5. #include <glew.h>
  6. #include "SDL/SDL.H"
  7. #include  "SDL/SDL_IMAGE.H"
  8. #include "SDL/SDL_OPENGL.H"
  9. #undef main
  10. #include <iostream>
  11. #include <cassert>
  12. #include <string>
  13. #include <algorithm>
  14. #include <boost/iostreams/stream.hpp>
  15. #include <libs/iostreams/example/container_device.hpp>
  16. #include <boost/filesystem.hpp>
  17. #include <boost/iostreams/categories.hpp>
  18. #include <boost/algorithm/string.hpp>
  19. #include <vector>
  20. #include <fstream>
  21. #include "GL/glu.h"
  22. #include <glm/glm.hpp>
  23. #include <glm/gtc/type_ptr.hpp>
  24. #include <glm/gtc/matrix_transform.hpp>
  25. /////////////////////////////
  26. using namespace std;
  27. using namespace boost::filesystem;
  28. namespace io = boost::iostreams;
  29. namespace ex = boost::iostreams::example;
  30. ////////////////////////////
  31.  
  32. //for holding the string
  33. typedef ex::container_source<string> string_source;
  34. typedef std::vector< std::string > string_list;
  35.  
  36. struct Point {float x,  y, z;};
  37. std::vector<unsigned int>faces;
  38. std::vector<Point>points;
  39. //Turn on FileReader
  40. bool ModelRender = false;
  41.  
  42. float
  43. leftandright = 0.0,
  44.              upanddown = 0.0,
  45.              forwardandbackward = 0.0;
  46. GLuint vertexbuffer;
  47. float minusyaxis = -sin(0);
  48. float yaxis = sin(0);
  49.  
  50.  
  51. ///////////////////////////
  52. //DEFINES
  53. ///////////////////////////
  54. #define SCREEN_WIDTH  1240
  55. #define SCREEN_HEIGHT 960
  56. #define BPP 32
  57. #define GL_GLEXT_PROTOTYPES
  58. #define printOpenGLError() printOglError(__FILE__, __LINE__)
  59.  
  60. int printOglError(char *file, int line)
  61. {
  62.  
  63.   GLenum glErr;
  64.   int    retCode = 0;
  65.  
  66.   glErr = glGetError();
  67.   if (glErr != GL_NO_ERROR)
  68.   {
  69.     printf("glError in file %s @ line %d: %s\n",
  70.         file, line, gluErrorString(glErr));
  71.     retCode = 1;
  72.   }
  73.   return retCode;
  74. }
  75.  
  76. ///////////////////////////
  77.  
  78. ////////////////////////////
  79. //Other Header Files
  80. ///////////////////////////
  81. #include "SurfaceStorage.h"
  82. #include "IndexAssignerFunction.h"
  83. //////////////////////////
  84.  
  85. ////////////////////////
  86. //Global Variables
  87. ////////////////////////
  88.  
  89. /////////////////////////
  90.  
  91. bool MoveObjVertex = false;
  92. bool modelloader = true;
  93.  
  94.  
  95.  
  96. float* point=reinterpret_cast<float*>(&points[0]);
  97. int num_bytes = points.size() * sizeof(points[0]);
  98. GLuint matrixuniform;
  99.  
  100. GLuint vao[2];
  101.  
  102. GLuint translation;
  103. GLuint Rotation;
  104. GLuint xvalue = 600.0f;
  105. GLuint objvbo;
  106. GLuint objtextureID;
  107. GLuint ModelTexture;
  108.  
  109.  
  110.  
  111. /////////////////
  112. //EVENT CREATION
  113. ////////////////
  114.  
  115. //SDL Event
  116. SDL_Event event;
  117.  
  118. int counter = 0;
  119. int indexID;
  120.  
  121. float xm;
  122. float ym;
  123.  
  124. int length;
  125. char * buffer;
  126.  
  127.  
  128. static GLfloat TranslationMatrix[] = {
  129.  
  130.   1.0, 0.0, 0.0, 0.0,
  131.   0.0, 1.0, 0.0, 0.0,
  132.   0.0, 0.0, 1.0, 0.0,
  133.   0.0, 0.0, 0.0, 1.0
  134.  
  135.  
  136. };
  137.  
  138.        //Translation Process
  139.   GLfloat RotationMatrix[] = {
  140.         1.0, 0.0, 0.0, 0.0,
  141.         0.0, 1.0, 0.0, 0.0,
  142.         0.0, 0.0, 1.0, 0.0,
  143.         0.0, 0.0, 0.0, 1.0
  144.  };
  145.  
  146.  
  147. //Obj Graphic
  148. static const GLfloat ObjGraphicVertices[] = {
  149.   //x      y        z
  150.   200.0f,  720.0f,   0.0f,   1.0f,  1.0f,   0.0f,
  151.   320.0f,  720.0f,   0.0f,   1.0f,  0.0f,   0.0f,
  152.   320.0f,  640.0f,   0.0f,   1.0f,  0.0f,   1.0f,
  153.   200.0f,  640.0f,   0.0f,   1.0f,  1.0f,   1.0f  
  154.  
  155. };
  156.  
  157.  
  158.  
  159. ////////////////////////
  160.  
  161. ///////////////////////
  162. //Structure Rectangle
  163. //////////////////////
  164.  
  165. struct rectangle
  166. {
  167.  
  168.   GLuint textureID;
  169.   GLuint vbo;
  170.   GLfloat matrix[16];
  171.  
  172. };
  173.  
  174. //////////////////////
  175. //Rectangle Variables
  176. /////////////////////
  177.  
  178. //////////////////////
  179. rectangle menubar;
  180. rectangle MainToolBox;
  181. rectangle BoxStorage;
  182. rectangle MenuContext;
  183. rectangle boxstorage;
  184. rectangle ObjGraphic;
  185. //////////////////////
  186.  
  187. glm::mat4 ortho;
  188.  
  189. ///////////////////////////////////
  190. //Prototypes
  191. //////////////////////////////////
  192. void create_rectangle(const char * const path, rectangle *r, const GLfloat *vertices, size_t byte_count, const GLfloat * transformation);
  193. void draw_rectangle(rectangle *r, const float *TranslationMatrix);
  194. void render_rectangle(void);
  195. void setup_sdl_glew (void);
  196. GLuint LoadProgram(const char * const vertex_file_path, const char * Fragment_file_path);
  197. int load_texture(const char * const path);
  198. void draw (void);
  199. /////////////////////////////////
  200.  
  201. //////////////////////////
  202. //Setup Vbo Function
  203. /////////////////////////
  204. void setup_vbos(void)
  205. {
  206.  
  207.   ///////////////////
  208.   //Vertex Cordinates
  209.   ///////////////////  
  210.  
  211.   ////////////////////////////
  212.   //Graphical User Interface
  213.   ///////////////////////////
  214.  
  215.   //1240 by 960
  216.  
  217.  
  218.   //Start GUI Cordinates x y z w - NEEDS TEX CORDS
  219.   static const GLfloat GUIVertices[] = {
  220.  
  221.     //ToolBar
  222.     1240.0f, 960.0f,  0.0f, 1.0f, 1.0f, 0.0f,
  223.     -1.0f,   960.0f,  0.0f, 1.0f, 0.0f, 0.0f,
  224.     -1.0f,   930.0f,  0.0f, 1.0f, 0.0f, 1.0f,
  225.     1240.0f, 930.0f,  0.0f, 1.0f, 1.0f, 1.0f
  226.  
  227.   };
  228.  
  229.   static const GLfloat MainToolBoxVertices[] = {
  230.  
  231.     //Main ToolBox
  232.     // x     y       z   w       X    Y
  233.     1240.0f, 120.0f,   0.0f, 1.0f, 1.0f,  0.0f,
  234.     -1.0f,   120.0f,   0.0f, 1.0f, 0.0f,  0.0f,
  235.     -1.0f,    0.0f,    0.0f, 1.0f, 0.0f,  1.0f,
  236.     1240.0f,  0.0f,    0.0f, 1.0f, 1.0f,  1.0f  
  237.  
  238.   };
  239.  
  240.   //Menu Context
  241.   static const GLfloat MenuContextVertices[] = {
  242.  
  243.     // x     y           z     w       X    Y
  244.     -0.76f,   0.94f,   0.0f, 1.0f,   1.0f, 0.0f,
  245.     -1.10f, 0.94f,   0.0f,   1.0f,   0.0f, 0.0f,
  246.     -1.10f, 0.60f,   0.0f,   1.0f,   0.0f, 1.0f,
  247.     -0.76f,   0.60f,   0.0f, 1.0f,   1.0f, 1.0f  
  248.  
  249.   };
  250.  
  251.   //Box Storage
  252.   static const GLfloat BoxStorageVertices[] = {
  253.  
  254.     1112.0f, 860.0f,   0.0f, 1.0f,        1.0f,  0.0f,
  255.     120.0f,   860.0f,   0.0f,   1.0f,     0.0f, 0.0f,
  256.     120.0f,   200.0f,   0.0f,   1.0f,    0.0f, 1.0f,
  257.     1112.0f, 200.0f,   0.0f, 1.0f,       1.0f,  1.0f  
  258.  
  259.   };
  260.  
  261.   ////////////////////////////////
  262.   //Generate Vertex Array Object
  263.   /////////////////////////////////
  264.  
  265.   glGenVertexArrays(1, &vao[0]);
  266.  
  267.   create_rectangle("GUI/FileMenu.tga", &menubar, GUIVertices, sizeof(GUIVertices), NULL);
  268.   create_rectangle("GUI/MainToolBox.tga", &MainToolBox, MainToolBoxVertices, sizeof(MainToolBoxVertices), NULL);
  269.   create_rectangle("GUI/MenuContext.tga", &MenuContext, MenuContextVertices, sizeof(MenuContextVertices), NULL);
  270.   create_rectangle("GUI/BoxStorage.tga", &boxstorage, BoxStorageVertices, sizeof(BoxStorageVertices), NULL);
  271.   create_rectangle("GUI/ObjGraphic.tga",  &ObjGraphic, ObjGraphicVertices,  sizeof(ObjGraphicVertices),  NULL);
  272.  
  273. }
  274.  
  275. //LOAD SHADER FUNCTION
  276. GLuint LoadProgram(const char * const vertex_file_path, const char * Fragment_file_path){
  277.  
  278.   GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  279.   GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  280.  
  281.   //READ THE VERTEX SHADER CODE
  282.   string VertexShaderCode;
  283.   ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  284.   if(VertexShaderStream.is_open())
  285.   {
  286.     string Line = "";
  287.     while(getline(VertexShaderStream, Line))
  288.       VertexShaderCode += "\n" + Line;
  289.     VertexShaderStream.close();  
  290.   }
  291.  
  292.   //READ THE FRAGMENT SHADER CODE
  293.   string FragmentShaderCode;
  294.   ifstream FragmentShaderStream(Fragment_file_path, std::ios::in);
  295.   if(FragmentShaderStream.is_open())
  296.   {
  297.  
  298.     string Line = "";
  299.     while(getline(FragmentShaderStream, Line))
  300.       FragmentShaderCode += "\n" + Line;
  301.     FragmentShaderStream.close();
  302.  
  303.   }
  304.  
  305.   GLint Result = GL_FALSE;
  306.   int InfoLogLength;
  307.  
  308.   //Compile Vertex Shader
  309.   printf("Compiling Shader : %s\n", vertex_file_path);
  310.   char const * VertexSourcePointer = VertexShaderCode.c_str();
  311.   glShaderSource(VertexShaderID,  1,  &VertexSourcePointer,  NULL);
  312.   glCompileShader(VertexShaderID);
  313.  
  314.   //Check Vertex Shader
  315.   glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS,  &Result);
  316.   glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  317.   vector<char> VertexShaderErrorMessage(InfoLogLength);
  318.   glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  319.   fprintf(stdout, "%s\n",  &VertexShaderErrorMessage[0]);
  320.  
  321.   //Compile Fragment Shader
  322.   printf("Compiling Shader : %s\n",  Fragment_file_path);
  323.   char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  324.   glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
  325.   glCompileShader(FragmentShaderID);
  326.  
  327.   //Check Fragment Shader
  328.   glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  329.   glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  330.   vector<char> FragmentShaderErrorMessage(InfoLogLength);
  331.   glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  332.   fprintf(stdout,  "%s\n",  &FragmentShaderErrorMessage[0]);
  333.  
  334.   fprintf(stdout,  "Linking Program\n");
  335.   GLuint ProgramID = glCreateProgram();
  336.  
  337.   //Bind Attribute
  338.   glBindAttribLocation(ProgramID, 0, "position");
  339.   glBindAttribLocation(ProgramID, 1, "Texcoord0");
  340.  
  341.   //Link The Program
  342.   glAttachShader(ProgramID, VertexShaderID);
  343.   glAttachShader(ProgramID, FragmentShaderID);
  344.   glLinkProgram(ProgramID);
  345.  
  346.   GLuint texture1;
  347.  
  348.   texture1 = glGetUniformLocation(ProgramID, "myTextureSampler");
  349.   matrixuniform =  glGetUniformLocation(ProgramID, "myMatrix");
  350.   translation = glGetUniformLocation(ProgramID, "TranslationMatrix");
  351.    Rotation = glGetUniformLocation(ProgramID, "RotationMatrix");
  352.  
  353.  
  354.   //Check The Program
  355.   glGetProgramiv(ProgramID,  GL_LINK_STATUS, &Result);
  356.   glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  357.   vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
  358.   fprintf(stdout,  "%s\n", &ProgramErrorMessage[0]);
  359.  
  360.   //Delete Shader
  361.   glDeleteShader(VertexShaderID);
  362.   glDeleteShader(FragmentShaderID);
  363.  
  364.   glUseProgram(ProgramID);
  365.  
  366.   //Return ProgramID
  367.   return ProgramID;
  368. }
  369.  
  370. /////////////////////////////////
  371. //Create Rectangle Function
  372. /////////////////////////////////
  373. void create_rectangle(const char * const path, rectangle *r, const GLfloat *vertices, size_t byte_count, const GLfloat * transformation)
  374. {
  375.  
  376.   glGenBuffers(1, &r->vbo);
  377.   glBindBuffer(GL_ARRAY_BUFFER, r->vbo);
  378.   glBufferData(GL_ARRAY_BUFFER, byte_count, vertices, GL_DYNAMIC_DRAW);
  379.   r->textureID = load_texture(path);
  380.  
  381. }
  382.  
  383. void draw_rectangle(rectangle *r, const float *TranslationMatrix,  const float *RotationMatrix)
  384. {
  385.  
  386.   /////////////////////////////////////////////////////
  387.   //Object Graphic <Vertex Attributes/Bindings/Drawing>
  388.   /////////////////////////////////////////////////////
  389.  
  390.   //Bind array buffer to Vertex Buffer Object
  391.   glBindBuffer(GL_ARRAY_BUFFER, r->vbo);
  392.  
  393.   // "Bind" the newly created texture : all future texture functions will modify this texture
  394.   glBindTexture(GL_TEXTURE_2D, r->textureID);
  395.  
  396.   //MenuContext Vertices cordinates
  397.   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),reinterpret_cast<const GLvoid *>(0 * sizeof(float)));
  398.  
  399.   //MenuContext Texture cordinates
  400.   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(float),reinterpret_cast<const GLvoid *>(4 * sizeof(float)));
  401.  
  402.   //Enable Attribute for "index 1"
  403.   glEnableVertexAttribArray(0);
  404.  
  405.   //Enable Attribute for "index 1"
  406.   glEnableVertexAttribArray(1);
  407.  
  408.   //Get Matrix Values/Matrix
  409.   glUniformMatrix4fv(matrixuniform, 1, TRUE, r->matrix);
  410.  
  411.   glUniformMatrix4fv(matrixuniform, 1, GL_FALSE, glm::value_ptr(ortho));
  412.  
  413.   glUniformMatrix4fv(translation, 1, TRUE, TranslationMatrix);
  414.  
  415.   glUniformMatrix4fv(Rotation, 1, TRUE, RotationMatrix);
  416.  
  417.   glDrawArrays(GL_QUADS, 0, 4);
  418.  
  419.   //END OF MOVE OBJ VERTEX
  420.  
  421. }
  422.  
  423. //Setup SDL/GLEW
  424. void setup_sdl_glew (void)
  425. {
  426.   /////////////////////////////////////////////////////////////////////////////////////////////////
  427.  
  428.   //////////////////
  429.   //SDL SETUP
  430.   /////////////////
  431.  
  432.   //ENABLE SDL
  433.   SDL_Init(SDL_INIT_EVERYTHING);
  434.  
  435.   //SCREEN POSITION
  436.   SDL_putenv("SDL_VIDEO_CENTERED=center");
  437.  
  438.   //SCREEN SETUP
  439.   Screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,BPP,SDL_OPENGL|SDL_DOUBLEBUF);
  440.  
  441.   ortho = glm::ortho(0.0f, (float)SCREEN_WIDTH, 0.0f, (float)SCREEN_HEIGHT);
  442.  
  443.   //////////////////
  444.   //GLEW SETUP
  445.   /////////////////
  446.  
  447.   //GLEW SETUP
  448.   GLenum err = glewInit();
  449.   glGetError();
  450.  
  451.   //GLEW ERROR CHECKING
  452.   if (GLEW_OK != err)
  453.   {
  454.     /* Problem: GlewInit failed, something is seriously wrong. */
  455.     fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  456.  
  457.   }
  458.   fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  459.   /////////////////////////////////////////////////////////////////////////////////////    
  460.  
  461. }
  462.  
  463. ///////////////////////
  464. //ON AND OFF SWITCHES
  465. //////////////////////
  466.  
  467. //ON AND OFF SWITCHES FOR APPLICATION
  468. bool done = false;
  469.  
  470. bool MenuContextToggle = false;
  471.  
  472. bool BoxStorageToggle = false;
  473.  
  474. bool ObjGraphicToggle = false;
  475.  
  476. //Load Texture
  477. int load_texture(const char * const path)
  478. {
  479.  
  480.   GLuint textureID;
  481.  
  482.   SDL_Surface *surface = IMG_Load(path);
  483.  
  484.   // Create one OpenGL textureID
  485.   glGenTextures(1, &textureID);
  486.  
  487.   // "Bind" the newly created textureID : all future textureID functions will modify r textureID
  488.   glBindTexture(GL_TEXTURE_2D, textureID);
  489.  
  490.   // Give the image to OpenGL
  491.   glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, surface->w,surface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, surface->pixels);
  492.  
  493.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  494.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  495.  
  496.   SDL_FreeSurface(surface);
  497.  
  498.   return textureID;
  499. }
  500.  
  501. //Draw Function for arrays <VERTEX POSITION DRAWING>
  502. void draw (void)
  503. {
  504.   draw_rectangle(&menubar, TranslationMatrix, RotationMatrix);
  505.   draw_rectangle(&MainToolBox,  TranslationMatrix, RotationMatrix);
  506.  
  507. }
  508.  
  509. void ModelLoad(const char * path, int Index)
  510. {
  511.  
  512. }
  513.  
  514. //Main Entry Point
  515. int main(int argc , char *argv[])
  516. {
  517.  
  518.   /////////////////
  519.   //SETUP
  520.   ////////////////
  521.  
  522.   //SDL and Glew
  523.   setup_sdl_glew();
  524.  
  525.   /////////////////
  526.  
  527.   objsurface = IMG_Load("GUI/ObjGraphic.tga");
  528.   ModelSurface = IMG_Load("GUI/Textures/model.tga");
  529.  
  530.   //Keyboard Setup
  531.   Uint8 *keystate = SDL_GetKeyState(NULL);
  532.  
  533.   SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL);
  534.  
  535.   //Setup Vertex Buffer Object
  536.   setup_vbos();
  537.  
  538.   /////////////////////////////
  539.   // OpenGL - Viewport Setup
  540.   /////////////////////////////
  541.  
  542.   //Clear Color
  543.   glClearColor(0.0,0.0,1.0,0.0);
  544.  
  545.   //View Port Setup
  546.   glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  547.  
  548.   ////////////////////////////////////////////////////////////////////////////////////////////////////
  549.  
  550.   ////////////////
  551.   //Load Shaders
  552.   ////////////////
  553.  
  554.   //Load Shader Program
  555.   LoadProgram("graphicaluserinterface.v", "graphicaluserinterface.f");
  556.  
  557.   ////////////////////////////////////////////////////////////////////////////////////////////////////
  558.  
  559.   int Mouse_X, Mouse_Y;
  560.  
  561.   glGenTextures(1, &ModelTexture);
  562.  
  563.   glBindTexture(GL_TEXTURE_2D, ModelTexture);
  564.  
  565.   glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA,  ModelSurface->w, ModelSurface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, ModelSurface->pixels);
  566.  
  567.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  568.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  569.  
  570.   glGenBuffers(1, &vertexbuffer);
  571.  
  572.   glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  573.  
  574.   glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(points), &points[0], GL_STATIC_DRAW);
  575.  
  576.  
  577.   //Main While Loop
  578.   while(!done)
  579.   {
  580.     //Get the X and Y Positions on Mouse Cursor and put it into Storage
  581.     SDL_GetMouseState(&Mouse_X,&Mouse_Y);
  582.  
  583.  
  584.     //Clear
  585.     glClear(GL_COLOR_BUFFER_BIT);
  586.  
  587.     //Logic
  588.  
  589.     //draw <Basic GUI>
  590.     draw();
  591.  
  592.     if(BoxStorageToggle == true)
  593.     {
  594.  
  595.  
  596.       draw_rectangle(&boxstorage, TranslationMatrix, RotationMatrix);
  597.  
  598.       //////////////////////////////
  599.       //Directory Reading Proccess
  600.       /////////////////////////////
  601.  
  602.       //File Path
  603.       path p("Box Storage");
  604.  
  605.       //String vector container
  606.       std::vector < std::string > list;
  607.  
  608.       ////////////////////////////
  609.  
  610.       //Iteration Loop
  611.       try
  612.       {  
  613.         for (directory_iterator it(p); it != directory_iterator(); ++it)
  614.         {
  615.  
  616.           //Output it contents to CMD window
  617.           //   cout << *it << endl;
  618.  
  619.           //find the it path names store it in s
  620.           string s = (*it).path().string();
  621.  
  622.           //then push back list
  623.           list.push_back( s );
  624.  
  625.         }
  626.  
  627.       }
  628.  
  629.       catch (const filesystem_error& ex)
  630.       {
  631.         //  cout << ex.what() << endl;
  632.       }
  633.  
  634.       //Float Containers
  635.       float row=0,column=0;
  636.       float quad_width = -146.0f , quad_height = -100.0f;
  637.  
  638.       //iteration Loop
  639.       for(int i=0;i<list.size();++i)
  640.       {
  641.         //if list is not at its full capacity
  642.         if(list[i].find(".obj") != std::string::npos)
  643.         {
  644.           //every 6th quad, change row
  645.           if(i % 6 == 0)
  646.           {
  647.             //beginning of a row
  648.             column = 0;
  649.  
  650.             //row iteration
  651.             ++row;
  652.           }
  653.            //Translation Process
  654.       GLfloat RotationMatrix[] = {
  655.         1.0, 0.0, 0.0, 0.0,
  656.         0.0, 1.0, 0.0, 0.0,
  657.         0.0, 0.0, 1.0, 0.0,
  658.         0.0, 0.0, 0.0, 1.0
  659.       };
  660.  
  661.           //Translation Process
  662.           GLfloat TranslationMatrix[] = {
  663.             1.0, 0.0, 0.0, 720.0+column*quad_width,
  664.             0.0, 1.0, 0.0, 82.0+row*quad_height,
  665.             0.0, 0.0, 1.0, 0.0,
  666.             0.0, 0.0, 0.0, 1.0
  667.           };
  668.  
  669.           //Send Translation Matrix up to the vertex shader
  670.           glUniformMatrix4fv(translation, 1, TRUE, TranslationMatrix);
  671.  
  672.           glUniformMatrix4fv(Rotation, 1, TRUE, RotationMatrix);
  673.           //Draw Rectangle to frustum
  674.           draw_rectangle(&ObjGraphic, TranslationMatrix, RotationMatrix);
  675.         }
  676.  
  677.         //column itearation
  678.         ++column;
  679.  
  680.         //Index Assigners
  681.         if(event.type == SDL_MOUSEBUTTONDOWN && SDL_BUTTON(SDL_BUTTON_LEFT))
  682.         {
  683.           //use Index Assigner
  684.           int index = IndexAssigner(1, 1);
  685.  
  686.           //make a fileobject and store list and the index of that list in a c string
  687.           ifstream file (list[index].c_str() );
  688.  
  689.          int face[4];
  690.  
  691.           //While FileObject not equal end of file
  692.           while (!file.eof() )
  693.           {
  694.  
  695.             char modelbuffer[2000000];
  696.  
  697.             file.getline(modelbuffer, 2000000);
  698.  
  699.             switch(modelbuffer[0])
  700.             {
  701.  
  702.               cout << " " << endl;
  703.  
  704.               case 'v' :
  705.  
  706.               Point p;
  707.  
  708.               sscanf(modelbuffer, "v %f %f %f",  &p.x, &p.y, &p.z);    
  709.  
  710.               points.push_back(p);    
  711.  
  712.               cout << " p.x = " << p.x << " p.y = " << p.y << " p.z = " << p.x << endl;
  713.  
  714.               break;
  715.  
  716.               cout << " " << endl;
  717.  
  718.               case 'f':
  719.  
  720.               int read_count = sscanf(modelbuffer, "f %i %i %i %i", &face[0], &face[1], &face[2],  &face[3]);
  721.  
  722.               cout << "face[0] = " << face[0] << " face[1] = " << face[1] << " face[2] = " << face[2] << " face[3] = " << face[3] << "\n";
  723.  
  724.               if(read_count !=4)
  725.               {
  726.  
  727.                 cout << "bad/n"; throw std::exception();
  728.  
  729.               }
  730.  
  731.               faces.push_back(face[0] - 1);
  732.               faces.push_back(face[1] - 1);
  733.               faces.push_back(face[2] - 1);
  734.               faces.push_back(face[3] - 1);
  735.  
  736.               cout << face[0] - 1 << face[1] - 1 << face[2] - 1 << face[3] - 1 << endl;
  737.  
  738.             }
  739.  
  740.             cout << " " << endl;
  741.  
  742.  
  743.             //Turn on ModelRender
  744.             ModelRender = true;
  745.  
  746.  
  747.           }
  748.  
  749.           for(std::vector<unsigned int>::iterator i = faces.begin(); i != faces.end(); ++i){ cout << *i; } cout << "\n";
  750.  
  751.         }
  752.  
  753.       }
  754.  
  755.     }
  756.  
  757.  
  758.     //If ModelRender is On
  759.     if(ModelRender == true)
  760.     {
  761.      
  762.       glBindTexture(GL_TEXTURE_2D, ModelTexture);
  763.  
  764.       //Enable Attribute for "index 0"
  765.       glEnableVertexAttribArray(0);
  766.  
  767.       //Enable Attribute for "index 1"
  768.       glEnableVertexAttribArray(1);
  769.  
  770.       glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  771.  
  772.       //Model Vertices cordinates
  773.       glVertexAttribPointer(0, 0, GL_INT, GL_FALSE,0,0);
  774.  
  775.       //Model Texture cordinates
  776.       glVertexAttribPointer(1, 0, GL_FLOAT, GL_FALSE,0,0);
  777.  
  778.            //Translation Process
  779.   GLfloat RotationMatrix[] = {
  780.         1.0, 0.0, 0.0, 0.0,
  781.         0.0, 1.0, 0.0, 0.0,
  782.         0.0, 0.0, 1.0, 0.0,
  783.         0.0, 0.0, 0.0, 1.0
  784.  };
  785.  
  786.  
  787.       //Translation Process
  788.       GLfloat TranslationMatrix[] = {
  789.         1.0, 0.0, 0.0, 0.0,
  790.         0.0, 1.0, 0.0, 0.0,
  791.         0.0, 0.0, 1.0, 1.0,
  792.         0.0, 0.0, 0.0, 1.0
  793.       };
  794.  
  795.       //Send Translation Matrix up to the vertex shader
  796.       glUniformMatrix4fv(translation, 1, TRUE, TranslationMatrix);
  797.      
  798.       //Send Rotation Matrix up to the vertex shader
  799.       glUniformMatrix4fv(Rotation, 1, TRUE, RotationMatrix);
  800.  
  801.       glDrawElements( GL_QUADS, faces.size(), GL_UNSIGNED_INT, reinterpret_cast<GLvoid*>(&faces[0]));
  802.  
  803.     }
  804.  
  805.  
  806.  
  807.     //Turn of Menu Context Toggle
  808.     if(keystate[SDLK_SPACE])
  809.     {
  810.  
  811.       MenuContextToggle = false;
  812.  
  813.     }
  814.  
  815.     //Turn on Box Storage
  816.     if(keystate[SDLK_b])
  817.     {
  818.  
  819.       BoxStorageToggle = true;
  820.  
  821.     }
  822.  
  823.     ///////////////////////////////////////////
  824.  
  825.  
  826.     /////////////////////////////////////////////////////////////////
  827.  
  828.     ///////////////////////
  829.     //Menu Context <SETUP>
  830.     //////////////////////
  831.  
  832.     ///////////////////////////////////////////////////////////////
  833.  
  834.     //SDL EVENT QUEUE
  835.     while(SDL_PollEvent(&event))
  836.     {
  837.       switch(event.type)
  838.       {
  839.  
  840.         //if User Presses Quit Button
  841.         case SDL_QUIT:
  842.           return 0;
  843.           break;    
  844.  
  845.           /////////////////////////////////////////////
  846.  
  847.           ////////////////
  848.           //MOUSE EVENTS
  849.           ///////////////
  850.  
  851.         case SDL_MOUSEBUTTONDOWN:
  852.  
  853.           if(Mouse_X <= 15, Mouse_Y <= 30)
  854.           {
  855.  
  856.             MenuContextToggle = true;
  857.  
  858.           }
  859.  
  860.           ///////////////////////////////////////////////
  861.  
  862.       }
  863.  
  864.     }
  865.     //Update
  866.     SDL_GL_SwapBuffers();
  867.   }
  868.  
  869. }
  870. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
clone this paste RAW Paste Data