ich habe ein für euch bestimmt simples Problem. Für eine Datensimulation müsste ich einzelne gerenderte Frames in Bilder schreiben. Dies habe ich in OpenGL wie folgt versucht:
Code: Alles auswählen
#include <GL/freeglut.h>
#include <cstring>
#include <iostream>
#include <tiffio.h>
const int windowWidth = 1300;
const int windowHeight = 800;
bool snapshotTIFF( const char *outFilename ) {
GLubyte *imageBuffer = (GLubyte *)malloc( windowWidth * windowHeight * 4 );
if( !imageBuffer ) {
return false;
}
memset( imageBuffer, 0, windowWidth * windowHeight * 4 );
TIFF *file = TIFFOpen(outFilename, "wb");
if( file ) {
glPixelStorei( GL_PACK_ALIGNMENT, 1 );
glReadPixels( 0, 0, windowWidth, windowHeight, GL_RGBA, GL_UNSIGNED_BYTE, imageBuffer );
TIFFSetField(file, TIFFTAG_IMAGEWIDTH, (uint32) windowWidth);
TIFFSetField(file, TIFFTAG_IMAGELENGTH, (uint32) windowHeight);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(file, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 4);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(file, TIFFTAG_IMAGEDESCRIPTION, "");
GLubyte *p = imageBuffer;
for( int i = windowHeight - 1; i >= 0; i-- ) {
if( TIFFWriteScanline(file, p, i, 0) < 0 ) {
free(imageBuffer);
TIFFClose(file);
return false;
}
}
p += windowWidth * sizeof(GLubyte) * 4 ;
}
TIFFClose(file);
free( imageBuffer );
return true;
}
void glutRenderingCallback() {
glClearColor( 1.0f, 1.0f, 0.5f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -5.0f );
glRotatef( 30.0f, 0.0f, 1.0f, 0.0f );
glRotatef( 30.0f, 1.0f, 0.0f, 0.0f );
glutSolidCube( 2.0f );
glutSwapBuffers();
}
void glutReshapeCallback( int width, int height ) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void glutKeyboardCallback( unsigned char key, int x, int y ) {
switch( key ) {
case 's':
glReadBuffer( GL_FRONT );
snapshotTIFF( "test_front.tif" );
glReadBuffer( GL_BACK );
snapshotTIFF( "test_back.tif" );
std::cout << "Took snapshot!" << std::endl;
break;
}
}
int main( int argc, char **argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize( windowWidth, windowHeight );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( "****** Simulation" );
glEnable( GL_DEPTH_TEST );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glutDisplayFunc( glutRenderingCallback );
glutReshapeFunc( glutReshapeCallback );
glutKeyboardFunc( glutKeyboardCallback );
glutMainLoop();
return 0;
}
Vielen Dank im Voraus.
Viele Grüße,
Tim