This project is read-only.

About TexImage2D!

Jan 18, 2012 at 4:12 PM
Edited Jan 18, 2012 at 4:12 PM

 

 private bool LoadBmpFile( string filename )
{
    //Bitmap img;
    Imga = null;
    Scan0 = IntPtr.Zero;
    Imga = (Bitmap)Image.FromFile(filename);
    if (Imga != null)
    {
        BitmapData bmData;
        Imga.RotateFlip(RotateFlipType.Rotate180FlipX );
        bmData = Imga.LockBits(new Rectangle(0, 0, Imga.Width, Imga.Height),
                                                 ImageLockMode.ReadWrite,
                                                 PixelFormat.Format24bppRgb);

        Scan0 = bmData.Scan0;
        int ByteNumber_Width = Imga.Width * 3;
        int  hh = Imga.Height;
        int ww = Imga.Width;
        int stride = bmData.Stride;
        int ByteOfSkip = stride - ByteNumber_Width;
        Imagedata= new byte[ByteNumber_Width * Imga.Height];
        int ii=0;

        unsafe
        {
            byte* p = (byte*)(void*)Scan0;

            for (int y = 0; y < hh; y++)
            {
                for (int x = 0; x < ww; x++)
                {
                    Imagedata[ii+2] = p[0];    // B
                    ++p;
                    Imagedata[ii+1] = p[0];        // G
                    ++p;
                    Imagedata[ii] = p[0];        // R
                    ++p;
                    ii += 3;
                }
                p += ByteOfSkip;
            }
        }
        Imga.UnlockBits(bmData);
        return true;
    }
    else
        return false;
}

Above the section is open image bmp file.But i am confused that if i use below line:
 gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0,OpenGL.GL_RGB,
        Imga.Width, Imga.Height, 0, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE,
        bitmapData.Scan0);

I get correct image in opencl control. But if i change below line:
 
gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGB, Imga.Width, Imga.Height, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, ImagePtr);

Then I get wrap image or gray image . I don't know what's wrong. Is the raw data Imagedata not correct or something wrong in TextImage2D?

The signature here:
protected static extern void glTexImage2D(uint target, int level, uint internalformat, int width, int height, int border, uint format, uint type, byte[] pixels);

and i add :
 public void TexImage2D(uint target, int level, uint internalformat, int width, int height, int border, uint format, uint type, byte[] pixels)
        {
            PreGLCall();
            glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
            PostGLCall();
        }