Advertisement
Guest User

Meshpathlines.h

a guest
Dec 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. // Std. Includes
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <vector>
  8. using namespace std;
  9. // GL Includes
  10. #include <glew.h> // Contains all the necessery OpenGL includes
  11. #include <glm.hpp>
  12. #include <gtc/matrix_transform.hpp>
  13.  
  14. #include "Shader.h"
  15.  
  16.  
  17. struct VertexPathlines {
  18.     // Position
  19.     glm::vec3 Position;
  20.     // Normal
  21.     glm::vec2 TimeAndVelocity;
  22.     // TexCoords
  23.     glm::vec2 TexCoords;
  24.     //Tangent
  25.     glm::vec3 Tangents;
  26. };
  27.  
  28. class MeshPathlines {
  29. public:
  30.     /*  Mesh Data  */
  31.     vector<VertexPathlines> vertices;
  32.     vector<GLuint> indices;
  33.     vector<Texture> textures;
  34.  
  35.     /*  Functions  */
  36.     // Constructor
  37.     MeshPathlines(vector<VertexPathlines> vertices, vector<GLuint> indices, vector<Texture> textures)
  38.     {
  39.         this->vertices = vertices;
  40.         this->indices = indices;
  41.         this->textures = textures;
  42.  
  43.         // Now that we have all the required data, set the vertex buffers and its attribute pointers.
  44.         this->setupMesh();
  45.     }
  46.  
  47.     // Render the mesh
  48.     void Draw(Shader shader)
  49.     {
  50.         //Draw Settings
  51.         glEnable(GL_STENCIL_TEST);
  52.         glStencilFunc(GL_EQUAL, 1, 1);
  53.         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  54.         glEnable(GL_PRIMITIVE_RESTART);
  55.         glPrimitiveRestartIndex(std::numeric_limits<GLuint>::max());
  56.  
  57.  
  58.         // Bind appropriate textures
  59.         GLuint diffuseNr = 1;
  60.         GLuint specularNr = 1;
  61.         for (GLuint i = 0; i < this->textures.size(); i++)
  62.         {
  63.             glActiveTexture(GL_TEXTURE0 + i); // Active proper texture unit before binding
  64.                                               // Retrieve texture number (the N in diffuse_textureN)
  65.             stringstream ss;
  66.             string number;
  67.             string name = this->textures[i].type;
  68.             if (name == "texture_diffuse")
  69.                 ss << diffuseNr++; // Transfer GLuint to stream
  70.             else if (name == "texture_specular")
  71.                 ss << specularNr++; // Transfer GLuint to stream
  72.             number = ss.str();
  73.             // Now set the sampler to the correct texture unit
  74.             glUniform1i(glGetUniformLocation(shader.Program, (name + number).c_str()), i);
  75.             // And finally bind the texture
  76.             glBindTexture(GL_TEXTURE_2D, this->textures[i].id);
  77.         }
  78.  
  79.         // Also set each mesh's shininess property to a default value (if you want you could extend this to another mesh property and possibly change this value)
  80.         glUniform1f(glGetUniformLocation(shader.Program, "material.shininess"), 16.0f);
  81.  
  82.         // Draw mesh
  83.         glBindVertexArray(this->VAO);
  84.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
  85.         glDrawElements(GL_LINE_STRIP, this->indices.size() * sizeof(GLuint), GL_UNSIGNED_INT, nullptr);//draw
  86.         //glDrawElements(GL_POINTS, this->indices.size() * sizeof(GLuint), GL_UNSIGNED_INT, nullptr);
  87.  
  88. //      glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
  89.         glBindVertexArray(0);
  90.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  91.  
  92.         //reset settings
  93.         glDisable(GL_PRIMITIVE_RESTART);
  94.         glDisable(GL_STENCIL_TEST);
  95.  
  96.  
  97.         // Always good practice to set everything back to defaults once configured.
  98.         for (GLuint i = 0; i < this->textures.size(); i++)
  99.         {
  100.             glActiveTexture(GL_TEXTURE0 + i);
  101.             glBindTexture(GL_TEXTURE_2D, 0);
  102.         }
  103.     }
  104.  
  105. private:
  106.     /*  Render data  */
  107.     GLuint VAO, VBO, EBO;
  108.  
  109.     /*  Functions    */
  110.     // Initializes all the buffer objects/arrays
  111.     void setupMesh()
  112.     {
  113.         // Create buffers/arrays
  114.         glGenVertexArrays(1, &this->VAO);
  115.         glGenBuffers(1, &this->VBO);
  116.         glGenBuffers(1, &this->EBO);
  117.  
  118.         glBindVertexArray(this->VAO);
  119.         // Load data into vertex buffers
  120.         glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
  121.         // A great thing about structs is that their memory layout is sequential for all its items.
  122.         // The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
  123.         // again translates to 3/2 floats which translates to a byte array.
  124.         glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(VertexPathlines), &this->vertices[0], GL_STATIC_DRAW);
  125.  
  126.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
  127.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW);
  128.  
  129.         // Set the vertex attribute pointers
  130.         // Vertex Positions
  131.         glEnableVertexAttribArray(0);
  132.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)0);
  133.         // Vertex Time and Velocity
  134.         glEnableVertexAttribArray(1);
  135.         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)offsetof(VertexPathlines, TimeAndVelocity));
  136.         // Vertex Texture Coords
  137.         glEnableVertexAttribArray(2);
  138.         glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)offsetof(VertexPathlines, TexCoords));
  139.         //freshly introduced tangents
  140.         glEnableVertexAttribArray(3);
  141.         glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)offsetof(VertexPathlines, Tangents));
  142.  
  143.         glBindVertexArray(0);
  144.     }
  145. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement