Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: The best way to draw a 2D image directly on a hooked window

  1. #1
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    4

    The best way to draw a 2D image directly on a hooked window

    What is the best way to draw a 2D image directly on a window which I hooked the SwapBuffers (gdi32.dll) function, without changing the existing state?

    I am currently doing this:

    1. Get current matrix mode
    2. glPushAttrib(GL_ALL_ATTRIB_BITS)
    3. glViewport(0, height - 64, 64, 64)
    4. Disable a bunch of things
    5. glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5f);
    6. Switch to projection matrix, push, loadidentity
    7. Switch to modelview matrix, push, loadidentity, glOrtho
    8. glRasterPos
    9. glDrawPixels to draw the image with alpha
    10. Restore matrices and attributes
    11. Back to original matrix mode


    The reason behind this is that I am trying to draw a fps counter on the top-left corner of the window, just like Fraps or other game recording program. Although I'm not familiar with OpenGL, I believe that using texture on a quad will change the state so I can't use texture.

    The drawing of the fps number happens directly before calling `SwapBuffers` in my hook.

    Is there a faster/better approach than what I'm doing?

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    You could try creating a second hdc and render context and swap these in for the render then restore the old hdc and render context. That way any state changes are isolated to your own private render context.

  3. #3
    Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    251
    Draw pixels is generally pretty slow. Just create a texture with the text on it. You can do this from the swap buffers function. Give it a random texture ID, instead of using an auto generated one. Because not all programs use auto generated texture ID names.

  4. #4
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    4
    Never mind drawPixels. I managed to bind and draw a texture. Thanks.
    Quote Originally Posted by dukey View Post
    Give it a random texture ID, instead of using an auto generated one. Because not all programs use auto generated texture ID names.
    Use a random texture name? I am not sure if this is a good idea. Shouldn't all applications use glGenTextures to generate texture names?


    Also, I have some questions on HDC and HGLRC. When wglSwapBuffers is called with a specified HDC, can I assume the current OpenGL rendering context is always the same and can simply draw and capture (glReadPixels) directly? Or do I need to manually find the context and set it to current?

  5. #5
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    4
    Hi, I tested drawing the texture on some OpenGL programs, and on some which uses custom shaders (e.g. some of OpenTK's examples), the texture appears black. Also, it doesn't work on the OpenGL 3.0 example.

    However I see that Fraps and other equivalents can draw it normally.

    Can anyone think of the cause of the texture not rendering properly?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •