Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. {
  2.     tinyobj::attrib_t attrib;
  3.     std::vector<tinyobj::shape_t> shapes;
  4.     std::vector<tinyobj::material_t> materials;
  5.  
  6.     std::string err;
  7.     std::vector<float> vertices;
  8.  
  9.     tinyobj::LoadObj(&attrib, &shapes, &materials, &err, a_strFileName.c_str());
  10.  
  11.     printf("# of vertices  = %d\n", (int)(attrib.vertices.size()) / 3);
  12.     printf("# of normals   = %d\n", (int)(attrib.normals.size()) / 3);
  13.     printf("# of texcoords = %d\n", (int)(attrib.texcoords.size()) / 2);
  14.     printf("# of materials = %d\n", (int)materials.size());
  15.     printf("# of shapes    = %d\n", (int)shapes.size());
  16.  
  17.  
  18.  
  19.    
  20.     for (size_t s = 0; s < shapes.size(); s++) {
  21.         std::vector<float> buffer;    // pos(3float), normal(3float), color(3float)
  22.  
  23.  
  24.         for(size_t f = 0 ; f < shapes[s].mesh.indices.size() / 3 ; f++) {
  25.             tinyobj::index_t idx0 = shapes[s].mesh.indices[3 * f + 0];
  26.             tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
  27.             tinyobj::index_t idx2 = shapes[s].mesh.indices[3 * f + 2];
  28.  
  29.             float tc[3][2];
  30.             if (attrib.texcoords.size() > 0) {
  31.                 if ((idx0.texcoord_index < 0) || (idx1.texcoord_index < 0) ||
  32.                     (idx2.texcoord_index < 0)) {
  33.                     // face does not contain valid uv index.
  34.                     tc[0][0] = 0.0f;
  35.                     tc[0][1] = 0.0f;
  36.                     tc[1][0] = 0.0f;
  37.                     tc[1][1] = 0.0f;
  38.                     tc[2][0] = 0.0f;
  39.                     tc[2][1] = 0.0f;
  40.                 }
  41.                 else {
  42.                     assert(attrib.texcoords.size() >
  43.                            size_t(2 * idx0.texcoord_index + 1));
  44.                     assert(attrib.texcoords.size() >
  45.                            size_t(2 * idx1.texcoord_index + 1));
  46.                     assert(attrib.texcoords.size() >
  47.                            size_t(2 * idx2.texcoord_index + 1));
  48.  
  49.                     // Flip Y coord.
  50.                     tc[0][0] = attrib.texcoords[2 * idx0.texcoord_index];
  51.                     tc[0][1] = 1.0f - attrib.texcoords[2 * idx0.texcoord_index + 1];
  52.                     tc[1][0] = attrib.texcoords[2 * idx1.texcoord_index];
  53.                     tc[1][1] = 1.0f - attrib.texcoords[2 * idx1.texcoord_index + 1];
  54.                     tc[2][0] = attrib.texcoords[2 * idx2.texcoord_index];
  55.                     tc[2][1] = 1.0f - attrib.texcoords[2 * idx2.texcoord_index + 1];
  56.                 }
  57.             }
  58.             else {
  59.                 tc[0][0] = 0.0f;
  60.                 tc[0][1] = 0.0f;
  61.                 tc[1][0] = 0.0f;
  62.                 tc[1][1] = 0.0f;
  63.                 tc[2][0] = 0.0f;
  64.                 tc[2][1] = 0.0f;
  65.             }
  66.  
  67.             float v[3][3];
  68.             for (int k = 0; k < 3; k++) {
  69.                 int f0 = idx0.vertex_index;
  70.                 int f1 = idx1.vertex_index;
  71.                 int f2 = idx2.vertex_index;
  72.                 assert(f0 >= 0);
  73.                 assert(f1 >= 0);
  74.                 assert(f2 >= 0);
  75.  
  76.                 v[0][k] = attrib.vertices[3 * f0 + k];
  77.                 v[1][k] = attrib.vertices[3 * f1 + k];
  78.                 v[2][k] = attrib.vertices[3 * f2 + k];
  79.             }
  80.  
  81.  
  82.             for (int k = 0; k < 3; k++) {
  83.                 buffer.push_back(v[k][0]);
  84.                 buffer.push_back(v[k][1]);
  85.                 buffer.push_back(v[k][2]);
  86.  
  87.                 buffer.push_back(tc[k][0]);
  88.                 buffer.push_back(tc[k][1]);
  89.  
  90.             }
  91.  
  92.  
  93.         }
  94.         m_Vertices.insert(m_Vertices.end(), buffer.begin(), buffer.end());
  95.  
  96.     }
  97.     m_iNumVerts = m_Vertices.size() / (3 + 2);
  98.  
  99.     FileSystem* FH = new FileSystem();
  100.  
  101.     int width, height;
  102.  
  103.     char *image = 0;
  104.     int imageNumber = Rand(5) + 1;
  105.     image = FH->Load((char*)"../Resources/Textures/raptor.png", &width, &height);
  106.  
  107.     glGenTextures(1, &m_Texture);
  108.     glBindTexture(GL_TEXTURE_2D, m_Texture);
  109.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  110.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  111.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  112.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  113.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
  114.     glBindTexture(GL_TEXTURE_2D, 0);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement