Find edge of window given a Z

Hello. I’d like to show some particles on the screen that are a random distance and x/y position from the user. The problem I’m running into is determining what the edges of the screen are for different resolutions and aspect.
I use the following code to set my screen up:


void glPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) {
	GLdouble xmin, xmax, ymin, ymax;
	ymax = zNear * tan(fovy * M_PI / 360.0);
	ymin = -ymax;
	xmin = ymin * aspect;
	xmax = ymax * aspect;
	glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
}

//...
glViewport(0,0,(GLsizei)currentWidth,(GLsizei)currentHeight);
glPerspective(60,(GLfloat)currentWidth/(GLfloat)currentHeight,0.4f,1000);

By trial and error I can determine that say the screen is 1680x1050 and the Z is -3 the mzximum X is around 5.7 to -5.7.

I haven’t been able to calculate a formula that given a width, height, and Z coordinate I can find the max X and Y so to make sure are particles drawn are visible to the user. Anyone have any experience with this?

Hi!

If I’m not mistaken I think you might have to learn more about the different coordinate systems usually encountered in a 3D application on a computer screen.

Here are some hints.

The corners of the screen are measured in pixels and are determined by your screen resolution.

The position of the corners of the window in which you have the graphics are also measured in pixels (screen coordinates) and you use the windowing system to set those, for example in GLUT you set the position and size in screen coordinates of the window in which you will render the graphics.

To give a hint about the solution to your question about visibility:
The visible points inside your 3D-world are those which lie inside the viewing frustum, defined by the xmin, xmax etc. parameters. The viewing frustum is a volume enclosed by 6 planes (left, right, top, bottom, near and far) and is in the eye coordinate system. You can calculate/represent those planes with the parameters you send to glFrustum.
When you have a mathematical representation of those planes, you can use linear algebra to calculate if a point is “above” or “below” each plane, as needed.
The 3D world coordinates are not measured in units of pixels. You choose yourself what units you want to use for your world.

I hope this helps!

I found a calculation that works. Here it is:


float fovCalc=1.0471976;
float aspectCalc=(GLfloat)(currentWidth)/(GLfloat)(currentHeight);
float zCalc=-3;
float maxYPos =  zCalc*tan(fovCalc/2);
float maxXPos =  zCalc*tan(fovCalc/2)*aspectCalc;

In the above, fovCalc is the field of view in radians (60 degrees = 1.0471976 radians).
aspectCalc is the aspect ratio.
zCalc is the z used.
The result of maxYPos and maxXPos is in opengl coordinates so if you take the negative or positive of them you get the edges of the screen where a point drawn still shows.