   How use Bitmap object for texture mapping
  In Opengl most of texture mapping examples use LoadBitmap() function with image name or pathid_texture=LoadBitmap("spaceshiptexture.bmp"); but i want to use vc++.net Bitmap object for it,I have bitmap object at runtime and pass into opengl for texture Mapping any function can i use instead of image path use Bitmap objectSystem::Drawing::Bitmap bmp=gcnew System::Drawing::Bitmap();........LoadBitmap(bmp);? i need this function implementationAny one help me.______________________Student Website[Edited by - usman1266 on April 22, 2010 9:54:02 AM]
  I'm not even aware that there is such a function as "LoadBitmap" in openGL, it sounds like a wrapper function from some library you might have. Look at the source of LoadBitmap and that will show you how.Otherwise, to load a texture to opengl you just send the pixel data through glTexImage2D. It doesn't matter what image format it comes from, you have to decode the image format into an array of RGB (or whatever format you want) values. Looks something like this:glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, 3, imagewidth, imageheight, 0, GL_RGB, GL_UNSIGNED_BYTE, imagedata);You can read the doc on these functions to understand what they do.;  Have a look at my texture loader here (C# & OpenTK).   ;  int LoadBitmapObject(Bitmap ^bmp) { System::Drawing::Rectangle rectangle(0, 0, 256, 256);System::Drawing::Imaging::BitmapData ^bitmapData;num_texture++; // The counter of the current texture is increasedbitmapData = bmp->LockBits(rectangle, System::Drawing::Imaging::ImageLockMode::ReadOnly, System::Drawing::Imaging::PixelFormat::Format24bppRgb);    glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameterarray<Byte> ^data = gcnew array<Byte>(256 * 256  * 3);System::Runtime::InteropServices::Marshal::Copy(bitmapData->Scan0,data,0 ,256 * 256  * 3);bmp->UnlockBits(bitmapData);// Finally we define the 2d textureglTexImage2D(GL_TEXTURE_2D, 0, (int)GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE,  &data); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //The minifying function return (num_texture); // Returns the current texture OpenGL ID}but i get an error message saying:"AccessViolationException was unhandled",... "Tried to read or write on protected memory,..."Any one tells me whats problem;  You have to generate a texture with glGenTextures before you bind it.   ;  Still give same error on glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // this line during debuging   ;  You provide a RGB bitmap :array<Byte> ^data = gcnew array<Byte>(256 * 256 * 3);and you tell Opengl it's RGBA :glTexImage2D(GL_TEXTURE_2D, 0, (int)GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);My advice, stick to RGBA format.PS: Why don't you use the Bitmap Size property instead of hardcoding the size ?   ;  I am trying this code glTexImage2D(GL_TEXTURE_2D, 0, 3, imagewidth, imageheight, 0, GL_RGB, GL_UNSIGNED_BYTE, imagedata);still same problem"AccessViolationException was unhandled",... "Tried to read or write on protected memory,...";  Keep the alpha component, so your data are 32bitspublic void Blit(Bitmap bitmap){Imaging.BitmapData bmdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb);byte[] data = new byte[bitmap.Size.Width * bitmap.Size.Height * 4];Marshal.Copy(bmdata.Scan0, data, 0, bitmap.Size.Width * bitmap.Size.Height * 4);bitmap.UnlockBits(bmdata);   // Bind the texture here   GL.BindTexture(###);GL.TexSubImage2D<byte>(TextureTarget.Texture2D, 0, 0, 0, bitmap.Width, bitmap.Height,PixelFormat.Bgra, PixelType.UnsignedByte,data);};  I use this code and now code is running without exception but problem is that its not show image,Plz tell any things missing System::Drawing::Rectangle rectangle(0, 0, bmp->Width,bmp->Height);// The rectangle for locking the Bitmap in memorySystem::Drawing::Imaging::BitmapData ^bitmapData;bitmapData = bmp->LockBits(rectangle, System::Drawing::Imaging::ImageLockMode::ReadOnly, System::Drawing::Imaging::PixelFormat::Format32bppArgb);array<Byte> ^data = gcnew array<Byte>(bmp->Width*bmp->Height  * 4);System::Runtime::InteropServices::Marshal::Copy(bitmapData->Scan0,data,0 ,bmp->Width*bmp->Height  * 4);glGenTextures(1, &texture[0]);// Create The TextureglBindTexture(GL_TEXTURE_2D, texture[0]);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //The minifying functionglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeatedglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);glTexImage2D(GL_TEXTURE_2D, 0, (int)GL_BGRA_EXT,bmp->Width,bmp->Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,&data);bmp->UnlockBits(bitmapData);  delete(bmp); 