Advertisement
Nightdra

Mesh.h

Jul 14th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #ifndef MESH_H
  2. #define MESH_H
  3.  
  4. #include <glad/glad.h> // holds all OpenGL type declarations
  5.  
  6. #include "/home/l/cpp/assimp-3.1.1/include/assimp/Importer.hpp"
  7.  
  8. #include <glm/glm.hpp>
  9. #include <glm/gtc/matrix_transform.hpp>
  10.  
  11. #include "Shader.h"
  12.  
  13. #include <string>
  14. #include <fstream>
  15. #include <sstream>
  16. #include <iostream>
  17. #include <vector>
  18. using namespace std;
  19.  
  20. struct Vertex {
  21.     // position
  22.     glm::vec3 Position;
  23.     // normal
  24.     glm::vec3 Normal;
  25.     // texCoords
  26.     glm::vec2 TexCoords;
  27.     // tangent
  28.     glm::vec3 Tangent;
  29.     // bitangent
  30.     glm::vec3 Bitangent;
  31. };
  32.  
  33. struct Texture {
  34.     unsigned int id;
  35.     string type;
  36.     aiString path;
  37. };
  38.  
  39. class Mesh {
  40. public:
  41.     /*  Mesh Data  */
  42.     vector<Vertex> vertices;
  43.     vector<unsigned int> indices;
  44.     vector<Texture> textures;
  45.     unsigned int VAO;
  46.  
  47.     /*  Functions  */
  48.     // constructor
  49.     Mesh(vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures)
  50.     {
  51.         this->vertices = vertices;
  52.         this->indices = indices;
  53.         this->textures = textures;
  54.  
  55.         // now that we have all the required data, set the vertex buffers and its attribute pointers.
  56.         setupMesh();
  57.     }
  58.  
  59.     // render the mesh
  60.     void Draw(Shader shader)
  61.     {
  62.         // bind appropriate textures
  63.         unsigned int diffuseNr  = 1;
  64.         unsigned int specularNr = 1;
  65.         unsigned int normalNr   = 1;
  66.         unsigned int heightNr   = 1;
  67.         for(unsigned int i = 0; i < textures.size(); i++)
  68.         {
  69.             glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
  70.             // retrieve texture number (the N in diffuse_textureN)
  71.             stringstream ss;
  72.             string number;
  73.             string name = textures[i].type;
  74.             if(name == "texture_diffuse")
  75.                 ss << diffuseNr++; // transfer unsigned int to stream
  76.             else if(name == "texture_specular")
  77.                 ss << specularNr++; // transfer unsigned int to stream
  78.             else if(name == "texture_normal")
  79.                 ss << normalNr++; // transfer unsigned int to stream
  80.              else if(name == "texture_height")
  81.                 ss << heightNr++; // transfer unsigned int to stream
  82.             number = ss.str();
  83.             // now set the sampler to the correct texture unit
  84.             glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
  85.             // and finally bind the texture
  86.             glBindTexture(GL_TEXTURE_2D, textures[i].id);
  87.         }
  88.  
  89.         // draw mesh
  90.         glBindVertexArray(VAO);
  91.         glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
  92.         glBindVertexArray(0);
  93.  
  94.         // always good practice to set everything back to defaults once configured.
  95.         glActiveTexture(GL_TEXTURE0);
  96.     }
  97.  
  98. private:
  99.     /*  Render data  */
  100.     unsigned int VBO, EBO;
  101.  
  102.     /*  Functions    */
  103.     // initializes all the buffer objects/arrays
  104.     void setupMesh()
  105.     {
  106.         // create buffers/arrays
  107.         glGenVertexArrays(1, &VAO);
  108.         glGenBuffers(1, &VBO);
  109.         glGenBuffers(1, &EBO);
  110.  
  111.         glBindVertexArray(VAO);
  112.         // load data into vertex buffers
  113.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  114.         // A great thing about structs is that their memory layout is sequential for all its items.
  115.         // The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
  116.         // again translates to 3/2 floats which translates to a byte array.
  117.         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
  118.  
  119.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  120.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
  121.  
  122.         // set the vertex attribute pointers
  123.         // vertex Positions
  124.         glEnableVertexAttribArray(0);
  125.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
  126.         // vertex normals
  127.         glEnableVertexAttribArray(1);
  128.         glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
  129.         // vertex texture coords
  130.         glEnableVertexAttribArray(2);
  131.         glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
  132.         // vertex tangent
  133.         glEnableVertexAttribArray(3);
  134.         glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
  135.         // vertex bitangent
  136.         glEnableVertexAttribArray(4);
  137.         glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
  138.  
  139.         glBindVertexArray(0);
  140.     }
  141. };
  142. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement