Jump to content
  • Advertisement
Sign in to follow this  

OpenGL SOIL_load_OGL_texture_from_memory?

This topic is 2298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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) {
return NULL;

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

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);
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;

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? Edited by Rectangle

Share this post

Link to post
Share on other sites
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. Edited by Rectangle

Share this post

Link to post
Share on other sites
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) {
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;
Works like a charm, and it's incredibly fast. I am quite pleased! Edited by Rectangle

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!