Advertisement
matejnevlud

load obj rasterizer

Mar 11th, 2021
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. void Rasterizer::LoadScene(string file_name) {
  2.  
  3.  
  4.     vector<GLfloat> vertex_data = {};
  5.  
  6.     vector<Surface *> surfaces;
  7.     vector<Material *> materials;
  8.  
  9.     LoadOBJ(file_name.c_str(), surfaces, materials, false, {0, 0, 0});
  10.  
  11.     for (int i = 0; i < surfaces.size(); i++) {
  12.         Surface * surface = surfaces.at(i);
  13.  
  14.         int no_triangles = surface->no_triangles();
  15.         Triangle * triangles = surface->get_triangles();
  16.  
  17.         for (int j = 0; j < no_triangles; j++) {
  18.             Triangle  triangle = triangles[j];
  19.  
  20.             for (int k = 0; k < 3; k++) {
  21.                 Vertex vertex = triangle.vertex(k);
  22.  
  23.                 // Push position data
  24.                 vertex_data.push_back(vertex.position.x);
  25.                 vertex_data.push_back(vertex.position.y);
  26.                 vertex_data.push_back(vertex.position.z);
  27.  
  28.                 // Push texture coords
  29.                 //vertex_data.push_back(vertex.texture_coords->u);
  30.                 //vertex_data.push_back(vertex.texture_coords->v);
  31.             }
  32.         }
  33.     }
  34.  
  35.     GLfloat vertices[vertex_data.size()];
  36.     for (int i = 0; i < vertex_data.size(); i++)
  37.         vertices[i] = vertex_data.at(i);
  38.  
  39.  
  40.     printf("Loaded %d verticies & uv`s\n", vertex_data.size() / 3);
  41.  
  42.     no_of_verticies = vertex_data.size() / 3;
  43.     const int vertex_stride = 0;
  44.    
  45.  
  46.  
  47.     // optional index array
  48.     unsigned int indices[] = {};
  49.    
  50.  
  51.    
  52.     glGenVertexArrays( 1, &vao );
  53.     glBindVertexArray( vao );
  54.    
  55.  
  56.     glGenBuffers( 1, &vbo ); // generate vertex buffer object (one of OpenGL objects) and get the unique ID corresponding to that buffer
  57.     glBindBuffer( GL_ARRAY_BUFFER, vbo ); // bind the newly created buffer to the GL_ARRAY_BUFFER target
  58.     glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW ); // copies the previously defined vertex data into the buffer's memory
  59.     // vertex position
  60.     glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0 );
  61.     glEnableVertexAttribArray( 0 );
  62.     // vertex texture coordinates
  63.     //glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 0, ( void* )( sizeof( float ) * 3 ) );
  64.     //glEnableVertexAttribArray( 1 );
  65.    
  66.  
  67.  
  68.     //glGenBuffers( 1, &ebo );
  69.     //glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ebo );
  70.     //glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( indices ), indices, GL_STATIC_DRAW );
  71.  
  72.  
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement