How use Bitmap object for texture mapping

In Opengl most of texture mapping examples use LoadBitmap() function with image name or path
id_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 object

System::Drawing::Bitmap bmp=gcnew System::Drawing::Bitmap();


LoadBitmap(bmp);? i need this function implementation

Any one help me.


http://ashscholar.com

I don’t use VC++.Net or Bitmaps so I can’t help you directly, but try downloading some OpenGL BMP loading code, delete everything related to loading data from file, leaving only glTexImage2D and glTexParameteri(f) calls.

After that, figure out how to extract pixel data from Bitmap object. OpenGL has no idea what bitmaps are, it only knows how to process pixel arrays (similarly stored as in BMP). BMP has no alpha channel, so the pixel data array will be declared as:

GLuchar* pixels = new GLuchar[texturewidthtextureheight3]; // 3 for RGB

Anyway, this is C++ syntax, I don’t know how exactly memory allocation works in VC++.NET (from your post it seems it’s garbage collected)

int LoadBitmapObject(Bitmap ^bmp)
{

System::Drawing::Rectangle rectangle(0, 0, 256, 256);
System::Drawing::Imaging::BitmapData ^bitmapData;
num_texture++;

bitmapData=bmp>LockBits(rectangle,System::Drawing::Imaging::ImageLockMode::ReadOnly,System::Drawing::Imaging::PixelFormat::Format24bppRgb);
glBindTexture(GL_TEXTURE_2D, num_texture);

glTexImage2D(GL_TEXTURE_2D, 0, (int)GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, &bitmapData->Scan0);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
bmp->UnlockBits(bitmapData);

return (num_texture); 

}

but i get an error message saying:
“AccessViolationException was unhandled”,…
“Tried to read or write on protected memory,…”
Any one tells me whats problem

I dont use .net, so take my advice with a grain of salt.

I can tell that the .net bmp object is not a simple array of pixels, but rather a class/structure that contains other useful information such height/width/format etc. This is usually the ‘header’ info.

OpenGL, on the other hand, only expects a raw array of pixels, and in an exact pre-specified format. I don’t think &bitmapData->Scan0 is the address of the first pixel in the array. A .net bitmap may not even store data contiguously in memory.

If it does have an array of contiguous pixels, then you simply need a pointer to it (as you tried to do already).

Also, perhaps you don’t want to lock the surface before passing it on as a pointer. Its probably why you are getting a memory access violation.

I use this code and now code is running without exception but problem is that its not show image,Plz help me any things missing
whats the problem.

System::Drawing::Rectangle rectangle(0, 0, bmp->Width,bmp->Height); // The rectangle for locking the Bitmap in memory
System::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->Widthbmp->Height * 4);
System::Runtime::InteropServices::Marshal::Copy(bitmapData->Scan0,data,0 ,bmp->Width
bmp->Height * 4);
glGenTextures(1, &texture[0]); // Create The Texture
glBindTexture(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 function
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
glTexParameterf(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);

Thanks for all member for help,now code work here is function for it

bool LoadBitmapObject(Bitmap ^bmp)
{
bmp->RotateFlip(RotateFlipType::RotateNoneFlipY);
System::Drawing::Rectangle rectangle(0, 0, bmp->Width,bmp->Height); // The rectangle for locking the Bitmap in memory
System::Drawing::Imaging::BitmapData ^bitmapData;
bitmapData = bmp->LockBits(rectangle, System::Drawing::Imaging::ImageLockMode::ReadOnly, System::Drawing::Imaging::PixelFormat::Format32bppArgb);
glGenTextures(1, &texture[0]); // Create The Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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_MIPMAP_NEAREST); //The minifying function
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don’t combine the color with the original surface color, use only the texture map.
glTexImage2D(GL_TEXTURE_2D, 0, (int)GL_BGRA_EXT,bmp->Width,bmp->Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,(void *) bitmapData->Scan0);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, bmp->Width, bmp->Height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (void *) bitmapData->Scan0);
bmp->UnlockBits(bitmapData);
delete(bmp);
return true; // Returns the current texture OpenGL ID
}

http://www.usmanweb.com/