Source Code [OpenGL] Draw a crosshair

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,109
78,998
2,388
Game Name
N/A
How long you been coding/hacking?
5 years


Update:
try this video to learn everything about opengl

Include & link whatever openGL library you use that support legacy OpenGL functionality, I'm currently using GLUT
Inject a working internal hack with a working hook
You will need address's that hold the current window width & height:
C++:
int * windowWidth = (int*)0xhhhhhhh;
int * windowHeight = (int*)0xhhhhhhh;
We are drawing to the screen so everything here is 2D. Before every draw you have to call a few openGL functions and restore to the previous state afterwards. I use this code everytime:
C++:
glPushMatrix();

glViewport(0, 0, *windowWidth, *windowHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, *windowWidth, *windowHeight, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/*  your draw code here */

glPopMatrix();
You have three options in openGL drawing
1)Drawing primitives using immediate mode
2)Drawing primitives using vertex array
3)Drawing primitives using a vertex buffer object(VBO) and vertex array objec(VAO)t

I will post source for the first 2 which use legacy functionality. Using a VBO/VAO puts the data on the GPU and uses the data efficiently, uses openGL3.0+ and we are just drawing 2 lines so I won't go over that today.

Drawing primitives using immediate mode(availalbe since 1992, deprecated)
C++:
void drawCrosshairOLD()
{
        glPushMatrix();
        glViewport(0, 0, *windowWidth, *windowHeight);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, *windowWidth, *windowHeight, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glColor3ub(240, 240, 240);//white
        glLineWidth(2.0);
        glBegin(GL_LINES);
        //horizontal line
        glVertex2i(*windowWidth / 2 - 7, *windowHeight / 2);
        glVertex2i(*windowWidth / 2 + 7, *windowHeight / 2);
        glEnd();
        //vertical line
        glBegin(GL_LINES);
        glVertex2i(*windowWidth / 2, *windowHeight / 2 + 7);
        glVertex2i(*windowWidth / 2, *windowHeight / 2 - 7);
        glEnd();

        glPopMatrix();
}

Drawing primitives using vertex array data and glDrawArrays(deprecated)
C++:
void drawCrosshair()
{
    glPushMatrix();
    glViewport(0, 0, *windowWidth, *windowHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, *windowWidth, *windowHeight, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3ub(240, 240, 240);//white
    glLineWidth(2.0);

    int crossHair[8] =
    {
    *windowWidth / 2 - 7, *windowHeight / 2, // horizontal line
    *windowWidth / 2 + 7, *windowHeight / 2,

    *windowWidth / 2, *windowHeight / 2 + 7, //vertical line
    *windowWidth / 2, *windowHeight / 2 - 7
    };

    // activate vertext array state and assign pointer to vertext array data
    glEnableClientState(GL_VERTEX_ARRAY);

    //2 = number of coordinates per vertext we are doing 2d so I don't need the Z coordinate
    // GL_INT = data type held in array
    // crossHair = pointer to vertext data array


    glVertexPointer(2, GL_INT, 0, crossHair);

    //draw primitive GL_LINES starting at the first vertex, use 2 total vertices
    glDrawArrays(GL_LINES, 0, 2); //draw horizontal line
    //Same as above but start at second vertex
    glDrawArrays(GL_LINES, 2, 2); //draw vertical line

    // deactivate vertex array state after drawing
    glDisableClientState(GL_VERTEX_ARRAY);
    glPopMatrix();


}
I'll convert to VAO/VBO in the future.
And special thanks to Krampus without using his RektCube source code as a guide I would have been lost!
 
Last edited:

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
How did you not get lost in the mangled code of RektCube o.o

Great job! It's awesome that you included a better method of drawing :D
 

speedy

Newbie
May 10, 2015
4
97
1
nice, but why not get viewport this way
C++:
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,109
78,998
2,388
I actually use that code in my world2screen, I never thought about using it in the rest of my code, thanks for the tip!
 
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