Guest User

Untitled

a guest
Jun 22nd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. pair<GLuint, int> Utils::Loader::loadOBJ(char* filename) {
  2.     stringstream ss;
  3.     ss << "res/data/" << filename << ".obj";
  4.  
  5.     ifstream file(ss.str().c_str(), ios::in | ios::binary);
  6.     if (!file) {
  7.         return pair<GLuint, int>(0, 0);
  8.     }
  9.  
  10.     bool hitVertices = false;
  11.     int verticesNum = 0;
  12.  
  13.     char temp = file.peek();
  14.     while (temp != 'v' && temp != -1) {
  15.         file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  16.         temp = file.peek();
  17.     }
  18.  
  19.     int startPos = file.tellg();
  20.     cout << startPos << endl;
  21.  
  22.     while (file.get() == 'v') {
  23.         if (file.get() != ' ') break;
  24.         file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  25.         verticesNum++;
  26.     }
  27.  
  28.     float* vertices;
  29.     vertices = new float[verticesNum*3];
  30.     int current = 0;
  31.  
  32.     file.seekg(startPos);
  33.  
  34.     while (file.get() == 'v') {
  35.         if (file.get() != ' ') break;
  36.         stringstream ss;
  37.         char n = 0;
  38.         for (int i = 0; i < 3; i++) {
  39.             n = file.get();
  40.             while (n != ' ' && n != '\n') {
  41.                 ss << n;
  42.                 n = file.get();
  43.             }
  44.             vertices[current++] = atof(ss.str().c_str());
  45.             ss.str("");
  46.         }
  47.     }
  48.  
  49.     file.close();
  50.  
  51.     GLuint vao;
  52.     glGenVertexArrays(1, &vao);
  53.     glBindVertexArray(vao);
  54.  
  55.     GLuint vbo;
  56.     glGenBuffers(1, &vbo);
  57.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  58.  
  59.     glEnableVertexAttribArray(0);
  60.     glBufferData(GL_ARRAY_BUFFER, verticesNum * 3 * sizeof(float), vertices, GL_STATIC_DRAW);
  61.    
  62.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
  63.     glBindVertexArray(0);
  64.    
  65.     delete[] vertices;
  66.     return pair<GLuint, int>(vao, verticesNum);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment