how to calculate the pwidth pheight (in pixels) of

Community Forums/General Help/how to calculate the pwidth pheight (in pixels) of

RemiD(Posted 2015) [#1]
Hello,


I need to know how to calculate the pwidth pheight (in pixels) of a 0.1*0.1quad (which is oriented perfectly toward the camera and is in the fov) depending on its distance from the camera.

I want to be able to check if a quad is visible on screen (if pixels are displayed to represent it) or not depending on its distance from the camera (assuming it is oriented perfectly toward the camera and it is in the fov).

What would be the formula to know this ?
I use 1024x768pixels for the graphics window.


Thanks,


Floyd(Posted 2015) [#2]
If this is Blitz3D then you could work it out using the following:

1. VertexX() etc. to get x,y,z for mesh coordinates of a vertex.
2. TFormPoint x,y,z, quad, 0 to transform to world coordinates.
3. TFormedX() etc. to retrieve location in 3D space.
4. CameraProject to transform to screen coordinates.
5. ProjectedX() etc. to retrieve location on screen.

That will tell you where a vertex is on screen. Do that for two opposite corners of the quad and you know width and height.


RemiD(Posted 2015) [#3]
Cool idea, thanks Floyd !


Floyd(Posted 2015) [#4]
It just occurred to me that I forgot about

6. Clip the step 5 screen coordinates to the screen/viewport as needed.

because CameraProject just projects into the view plane. It can give values that are off screen.


Kryzon(Posted 2015) [#5]
If I'm not mistaken, as an alternative, the size of the 0.1 x 0.1 quad can be measured according to its distance from the camera.

The viewport is the near plane in the 3D scene.
When exactly on the near plane, the width of the quad in pixels is the number of 3D units times the width (in pixels) of the graphics screen.
When the quad moves away from the camera, it shrinks in a linear way that is inversely proportional to the distance.

If you can guarantee that the quad always faces the camera, then it should be this:
Const SCREEN_WIDTH# = 1024.0
Const QUAD_WIDTH_3D# = 0.1

Const QUAD_WIDTH_PIXEL# = SCREEN_WIDTH * QUAD_WIDTH_3D

;When it's time to calculate the visible width and height:

Local currentWidthPixel# = QUAD_WIDTH_PIXEL / EntityDistance( camera, quad )
This means that when the quad is at distance 2.0, it should be 50% smaller.
I think the above only works with a zoom of 90 degrees (the default when you create a camera).


Matty(Posted 2015) [#6]
Yep. You can also calculate for different zooms with a few adjustments. Wikipedia and other sources are okay to help with this. Look up 3d perspective calculations


Kryzon(Posted 2015) [#7]
I'm too tired to think much now, but I think on a zoom of 1.0 (the default), the near-plane is one 3D unit wide -- you can fit the whole screen horizontally with a centered 1.0 wide quad.
When you zoom in, the near-plane shrinks, so the pixel size decreases in relation to the 3D units.

I also think it's with a linear relation, so it should be something like this, replacing that constant:

Global SCREEN_WIDTH# = 1024.0 * myCameraZoom

Which is why with a 1.0 zoom there's no difference.


RemiD(Posted 2015) [#8]
I have some tests and what i wanted to do is too slow for realtime, so... whatever, forget about it !

Thanks for the ideas anyway. :)


big10p(Posted 2015) [#9]
Tell us what you wanted to do and you may get alternative ways of doing it. :)