# OpenGL problem with simple screen capture to ppm

This topic is 3971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, For some time I have implemented screen captures in my OpenGL programs using the following function (which a kind soul gave me in another forum)
int capture_to_ppm(void)
{
int width, height, colorDepth, maxColorValue,y;
unsigned char	*pixels;
int fd;
char sbuf[256]; /* for sprintf() */

/* open output file: you can name it as you like */
fd = open(picfile,O_CREAT|O_TRUNC|O_WRONLY,
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd == -1) return -1;

/* width & height of the window */
width = glutGet(GLUT_WINDOW_WIDTH);
height = glutGet(GLUT_WINDOW_HEIGHT);

/* maxColorValue is 255 in most cases... */
colorDepth = glutGet(GLUT_WINDOW_RED_SIZE);
maxColorValue = (1 << colorDepth) - 1;

/* allocate pixels[]: 3 is for RGB */
pixels = malloc(3*width*height);
if( !pixels ) return -2;

/* get RGB values from the frame buffer into pixels[] */
glReadBuffer(GL_FRONT); /* if you are using "double buffer" */

/* write ppm file header */
sprintf(sbuf,"P6 %d %d %d\n",width,height,maxColorValue);
write(fd,sbuf,strlen(sbuf));

/* write ppm RGB data: we must invert upside down */
for(y = height-1; y >= 0; --y) {
write(fd, pixels+3*width*y, 3*width);
}

close(fd);
free(pixels);
return 0;
}


It has always worked very nicely, provided that my OpenGL program does not open subwindows. I can open any number of windows, and get a perfect window grab of one of them using a glutSetWindow() call. Now this same code is failing when I use it to grab a subwindow. I have a program that opens a main window with 3 subwindows inside. I need to grab one of the subwindows. I call the capture window this way:
    glutSetWindow(modelwin);
capture_to_ppm();


where modelwin is the subwindow to be grabbed. The problem is I get a skewed image (the pixels' x coordinate is displaced from its expected position), and without colors. I have been trying to read about glReadPixels(), where I think the problem resides, but I still do not know how to correct it (I read something about setting GL_UNPACK_ALIGNMENT differently than default, but whatever I did had no effect). Could anyone give me an idea what I need to do? Thanks a million, mc61

##### Share on other sites
GL_UNPACK_ALIGNMENT is for transferring pixel data to OpenGL. Notice the word, "unpack".

For reading data from OpenGL to your application, GL_PACK_ALIGNMENT should be used. Try to set this state to 1 if it fixes your problem.

##### Share on other sites
Thank you, it worked!!!

Naively I thought I was "unpacking" the pixels, while I was actually packing them it seems [smile].

Since you have been so kind, I have one more related question, just to show how little I really know about OpenGL. I set this by placing glPixelStoref(GL_PACK_ALIGNMENT,1) inside my capture_to_ppm function. However, it seems to me that this should be set once, and not every time the screen capture is invoked. Where should it go?

mc61

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013754
×