🔒 Locked
Loading Textures
Advertisement
Original Post
I''m trying to build a texture using a bitmap i loaded from a resource script in VC++, my code looks like this:
BITMAP tex;
loadBmpResource(IDB_STEEL, &tex);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.bmWidth,
tex.bmHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.bmBits);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glEnable(GL_TEXTURE_2D);
/* drawing code goes here */
the thing is... the texture doesn''t show up on the cube i draw, i think it has a problem with the bmBits field in the BITMAP struct, but i can''t see why not. A little help here...
Fooberman~
tough question......
"A cow, now that cuold be useful" - Conker(Conkers BFD for N64)
"A cow, now that cuold be useful" - Conker(Conkers BFD for N64)
First of all, I don''t think that you would want to load your bitmaps from an rc file. Second, I don''t think you can in OpenGL. It will save a lot of headaches if you just use texture objects and manually code the bitmap loading. This is near effortless if you use glaux, but some have a moral objection to it. whatever is easiest for you
Actually, they must only be powers of two if your video card doesn''t support arbitrary dimensions of textures. It''s best to make them powers of two either way, since a lot of people still have Voodoo 3''s which only support powers of two up to 256x256.
[Resist Windows XP''s Invasive Production Activation Technology!]
[Resist Windows XP''s Invasive Production Activation Technology!]
I have the following code to get a texture to sphere map from a resource file.
//----------------------------------------------------------------------------------------------------
// LoadBitmapResource:
//
// Loads a bitmap straight from resources
//----------------------------------------------------------------------------------------------------
BYTE* LoadBitmapResource(int iResource, int* iTextureWidth, int* iTextureHeight)
{
// Find the bitmap resource
HINSTANCE hInstance = GetModuleHandle(NULL);
HANDLE hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(iResource));
// Get the details
BITMAPINFO bmInfo;
GetObject(hBitmap, sizeof(BITMAPINFO), &bmInfo);
DeleteObject(hBitmap);
// Reload and save a the data
hBitmap = LoadResource(hInstance, FindResource(hInstance,MAKEINTRESOURCE(iResource), RT_BITMAP));
BYTE* pTextureImage = (BYTE*)LockResource(hBitmap);
// Shift the pointer on to the image data itself
pTextureImage += sizeof(BITMAPINFO)-1;
// Save the bitmap dimensions
*iTextureWidth = bmInfo.bmiHeader.biWidth;
*iTextureHeight = bmInfo.bmiHeader.biHeight;
return pTextureImage;
}
//----------------------------------------------------------------------------------------------------
// LoadTexture:
//
// Loads bitmaps and converts them to textures
//----------------------------------------------------------------------------------------------------
bool LoadTexture(int iResource, GLuint* piTexture)
{
// Set up a return value and image storage
bool fSuccessful = false;
// Set the data to catch the bmp resource details
BYTE* pTextureImage = NULL;
int iTextureWidth;
int iTextureHeight;
if (pTextureImage = LoadBitmapResource(iResource, &iTextureWidth, &iTextureHeight))
{
glGenTextures(1, piTexture);
// Create Linear Filtered Texture
glBindTexture(GL_TEXTURE_2D, *piTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, iTextureWidth, iTextureHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pTextureImage);
// Mark the success
fSuccessful = true;
}
return fSuccessful;
}
This works for me. I am only doing simple demos so I much prefer to have bmps in resource so I can have a stand alone exe/scr etc.
The full example in which this is used is on my site www.knownoffender.btinternet.co.uk - in the transparentblock demo in the openGL bit. All source is there. Hope this helps.
//----------------------------------------------------------------------------------------------------
// LoadBitmapResource:
//
// Loads a bitmap straight from resources
//----------------------------------------------------------------------------------------------------
BYTE* LoadBitmapResource(int iResource, int* iTextureWidth, int* iTextureHeight)
{
// Find the bitmap resource
HINSTANCE hInstance = GetModuleHandle(NULL);
HANDLE hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(iResource));
// Get the details
BITMAPINFO bmInfo;
GetObject(hBitmap, sizeof(BITMAPINFO), &bmInfo);
DeleteObject(hBitmap);
// Reload and save a the data
hBitmap = LoadResource(hInstance, FindResource(hInstance,MAKEINTRESOURCE(iResource), RT_BITMAP));
BYTE* pTextureImage = (BYTE*)LockResource(hBitmap);
// Shift the pointer on to the image data itself
pTextureImage += sizeof(BITMAPINFO)-1;
// Save the bitmap dimensions
*iTextureWidth = bmInfo.bmiHeader.biWidth;
*iTextureHeight = bmInfo.bmiHeader.biHeight;
return pTextureImage;
}
//----------------------------------------------------------------------------------------------------
// LoadTexture:
//
// Loads bitmaps and converts them to textures
//----------------------------------------------------------------------------------------------------
bool LoadTexture(int iResource, GLuint* piTexture)
{
// Set up a return value and image storage
bool fSuccessful = false;
// Set the data to catch the bmp resource details
BYTE* pTextureImage = NULL;
int iTextureWidth;
int iTextureHeight;
if (pTextureImage = LoadBitmapResource(iResource, &iTextureWidth, &iTextureHeight))
{
glGenTextures(1, piTexture);
// Create Linear Filtered Texture
glBindTexture(GL_TEXTURE_2D, *piTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, iTextureWidth, iTextureHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pTextureImage);
// Mark the success
fSuccessful = true;
}
return fSuccessful;
}
This works for me. I am only doing simple demos so I much prefer to have bmps in resource so I can have a stand alone exe/scr etc.
The full example in which this is used is on my site www.knownoffender.btinternet.co.uk - in the transparentblock demo in the openGL bit. All source is there. Hope this helps.
Thanks, that helps, now i can see what i''m doing differently. I did get it to work, sorta. Instead of calling glTexImage2D i call the equivalent function that makes mipmap textures, and it works. I think maybe there''s a bug somewhere, compiler, dll file, something, cause they take the two functions take the same data, right?
Chris~
Chris~
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement