ColorCube.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "ColorCube.h"
  2. static unsigned int s_nbColorCubes=0;
  3. // Constructeur et Destructeur
  4. ColorCube::ColorCube()
  5. {
  6. // Incrémentation du nombre de cubes
  7. s_nbColorCubes++;
  8. // Vertices
  9. float verticesTmp[42] = {0.0f,0.0f,1.0f, 1.0f,1.0f,1.0f, 1.0f,0.0f,1.0f, 1.0f,0.0f,0.0f, 0.0f,0.0f,1.0f, 0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f,
  10. 1.0f,0.0f,0.0f, 1.0f,1.0f,0.0f, 1.0f,1.0f,1.0f, 0.0f,1.0f,0.0f, 0.0f,1.0f,1.0f, 0.0f,0.0f,1.0f, 1.0f,1.0f,1.0f};
  11. for(int i(0); i < 42; i++)
  12. m_vertices[i] = verticesTmp[i];
  13. // Couleurs
  14. float couleursTmp[42] = {1.0,1.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 0.0,0.0,1.0, 1.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0,
  15. 0.0,0.0,1.0, 0.0,0.0,1.0, 1.0,0.0,0.0, 0.0,1.0,0.0, 1.0,1.0,0.0, 1.0,1.0,0.0, 1.0,0.0,0.0};
  16. for (int i(0); i<42; i++)
  17. m_couleurs[i] = couleursTmp[i];
  18. }
  19. ColorCube::ColorCube(float taille, float red, float green, float blue)
  20. {
  21. // Incrémentation du nombre de cubes
  22. s_nbColorCubes++;
  23. // Vertices
  24. float verticesTmp[42] = {0.f,0.0f,taille, taille,taille,taille, taille,0.0f,taille, taille,0.0f,0.0f, 0.0f,0.0f,taille, 0.0f,0.0f,0.0f, 0.0f,taille,0.0f,
  25. taille,0.0f,0.0f, taille,taille,0.0f, taille,taille,taille, 0.0f,taille,0.0f, 0.0f,taille,taille, 0.0f,0.0f,taille, taille,taille,taille};
  26. for(int i(0); i < 42; i++)
  27. m_vertices[i] = verticesTmp[i];
  28. // Couleurs
  29. setColor(red,green,blue);
  30. }
  31. ColorCube::~ColorCube()
  32. {
  33. s_nbColorCubes--;
  34. }
  35. // Méthodes
  36. void ColorCube::setColor(float red, float green, float blue)
  37. {
  38. // Attribution des couleurs
  39. for(int i(0); i < 42; i++)
  40. {
  41. switch (i%3)
  42. {
  43. case 0:
  44. m_couleurs[i] = red;
  45. break;
  46. case 1:
  47. m_couleurs[i] = green;
  48. break;
  49. case 2:
  50. m_couleurs[i] = blue;
  51. break;
  52. }
  53. }
  54. }
  55. void ColorCube::afficher()
  56. {
  57. // Envoi des vertices
  58. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertices);
  59. glEnableVertexAttribArray(0);
  60. // Envoi de la couleur
  61. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, m_couleurs);
  62. glEnableVertexAttribArray(1);
  63. // Rendu
  64. glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
  65. // Désactivation des tableaux
  66. glDisableVertexAttribArray(1);
  67. glDisableVertexAttribArray(0);
  68. }
  69. void ColorCube::move(glm::vec3 deplacement)
  70. {
  71. for(int i(0); i < 42; i++)
  72. {
  73. switch (i%3)
  74. {
  75. case 0:
  76. m_vertices[i] += deplacement.x;
  77. break;
  78. case 1:
  79. m_vertices[i] += deplacement.y;
  80. break;
  81. case 2:
  82. m_vertices[i] += deplacement.z;
  83. break;
  84. }
  85. }
  86. }
  87. unsigned int getNbColorCubes()
  88. {
  89. return s_nbColorCubes;
  90. }