Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- // Std. Includes
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <vector>
- using namespace std;
- // GL Includes
- #include <glew.h> // Contains all the necessery OpenGL includes
- #include <glm.hpp>
- #include <gtc/matrix_transform.hpp>
- #include "Shader.h"
- struct VertexPathlines {
- // Position
- glm::vec3 Position;
- // Normal
- glm::vec2 TimeAndVelocity;
- // TexCoords
- glm::vec2 TexCoords;
- //Tangent
- glm::vec3 Tangents;
- };
- class MeshPathlines {
- public:
- /* Mesh Data */
- vector<VertexPathlines> vertices;
- vector<GLuint> indices;
- vector<Texture> textures;
- /* Functions */
- // Constructor
- MeshPathlines(vector<VertexPathlines> vertices, vector<GLuint> indices, vector<Texture> textures)
- {
- this->vertices = vertices;
- this->indices = indices;
- this->textures = textures;
- // Now that we have all the required data, set the vertex buffers and its attribute pointers.
- this->setupMesh();
- }
- // Render the mesh
- void Draw(Shader shader)
- {
- //Draw Settings
- glEnable(GL_STENCIL_TEST);
- glStencilFunc(GL_EQUAL, 1, 1);
- glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
- glEnable(GL_PRIMITIVE_RESTART);
- glPrimitiveRestartIndex(std::numeric_limits<GLuint>::max());
- // Bind appropriate textures
- GLuint diffuseNr = 1;
- GLuint specularNr = 1;
- for (GLuint i = 0; i < this->textures.size(); i++)
- {
- glActiveTexture(GL_TEXTURE0 + i); // Active proper texture unit before binding
- // Retrieve texture number (the N in diffuse_textureN)
- stringstream ss;
- string number;
- string name = this->textures[i].type;
- if (name == "texture_diffuse")
- ss << diffuseNr++; // Transfer GLuint to stream
- else if (name == "texture_specular")
- ss << specularNr++; // Transfer GLuint to stream
- number = ss.str();
- // Now set the sampler to the correct texture unit
- glUniform1i(glGetUniformLocation(shader.Program, (name + number).c_str()), i);
- // And finally bind the texture
- glBindTexture(GL_TEXTURE_2D, this->textures[i].id);
- }
- // 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)
- glUniform1f(glGetUniformLocation(shader.Program, "material.shininess"), 16.0f);
- // Draw mesh
- glBindVertexArray(this->VAO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
- glDrawElements(GL_LINE_STRIP, this->indices.size() * sizeof(GLuint), GL_UNSIGNED_INT, nullptr);//draw
- //glDrawElements(GL_POINTS, this->indices.size() * sizeof(GLuint), GL_UNSIGNED_INT, nullptr);
- // glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- //reset settings
- glDisable(GL_PRIMITIVE_RESTART);
- glDisable(GL_STENCIL_TEST);
- // Always good practice to set everything back to defaults once configured.
- for (GLuint i = 0; i < this->textures.size(); i++)
- {
- glActiveTexture(GL_TEXTURE0 + i);
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- }
- private:
- /* Render data */
- GLuint VAO, VBO, EBO;
- /* Functions */
- // Initializes all the buffer objects/arrays
- void setupMesh()
- {
- // Create buffers/arrays
- glGenVertexArrays(1, &this->VAO);
- glGenBuffers(1, &this->VBO);
- glGenBuffers(1, &this->EBO);
- glBindVertexArray(this->VAO);
- // Load data into vertex buffers
- glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
- // A great thing about structs is that their memory layout is sequential for all its items.
- // The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
- // again translates to 3/2 floats which translates to a byte array.
- glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(VertexPathlines), &this->vertices[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW);
- // Set the vertex attribute pointers
- // Vertex Positions
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)0);
- // Vertex Time and Velocity
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)offsetof(VertexPathlines, TimeAndVelocity));
- // Vertex Texture Coords
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)offsetof(VertexPathlines, TexCoords));
- //freshly introduced tangents
- glEnableVertexAttribArray(3);
- glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPathlines), (GLvoid*)offsetof(VertexPathlines, Tangents));
- glBindVertexArray(0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement