Bonjour,
Je programme en C++ et j'utilise OpenGL pour faire une animation. Je n'arrive pas cependant à sauvegarder les images créées par opengl. J'utilise glreadbuffer et glreadpixels mais l'image créée ne contient que la moitié du buffer.
Voici ma fonction display
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.8f, 0.8f, 0.8f, 0.0f);
glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);
displayObject();
glPopMatrix();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 3000, 0, 3000);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
OGL_SaveScreenshot(counter++);
glutSwapBuffers();
}
et la fonction pour sauvegarder le contenu du buffer:
void OGL_SaveScreenshot(int i)
{
char filename[2048];
std::ostringstream buff; buff<<"SavedImage"<<i<<".png";
strcpy(filename, buff.str().c_str());
png_structp png_write = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, OGL_png_error, OGL_png_warn);
if (!png_write)
{
printf("Error creating PNG write struct.\n");
return;
}
png_infop png_info = png_create_info_struct(png_write);
if (!png_info)
{
png_destroy_write_struct(&png_write, (png_infopp)NULL);
printf("Error creating PNG info struct.\n");
return;
}
if (setjmp(png_jmpbuf(png_write)))
{
png_destroy_write_struct(&png_write, &png_info);
printf("Error calling setjmp()\n");
return;
}
// open the file to write
FILE *savefile = fopen(filename, "wb");
if (savefile == NULL)
{
printf("Error opening '%s' to save screenshot.\n", filename);
return;
}
png_init_io(png_write, savefile);
unsigned char *pixels = (unsigned char*)malloc( glWindowWidth * glWindowHeight*3 );
glReadBuffer(GL_BACK);
glReadPixels(0,0,glWindowWidth,glWindowHeight,GL_RGB,GL_UNSIGNED_BYTE,pixels);
if(NULL==pixels)
printf("Error NO PIXELS \n");
// set the info
png_set_IHDR(png_write, png_info, glWindowWidth, glWindowHeight, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
// lock the screen, get a pointer and row pitch
long pitch = glWindowWidth * 3;
png_byte **row_pointers;
row_pointers = (png_byte **) malloc(glWindowHeight * sizeof(png_bytep));
for (int i = 0; i < glWindowHeight; i++)
{
row_pointers[i] = (png_byte *) (pixels + i * pitch);
}
png_set_rows(png_write, png_info, row_pointers);
png_write_png(png_write, png_info, 0, NULL);
// free memory
free(row_pointers);
png_destroy_write_struct(&png_write, &png_info);
free(pixels);
}
Est-ce que quelqu'un aurait une idée ?
Merci