Hi,
I have used Allegro 5 to setup things for OPENGL and am now quite happily bouncing a sphere, cylinder and flat surface around the screen..
What I now want to do (and not sure if this will kill framerate) is to write to the flat surface texture - for now just drawing a rectangle with Allegro 5 functions (guess I'd need to al_get_opengl_texture() again?)...
I can't seem to get this to work - guess I'm screwing up the matrix stack in some way.
So.. I tried saving the (GL_MODELVIEW_MATRIX) stack and restoring it - but again no luck.
Can anyone give me a few pointers please?
Kev
You should be able to draw normally to your surface. Each bitmap stores its own transformations. al_set_target_bitmap should take care of restoring its own transformations.
Show more code. Like your draw_opengl_objects function.
EDIT
The problem may be with these two lines :
The call to LoadMatrix may be overwriting a matrix set by al_set_target_bitmap.
Here's a simplified version..
In effect as soon as I mess around using al_set_target_bitmap() to the bitmap I want updated, the display gets corrupted in some way.
I thought saving and restoring the matrix stack(s) may help as imagine calling
al_set_target_bitmap() and al_draw_rectangle() use opengl so would interfere with the opengl bits in some way.
and for completeness -
Like I said, your call to LoadMatrix may be overwriting the projection view matrix that opengl uses. You're not setting it at all, so it's being changed by allegro, but not set back to normal by you.
Also, you can use allegro's transforms to accomplish the same thing.
Sorry I'm confused a bit
I'm not using 'LoadMatrix' ..
Do you mean glLoadIdentity()..?
Apologies if I'm being dumb, opengl is new to me.
I'd like to see if I can continue using opengl (as I wanted to understand it a bit better), but may well decide to stick with Allegro.
Edit:
Ah..re-read your comment again..
I'm not setting something up that I should be..?
I have setup my projection matrix earlier; but I need to reset it again if I use allegro functions?? Hadn't imagined that stack would have been touched.
Yes, I believe allegro uses an orthographic projection matrix to draw to the back buffer. Each bitmap has two matrices associated with it.
Edit - can you post a zip of your code? I'd like to play around with it a bit.
Edit - at the very least you need to reset your model view and projection matrices after using allegro.
No problem, I won't be able to till Monday unfortunately.
Will look at re-setting up the projection matrix then and see what happens..
Resetting both matrices should fix your problem. They work in tandem. The MODELVIEW matrix takes care of moving the 'camera' and the PROJECTION matrix takes care of defining the view volume.
So something like :
/// Setup your camera with gluLookAt glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /// gluLookAt /// Setup an orthographic or perspective projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); /// glFrustum /// glOrtho
See https://www.opengl.org/archives/resources/faq/technical/viewing.htm for some details.
Still no luck 
- source attached
As soon as I call al_set_target_bitmap() [line 146] everything goes awry - re-setting up the cameras has no benefit.
May have to delve again into library code to see/understand what's happening - suspect my knowledge of how the opengl pipeline works isn't helping.
I figured out the problem. In addition to resetting the model view and projection matrices, you need to set the bitmap drawing target to the backbuffer again. You're just drawing to the bitmap. That's why the results never show up.
EDIT
I haven't been able to successfully modify the texture yet though... there are some strange results when I draw to the bitmap.
Well that makes (pretty obvious) sense, will mess around a bit with it tomorrow. Thanks.
Thought I'd tried this, but probably without the resetting of the projection, etc..
Update -
Well that's an improvement I think - I'm drawing rectangle(s) on the target and it's displaying it correctly when the camera is face-on.. [Update 2 - Actually not too sure how many rectangles are being drawn.. maybe only the first one - all very weird.]
2 very strange things though -
. When the camera gets at an acute angle, the bitmap gets a sheen whereby you can see the original bitmap underneath with the rectangles fading on top. Maybe I'm seeing some default OPENGL lighting??
. Calling al_clear_to_color() after setting the target bitmap to the texture, fills the whole screen with colour rather than just the target.
I'm thinking at the moment that A5 and OpenGL don't work together as easily as I'd hoped
Maybe I'm just not hitting the right buttons in the right order.
EDIT:
Not that anyone seems very interested
- but I've fixed this by rewriting everything.
Once it's tidied up I'll post the code here in case anyone in future has issues.
Don't forget to use 'send to top' when you make big edits like that. Something I see is that you're setting and loading the model view identity matrix twice. It's just extra code that doesn't do anything.
As for clearing to color on the bitmap affecting the screen, I think there's something funny going on with the allegro part of things. I don't think it's drawing to the right texture target. It seems to draw fine to it's own bitmap, but not to the associated texture in my test cases. I'll keep investigating this when I have time. I made a post to [AD] about this, but no reply yet.
Thanks for the help Edgar.
Will try to post my updated source tomorrow, I virtually started from scratch after what I'd learnt from your input. I now have a 'draw' loop that does some A5 2d, then some opengl and then more 2d before I flipscreen() ..
I'm not in any way saying I'm doing it the correct way but it's looking hopeful..!
UPDATE - Latest Code - Draws 10 cubes, all of same size, moves the camera to a new viewing position and displays some text on-screen. Seems way better mixing allegro and opengl now.
Problem I now have (and I'm sure I've set something up incorrectly) is that near objects are smaller than distant objects.. Any idea on why this would happen? I'm guessing it's something to do with the way I've setup the depth buffer or something along those lines.
UPDATE - SOLVED 
Required - al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_SUGGEST);
Did you get your program to correctly draw to a texture and display the updated texture?
Please post the entire code in a zip so I can play around with it again.
Yes I did
still more tidying up to do as I get to understand a little more what the issues are and what doesn't have to be reset each frame...
Unfortunately I don't have access to my PC, so here's a link to the zip I just created on my mobile.
https://drive.google.com/file/d/0B1W53uB-s4sDRHpSSnlwcVZVUEE/view?usp=drivesdk
Hopefully that will work.
DOH. It doesn't work because your images aren't included with the zip. Or the font. I just replaced your resources with made up stuff and it works now.
Ah yeah sorry, thought you had those already
Seems to be way better, don't know why I cant place text 'behind' 3d objects using allegro's functions, but could obviously work around that.
Also i guess a fair bit of optimisation is possible as I'm calling a lot every frame, would imagine some of its not required apart from at initialisation.
don't know why I cant place text 'behind' 3d objects using allegro's functions
You can, you just have to draw it at the proper depth, either using z values, or a z translation matrix. Of course it will shrink into the background, so you have to draw it at 'real size'. You can also fake it by drawing your scene onto a buffer and then drawing your text on screen and your buffer on top of it.
Yeah know I can use an interim texture to do it.. (seems wasteful though) .
Do you mean using allegro's transform functions..?
Sure, you can draw it farther away at a larger scale, with OpenGL (the MODELVIEW matrix) or Allegro (a scale transform).
Alternatively, you can draw your scene with an orthographic transform, but then everything the same size will be the same size regardless of distance.
Perhaps you could combine drawing your scene in perspective projection and your text with orthographic projection and thereby 'cheat'. Worth a try anyway.