Mosaic.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "Mosaic.h"
  2. //Constructeurs et destructeurs
  3. Mosaic::Mosaic(std::string const vertexShader, std::string const fragmentShader, std::string const fichierImg, Uint8 redMask, Uint8 greenMask, Uint8 blueMask)
  4. :m_shader(vertexShader, fragmentShader)
  5. {
  6. /// Chargement de l'image
  7. SDL_Surface* image = SDL_LoadBMP(fichierImg.c_str());
  8. SDL_LockSurface( image );
  9. /// Attribution des couleurs
  10. if (image!=0)
  11. {
  12. // Préaparation des variables d'info et de manip
  13. unsigned char* pixels = (unsigned char*) image->pixels;
  14. Uint8 taille(image->format->BytesPerPixel);
  15. Uint32 ligne(DIM*taille);
  16. Uint8 red,green,blue,addRed,addBlue;
  17. if(image->format->Rmask == 0xff){
  18. addRed = 0;
  19. addBlue = 2;
  20. }
  21. else{
  22. addRed = 2;
  23. addBlue = 0;
  24. }
  25. // Balayage
  26. for (int y(0); y<DIM; y++)
  27. for (int x(0); x<DIM; x++)
  28. {
  29. // Chargement des couleurs
  30. red = pixels[(ligne*y)+x*taille+addRed];
  31. green = pixels[(ligne*y)+x*taille+1];
  32. blue = pixels[(ligne*y)+x*taille+addBlue];
  33. // Voxel non-affiché
  34. if (red==redMask && green==greenMask && blue==blueMask)
  35. m_opaque[DIM-y-1][x]=false;
  36. // Voxel affiché : attribution des couleurs
  37. else
  38. {
  39. m_opaque[DIM-y-1][x]=true;
  40. m_mosaic[DIM-y-1][x].setColor((float)red/255.0f,(float)green/255.0f,(float)blue/255.0f);
  41. }
  42. }
  43. }
  44. else
  45. std::cout <<"Impossible de charger "<<fichierImg<<" dans Mosaic."<< std::endl;
  46. /// Shader
  47. m_shader.charger();
  48. /// Placement des cubes
  49. glm::vec3 deplacement(-DIM/2,-DIM/2,-0.5f);
  50. for (int y(0); y<DIM; y++)
  51. {
  52. for (int x(0); x<DIM; x++)
  53. {
  54. m_mosaic[y][x].move(deplacement);
  55. deplacement.x++;
  56. }
  57. deplacement.y++;
  58. deplacement.x=-DIM/2;
  59. }
  60. /// Déverouillage
  61. SDL_UnlockSurface( image );
  62. // Libération de la mémoire
  63. SDL_FreeSurface(image);
  64. }
  65. Mosaic::~Mosaic()
  66. {
  67. }
  68. //Méthodes
  69. void Mosaic::afficher(glm::mat4 &projection, glm::mat4 modelview)
  70. {
  71. /// OpenGL verrouillages
  72. // Activation du shader
  73. glUseProgram(m_shader.getProgramID());
  74. // Envoi des matrices
  75. glUniformMatrix4fv(glGetUniformLocation(m_shader.getProgramID(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
  76. glUniformMatrix4fv(glGetUniformLocation(m_shader.getProgramID(), "modelview"), 1, GL_FALSE, glm::value_ptr(modelview));
  77. /// Balayage
  78. for (int y(0); y<DIM; y++)
  79. for (int x(0); x<DIM; x++)
  80. if(m_opaque[y][x])
  81. m_mosaic[y][x].afficher();
  82. /// OpenGL déverrouillages
  83. // Désactivation du shader
  84. glUseProgram(0);
  85. }