Mosaic.cpp 2.8 KB

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