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:

[ol]
[li]Get current matrix mode
[/li][li]glPushAttrib(GL_ALL_ATTRIB_BITS)
[/li][li]glViewport(0, height - 64, 64, 64)
[/li][li]Disable a bunch of things
[/li][li]glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5f);
[/li][li]Switch to projection matrix, push, loadidentity
[/li][li]Switch to modelview matrix, push, loadidentity, glOrtho
[/li][li]glRasterPos
[/li][li]glDrawPixels to draw the image with alpha
[/li][li]Restore matrices and attributes
[/li][li]Back to original matrix mode
[/li][/ol]

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?

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.

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.

Never mind drawPixels. I managed to bind and draw a texture. Thanks.

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?

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?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.