Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1. // Below ifdef required to remove warnings for unsafe version of fopen and fscanf.
  2. // Secure version won't work cross-platform, forcing this small hack.
  3. #ifdef _MSC_VER
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #endif
  6.  
  7. #include "model.h"
  8.  
  9. bool Model::load(char* modelFilename, char* textureFilename)
  10. {
  11.     bool result;
  12.  
  13.     // Load in the model data,
  14.     result = loadModel(modelFilename);
  15.     if (!result)
  16.     {
  17.         MessageBox(NULL, "Model failed to load", "Error", MB_OK);
  18.         return false;
  19.     }
  20.  
  21.     // Load the texture for this model.
  22.     loadTexture(textureFilename);
  23.  
  24.     return true;
  25. }
  26.  
  27. void Model::render()
  28. {
  29.     // TODO: Need to add code here to render the loaded model
  30.     // How this is done is based on how you stored and sorted the data
  31.  
  32.     glEnableClientState(GL_VERTEX_ARRAY);
  33.     glEnableClientState(GL_NORMAL_ARRAY);
  34.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  35.  
  36.     glVertexPointer(3, GL_FLOAT, 0, vertex.data());
  37.     glNormalPointer(GL_FLOAT, 0, normals.data());
  38.     glTexCoordPointer(2, GL_FLOAT, 0, texCoords.data());
  39.  
  40.     glDrawArrays(GL_TRIANGLES, 0, m_vertexCount);
  41.  
  42.     glDisableClientState(GL_VERTEX_ARRAY);
  43.     glDisableClientState(GL_NORMAL_ARRAY);
  44.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  45. }
  46.  
  47.  
  48. // Modified from a mulit-threaded version by Mark Ropper.
  49. bool Model::loadModel(char* filename)
  50. {
  51.     vector<Vector3> verts;
  52.     vector<Vector3> norms;
  53.     vector<Vector3> texCs;
  54.     vector<unsigned int> faces;
  55.  
  56.     FILE* file = fopen(filename, "r");
  57.     if (file == NULL)
  58.     {
  59.         return false;
  60.     }
  61.     while (true)
  62.     {
  63.         char lineHeader[128];
  64.  
  65.         // Read first word of the line
  66.         int res = fscanf(file, "%s", lineHeader);
  67.         if (res == EOF)
  68.         {
  69.             break; // exit loop
  70.         }
  71.         else // Parse
  72.         {
  73.             if (strcmp(lineHeader, "v") == 0) // Vertex
  74.             {
  75.                 Vector3 vertex;
  76.                 fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
  77.                 verts.push_back(vertex);
  78.             }
  79.             else if (strcmp(lineHeader, "vt") == 0) // Tex Coord
  80.             {
  81.                 Vector3 uv;
  82.                 fscanf(file, "%f %f\n", &uv.x, &uv.y);
  83.                 texCs.push_back(uv);
  84.             }
  85.             else if (strcmp(lineHeader, "vn") == 0) // Normal
  86.             {
  87.                 Vector3 normal;
  88.                 fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
  89.                 norms.push_back(normal);
  90.             }
  91.             else if (strcmp(lineHeader, "f") == 0) // Face
  92.             {
  93.                 unsigned int face[9];
  94.                 int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &face[0], &face[1], &face[2],
  95.                     &face[3], &face[4], &face[5],
  96.                     &face[6], &face[7], &face[8]);
  97.                 if (matches != 9)
  98.                 {
  99.                     // Parser error, or not triangle faces
  100.                     return false;
  101.                 }
  102.  
  103.                 for (int i = 0; i < 9; i++)
  104.                 {
  105.                     faces.push_back(face[i]);
  106.                 }
  107.             }
  108.         }
  109.     }
  110.  
  111.     // "Unroll" the loaded obj information into a list of triangles.
  112.     // TODO: By this point all model has been read from the file, but is not in the correct order.
  113.     // You NEED to loop over all the data and sort it into a render ready order/format.
  114.  
  115.     for (int i = 0; i < faces.size(); i += 3)
  116.     {
  117.         vertex.push_back(verts[faces[i] - 1].x);
  118.         vertex.push_back(verts[faces[i] - 1].y);
  119.         vertex.push_back(verts[faces[i] - 1].z);
  120.  
  121.         texCoords.push_back(texCs[faces[i+1] - 1].x);
  122.         texCoords.push_back(texCs[faces[i+1] - 1].y);
  123.  
  124.         normals.push_back(norms[faces[i + 2] - 1].x);
  125.         normals.push_back(norms[faces[i + 2] - 1].y);
  126.         normals.push_back(norms[faces[i + 2] - 1].z);
  127.  
  128.         m_vertexCount += 1;
  129.     }
  130.  
  131.  
  132.     // Once data has been sorted clear read data (which has been copied and are no longer needed).
  133.     verts.clear();
  134.     norms.clear();
  135.     texCs.clear();
  136.     faces.clear();
  137.  
  138.     return true;
  139. }
  140.  
  141. void Model::loadTexture(char* filename)
  142. {
  143.     texture = SOIL_load_OGL_texture
  144.     (
  145.         filename,
  146.         SOIL_LOAD_AUTO,
  147.         SOIL_CREATE_NEW_ID,
  148.         SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_INVERT_Y // Depending on texture file type some need inverted others don't.
  149.     );
  150.  
  151.     //check for an error during the load process
  152.     if (texture == 0)
  153.     {
  154.         printf("SOIL loading error: '%s'\n", SOIL_last_result());
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement