Search code examples
c++openglglfwwgl

openGL PBO use wglcontext, Not create window


My goal is get pixel data from main window. nothing any rendering. like we see the monitor such as screencapture.

I tried to TRANSPARENT windows, glReadPixel. so I have a TRANSPARENT windows and context.

glfwSetErrorCallback(errorCallback);

if (!glfwInit()) {
    std::cerr << "Error: GLFW " << std::endl;
    exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_DEPTH_BITS, 16);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);  
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, 4);


const int Monitor_count = GetMonitors();

GLwindow = glfwCreateWindow(
    nWidth, // width
    nHeight, // height
    "OpenGL_Test", // window title
    NULL, NULL);
if (!GLwindow) {
    glfwTerminate();
    exit(EXIT_FAILURE);
}
glfwSwapInterval(1);    
//glfwShowWindow(GLwindow);


if (glfwGetWindowAttrib(GLwindow, GLFW_TRANSPARENT_FRAMEBUFFER))
{
    // ..
}
glfwSetWindowOpacity(GLwindow, 0.0f);

glfwMakeContextCurrent(GLwindow);
glfwSetKeyCallback(GLwindow, keyCallback);
glewExperimental = GL_TRUE;
GLenum errorCode = glewInit();

But I want get pixel data from GPU without create windows.

So I use wglcreatecontext Get mother window DC, HGLRC.

And When I set bind buffer, It gives runtime error.

if (!glfwInit()) {
    std::cerr << "Error: GLFW" << std::endl;
    exit(EXIT_FAILURE);
}
HDCC = GetDC(m_hWndCopy);
// HDC TDC = CreateCompatibleDC(HDCC);
HGLRC DC = wglCreateContext(HDCC);

GLuint pbo;
glGenBuffersARB(1, &pbo);     <<Error Here
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pbo);

How can I solve problem?

Any idea or link?


Solution

  • From your question and comment replies I gather, that you want to use OpenGL to grab a screenshot of an arbitrary window? If so, then this is not what OpenGL is meant for. You cannot use OpenGL for taking screenshots reliably.

    glReadPixels will work reliably only for things that you did draw with OpenGL in the first place!