Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <GLFW\glfw3.h>
- #include <glad\glad.h>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <map>
- #include <vector>
- #include <glm\glm.hpp>
- #include "lodepng.h"
- #include <assimp\Importer.hpp>
- #include <assimp\scene.h>
- #include <assimp\postprocess.h>
- #include "Models.h"
- struct Vertex
- {
- glm::vec3 position;
- glm::vec3 normal;
- glm::vec2 uv;
- };
- static Mesh process_mesh(aiMesh *mesh, const aiScene *scene)
- {
- std::vector<Vertex> vertices;
- std::vector<GLuint> indices;
- for (GLuint i = 0; i < mesh->mNumVertices; i++)
- {
- Vertex vertex;
- glm::vec3 vector;
- vector.x = mesh->mVertices[i].x;
- vector.y = mesh->mVertices[i].y;
- vector.z = mesh->mVertices[i].z;
- vertex.position = vector;
- if (mesh->mNormals != nullptr)
- {
- vector.x = mesh->mNormals[i].x;
- vector.y = mesh->mNormals[i].y;
- vector.z = mesh->mNormals[i].z;
- vertex.normal = vector;
- }
- else
- {
- vertex.normal = glm::vec3(0, 0, 0);
- }
- if (mesh->mTextureCoords[0])
- {
- glm::vec2 uv;
- uv.x = mesh->mTextureCoords[0][i].x;
- uv.y = mesh->mTextureCoords[0][i].y;
- vertex.uv = uv;
- }
- else
- {
- vertex.uv = glm::vec2(0, 0);
- }
- vertices.push_back(vertex);
- }
- for (GLuint i = 0; i < mesh->mNumFaces; i++)
- {
- aiFace face = mesh->mFaces[i];
- for (GLuint j = 0; j < face.mNumIndices; j++)
- {
- indices.push_back(face.mIndices[j]);
- }
- }
- std::vector<GLfloat> vertices_float;
- for (GLuint i = 0; i < vertices.size(); i++)
- {
- Vertex v = vertices[i];
- vertices_float.push_back(v.position.x);
- vertices_float.push_back(v.position.y);
- vertices_float.push_back(v.position.z);
- vertices_float.push_back(v.normal.x);
- vertices_float.push_back(v.normal.y);
- vertices_float.push_back(v.normal.z);
- vertices_float.push_back(v.uv.x);
- vertices_float.push_back(v.uv.y);
- }
- return Mesh(vertices_float, indices);
- }
- static void process_node(aiNode *node, const aiScene *scene, std::vector<Mesh> *meshes)
- {
- for (GLuint i = 0; i < node->mNumMeshes; i++)
- {
- aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
- meshes->push_back(process_mesh(mesh, scene));
- }
- for (GLuint i = 0; i < node->mNumChildren; i++)
- {
- process_node(node->mChildren[i], scene, meshes);
- }
- }
- static Model load_model(const char *path)
- {
- Assimp::Importer importer;
- const aiScene *assimp_scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
- if (!assimp_scene || assimp_scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !assimp_scene->mRootNode)
- {
- std::cout << "LOADER::LOAD_MODEL::ASSIMP::ERROR\n" << importer.GetErrorString() << "\n";
- return Model(std::vector<Mesh>());
- }
- std::vector<Mesh> meshes = std::vector<Mesh>();
- process_node(assimp_scene->mRootNode, assimp_scene, &meshes);
- importer.~Importer();
- return Model(meshes);
- }
- static GLuint load_texture(const char *path)
- {
- GLuint texture;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- std::vector<unsigned char> buf;
- unsigned int error, width, height;
- if ((error = lodepng::decode(buf, width, height, path, LodePNGColorType::LCT_RGBA)) != 0)
- {
- std::cout << "LOADER::LOAD_TEXTURE::ERROR::TEXTURE_LOAD_FAILED";
- }
- unsigned char* image = buf.data();
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
- glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
- return texture;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement