print screen in OpenGL

Hello everybody

I want to print the image rendered by OpenGL and save it in a file. Is there any function in OpenGL to do that? the function should work as if we use the print screen button.
I found a code for (glgrab) but it uses few undefined classes.
this is the link for glgrab:

http://code.google.com/p/telekinesis/source/browse/trunk/Mac/Source/glgrab.c?r=140#

.

Check out glReadPixels(…). It allows you to grab the color values from the framebuffer according to the current state. Then you can write the image in whatever format you want.

how to save a print screen in a vectorial format? (like eps, pdf, emf, or other)

That is simple - you don’t.

i was think about taking the position of each vertex in the 2D rastered image and draw the faces/edges along with the topology info. Sort of drawing straight lines for the edges with the vectorial file syntax and doing the same for the faces. We would probably need the info about the pixel position of the vertices and the acknowledgment of which triangles are drawn. What you think about it?

Thanks for replies

I am trying to use glReadPixels to read 300 X 300 pixels from the front buffer(i am not sure if that is correct) and I want to see them on my monitor using glDrawPixels. but I get a consol mesage says:

GLUT: Warning in d:\Documents and Settings…\Desktop…\Release(my application name).exe: The following is a new check for GLUT 3.0; update your co
de.
GLUT: Fatal Error in d:\Documents and Settings…\Desktop…(application name)\Release???.exe: redisplay needed for window 1, but no display callback.
Press any key to continue . . .

Can any one tell me what is the problem with using both of them and how to read a block of pixels from the scene and save them together with other blocks and finally display them all together in one image?
It is a vital task for me. I appreciate your help so much.

[b]
int width_p = 300;
int height_p = 300;

float* pixels_ptr = new float[3*(width_p)*(height_p)];

int main(int argc, char** argv) {
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	
glutInitWindowSize(1024, 768);
glutCreateWindow("?????????");
initRendering();
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glRasterPos2i(0,0);
glReadBuffer(GL_FRONT);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
	
glReadPixels(0,0,width_p,hight_p,GL_RGB,GLUT_DOUBLE, pixels_ptr);
glDrawPixels(width_p,hight_p,GL_RGB,GLUT_DOUBLE, pixels_ptr);

//glCopyPixels(0,0, width_p, height_p,GL_COLOR);

glutPostRedisplay();
glutMainLoop();
return 0;

}[/b]

When you specify functions like glutKeyboardFunc or glutReshapeFunc you are providing callbacks; GLUT calls those functions when something important happens. The error you get is telling you there is no display callback - you never specified a function that does display, so glutMainLoop() can’t call it. That function is specified with glutDisplayFunc.

Many thanks, that is very useful. I hope I understood your idea correctly. I used glutDisplayFunc to call the function (RenderMyFunction). I got an empty black screen. Any suggestion?
[b]
void RenderMyFunction()
{

int width_p = 300;
int height_p = 300;

float* pixels_ptr = new float[3*(width_p)*(height_p)];

glRasterPos2i(0,0);
glReadBuffer(GL_FRONT);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
//glCopyPixels(0,0,width_p,height_p,GL_COLOR);

glReadPixels(0,0,width_p,height_p,GL_RGB,GL_UNSIGNED_BYTE, pixels_ptr);
glDrawPixels(width_p,height_p,GL_RGB,GL_UNSIGNED_BYTE, pixels_ptr);
glutPostRedisplay();
glutSwapBuffers();
}

int main(int argc, char** argv) {
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);		

glutInitWindowSize(1024, 768);



glutCreateWindow("?????");

initRendering();
//glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);


glutDisplayFunc(RenderMyFunction);
//glutTimerFunc(25, update, 0);


glutMainLoop();
return 0;

}[/b]

Do you ever actually draw anything? All the code here does is read from the buffer and draw it back. If there’s nothing there to begin with, then nothing ever gets drawn.