Source Code OpenGL Text Rendering Class

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,153
78,998
2,396
Game Name
N/A
Tutorial Link
N/A
How long you been coding/hacking?
4 Years
Drawing text in OpenGL could not be easier using this lovely class I have created from the knowledge passed down to me from @c5. You do not need GLUT to do this, it is very efficient and you have many options in the creation of your fonts.

I teach you how to make this in this video:


gltext.h
C++:
//https://guidedhacking.com and c5
#pragma once
#include <windows.h>
#include <stdio.h>
#include <gl\GL.h>

struct vec3 { float x, y, z; };

namespace GL
{
    class Font
    {
    public:
        bool bBuilt = false;
        unsigned int base;
        HDC hdc = nullptr;
        int height;
        int width;

        void Build(int height);
        void Print(float x, float y, const unsigned char color[3], const char *format, ...);

        //center on X and Y axes
        vec3 centerText(float x, float y, float width, float height, float textWidth, float textHeight);
        //center on X axis only
        float centerText(float x, float width, float textWidth);
    };
};
gltext.cpp
C++:
#include "stdafx.h"
#include "gltext.h"

void GL::Font::Build(int height)
{
    hdc = wglGetCurrentDC();
    base = glGenLists(96);
    HFONT hFont = CreateFontA(-(height), 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FF_DONTCARE | DEFAULT_PITCH, "Consolas");
    HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
    wglUseFontBitmaps(hdc, 32, 96, base);
    SelectObject(hdc, hOldFont);
    DeleteObject(hFont);

    bBuilt = true;
}

void GL::Font::Print(float x, float y, const unsigned char color[3], const char *format, ...)
{
    glColor3ub(color[0], color[1], color[2]);
    glRasterPos2f(x, y);

    char text[100];
    va_list    args;

    va_start(args, format);
    vsprintf_s(text, 100, format, args);
    va_end(args);

    glPushAttrib(GL_LIST_BIT);
    glListBase(base - 32);
    glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
    glPopAttrib();
}

//center on X and Y axes
vec3 GL::Font::centerText(float x, float y, float width, float height, float textWidth, float textHeight)
{
    vec3 text;
    text.x = x + (width - textWidth) / 2;
    text.y = y + textHeight;
    return text;
}

//center on X axis only
float GL::Font::centerText(float x, float width, float textWidth)
{
    if (width > textWidth)
    {
        float difference = width - textWidth;
        return (x + (difference / 2));
    }

    else
    {
        float difference = textWidth - width;
        return (x - (difference / 2));
    }
}
You have to do it in an openGL hook because you need wglGetCurrentDC() to return the correct device context. If a new map is loaded or for some other reason the current device context changes, you need to rebuild the font for the new context. That will be accomplished if in your openGL hook you do this:
C++:
HDC currentHDC = wglGetCurrentDC();

if (!glFont.bBuilt || currentHDC != glFont.hdc)
{
    glFont.Build(MENU_FONT_HEIGHT);
}

SetupOrtho();
DrawMenu();
RestoreGL();
 
Last edited:

Kinvaras

Full Member
Nobleman
Dec 18, 2017
99
528
10
I cant get this to work in urt...

hooked the glswapbuffers for the build font part and for the draw i tried in glswapbuffers and in gldrawelements.
None of those worked for the font... I can draw glquads vertexes, but this font code is not working.

I also tried drawing between these two functions you sugested in another thread:

C++:
void GL::SetupOrtho()
{
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    <span class="highlight">glPushMatrix</span>();
    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    glViewport(0, 0, viewport[2], viewport[3]);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glDisable(GL_DEPTH_TEST);
}
 
void GL::RestoreGL()
{
    glPopMatrix();
    glPopAttrib();
}
But its not working also...
Any tips?
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,153
78,998
2,396
Hmm zip up your project and I'd be happy to take a look tonight
 

Kinvaras

Full Member
Nobleman
Dec 18, 2017
99
528
10
I'm using a clean opengl hook to test this. I never learn new stuff on my main project so things dont overlap.

this is the opengl hook base im using:

main.h:
C++:
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib, "gl/Glu32.lib")
#pragma comment( lib, "Detours/lib.X86/detours.lib" )

#include <Windows.h>
#include <gl/GL.h>
#include <gl/glu.h> //gluProject
#include <string> //std::string
#include <fstream> //ofstream
#include "Detours\include\detours.h"
#include <iostream>

