Ybalrid

Write Ogre GLTexture to ppm file via OpenGL

Jul 14th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. void OgreOculusRender::writeTextureToFile()
  2. {  
  3.     //It's a C style function, yeah...
  4.    
  5.     char path[] = "debug.ppm";
  6.     FILE* debugFile = fopen(path,"w");
  7.     fprintf(debugFile,"P3\n");//PPM ASCII MAGIC NUMBER
  8.    
  9.     //W H size
  10.     fprintf(debugFile, "%d %d\n" ,texSizeL.w, texSizeL.h);
  11.    
  12.     //Compute the biggest nomber an unsigned int cat represent on this computer :
  13.     unsigned int  max = 0;
  14.     max--;
  15.  
  16.     //MAX SUBPIXEL VALULE
  17.     fprintf(debugFile, "%u\n", 255);
  18.    
  19.     size_t arraySize =  texSizeL.w*texSizeL.h;
  20.     //Cast because C++ will not like affecting a void* on a int*
  21.     unsigned int* rIntArray = static_cast<unsigned int*>(malloc(sizeof(unsigned int) * arraySize));
  22.     unsigned int* gIntArray = static_cast<unsigned int*>(malloc(sizeof(unsigned int) * arraySize));
  23.     unsigned int* bIntArray = static_cast<unsigned int*>(malloc(sizeof(unsigned int) * arraySize));
  24.    
  25.     GLuint tex = static_cast<Ogre::GLTexture*>(Ogre::GLTextureManager::getSingleton().getByName("RttTexL").get())->getGLID();
  26.    
  27.     glBindTexture(GL_TEXTURE_2D, tex);
  28.    
  29.     //I'm not searching to do this right, I just want to get the data in any format I can...
  30.     glGetTexImage(GL_TEXTURE_2D,0, GL_RED,GL_UNSIGNED_INT, static_cast<GLvoid*>(rIntArray));
  31.     glGetTexImage(GL_TEXTURE_2D,0, GL_GREEN, GL_UNSIGNED_INT, static_cast<GLvoid*>(gIntArray));
  32.     glGetTexImage(GL_TEXTURE_2D,0, GL_BLUE, GL_UNSIGNED_INT, static_cast<GLvoid*>(bIntArray));
  33.  
  34.     //Convert to a 255 max value
  35.     for(int i = 0; i < arraySize; i++)
  36.     {  
  37.         rIntArray[i] =(int) (((float)rIntArray[i]/(float)max)*(float)255);
  38.         gIntArray[i] =(int) (((float)gIntArray[i]/(float)max)*(float)255);
  39.         bIntArray[i] =(int) (((float)bIntArray[i]/(float)max)*(float)255);
  40.     }
  41.    
  42.     for(int i = 0; i < arraySize; i++)
  43.     {  
  44.         fprintf(debugFile,"%u %u %u ",rIntArray[i], gIntArray[i], bIntArray[i]);
  45.     }
  46.    
  47.     free(rIntArray);
  48.     free(gIntArray);
  49.     free(bIntArray);
  50.    
  51.     fclose(debugFile);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment