# Thread: How do I find screen coordinates?

1. ## How do I find screen coordinates?

I am trying to make a 2D game and I need to know how to find/calculate/define the edges of my screen. Aka: I want to know how big to make my triangles so the textures aren't warped and that a pixel on the texture is it's own single pixel on the screen.

I dont care how to determine this, other than brute forcing it (such as creating a triangle, and if it's too small, increase the length/width by .001 to see if it made a difference). So, if there's a function I could use to declare that i want the left edge of the screen to be -10, the right edge to be 10, the top edge to be 4 and the bottom edge to be -4, i'd be happy.

Things to note: I am planning on using an orthographic perspective, if that helps at all.

2. ## Re: How do I find screen coordinates?

You mean you want to use 2D pixel coordinates for a window that is x * y pixels ?

glViewport(0,0,x,y);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,x,0,y);
glMatrixMode(GL_MODELVIEW);

3. ## Re: How do I find screen coordinates?

Ok, well, maybe I didn't explain it well enough.

First of all, let me mention that I do not understand what glViewport does. I'm new to OpenGL and the descriptions I am recieving is only confusing me because it's using terms that I do not understand.

I want to know where the edges of the screen are in terms of x,y, and z. Let's just say that the camera is facing 0,0,0 at 0,0,-1. Now, in an orthographic projection (if I used the right word, it should mean that objects that are further away do not decrease in size) the edges of the screen should have the same x and y coordinates no matter what z value they have. How do I find what these values are without creating a square and expand it and expand it and expand it until I think it starts displaying off the screen?

Is there a way for me to declare that I want those edges to be at -10 to 10 width wise and -8 to 8 height wise? If not, how could I calculate what the coordinates of the screen edges are?

EDIT:

AHH!! I see now! It glOrtho() does it! Thank you for helping me!

4. ## Re: How do I find screen coordinates?

Another way to do it, is this:

glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);

// setup OpenGL so that the lower left corner
// of the device is the origin
float mat[16];
memset(mat,0,sizeof(GLfloat) * 16);
mat[10] = mat[15] = 1.0f;
mat[0] = 1.0f / (((float)screenwidth_inpixels) / 2.0f);
mat[5] = 1.0f / (((float)screenheight_inpixels) / 2.0f);

memset(mat,0,sizeof(GLfloat) * 16);
mat[0] = mat[5] = mat[10] = mat[15] = 1.0f;
mat[12] = -(((float)screenwidth_inpixels) / 2.0f);
mat[13] = -(((float)screenheight_inpixels) / 2.0f);
glMultMatrixf(mat);

At this point, screen coordinate (0,0) is the lower left corner and coordinate (screenwidth_inpixels, screenheight_inpixels) is the upper right corner.

5. ## Re: How do I find screen coordinates?

Obviously, ZbuffeR's method is more strait-forward. But sometimes it pays to understand how the matrix stuff works.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•