Advertisement
Guest User

demo controls

a guest
Feb 22nd, 2011
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1.  
  2. void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message)
  3. {
  4.     std::cout<<"FreeImage error ("<<FreeImage_GetFormatFromFIF(fif)<<"): "<<message<<"\n";
  5. }
  6.  
  7. //check std extension
  8. HRESULT CTexture::LoadTextureFromOtherFormat(e3d_str FileName)
  9. {
  10. //  try
  11.     {
  12.         e3d_Std(ES"Trying to load image with FreeImage: "+ES FileName);
  13.         FreeImage_Initialise(true);
  14.         FreeImage_SetOutputMessage(&FreeImageErrorHandler);
  15.         //FIMEMORY *mem = 0;
  16.         FIBITMAP *img = 0;
  17.  
  18.         FREE_IMAGE_FORMAT fmt=FreeImage_GetFileTypeU(FileName.buf);
  19.  
  20.         img = FreeImage_LoadU(fmt,FileName.buf);
  21.         if (img == 0)
  22.         {
  23.             e3d_Warn(ES"Unable to load image, FreeImage_LoadFromMemory failed");
  24.             return E_FAIL;
  25.         }
  26.            
  27.  
  28.         FIBITMAP *newImg = FreeImage_ConvertTo32Bits(img);
  29.         if (newImg == 0)
  30.         {
  31.             e3d_Warn(ES"Unable to convert image, FreeImage_ConvertTo32Bits failed");
  32.             return E_FAIL;
  33.         }
  34.         FreeImage_Unload(img);
  35.  
  36.         img = newImg;
  37.         newImg = 0;
  38.  
  39.         typedef UINT uint;
  40.         typedef unsigned char uint8;
  41.  
  42.         uint pitch = FreeImage_GetPitch(img);
  43.         uint height = FreeImage_GetHeight(img);
  44.         uint width = FreeImage_GetWidth(img);
  45.         uint8 *rawBuf = (uint8*)CoreAlloc(width * height << 2);
  46.  
  47.         FreeImage_ConvertToRawBits(rawBuf, img, pitch, 32,
  48.             FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true);
  49.  
  50.        
  51.         // We need to convert pixel format a little
  52.         // NB: little endian only - I think(!)
  53. #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
  54.         for (uint i = 0; i < height; ++i)
  55.         {
  56.             for (uint j = 0; j < width; ++j)
  57.             {
  58.                 uint p = *(((uint*)(rawBuf + i * pitch)) + j);
  59.                 uint r = (p >> 16) & 0x000000FF;
  60.                 uint b = (p << 16) & 0x00FF0000;
  61.                 p &= 0xFF00FF00;
  62.                 p |= r | b;
  63.                 // write the adjusted pixel back
  64.                 *(((uint*)(rawBuf + i * pitch)) + j) = p;
  65.             }
  66.         }
  67. #endif
  68.  
  69.         FreeImage_Unload(img);
  70.         img = 0;
  71.  
  72.         //create tex2D
  73.         D3D11_TEXTURE2D_DESC tex_desc;
  74.         //ZeroMemory(&tex_desc, sizeof(tex_desc));
  75.         tex_desc.Width = width;
  76.         tex_desc.Height = height;
  77.         tex_desc.ArraySize = 1;
  78.         tex_desc.SampleDesc.Count = 1;
  79.         tex_desc.SampleDesc.Quality = 0;
  80.         tex_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  81.         tex_desc.Usage = D3D11_USAGE_DEFAULT;
  82.         tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET|D3D11_BIND_SHADER_RESOURCE;
  83.         tex_desc.CPUAccessFlags = 0;
  84.         tex_desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
  85.         tex_desc.MipLevels = 0;
  86.  
  87.         this->CreateFromDesc(&tex_desc,e3d_Tex2D,NULL,NULL);
  88.         devc11->UpdateSubresource(TexRes,D3D11CalcSubresource(0,0,0),NULL,rawBuf,4 * tex_desc.Width,0);
  89.         CoreFree(rawBuf);
  90.  
  91.         e3d_T2D ldesc;
  92.         this->GetDesc(&ldesc,e3d_Tex2D);
  93.  
  94.         D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
  95.         srvd.Format = ldesc.Format;
  96.         srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  97.         srvd.Texture2D.MostDetailedMip = 0;
  98.         srvd.Texture2D.MipLevels = ldesc.MipLevels;
  99.         this->CreateFromDesc(&srvd,e3d_TexSRV);
  100.         devc11->GenerateMips(SRV);
  101.  
  102.         FreeImage_DeInitialise();
  103.  
  104.     }
  105.  
  106.     return S_OK;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement