diff --git a/LoadingModels/ImportedModel.cpp b/LoadingModels/ImportedModel.cpp new file mode 100644 index 0000000..ee6df67 --- /dev/null +++ b/LoadingModels/ImportedModel.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include "ImportedModel.h" +using namespace std; + +ImportedModel::ImportedModel() {} + +ImportedModel::ImportedModel(const char *filePath) { + ModelImporter modelImporter = ModelImporter(); + modelImporter.parseOBJ(filePath); + numVertices = modelImporter.getNumVertices(); + std::vector verts = modelImporter.getVertices(); + std::vector tcs = modelImporter.getTextureCoordinates(); + std::vector normals = modelImporter.getNormals(); + + for (int i = 0; i < numVertices; i++) { + vertices.push_back(glm::vec3(verts[i*3], verts[i*3+1], verts[i*3+2])); + texCoords.push_back(glm::vec2(tcs[i*2], tcs[i*2+1])); + normalVecs.push_back(glm::vec3(normals[i*3], normals[i*3+1], normals[i*3+2])); + } +} + +int ImportedModel::getNumVertices() { return numVertices; } +std::vector ImportedModel::getVertices() { return vertices; } +std::vector ImportedModel::getTextureCoords() { return texCoords; } +std::vector ImportedModel::getNormals() { return normalVecs; } + +// --------------------------------------------------------------- + +ModelImporter::ModelImporter() {} + +void ModelImporter::parseOBJ(const char *filePath) { + float x, y, z; + string content; + ifstream fileStream(filePath, ios::in); + string line = ""; + while (!fileStream.eof()) { + getline(fileStream, line); + if (line.compare(0, 2, "v ") == 0) { + stringstream ss(line.erase(0, 1)); + ss >> x; ss >> y; ss >> z; + vertVals.push_back(x); + vertVals.push_back(y); + vertVals.push_back(z); + } + if (line.compare(0, 2, "vt") == 0) { + stringstream ss(line.erase(0, 2)); + ss >> x; ss >> y; + stVals.push_back(x); + stVals.push_back(y); + } + if (line.compare(0, 2, "vn") == 0) { + stringstream ss(line.erase(0, 2)); + ss >> x; ss >> y; ss >> z; + normVals.push_back(x); + normVals.push_back(y); + normVals.push_back(z); + } + if (line.compare(0, 2, "f ") == 0) { + string oneCorner, v, t, n; + stringstream ss(line.erase(0, 2)); + for (int i = 0; i < 3; i++) { + getline(ss, oneCorner, ' '); + stringstream oneCornerSS(oneCorner); + getline(oneCornerSS, v, '/'); + getline(oneCornerSS, t, '/'); + getline(oneCornerSS, n, '/'); + + int vertRef = (stoi(v) - 1) * 3; + int tcRef = (stoi(t) - 1) * 2; + int normRef = (stoi(n) - 1) * 3; + + triangleVerts.push_back(vertVals[vertRef]); + triangleVerts.push_back(vertVals[vertRef + 1]); + triangleVerts.push_back(vertVals[vertRef + 2]); + + textureCoords.push_back(stVals[tcRef]); + textureCoords.push_back(stVals[tcRef + 1]); + + normals.push_back(normVals[normRef]); + normals.push_back(normVals[normRef + 1]); + normals.push_back(normVals[normRef + 2]); + } + } + } +} +int ModelImporter::getNumVertices() { return (triangleVerts.size()/3); } +std::vector ModelImporter::getVertices() { return triangleVerts; } +std::vector ModelImporter::getTextureCoordinates() { return textureCoords; } +std::vector ModelImporter::getNormals() { return normals; } \ No newline at end of file diff --git a/LoadingModels/ImportedModel.h b/LoadingModels/ImportedModel.h new file mode 100644 index 0000000..17a123b --- /dev/null +++ b/LoadingModels/ImportedModel.h @@ -0,0 +1,35 @@ +#include + +class ImportedModel +{ +private: + int numVertices; + std::vector vertices; + std::vector texCoords; + std::vector normalVecs; +public: + ImportedModel(); + ImportedModel(const char *filePath); + int getNumVertices(); + std::vector getVertices(); + std::vector getTextureCoords(); + std::vector getNormals(); +}; + +class ModelImporter +{ +private: + std::vector vertVals; + std::vector triangleVerts; + std::vector textureCoords; + std::vector stVals; + std::vector normals; + std::vector normVals; +public: + ModelImporter(); + void parseOBJ(const char *filePath); + int getNumVertices(); + std::vector getVertices(); + std::vector getTextureCoordinates(); + std::vector getNormals(); +}; \ No newline at end of file diff --git a/LoadingModels/LoadingModels.vcxproj b/LoadingModels/LoadingModels.vcxproj index 89ee3cc..b7fb5ea 100644 --- a/LoadingModels/LoadingModels.vcxproj +++ b/LoadingModels/LoadingModels.vcxproj @@ -139,6 +139,7 @@ + @@ -148,6 +149,7 @@ + diff --git a/LoadingModels/LoadingModels.vcxproj.filters b/LoadingModels/LoadingModels.vcxproj.filters index 86abf57..6e06ca3 100644 --- a/LoadingModels/LoadingModels.vcxproj.filters +++ b/LoadingModels/LoadingModels.vcxproj.filters @@ -24,6 +24,9 @@ Source Files + + Source Files + @@ -46,5 +49,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/LoadingModels/main.cpp b/LoadingModels/main.cpp index 6b36085..011b5e1 100644 --- a/LoadingModels/main.cpp +++ b/LoadingModels/main.cpp @@ -10,9 +10,10 @@ #include #include "Sphere.h" #include "Torus.h" +#include "ImportedModel.h" #define numVAOs 1 -#define numVBOs 8 +#define numVBOs 11 GLuint renderingProgram; GLuint vao[numVAOs]; @@ -29,6 +30,7 @@ GLuint pyrTex; Sphere sphere(48); Torus torus(1.9,0.4,48); +ImportedModel shuttle("shuttle.obj"); void setupVertices(void) { glGenVertexArrays(1, vao); @@ -96,10 +98,37 @@ void setupVertices(void) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[7]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(float), indices.data(), GL_STATIC_DRAW); + + vertices=shuttle.getVertices(); + uvs=shuttle.getTextureCoords(); + normals=shuttle.getNormals(); + + pvalues.clear(); + tvalues.clear(); + nvalues.clear(); + for(int i=0;i