GLReadPixels返回一个重复出现的错误值

GLReadPixels returns a single,incorrect, value repeated over and over

本文关键字:错误 一个 返回 GLReadPixels      更新时间:2023-10-16

我目前正在尝试用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,所以它似乎至少输出一种颜色。

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

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

相关文章: