GLUT screen capture in C

亡梦爱人 提交于 2019-12-01 12:12:08

glReadPixels doesn't allocate memory for you, it merely stores the pixel data in the buffer that you give it in the last parameter. You're giving it a NULL pointer, so it's trying to store data at address 0, which obviously results in an access violation.

You need to allocate the memory first, pass it to glReadPixels, and then deallocate it. You also need to make sure that you call glPixelStorei to ensure that the pixel data is returned packed, without any padding (alternatively, you can write each scan line individually, but that requires a little extra effort).

For example:

// Error checking omitted for expository purposes
char *pixels = malloc(width * height * 3);  // Assuming GL_RGB
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(..., pixels);
...
fwrite(pixels, ...);
...
free(pixels);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!