Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: razormta en 27 Febrero 2014, 01:35 am



Título: Cargar y mostrar image BMP c++
Publicado por: razormta en 27 Febrero 2014, 01:35 am
¿ Que ven mal en mi codigo ?
Es que me muestra un cuadro gris pero no carga la textura .... no entiendo porque
Estoy algo desesperado y puede que este pasando algo por alto, si alguien ve algo malo digame T_T

PD: empeze a hacerlo hace unas horas y no he estudiado mucho con respecto al manejo de BMP's, tal ves ese sea mi problema

Código
  1.  
  2. bool SPRITE::LoadBMP(const char * path, GLuint * texture)
  3. {
  4. std::ifstream img(path, std::ios::binary);
  5. if( !img )
  6. return(False);
  7.  
  8. BITMAPFILEHEADER imgHeader;
  9. BITMAPINFOHEADER imgInfo;
  10.  
  11. img.read( (char*)&imgHeader,sizeof(BITMAPFILEHEADER) );
  12. img.read( (char*)&imgInfo, sizeof( BITMAPINFOHEADER ) );
  13.  
  14. if (imgHeader.bfType != 0x4D42)
  15. return(False);
  16.  
  17. int size = ( imgInfo.biSizeImage );
  18.  
  19. BYTE * pix =new BYTE[size];
  20. img.read( (char*)pix, size );
  21.  
  22. img.close();
  23.  
  24. BYTE * data = new BYTE[size];
  25. for (int i = 0; i < size; i += 3)
  26. {
  27. data[i+0] = pix[i+2];
  28. data[i+1] = pix[i+1];
  29. data[i+2] = pix[i+0];;
  30. }
  31.  
  32. unsigned int w = imgInfo.biWidth;
  33. unsigned int h = imgInfo.biHeight;
  34.  
  35. glGenTextures(1, texture);          
  36. glBindTexture(GL_TEXTURE_2D, *texture);            
  37.  
  38. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  39. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  40.  
  41. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  42.  
  43. glBindTexture(GL_TEXTURE_2D, NULL);
  44.  
  45. return(True);
  46. }
  47.  
  48.  


Título: Re: Cargar y mostrar image BMP c++
Publicado por: razormta en 27 Febrero 2014, 01:49 am
Resolver problemas es mi mayor adiccion, ya lo resolvi... pff ... punteros xd , de todas maneras deberia borrar el post no ?, haha ... no me hace falta un foro lml okno xd


Título: Re: Cargar y mostrar image BMP c++
Publicado por: engel lex en 27 Febrero 2014, 03:12 am
no, no se borra :P coloca tu solucion... por si alguien se enfrenta a eso luego... o de todas formas queda como un aporte


Título: Re: Cargar y mostrar image BMP c++
Publicado por: razormta en 1 Marzo 2014, 02:54 am
Haciendo caso al chico de arriba, aca mi solucion, primero estudiar a fondo como funcionan las BMP's, todo acerca de el padding y aprender la diferencia de imagenes de 8 bits, 16 bits , 24 bits etc ...

Por ahora mi programa trabaja con imagenes bmp de 24 bits, pero me animare para hacerlo de todos los formatos posibles, algo asi como freeimage xd

Código
  1. bool SPRITE::LoadBMP(const char * path, GLuint * texture)
  2. {
  3. /**/
  4. std::ifstream img(path, std::ios::binary);
  5. if( !img )
  6. return(False);
  7. /**/
  8. BITMAPINFOHEADER imgHeader;
  9. BITMAPFILEHEADER imgInfo;
  10. img.read( (char*)&imgHeader,sizeof(BITMAPFILEHEADER) );
  11. img.read( (char*)&imgInfo, sizeof( BITMAPINFOHEADER ) );
  12. /**/
  13. if (imgHeader.bfType != 0x4D42)
  14. {
  15. img.close();
  16. return(False);
  17. }
  18. /**/
  19. int W =  imgInfo.biWidth;
  20. int w = 4* ( ( W * 24 + 31 ) /32 );
  21. int h = imgInfo.biHeight;
  22. int size = abs(w*h);
  23. /**/
  24. BYTE * pix = new BYTE[size];
  25. img.seekg(imgHeader.bfOffBits);
  26. img.read( (char*)pix, size );
  27. /**/
  28. BYTE * data = new BYTE[size];
  29. bool done = true;
  30.  
  31. switch(imgInfo.biBitCount)
  32. {
  33. case 24:
  34. BMP_24_TO_24( pix, data, (W*h) );
  35. }
  36. /**/
  37. if( done )
  38. {
  39. glGenTextures(1, texture);          
  40. glBindTexture(GL_TEXTURE_2D, *texture);            
  41.  
  42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  43. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  44.  
  45. glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGB, W, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  46.  
  47. glBindTexture(GL_TEXTURE_2D, NULL);
  48. }
  49. /**/
  50. delete[] pix;
  51. delete[] data;
  52. /**/
  53. return(True);
  54. }
  55.  

Es probable que la funcion no sea muy elegante , pero deben tener en cuenta que lo que se de programacion lo aprendi de ejemplos y no de manuales xd

Código
  1. void SPRITE::BMP_24_TO_24( BYTE * data, BYTE * new_data, int Size )
  2. {
  3. int index = 0;
  4. do
  5. {
  6. new_data[(index*3)+0] = data[(index*3)+2];
  7. new_data[(index*3)+1] = data[(index*3)+1];
  8. new_data[(index*3)+2] = data[(index*3)+0];
  9. index++;
  10. }while( index < Size );
  11. }
  12.  

Mmmm, para mostrar una imagen de 8 bits, primero la transformare a 24 bits y luego llamare a la funcion BMP_24_TO_24, pero eso me tomara un tiempo, hasta ahora tnego solo esto xd