posted April 17, 2006 02:25 PM
There are 2 ways of achieving this depending on how strict your requirements are:1. Obtaining the exact contents of the framebuffer
You can do this by using HOOPS IM to register a callback at the 'finish picture' callback point (see the IM documentation), and from within that callback, call glReadPixels to query the contents of the frame buffer. The reason your attempt at glReadPixels failed is most likely due to the fact that no current OpenGL context was active at the time of the call. However, an OGL context is active during the HOOPS immediate-mode rendering logic, which is what the various IM callbacks interrupt.
2. Using HOOPS to render to an offscreen OGL frame buffer, and querying that.
This is a little more elegent than #1 since it uses HOOPS' official offscreen OGL-rendering support, but because the 'virtual' offscreen OGL context might have different capabilities that the on-screen one, there is a risk of the image not exactly matching what is visible on-screen. Some sample code:
static const char* extra_pointer_format(void)
{
char buffer[4096];
sprintf (buffer, "%p", (void*) buffer);
if (buffer[0] == '0' && buffer[1] == 'x')
return "";
else
return "0x";
}
void create_ogl_based_image()
{
HC_KEY image_key;
char tempstr[1024];
int w = 1024, h = 1024;
char const *imageDriver = "?driver/opengl/image";
HC_Open_Segment("/my_image");
HC_Set_Visibility("off");
image_key = HC_KInsert_Image (0.0, 0.0, 0.0, "rgb, name = bnc", w, h, null);
HC_Close_Segment ();
HC_Open_Segment (imageDriver);
/* give the key to the image, let the driver know that the key is in fact an image */
sprintf (tempstr, "use window id = %s%p, debug = %d",
extra_pointer_format(), (void*)image_key,
Debug_USE_WINDOW_IS_IMAGE);
HC_Set_Driver_Options (tempstr);
/* the offscreen render should be isolated from the main window */
HC_Set_Driver_Options ("isolated, subscreen = (-1, 1, -1, 1)");
/* setup the desired rendering options, making sure the match the options in
the on-screen window */
HC_Set_Rendering_Options("hsra = hardware z buffer");
HC_Set_Window_Frame("off");
/* include the scene information
HC_Read_Metafile("bnc.hmf", ".", "");
HC_Update_One_Display (".");
HC_Close_Segment ();
/* the image now contains the scene and can be queried. We can now delete the driver */
HC_Delete_Segment (imageDriver);
HC_Update_Display ();
}