Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include "./../include/IndexedCubeRenderable.hpp"
  2. #include "./../include/gl_helper.hpp"
  3. #include "./../include/log.hpp"
  4. #include "./../include/Utils.hpp"
  5.  
  6. #include <glm/gtc/type_ptr.hpp>
  7. #include <GL/glew.h>
  8.  
  9. IndexedCubeRenderable::IndexedCubeRenderable(ShaderProgramPtr shaderProgram) :
  10. Renderable(shaderProgram),
  11. m_pBuffer(0),
  12. m_cBuffer(0),
  13. m_iBuffer(0)
  14. {
  15. m_model = glm::mat4(1.0);
  16.  
  17. int nbPoints = 8;
  18. int nbPointsDistincts = 36;
  19. int points[] = {
  20. 1,1,1,
  21. 1,-1,-1,
  22. 1,-1,1,
  23. 1,1,-1,
  24. -1,1,1,
  25. -1,1,-1,
  26. -1,-1,1,
  27. -1,-1,-1
  28. };
  29.  
  30. for (size_t i = 0; i < nbPoints; i++) {
  31. m_positions.push_back(glm::vec3(points[3*i], points[3*i+1], points[3*i+2]));
  32. m_colors.push_back(randomColor());
  33. }
  34.  
  35. m_indices.push_back(glm::ivec3(0, 1, 2));
  36. // m_indices.push_back(glm::ivec3(1));
  37. // m_indices.push_back(glm::ivec3(2));
  38.  
  39. glGenBuffers(1, &m_pBuffer);
  40. glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer);
  41. glBufferData(GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW);
  42.  
  43. glGenBuffers(1, &m_cBuffer);
  44. glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer);
  45. glBufferData(GL_ARRAY_BUFFER, m_colors.size()*sizeof(glm::vec4), m_colors.data(), GL_STATIC_DRAW);
  46.  
  47. glGenBuffers(1, &m_iBuffer);
  48. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iBuffer);
  49. glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size()*sizeof(glm::ivec3), m_indices.data(), GL_STATIC_DRAW);
  50. }
  51.  
  52. void IndexedCubeRenderable::do_draw()
  53. {
  54. int modelLocation = m_shaderProgram->getUniformLocation("modelMat");
  55. glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(m_model));
  56.  
  57. int positionLocation = m_shaderProgram->getAttributeLocation("vPosition");
  58. glEnableVertexAttribArray(positionLocation);
  59. glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer);
  60. glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  61.  
  62. int colorLocation = m_shaderProgram->getAttributeLocation("inColor");
  63. glEnableVertexAttribArray(colorLocation);
  64. glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer);
  65. glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
  66.  
  67. glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, &m_indices);
  68.  
  69. glDisableVertexAttribArray(positionLocation);
  70. glDisableVertexAttribArray(colorLocation);
  71. }
  72.  
  73. void IndexedCubeRenderable::do_animate(float time) {}
  74.  
  75. IndexedCubeRenderable::~IndexedCubeRenderable()
  76. {
  77. glDeleteBuffers(1, &m_pBuffer);
  78. glDeleteBuffers(1, &m_cBuffer);
  79. glDeleteBuffers(1, &m_iBuffer);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement