Advertisement
Guest User

Mesh.cpp

a guest
Nov 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include "Mesh.h"
  2.  
  3. #include <glm\gtc\matrix_transform.hpp>
  4. #include <glm\gtc\type_ptr.hpp>
  5.  
  6. #define STB_IMAGE_IMPLEMENTATION
  7. #include <stb_image.h>
  8.  
  9. Mesh::Mesh()
  10. {
  11.     glGenVertexArrays(1, &m_vertexArrayObject);
  12.     glGenBuffers(NUMBER_OF_BUFFERS, m_buffers);
  13. }
  14. Mesh::~Mesh()
  15. {
  16.     glDeleteVertexArrays(1, &m_vertexArrayObject);
  17.     glDeleteBuffers(NUMBER_OF_BUFFERS, m_buffers);
  18. }
  19.  
  20. void Mesh::addData(const glm::vec3& vertexPosition, const glm::vec2& textureCoordinates)
  21. {
  22.     m_vertexPositions.push_back(vertexPosition.x);
  23.     m_vertexPositions.push_back(vertexPosition.y);
  24.     m_vertexPositions.push_back(vertexPosition.z);
  25.  
  26.     m_textureCoordinates.push_back(textureCoordinates.x);
  27.     m_textureCoordinates.push_back(textureCoordinates.y);
  28. }
  29.  
  30. void Mesh::upload()
  31. {
  32.     glBindVertexArray(m_vertexArrayObject);
  33.  
  34.     /* Vertex Buffer*/
  35.     glBindBuffer(GL_ARRAY_BUFFER, m_buffers[VERTEX_BUFFER]);
  36.     glBufferData(GL_ARRAY_BUFFER, m_vertexPositions.size() * sizeof(m_vertexPositions.size()), m_vertexPositions.data(), GL_STATIC_DRAW);
  37.  
  38.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  39.     glEnableVertexAttribArray(0);
  40.  
  41.     /* Texture buffer */
  42.     glBindBuffer(GL_ARRAY_BUFFER, m_buffers[TEXTURE_BUFFER]);
  43.     glBufferData(GL_ARRAY_BUFFER, m_textureCoordinates.size() * sizeof(m_textureCoordinates.size()), m_textureCoordinates.data(), GL_STATIC_DRAW);
  44.  
  45.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  46.     glEnableVertexAttribArray(1);
  47.  
  48.     /* Index Buffer */
  49.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buffers[INDEX_BUFFER]);
  50.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(m_indices.size()), m_indices.data(), GL_STATIC_DRAW);
  51.  
  52.     glBindVertexArray(0);
  53. }
  54.  
  55. void Mesh::clear()
  56. {
  57.     m_vertexPositions.clear();
  58.     m_textureCoordinates.clear();
  59.     m_indices.clear();
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement