Handout 2

Saving OpenGL Images as .jpg files


Description

The mkOpenGLJPEGImage Class is a C++ class which virtually automates the use of JPEGs in OpenGL Win32 applications. The mkOpenGLJPEGImage class can save the current OpenGL screen to a JPEG file or load a JPEG or BMP into an OpenGL texture object in just 3 lines of code. This class is encapsulated in a Win32 DLL and is distributed as an SDK, complete with documentation and several sample programs with source code.

Downloads

OpenGL graphics can be saved as .jpg files. Tools available to do this include:

Sample Project Driver

/****************************************************************
* File:     saveJPG.cpp
* Use:  Press "s" to save the GLUT image as a .jpg file
*
*****************************************************************/

#include <GL/glut.h>
#include "mkOpenGLJPEGImage.h"
int imageWidth=400,imageHeight=300;
//  mkOpenGLJPEGImage example in 3 lines

void SaveJPG()
{
	mkOpenGLJPEGImage jpgScreen;
	jpgScreen.SetDebugMode(true);
	jpgScreen.GetOpenGLScreenImage(imageWidth, imageHeight);
	jpgScreen.SaveToFile("ScreenShot.jpg");
}

void display(void) {

        glClear( GL_COLOR_BUFFER_BIT ); // First clear the buffer
        glPolygonMode( GL_FRONT, GL_FILL );        //Draw all shapes solid (not wireframe)

        glBegin(GL_TRIANGLES ); // define the vertices of a single triangle

        glVertex3f( 0.0, 0.0, 1.0);
        glVertex3f( 1.0, 0.0, 1.0);
        glVertex3f( 1.0, 1.0, 1.0);

        glEnd();
        
        glutSwapBuffers(); //Copy the frame to the front buffer
}

void parsekey(unsigned char key, int x, int y){	switch (key)	{	case 's': case 'S': 
		SaveJPG();
		break;	}}int main( int argc, char **argv ) {

        // Initialize display mode

        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA ); // Set the display to Double-buffered
        glutInitWindowSize( imageWidth, imageHeight ); // Set the size of the window (x,y)
        glutInitWindowPosition( 100, 100 ); // Set the location of the window 
		
        glutCreateWindow( "OpenGL1" ); // Show the window, and give it a caption of "OpenGL1"

        //Setup callbacks

        glutDisplayFunc( display ); // draws the display
        glutKeyboardFunc(parsekey); // keyboard handling
        glutMainLoop(); // This starts the main event loop
        return 0;
}

Important Notes:

To use the package, the mkOpenGLJPEGImage.h file must be availabe to your application and the mkOpenGLJPEGImag.lib file must be included in the Project | Settings | Link portion of the VC++ project settings.

The mkOpenGLJPEGImag.dll must also be accessable to your program. Placing it in the Debug folder associated with you project will make it accessable.Or you make place it with other .dll files accessed by VC++.

The mkOpenGLJPEGImag.lib must also be accessable to your program. Placing it the project folder will make it accessable.Or you make place it with other .lib files accessed by VC++.