Понадобилось реализовать анализ экрана (средний уровень яркости). Решил воспользоваться OpenGL, т.к. давно хотел поработать с этой библиотекой. Конечно же, Гугл подсказал использовать glReadPixels. Но вот проблема: функция полностью заполняет передаваемый массив нулями. Во время инициализации OpenGL ошибок не возникает. Разрешение экрана определяется верно. В чем может быть проблема?
Код:
// Logger macro
#define LOG(P) logger << P << endl
bool EnableOpenGL(HWND, HDC*, HGLRC*, GLuint&, GLuint&);
bool DisableOpenGL(HWND, HDC*, HGLRC*);
///////////////////////////////
// Application main function //
///////////////////////////////
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Prepare log file
ofstream logger("log.txt");
if (logger.fail())
return FALSE;
HWND hWnd;
hWnd = GetDesktopWindow(); // Desktop-window descriptor
if (!hWnd)
return FALSE;
HDC hDC; // Device context descriptor
HGLRC hRC; // OpenGL rendering context descriptor
GLuint sW=0, sH=0; // Screen width and height
GLubyte *pixelsArray; // Array of screen pixels information (RGB for each pixel)
// OpenGL initialization
if (EnableOpenGL(hWnd, &hDC, &hRC, sW, sH))
LOG("OpenGL was successfully initialized.");
else
{
LOG("Cannot initialize OpenGL.");
return FALSE;
}
long pixelsArraySize = sW*sH*3; // Calculate size of pixels array
LOG("Screen size: " << sW << "x" << sH);
pixelsArray = new GLubyte[pixelsArraySize]; // Allocating memory for pixels array
FillMemory(pixelsArray, sizeof(GLubyte)*pixelsArraySize, 1); // Fill pixels array with 1 (for test)
Sleep(3000);
glPixelStorei(GL_PACK_ALIGNMENT,1);
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, sW, sH, GL_RGB, GL_UNSIGNED_BYTE, pixelsArray); // Copy all pixels to array
// Writing information about first 10 pixels to log file
for (int i=0; i<10; i++)
LOG((unsigned int)(pixelsArray[i]) << "," << (unsigned int)(pixelsArray[i+1]) << "," << (unsigned int)(pixelsArray[i+2]));
if (DisableOpenGL(hWnd, &hDC, &hRC))
LOG("OpenGL was successfully disabled.");
else
{
LOG("Cannot disable OpenGL.");
return FALSE;
}
logger.close();
return TRUE;
}
//////////////////////////////
// OpenGL specific routines //
//////////////////////////////
bool EnableOpenGL(HWND hWnd, HDC *hDC, HGLRC *hRC, GLuint& screenWidth, GLuint& screenHeight)
{
PIXELFORMATDESCRIPTOR pfd;
int pixelFormat;
// get the device context (DC)
*hDC = GetDC(hWnd);
//*hDC = GetDC(NULL);
// set the pixel format for the DC
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
pixelFormat = ChoosePixelFormat(*hDC, &pfd);
if (!pixelFormat)
return false;
if (!SetPixelFormat(*hDC, pixelFormat, &pfd))
return false;
// create and enable the render context (RC)
*hRC = wglCreateContext(*hDC);
if (!hRC)
return false;
if (!wglMakeCurrent(*hDC, *hRC))
return false;
// Get screen resolution
RECT screenRect;
GetWindowRect(hWnd, &screenRect);
screenWidth = screenRect.right-screenRect.left;
screenHeight = screenRect.bottom-screenRect.top;
return true;
}
bool DisableOpenGL(HWND hWnd, HDC* hDC, HGLRC* hRC)
{
if (!wglMakeCurrent(NULL, NULL))
return false;
if (!wglDeleteContext(*hRC))
return false;
if (!ReleaseDC(hWnd, *hDC))
return false;
return true;
}
Заранее спасибо.