I've ran into some problems when creating OGL textures from Allegro bitmaps. For the classic 2D texture I'm currently using this code which runs fine:
ALLEGRO_BITMAP *bitmap = al_load_bitmap(path); GLuint texName = al_get_opengl_texture(bitmap);
After that I can just use texName everywhere I want to use GL_TEXTURE2D. But, I also want to use cubemap textures, which are essentially 6 2D textures. Therefore to load it I have a function that loads single bitmap containing all six cube faces and creates a GL_TEXTURE_CUBE_MAP from it:
This code works, but produces garbage. My guess is wrong data formats. I've been also taking a look at ogl_bitmap.c in allegro sources. So, essentially two questions are at hand:
1. What am I doing wrong.
2. Is there a better way of doing this, i.e., creating a cubemap texture from bitmap/bitmaps?
There is no ->format or ->memory. You need to lock the bitmap with al_lock_bitmap to the format you want.
Allright, seems I haven't understood bitmap locking. This time I think I have everything correct, but it still produces garbage:
The first cubemap face (positive x) is full of garbage (see attached image below). Inspecting mBitmap and face in debugger provides following interesting bits for the first iteration:
mBitmap: format: 9 pitch: 512 locked: true lock_x: 0 lock_y: 0 lock_w: 128 lock_h: 128 lock_flags: 1 face: format: 17 pitch: -512 pixel_size: 4
{"name":"606868","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/1\/91c186a55a15f92dcbb50e7dd80df6c4.png","w":646,"h":507,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/1\/91c186a55a15f92dcbb50e7dd80df6c4"}
Try face>data + face->pitch * w.
This did the trick:
(face->data + (int)w * face->pitch)
Thanks for help. One more question though, should I understand it that the actual pixel data are prepended by pitch * height of something else?
The only thing I need to solve now, is that apparently each face gets one line from the previous one accidentally. Anyway cookies
No, face->data simply points to the beginning of the last row (because in Allegro bitmaps row 0 is the top-most row while in OpenGL textures row 0 is the bottom-most row).
The correct and only documented way of accessing the data is row-by-row, so face->data + row * face->pitch where row can go from 0 to (h-1). Since in your case you know that the data is an OpenGL texture it's not a horrible hack to just access the whole memory I'd say.
Well, yeah. That also explains the pitch being negative.