Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pair<GLuint, int> Utils::Loader::loadOBJ(char* filename) {
- stringstream ss;
- ss << "res/data/" << filename << ".obj";
- ifstream file(ss.str().c_str(), ios::in | ios::binary);
- if (!file) {
- return pair<GLuint, int>(0, 0);
- }
- bool hitVertices = false;
- int verticesNum = 0;
- char temp = file.peek();
- while (temp != 'v' && temp != -1) {
- file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- temp = file.peek();
- }
- int startPos = file.tellg();
- cout << startPos << endl;
- while (file.get() == 'v') {
- if (file.get() != ' ') break;
- file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- verticesNum++;
- }
- float* vertices;
- vertices = new float[verticesNum*3];
- int current = 0;
- file.seekg(startPos);
- while (file.get() == 'v') {
- if (file.get() != ' ') break;
- stringstream ss;
- char n = 0;
- for (int i = 0; i < 3; i++) {
- n = file.get();
- while (n != ' ' && n != '\n') {
- ss << n;
- n = file.get();
- }
- vertices[current++] = atof(ss.str().c_str());
- ss.str("");
- }
- }
- file.close();
- GLuint vao;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- GLuint vbo;
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glEnableVertexAttribArray(0);
- glBufferData(GL_ARRAY_BUFFER, verticesNum * 3 * sizeof(float), vertices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
- glBindVertexArray(0);
- delete[] vertices;
- return pair<GLuint, int>(vao, verticesNum);
- }
Advertisement
Add Comment
Please, Sign In to add comment