Facebook
From Sploky, 2 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 113
  1. #pragma once
  2. #include <glm/glm.hpp>
  3. #include <glm/gtc/matrix_transform.hpp>
  4. #include <vector>
  5. #include "texture.h"
  6. #include "shader.h"
  7.  
  8. namespace glg {
  9.         struct Vertex {
  10.                 glm::vec3 position;
  11.                 glm::vec3 normal;
  12.                 glm::vec2 texCoords;
  13.         };
  14.  
  15.         class Mesh {
  16.         public:
  17.                 std::vector<Vertex>     vertices;
  18.                 std::vector<unsigned int> indices;
  19.                 std::vector<Texture2D> textures;
  20.  
  21.                 Mesh(std::vector<Vertex>& vertices, std::vector<unsigned int>& indices, std::vector<Texture2D>& textures) {
  22.                         this->vertices = vertices;
  23.                         this->indices = indices;
  24.                         this->textures = textures;
  25.  
  26.                         setupMesh();
  27.                 }
  28.  
  29.                 void draw(Shader& shader) {
  30.                         unsigned int diffuseNr = 1;
  31.                         for (unsigned int i = 0; i < textures.size(); i++) {
  32.                                 std::string number;
  33.                                 std::string name = "diffuse";
  34.                                 if (name == "diffuse") {
  35.                                         number = std::to_string(diffuseNr);
  36.                                         diffuseNr++;
  37.                                 }
  38.  
  39.                                 shader.setInt("texture_" + name + number, i);
  40.                                 textures[i].activate(i);
  41.                         }
  42.                        
  43.                         glBindVertexArray(VAO);
  44.                         glDrawElements(GL_TRIANGLES, (int) indices.size(), GL_UNSIGNED_INT, 0);
  45.                         glBindVertexArray(0);
  46.                         glActiveTexture(GL_TEXTURE0);
  47.  
  48.                 }
  49.  
  50.         private:
  51.                 unsigned int VAO, VBO, EBO;
  52.  
  53.                 void setupMesh() {
  54.                         glGenVertexArrays(1, &VAO);
  55.                         glGenBuffers(1, &VBO);
  56.                         glGenBuffers(1, &EBO);
  57.  
  58.                         glBindVertexArray(VAO);
  59.                         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  60.  
  61.                         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
  62.                        
  63.                         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  64.                         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
  65.  
  66.                         // vertex positions
  67.                         glEnableVertexAttribArray(0);
  68.                         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
  69.                         // vertex texture coords
  70.                         glEnableVertexAttribArray(1);
  71.                         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoords));
  72.  
  73.                         glBindVertexArray(0);
  74.                 }
  75.         };
  76. }
captcha