GLReadPixels一遍又一遍地返回一个不正确的值

时间:2013-08-11 15:09:58

标签: c++ opengl

我目前正在尝试使用OpenGL编写一个简单的屏幕捕获应用程序。 我目前的来源如下:

#include <Windows.h>
#include <iostream>
#include <gl/GL.h>
#include <fstream>

using namespace std;

void main()
{
int nWidth = 1920; // use these dimentions, We'll work work out a calculation to get the real dimentions later.
int nHeight = 1080;
unsigned char* buffer;//declair the buffer here plox
int size = nWidth*nHeight*3; //number of bytes the image will take up.
char input;

buffer = (GLubyte *)malloc(size); // acctually assign some RAM for the program



glReadBuffer ( GL_BACK ); //which buffer we are reading from.

glReadPixels ( 0, 0, nWidth, nHeight, GL_RGB, GL_UNSIGNED_BYTE,buffer); 

const char* out=(const char*)buffer;

std::ofstream outfile ("out.crw",std::ifstream::binary);

outfile.write (out,size);
cout << out;
cin>> input;
return;

}

它不会产生任何错误。虽然,GLReadPixels的返回值是在输出文件中反复重复的字符“í”。 “í”的十六进制值为“CD”,“CD”的颜色值为红色205,因此它似乎至少为输出颜色。

我期待返回多个值,表示包含多种颜色的图像。我做错了什么?

1 个答案:

答案 0 :(得分:1)

即使您实际上要设置GL上下文,也无法通过OpenGL捕获屏幕。您只能回读使用GL渲染的内容。 GL的帧缓冲区之外的所有内容都无法通过这种方式访问​​。您将需要使用一些特定于平台的API来执行屏幕捕获。