Advertisement
Guest User

Untitled

a guest
Jun 13th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. #include "utils.h"
  2. #include <GL/glew.h>
  3. #include <GL/gl.h>
  4. #include <stdio.h>
  5. #include <cstring>
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9. #include <vec2.h>
  10. #include <vec3.h>
  11. #include "Mesh.h"
  12.  
  13. using namespace std;
  14.  
  15.  
  16.  
  17. GLuint loadShadingProgram(const char* vs_path, const char* fs_path) {
  18.     // Load the vertex shader:
  19.     GLuint vs_id = loadShader(vs_path, GL_VERTEX_SHADER);
  20.     // Load the fragment shader.
  21.     GLuint fs_id = loadShader(fs_path, GL_FRAGMENT_SHADER);
  22.  
  23.     // Create and link the program:
  24.     GLuint program_id = glCreateProgram();
  25.     glAttachShader(program_id, vs_id);
  26.     glAttachShader(program_id, fs_id);
  27.     glLinkProgram(program_id);
  28.  
  29.     return program_id;
  30. }
  31.  
  32.  
  33. GLuint loadShader(const char* path, GLenum shader_type){
  34.     string contents;
  35.     ifstream file_stream(path);
  36.     if(file_stream.is_open()){
  37.  
  38.         string line;
  39.         while(getline(file_stream, line)){
  40.             contents += line + "\n";
  41.         }
  42.         file_stream.close();
  43.     }
  44.  
  45.     GLuint shader_id;
  46.     shader_id = glCreateShader(shader_type);
  47.     const char* shader_source = contents.c_str();
  48.     cout << shader_source;
  49.  
  50.     glShaderSource(shader_id, 1, &shader_source, NULL );
  51.     glCompileShader(shader_id);
  52.  
  53.     int success;
  54.     char info_log[512];
  55.     glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
  56.     if(!success) {
  57.         glGetShaderInfoLog(shader_id, 512, NULL, info_log);
  58.         cout << "\nERROR COMPILING SHADER: \n" << info_log << endl;
  59.     }
  60.     return shader_id;
  61. }
  62.  
  63.  
  64. Mesh* loadMesh(const char* path) {
  65.     printf("Loading OBJ file %s...\n", path);
  66.  
  67.     vector<unsigned int> vertexIndices, uvIndices, normalIndices;
  68.     vector<vec3> temp_vertices;
  69.     vector<vec2> temp_uvs;
  70.     vector<vec3> temp_normals;
  71.  
  72.     FILE * file = fopen(path, "r");
  73.     if( file == NULL ){
  74.         printf("Impossible to open the file !\n");
  75.         return NULL;
  76.     }
  77.  
  78.     while( 1 ){
  79.  
  80.         char lineHeader[128];
  81.         // read the first word of the line
  82.         int res = fscanf(file, "%s", lineHeader);
  83.         if (res == EOF)
  84.             break; // EOF = End Of File. Quit the loop.
  85.  
  86.         // else : parse lineHeader
  87.  
  88.         if ( strcmp( lineHeader, "v" ) == 0 ){
  89.             vec3 vertex;
  90.             fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
  91.             temp_vertices.push_back(vertex);
  92.         }
  93.         else if ( strcmp( lineHeader, "vt" ) == 0 ){
  94.             vec2 uv;
  95.             fscanf(file, "%f %f\n", &uv.x, &uv.y );
  96.             uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
  97.             temp_uvs.push_back(uv);
  98.         }
  99.         else if ( strcmp( lineHeader, "vn" ) == 0 ){
  100.             vec3 normal;
  101.             fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
  102.             temp_normals.push_back(normal);
  103.         }
  104.         else if ( strcmp( lineHeader, "f" ) == 0 ){
  105.             string vertex1, vertex2, vertex3;
  106.             unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
  107.             int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2] );
  108.             if (matches != 9){
  109.                 printf("File can't be read by our simple parser :-( Try exporting with other options\n");
  110.                 fclose(file);
  111.                 return NULL;
  112.             }
  113.             vertexIndices.push_back(vertexIndex[0]);
  114.             vertexIndices.push_back(vertexIndex[1]);
  115.             vertexIndices.push_back(vertexIndex[2]);
  116.             uvIndices    .push_back(uvIndex[0]);
  117.             uvIndices    .push_back(uvIndex[1]);
  118.             uvIndices    .push_back(uvIndex[2]);
  119.             normalIndices.push_back(normalIndex[0]);
  120.             normalIndices.push_back(normalIndex[1]);
  121.             normalIndices.push_back(normalIndex[2]);
  122.         }
  123.         else{
  124.             // Probably a comment, eat up the rest of the line
  125.             char stupidBuffer[1000];
  126.             fgets(stupidBuffer, 1000, file);
  127.         }
  128.  
  129.     }
  130.  
  131.     vector<float> data;
  132.  
  133.  
  134.     cout << endl << "vertexIndices.size:" << vertexIndices.size();
  135.  
  136.     // For each vertex of each triangle
  137.     for( unsigned int i=0; i<vertexIndices.size(); i++ ){
  138.  
  139.         // Get the indices of its attributes
  140.         unsigned int vertexIndex = vertexIndices[i];
  141.         unsigned int uvIndex = uvIndices[i];
  142.         unsigned int normalIndex = normalIndices[i];
  143.  
  144.         // Get the attributes thanks to the index
  145.         vec3 vertex = temp_vertices[ vertexIndex-1 ];
  146.         vec2 uv = temp_uvs[ uvIndex-1 ];
  147.         vec3 normal = temp_normals[ normalIndex-1 ];
  148.  
  149.         // Put the attributes in buffers
  150.         data.push_back(vertex.x);
  151.         data.push_back(vertex.y);
  152.         data.push_back(vertex.z);
  153.  
  154.         data.push_back(uv.x);
  155.         data.push_back(uv.y);
  156.  
  157.         data.push_back(normal.x);
  158.         data.push_back(normal.y);
  159.         data.push_back(normal.z);
  160.  
  161.  
  162.  
  163.     }
  164.     fclose(file);
  165.  
  166.     Mesh* mesh = new Mesh(data);
  167.     return mesh;
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement