# Source Code[OpenGL] World To Screen

#### Rake

Game Name
N/A
How long you been coding/hacking?
5 years
This is a GuidedHacking Exclusive Codenz boys, I spent 3 weeks learning 3D Game programming, openGL and trigonometry. It was a major pain in the ass and a humbling experience. I hope people understand the commitment and hard work that goes into making these hacks, if you think you can spend a few afternoons copy and pasting code and call yourself a hacker, you're DEAD WRONG.

I used these 2 pages as reference:
OpenGL Transformation
OpenGL - Transformations

I wrote it verbosely so people can understand what it does.

You get mvpmatrix by taking the dot product of the model, view and projection matrices. In other games just using the Projection Matrix may work, I'm not 100% sure. Maybe a tutorial in the future on this topic.

C++:
``````struct glmatrixf
{
float v[16];

float operator[](int i) const { return v[i]; }
float &operator[](int i) { return v[i]; }
}

vec W2S(glmatrixf *mvpmatrix, vec vPlayerLoc)
{
//GuidedHacking.com : AnomanderRake

//Matrix-Vector Product, multiplying world(eye) coordinates by projection matrix = clipCoords
vec4 clipCoords;
clipCoords.x = vPlayerLoc.x*mvpmatrix->v[0] + vPlayerLoc.y*mvpmatrix->v[4] + vPlayerLoc.z*mvpmatrix->v[8] + mvpmatrix->v[12];
clipCoords.y = vPlayerLoc.x*mvpmatrix->v[1] + vPlayerLoc.y*mvpmatrix->v[5] + vPlayerLoc.z*mvpmatrix->v[9] + mvpmatrix->v[13];
clipCoords.z = vPlayerLoc.x*mvpmatrix->v[2] + vPlayerLoc.y*mvpmatrix->v[6] + vPlayerLoc.z*mvpmatrix->v[10] + mvpmatrix->v[14];
clipCoords.w = vPlayerLoc.x*mvpmatrix->v[3] + vPlayerLoc.y*mvpmatrix->v[7] + vPlayerLoc.z*mvpmatrix->v[11] + mvpmatrix->v[15];

//perspective division, dividing by clip.W = NDC
vec normalizedDeviceCoordinates;
normalizedDeviceCoordinates.x = clipCoords.x / clipCoords.w;
normalizedDeviceCoordinates.y = clipCoords.y / clipCoords.w;
normalizedDeviceCoordinates.z = clipCoords.z / clipCoords.w;

//viewport tranform to screenCooords
GLint viewport[4] = { 0 };
GLfloat depthrange[2] = { 0 };
glGetIntegerv(GL_VIEWPORT, viewport); // viewport = (0, 0, width, height)
glGetFloatv(GL_DEPTH_RANGE, depthrange); //depthrange = (0,1)

vec playerScreenCoords;
playerScreenCoords.x =   (viewport[2] / 2 * normalizedDeviceCoordinates.x) + (normalizedDeviceCoordinates.x + viewport[2] / 2);
playerScreenCoords.y =  -(viewport[3] / 2 * normalizedDeviceCoordinates.y) + (normalizedDeviceCoordinates.y + viewport[3] / 2);
//playerScreenCoords.z = ((depthrange[1] - depthrange[0]) / 2 * normalizedDeviceCoordinates.z) + ((depthrange[1] + depthrange[0]) / 2);
//because we are drawing 2D we just set Z to 0
playerScreenCoords.z = 0;

return playerScreenCoords;
}``````
Update:

Last edited:

#### squeenie

##### Hacker
Meme Tier VIP
Dank Tier Donator
Nice one dude. Can't wait to finish my assignment and fiddle around with ogl

#### Liduen

##### Hacker
Dank Tier VIP
Thanks for the code!

#### till0sch

##### Respected Hacker
Dank Tier VIP
Dank Tier Donator
thanks imma try to get this working for warsow

#### speedy

##### Newbie
here is another, credits sinner
C++:
``````void ObjectToWindowShort(float WindowOut[3])
{
float mvx[16], px[16];
int vp[4];

glGetFloatv(GL_MODELVIEW_MATRIX, mvx);
glGetFloatv(GL_PROJECTION_MATRIX, px);
glGetIntegerv(GL_VIEWPORT, vp);

float x2 = px[0]*mvx[12]+px[4]*mvx[13]+px[8]*mvx[14]+px[12];
float y2 = px[1]*mvx[12]+px[5]*mvx[13]+px[9]*mvx[14]+px[13];
float z2 = px[2]*mvx[12]+px[6]*mvx[13]+px[10]*mvx[14]+px[14];
float w2 = px[3]*mvx[12]+px[7]*mvx[13]+px[11]*mvx[14]+px[15];

WindowOut[0] = (float) vp[0] + vp[2] * 0.5f * (x2/w2 + 1.0f);
WindowOut[1] = (float) vp[1] + vp[3] * 0.5f * (y2/w2 + 1.0f);
WindowOut[2] = (float)                 0.5f * (z2/w2 + 1.0f);
WindowOut[1] = (float) (vp[3] - WindowOut[1]);
}

usage:
ObjectToWindowShort(Window);
//Window[0] is x
//Window[1] is y``````

Attention! Before you post:

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