diff --git a/LearnOpenGL/OpenGLWrapper.cpp b/LearnOpenGL/OpenGLWrapper.cpp index 4d39a5cf0887e79728043cd747abc2466e9d0576..8d35bdcf0f1a5fa6b09ab7c9a6dd784664a040ee 100644 --- a/LearnOpenGL/OpenGLWrapper.cpp +++ b/LearnOpenGL/OpenGLWrapper.cpp @@ -326,15 +326,37 @@ GLuint allocate_VBO(const GLuint attribIndex, std::vector<glm::vec3> *VBO) return VBOIndex; } -GLuint *allocate_VBOs(GLuint VAO, std::vector<std::vector<glm::vec3> *> &vertexInfo) +GLuint allocate_VBO(const GLuint attribIndex, std::vector<glm::vec2> *VBO) { - GLuint *VBOindicies = new GLuint[vertexInfo.size()]; + GLuint VBOIndex = 0; + + glGenBuffers(1, &VBOIndex); + glBindBuffer(GL_ARRAY_BUFFER, VBOIndex); + glBufferData(GL_ARRAY_BUFFER, VBO->size() * sizeof(glm::vec2), &(VBO->front()), GL_STATIC_DRAW); + + glEnableVertexAttribArray(attribIndex); + glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + + return VBOIndex; +} + +GLuint *allocate_VBOs(GLuint VAO, std::vector<std::vector<glm::vec3> *> &vertexInfoVec3, std::vector<std::vector<glm::vec2> *> &vertexInfoVec2) +{ + auto size = vertexInfoVec3.size() + vertexInfoVec2.size(); + GLuint *VBOindicies = new GLuint[size]; glBindVertexArray(VAO); - for (GLuint i = 0; i < vertexInfo.size(); i++) + GLuint i = 0; + for (i = 0; i < vertexInfoVec3.size(); i++) + { + VBOindicies[i] = allocate_VBO(i, vertexInfoVec3.at(i)); + } + for (i = vertexInfoVec3.size(); i < size; i++) { - VBOindicies[i] = allocate_VBO(i, vertexInfo.at(i)); + VBOindicies[i] = allocate_VBO(i, vertexInfoVec2.at(i - vertexInfoVec3.size())); } glBindVertexArray(0); @@ -354,7 +376,7 @@ GLuint allocate_VAO() Mesh *make_mesh(const std::string fileName) { std::vector<glm::vec3> vertices; - std::vector<glm::vec3> vertexTexCoord; + std::vector<glm::vec2> vertexTexCoord; std::vector<glm::vec3> vertexNormals; const std::string ext = get_extension(fileName); @@ -367,19 +389,21 @@ Mesh *make_mesh(const std::string fileName) std::cout << "Can't Open " + fileName + " Extension." << std::endl; } - std::vector<std::vector<glm::vec3> *> vertexInfo; - vertexInfo.push_back(&vertices); + std::vector<std::vector<glm::vec3> *> vertexInfoVec3; + std::vector<std::vector<glm::vec2> *> vertexInfoVec2; + + vertexInfoVec3.push_back(&vertices); if (vertexTexCoord.size() > 0) { - vertexInfo.push_back(&vertexTexCoord); + vertexInfoVec2.push_back(&vertexTexCoord); } if (vertexNormals.size() > 0) { - vertexInfo.push_back(&vertexNormals); + vertexInfoVec3.push_back(&vertexNormals); } auto VAO = allocate_VAO(); - auto VBOs = allocate_VBOs(VAO, vertexInfo); + auto VBOs = allocate_VBOs(VAO, vertexInfoVec3, vertexInfoVec2); Mesh *m = new Mesh(vertices.size(), VAO, VBOs); diff --git a/LearnOpenGL/ResourceLoader.cpp b/LearnOpenGL/ResourceLoader.cpp index 07853fc7c476c743270ba2264628985b17ef0313..1d33f2a3989a8b97ca076a248f167e36e02c4a29 100644 --- a/LearnOpenGL/ResourceLoader.cpp +++ b/LearnOpenGL/ResourceLoader.cpp @@ -31,7 +31,7 @@ std::string get_extension(const std::string &filePath) return filePath.substr(filePath.find_last_of(".") + 1); } -bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec3> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals) +bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec2> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals) { vertices.clear(); vertexTexCoord.clear(); @@ -42,7 +42,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: char op[3]; std::vector<glm::vec3> vertexIndices; - std::vector<glm::vec3> vertexTexCoordIndices; + std::vector<glm::vec2> vertexTexCoordIndices; std::vector<glm::vec3> vertexNormalIndices; ifs.open("../Models/" + fileName); @@ -74,7 +74,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: } else if (strcmp(op, "vt") == false) { - glm::vec3 pos = { 0,0,0 }; + glm::vec2 pos = { 0,0 }; sscanf_s(line.c_str(), "%f %f", &pos.x, &pos.y); vertexTexCoordIndices.push_back(pos); } diff --git a/LearnOpenGL/ResourceLoader.h b/LearnOpenGL/ResourceLoader.h index 5123afe10061e087c74a4c13added1a5331512a0..21b7fa35dd472153c3c27f5a45cafd7472e486ad 100644 --- a/LearnOpenGL/ResourceLoader.h +++ b/LearnOpenGL/ResourceLoader.h @@ -31,6 +31,6 @@ public: }; std::string get_extension(const std::string &filePath); -bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec3> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals); +bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec2> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals); Image *load_Image(std::string fileName, int *width, int *height, int *nrChannels); void free_image(Image *img); \ No newline at end of file diff --git a/LearnOpenGL/Source.cpp b/LearnOpenGL/Source.cpp index 196e5abdd4b9d168cab182a8e4dce52103b35b65..b5e04867e60dd311a6022b2a0610e2d4698ed623 100644 --- a/LearnOpenGL/Source.cpp +++ b/LearnOpenGL/Source.cpp @@ -70,7 +70,7 @@ int main() auto container_specular = load_image("container2_specular.png"); auto defaultMaterial = new Material(lightmap, orange, transparent); - auto planeMaterial = new Material(texture_shader, container_diffuse, transparent); + auto planeMaterial = new Material(lightmap, container_diffuse, transparent); auto teapot = make_render_object(make_mesh("teapot.obj")); { @@ -106,7 +106,7 @@ int main() plane->set_scale(glm::vec3(10, 10, 1)); } { - plane->set_material(planeMaterial); + plane->set_material(defaultMaterial); } auto cube3 = make_render_object(cube); @@ -135,7 +135,7 @@ int main() cube1->render(camera); cube2->render(camera); cube3->render(camera); - //plane->render(camera); + plane->render(camera); } glfwSwapBuffers(window);