PDA

View Full Version : How do I find screen coordinates?

DemiImp
02-08-2008, 08:56 PM
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.

ZbuffeR
02-09-2008, 12:11 AM
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);

DemiImp
02-09-2008, 02:17 AM
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! :)

CRasterImage
02-10-2008, 05:39 PM
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);