iDevGames Forums
OpenGL Texture from Char* Buffer - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenGL Texture from Char* Buffer (/thread-1287.html)



OpenGL Texture from Char* Buffer - gradyeightythree - May 15, 2009 08:26 PM

I'm not sure if anyone here has read Game Coding Complete by Mike McShaffry (it deals with a lot of Win32/DirectX specific issues) but it lays out a solid framework for a game engine using c++. I decided it would be a fun project to (attempt to) port to Mac OS X/OpenGL using mostly c++ with a little obj-c and I have a question:

He uses a resource cache that loads game assets from zip files (using zlib), storing the contents of each individual file (whether it be a .bmp or a .jpeg or a .wav or whatever) in a char* buffer. This is very useful under Windows/DirectX as there is a Direct3D CreateTextureFromMemory() function that takes the char* buffer and spits out a D3D texture.

I'm trying to figure out the best way to go about doing this on a Mac for an OpenGL texture. I tried the following:

Code:
    CGDataProviderRef    pData;
CGImageRef            pImage;
    
const char* textBuffer = texture->Buffer();
size_t textSize = texture->Size();
    
pData = CGDataProviderCreateWithData(NULL, (char*)textBuffer, textSize, NULL);
pImage = CGImageCreateWithJPEGDataProvider(pData, NULL, false, kCGRenderingIntentDefault);
        
size_t width = CGImageGetWidth(pImage);
size_t height = CGImageGetHeight(pImage);
    
std::cout << "Image width: " << width << std::endl;
std::cout << "Image height: " << height << std::endl;

From there I could get the CGImageRef in to a texture, but it doesn't seem to be reading the char buffer correctly as my width and height always end up being 0.

Does anyone have any suggestions/ideas? Thanks a lot!