PDA

View Full Version : The best way to draw a 2D image directly on a hooked window



alvinhochun
05-01-2013, 08:37 PM
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:


Get current matrix mode
glPushAttrib(GL_ALL_ATTRIB_BITS)
glViewport(0, height - 64, 64, 64)
Disable a bunch of things
glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5f);
Switch to projection matrix, push, loadidentity
Switch to modelview matrix, push, loadidentity, glOrtho
glRasterPos
glDrawPixels to draw the image with alpha
Restore matrices and attributes
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?

tonyo_au
05-01-2013, 09:22 PM
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.

dukey
05-02-2013, 04:14 AM
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.

alvinhochun
05-02-2013, 09:19 PM
Never mind drawPixels. I managed to bind and draw a texture. Thanks.

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?

alvinhochun
05-03-2013, 04:14 AM
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?