SOIL_load_OGL_texture_from_memory?

Started by
1 comment, last by Rectangle 11 years, 8 months ago
In my OpenGL app, I'm trying to get SOIL to load a bitmap image from an embedded resource. Here is my code so far:

[source lang="cpp"]bool GetBitmapBitsFromResource(int id, unsigned char** buffer, int* bufferLength)
{
HBITMAP hBmp = (HBITMAP)::LoadBitmap( (HINSTANCE)::GetModuleHandle(NULL), MAKEINTRESOURCE(id) );
if(!hBmp) return false;

HDC hDC = CreateCompatibleDC(NULL);
if(!hDC) {
DeleteObject(hBmp);
return NULL;
}

HGDIOBJ oldObj = SelectObject(hDC, hBmp);
unsigned char* lpvBits = NULL;
BITMAPINFO bi;

ZeroMemory(&bi.bmiHeader, sizeof(BITMAPINFOHEADER));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

if ((lpvBits = new unsigned char[bi.bmiHeader.biSizeImage]) == NULL) return false;
if (!GetDIBits(hDC, hBmp, 0, bi.bmiHeader.biHeight, lpvBits, &bi, DIB_RGB_COLORS)) return false;
*bufferLength = sizeof(unsigned char) * bi.bmiHeader.biSizeImage;
*buffer = lpvBits;

SelectObject(hDC, oldObj);
DeleteObject(hBmp);
DeleteObject(hDC);
return true;
}

bool Mesh::ApplyTextureFromResource(int id)
{
unsigned char* buffer = NULL;
int bufferLength = 0;
if(!GetBitmapBitsFromResource(id, &buffer, &bufferLength)) return false;
textureObject = SOIL_load_OGL_texture_from_memory(buffer, bufferLength, SOIL_LOAD_RGB, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);
glBindTexture(GL_TEXTURE_2D, textureObject);
return true;
}[/source]

But for some reason, SOIL_load_OGL_texture_from_memory keeps returning 0.
Perhaps I'm not loading it properly? Can anyone tell me what's wrong, or if there is a better way of doing this?
Advertisement
Oddly enough, I can't even find too many uses of this function in web searches. Most people seem to be happy with SOIL_load_OGL_texture instead, which doesn't appear to be an option for me since I need to keep my resources embedded in my project. And there doesn't seem to be any real documentation for SOIL, other than the short "Usage" section on their main page, which doesn't go into any real detail.

If anyone can offer an alternative, I'll take it. I just need to import a one-part, static textured mesh into opengl with as little hassle as possible so that I can move on back to my main project again.
EDIT: Well I just came across the Open Asset Import Library, which looks incredibly promising and can load my mesh flawlessly. I'm gonna test it out and try to find a way to load it from a resource into this lib.
Ok well, that was a piece of cake by comparison to everything else i've been doing over the weekend.
I opened up the SimpleOpenGL sample from the Assimp SDK, imported a binary resource of type "RCDATA" (just a simple 3ds model), then added the following lines to the app:
[source lang="cpp"]#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#pragma comment(lib, "user32.lib")

int loadasset (int id)
{
HRSRC myResource = ::FindResource(NULL, MAKEINTRESOURCE(id), RT_RCDATA);
unsigned int myResourceSize = ::SizeofResource(NULL, myResource);
HGLOBAL myResourceData = ::LoadResource(NULL, myResource);
char* pMyBinaryData = (char*)::LockResource(myResourceData);

scene = aiImportFileFromMemory(pMyBinaryData, myResourceSize, aiProcessPreset_TargetRealtime_MaxQuality, "3ds");

if (scene) {
get_bounding_box(&scene_min,&scene_max);
scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
scene_center.z = (scene_min.z + scene_max.z) / 2.0f;
return 0;
}
return 1;
}[/source]
Works like a charm, and it's incredibly fast. I am quite pleased!

This topic is closed to new replies.

Advertisement