typedef void (WINAPI *glDrawElements_t) (GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
typedef void (WINAPI *glVertexPointer_t) (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
typedef void (WINAPI *glBindTexture_t) (GLenum target, GLuint texture);
typedef BOOL(__stdcall * twglSwapBuffers) (_In_ HDC hDc);

twglSwapBuffers original_glSwapBuffers;
glBindTexture_t original_BindTexture;
glDrawElements_t original_glDrawElements;
glVertexPointer_t original_glVertexPointer;

unsigned int hackFont;
bool bFontBuilt = false;
HDC fontHDC = nullptr;

void buildFonts()
{
	fontHDC = wglGetCurrentDC();
	hackFont = glGenLists(96);

	HFONT hFont = CreateFontA(-15, 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FF_DONTCARE | DEFAULT_PITCH, "Consolas");
	//HFONT hFont = CreateFont(-15, 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FF_DONTCARE | DEFAULT_PITCH, "Consolas");
	//HFONT hFont = CreateFontW(-15, 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FF_DONTCARE | DEFAULT_PITCH, L"Consolas");

	HFONT hOldFont = (HFONT)SelectObject(fontHDC, hFont);
	wglUseFontBitmaps(fontHDC, 32, 96, hackFont);
	SelectObject(fontHDC, hOldFont);
	DeleteObject(hFont);

	bFontBuilt = true;
}

void printText(float x, float y, /*const unsigned char color[3],*/ const char *format, ...)
{
	//glColor3ub(color[0], color[1], color[2]);
	//glColor3ub(1, 0, 0);
    glColor3ub(255, 0, 0);
	glRasterPos2f(x, y);

	char text[100];
	va_list args;

	va_start(args, format);
	vsprintf_s(text, 100, format, args);
	va_end(args);

	glPushAttrib(GL_LIST_BIT);
	glListBase(hackFont - 32);
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
	glPopAttrib();
}
main.cpp
C++:
#include "dllmain.h"

void WINAPI hooked_BindTexture(GLenum target, GLuint texture)
{
	original_BindTexture(target, texture);
}

void WINAPI hooked_glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid* pointer)
{
	original_glVertexPointer(size, type, stride, pointer);
}

void WINAPI hooked_glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid * indices)
{
	original_glDrawElements(mode, count, type, indices);
}

BOOL __stdcall hooked_glSwapBuffers(_In_ HDC hDc)
{
	HDC currentHDC = wglGetCurrentDC(); // if it works remove and try use hDc from this hook

	if (!bFontBuilt || currentHDC != fontHDC)
	{
		buildFonts();
		std::cout << "Built fonts" << std::endl;
	}

	printText(100, 200, "message here");

	return original_glSwapBuffers(hDc);
}

void HookFunctions()
{
	HMODULE oMod = GetModuleHandle("opengl32.dll");

	original_glVertexPointer = (glVertexPointer_t)(DWORD)GetProcAddress(oMod, "glVertexPointer");
	original_glDrawElements = (glDrawElements_t)(DWORD)GetProcAddress(oMod, "glDrawElements");
	original_glSwapBuffers = (twglSwapBuffers)(DWORD)GetProcAddress(oMod, "wglSwapBuffers");
	original_BindTexture = (glBindTexture_t)(DWORD)GetProcAddress(oMod, "glBindTexture");


	DetourTransactionBegin();
	DetourUpdateThread(GetCurrentThread());
	DetourAttach(&(PVOID &)original_glVertexPointer, hooked_glVertexPointer);
	DetourAttach(&(PVOID &)original_glDrawElements, hooked_glDrawElements);
	DetourAttach(&(PVOID &)original_glSwapBuffers, hooked_glSwapBuffers);
	DetourAttach(&(PVOID &)original_BindTexture, hooked_BindTexture);

	DetourTransactionCommit();
}

DWORD __stdcall Thread(void* pParam)
{
	FILE *pFile = nullptr;
	AllocConsole();
	freopen_s(&pFile, "CONOUT$", "w", stdout);
	std::cout << "-INJECTED-" << std::endl;
	HookFunctions();

	return 0;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
			DisableThreadLibraryCalls(hModule);
			CreateThread(nullptr, 0, Thread, nullptr, 0, nullptr);
			break;
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
			break;
		case DLL_PROCESS_DETACH:
			DetourTransactionBegin();
			DetourUpdateThread(GetCurrentThread());
			DetourDetach(&(PVOID &)original_glDrawElements, hooked_glDrawElements);
			DetourDetach(&(PVOID &)original_glVertexPointer, hooked_glVertexPointer);
			DetourDetach(&(PVOID &)original_glSwapBuffers, hooked_glSwapBuffers);
			DetourDetach(&(PVOID &)original_BindTexture, hooked_BindTexture);
			DetourTransactionCommit();
			::FreeLibrary(hModule);
			hModule = 0;
			break;
		default:
			break;
	}
	return TRUE;
}
I had the project in Unicode, and i thought it was that, but i changed it to non unicode and the behaviour is the same...
 
  • Like
Reactions: Rake and Meow

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,153
78,998
2,396
been screwing with it for about an hour, not having much luck :(
 

Kinvaras

Full Member
Nobleman
Dec 18, 2017
99
528
10
Same, but im sure there is a way... Linduen had a menu in his hack i believe...
 

Meow

Gimme food hooman.
Fleep Tier Donator
Nobleman
Apr 24, 2020
145
3,743
4
I know this is a old thread. But thats exactly what I needed. Thanks once again Rake.

Full code explanation here btw.

I also want to mention that glRasterPos2f almost killed me. Guess you need to setup a raster or something to make it work. I've used glWindowPos2d
 
  • Like
Reactions: Kleon742

Meow

Gimme food hooman.
Fleep Tier Donator
Nobleman
Apr 24, 2020
145
3,743
4
If anyone is able to get outlined Bitmap Fonts working let me know. Would love to use them.
Tried it but didn't got it working.
Tutorial: here
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods