2011-02-05 86 views
0

我想在Visual C++ .NET 2008中使用OpenGL創建一個帶紋理的立方體。 在使用GLU進行一些Googgling之後,我發現我必須使用gluBuild2DMipmaps。如何正確地將託管位圖轉換爲非託管位圖?

的問題是,我讀使用System ::繪圖:: Bitmap.FromFile()

位圖現在,我如何通過這個管理Bitmap對象gluBuild2DMipmaps,其接受常量無效*參數???

我已經在使用這些代碼嘗試LockBits:

BYTE * data; 
/*ambil raw data*/ 
System::Drawing::Rectangle rect = System::Drawing::Rectangle(0,0,b->Width,b->Height); 
System::Drawing::Imaging::BitmapData^bitmapData; 
    b->LockBits(
     rect, 
     System::Drawing::Imaging::ImageLockMode::ReadWrite, 
     b->PixelFormat ,bitmapData); 

    ::memcpy(data,bitmapData->Scan0.ToPointer(),b->Width * b->Height); 
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, 
        GL_RGB, GL_UNSIGNED_BYTE, data); 

使用和24bpp BMP文件,它停留在與消息System.ArgumentException行lockbit:附加信息:參數無效。

我做錯了什麼?


我剛找到正確的解決方案。這是因爲幾個問題:

  1. 我使用了錯誤的重載函數。看到我如何調用lockBits
  2. 我不是初始化指針dataBitmap
  3. 此外,由於GLuint * texture;是一個託管指針,我必須使用pin_pointer將其轉換爲非託管指針。

謝謝,這是正確的方法:

void CBox::LoadTextureRaw(String^filename, int wrap) 
{ 
    //GLuint texture; 

    System::Drawing::Bitmap^bitmap = gcnew Bitmap(filename); 
    int h = bitmap->Height; 
    int w = bitmap->Width; 
    int s = w * h; 
    dataTexture = new BYTE[s * 3]; 

    System::Drawing::Rectangle rect = System::Drawing::Rectangle(0,0,bitmap->Width,bitmap->Height); 
    System::Drawing::Imaging::BitmapData^bitmapData = 
     bitmap->LockBits(rect,System::Drawing::Imaging::ImageLockMode::ReadWrite , System::Drawing::Imaging::PixelFormat::Format24bppRgb); 

    ::memcpy(dataTexture,bitmapData->Scan0.ToPointer(),s); 


    bitmap->UnlockBits(bitmapData); 
    pin_ptr<GLuint*> pt = &texture;//pin managed pointer, to be unmanaged... asyeeeem 
    **pt = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h,GL_RGB, GL_UNSIGNED_BYTE, dataTexture);  
} 

回答

1

我用下面的代碼複製位圖數據:

Image image = null; 
Bitmap iBitmap = new Bitmap(fs); 
BitmapData iBitmapData; 
GCHandle hImageData; 

if ((iBitmap.Flags & (int)ImageFlags.ColorSpaceRgb) != 0) { 
    switch (iBitmap.PixelFormat) { 
     case PixelFormat.Format1bppIndexed: 
     case PixelFormat.Format4bppIndexed: 
     case PixelFormat.Format8bppIndexed: 
      // Allocate image raster 
      image = new RasterImage<ColorBGR8>(iBitmap.Width, iBitmap.Height); 
      break; 
     case PixelFormat.Format16bppRgb565: 
      // Allocate image raster 
      image = new RasterImage<ColorBGR16>(iBitmap.Width, iBitmap.Height); 
      break; 
     case PixelFormat.Format24bppRgb: 
      // Allocate image raster 
      image = new RasterImage<ColorBGR24>(iBitmap.Width, iBitmap.Height); 
      break; 
     case PixelFormat.Format32bppRgb: 
      // Allocate image raster 
      image = new RasterImage<ColorBGR32>(iBitmap.Width, iBitmap.Height); 
      break; 
     case PixelFormat.Format32bppArgb: 
      // Allocate image raster 
      image = new RasterImage<ColorABGR32>(iBitmap.Width, iBitmap.Height); 
      break; 
     default: 
      throw new Exception("Image RGB pixel format not supported"); 
    } 
} else if ((iBitmap.Flags & (int)ImageFlags.ColorSpaceGray) != 0) { 
    switch (iBitmap.PixelFormat) { 
     case PixelFormat.Format1bppIndexed: 
     case PixelFormat.Format4bppIndexed: 
     case PixelFormat.Format8bppIndexed: 
      // Allocate image raster 
      image = new RasterImage<ColorY8>(iBitmap.Width, iBitmap.Height); 
      break; 
     default: 
      throw new Exception("Image GRAY pixel format not supported"); 
    } 

    if (RenderContext.Caps.TextureSwizzle == false) { 
     throw new Exception("unable to load GRAY pixel format image (ARB_texture_swizzle extension not supported)"); 
    } 
} else if ((iBitmap.Flags & (int)ImageFlags.ColorSpaceYcbcr) != 0) { 
    throw new Exception("Image YCbCr pixel format not supported"); 
} else if ((iBitmap.Flags & (int)ImageFlags.ColorSpaceYcck) != 0) { 
    throw new Exception("Image YCCK pixel format not supported"); 
} else if ((iBitmap.Flags & (int)ImageFlags.ColorSpaceCmyk) != 0) { 
    throw new Exception("Image CMYK pixel format not supported"); 
} else 
    throw new Exception("Image pixel format not supported"); 

// Obtain source and destination data pointers 
iBitmapData = iBitmap.LockBits(new Rectangle(0, 0, iBitmap.Width, iBitmap.Height), ImageLockMode.ReadOnly, iBitmap.PixelFormat); 
hImageData = GCHandle.Alloc(image.GetPixelData(), GCHandleType.Pinned); 

    // Alternative without using custom Image class: 
    //T[,] buffer = new ColorRGB24[iBitmap.Width, iBitmap.Height]; 
    //hImageData = GCHandle.Alloc(buffer, GCHandleType.Pinned); 

// Copy Bitmap data to Image 
memcpy(hImageData.AddrOfPinnedObject(), iBitmapData.Scan0, iBitmapData.Stride*iBitmapData.Height); 

hImageData.Free(); 
iBitmap.UnlockBits(iBitmapData); 

return (image); 

你可以注意到,我不得不的P/Invoke memcpy例程來複制鎖定的內存。

圖片類)在我的項目,它定義了方法GetPixelData(定義的類作爲一個簡單的程序返回一個T [,],其中Ť是像下面

的結構
[StructLayout(LayoutKind.Sequential, Pack = 1)] 
public struct ColorRGB24 : IColorRGB<byte> 
{ 
    public byte r; 
    public byte g; 
    public byte b; 
} 

閱讀以下報價:

Using System.Drawing.Imaging.PixelFormat values, such as Indexed and Gdi, will throw an System.ArgumentException 

因此,請確保沒有索引Gdi像素格式值。

+0

代碼看起來是一樣的,期望我調用一個不同的LockBits重載方法。 – Luca 2011-02-06 10:06:34

相關問題