Dll injection: drawing a simple overlay game with opengl

I am trying to draw a custom opengl overlay (steam does this, for example) in a 3D board game. This overlay should basically be able to display the state of some variables that the user can influence by pressing some keys. Think of it like a game coach. The goal, first of all, is to draw a few primitives at a specific point on the screen. Later, I want the game window to have a slightly nice “gui” component. The game uses the SwapBuffers method from GDI32.dll. Currently, I can enter a custom DLL file into the game and enable the SwapBuffers method. My first idea was to insert an overlay pattern into this function. This can be done by switching the 3D drawing mode from the game to 2d, then draw a 2d overlay on the screen and turn it back on, for example:

//SwapBuffers_HOOK (HDC)
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glOrtho(0.0, 640, 480, 0.0, 1.0, -1.0);

//"OVERLAY"
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0, 0);
glVertex2f(0.5f, 0);
glVertex2f(0.5f, 0.5f);
glVertex2f(0.0f, 0.5f);
glEnd();

glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

SwapBuffers_OLD(HDC);

.

  • , ( 3d-2d)?
  • , - . ( - opengl - , ...?)
  • SwapBuffers ?

, - . , , 1.6, .

.

EDIT:

, opengl, "derHass". :

//1. At the beginning of the hooked gdiSwapBuffers(HDC hdc) method save the old context
GLboolean gdiSwapBuffersHOOKED(HDC hdc) {
    HGLRC oldContext =  wglGetCurrentContext();
//2. If the new context has not been already created - create it   
//(we need the "hdc" parameter for the current window, so the initialition 
//process is happening in this method - anyone has a better solution?)
//Then set the new context to the current one.
    if (!contextCreated) {
        thisContext = wglCreateContext(hdc);
        wglMakeCurrent(hdc, thisContext);
        initContext();
    }
    else {
        wglMakeCurrent(hdc, thisContext);
    }
    //Draw the quad in the new context and switch back to the old one.
    drawContext();
    wglMakeCurrent(hdc, oldContext);
    return gdiSwapBuffersOLD(hdc);
}

GLvoid drawContext() {
    glColor3f(1.0f, 0, 0);
    glBegin(GL_QUADS);
    glVertex2f(0,190.0f);
    glVertex2f(100.0f, 190.0f);
    glVertex2f(100.0f,290.0f);
    glVertex2f(0, 290.0f);
    glEnd();
}

GLvoid initContext() {
    contextCreated = true;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 640, 480, 0.0, 1.0, -1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0, 0, 0, 1.0);
}

: cs , , ..

.

+2
1

OpenGL, SwapBuffers. , , , , , .

OpenGL, :

  • OpenGL - . GL. , , , , - . , , , , , . , , . , currect culling. GL_FALSE , , .

    , "reset" . , GL_MODELVIEW. . GL_PROJECTION GL_TEXTURE. glOrtho , , , .

  • OpenGL , , . push/pop. , , . 1, , . OpenGL, glPushAttrib() .

  • SwapBuffers GL, API . drawable GL. , GL- . , GL, MakeCurrent. ( ) GL, , SwapBuffers, .

, : GL-, SwapBuffers . , GL . - , , , GL. , , .

, , , , GL SwapBuffers. , apitrace, .

+1

Source: https://habr.com/ru/post/1523671/


All Articles