PDA

View Full Version : How use Bitmap object for texture mapping



Usman Ali
04-22-2010, 04:53 AM
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

Jacek Nowak
04-22-2010, 07:08 AM
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[texturewidth*textureheight*3]; // 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)

Usman Ali
04-22-2010, 11:20 PM
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::Image LockMode::ReadOnly,System::Drawing::Imaging::Pixel Format::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

Fugitive
04-23-2010, 04:56 AM
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.

Usman Ali
04-26-2010, 05:58 AM
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::Format32bpp Argb);

array<Byte> ^data = gcnew array<Byte>(bmp->Width*bmp->Height * 4);
System::Runtime::InteropServices::Marshal::Copy(bi tmapData->Scan0,data,0 ,bmp->Width*bmp->Height * 4);
glGenTextures(1, &amp;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,&amp;data);
bmp->UnlockBits(bitmapData);
delete(bmp);

Usman Ali
04-27-2010, 09:08 AM
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::Format32bpp Argb);
glGenTextures(1, &amp;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/