location of code correlating FOV and "Lens" settin

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

location of code correlating FOV and "Lens" settin

Post by chipmasque »

I've searched a number of keywords in this and a number of other Blender forums and no one seems to really know how the Blender "Lens" setting (ostensibly a focal length -- FL -- in mm) relates to the FOV ("Degrees" option in Lens setting) parameter. Most seem to agree that the "mm" value does not match any real-world FL with any accuracy. I'd like to investigate how that value is determined, with the aim of improving the Camera settings for use in matching real-world-to-Blender setups.

But a couple of hours searching the code base has gotten me pretty much nowhere. Can anyone at least point me to the right sections of code so I can search less randomly? Thanks.

BeBraw
Posts: 0
Joined: Sun Feb 19, 2006 4:14 pm

Post by BeBraw »

See buttons_editing.c. You can find the user interface and blender unit <-> degree conversion there. Relevant functions are editing_panel_camera_type(Object *ob, Camera *cam),
do_angletolensconversion_cb(void *lens1, void *angle1)
and void do_lenstoangleconversion_cb(void *lens1, void *angle1) .

Hopefully that gets you started. :)

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

Post by chipmasque »

Excellent, BeBraw, this is exactly what I was trying to find.

Now, can you (or anyone else) shed any light on the rationale behind the equations used to convert between FOV ("angle") and a mm ("lens") setting? They seem much too simplistic for an accurate calculation, and I'm wondering where the constant 16.0f comes from?

My own work on this subject, for another 3D app, takes a specified camera lens focal length, film aperture dimensions (i.e., dimensions of exposed area), and camera-to-subject distance, and uses standard algebraic and trig equations to solve for a proper FOV to match. It takes into account the magnification factor of a real-world lens, which makes it useful mainly for non-macro setups with camera-to-subject distance > ~3xFL of the sim'd lens, but within that limitation it tests accurately.

I've tried this in Blender with very good results so far, but there's always a discrepancy between the returned Blender "Lens" value (mm) for any given FOV I plug in, e.g. for a 50mm lens at 250mm from a subject imaging a 36x24mm aperture (135 roll film), my calc'd FOV is 31.133deg, but Blender returns a value of 55.56mm equivalent for this angle setting.

I don't want to assume that the equations Blender uses are inaccurate until I understand why the values they use were chosen. Within the constraints of those choices, the math may be quite accurate.

BeBraw
Posts: 0
Joined: Sun Feb 19, 2006 4:14 pm

Post by BeBraw »

There is some basic information about the camera at http://en.wikibooks.org/wiki/Blender_3D ... a_Settings .

I am not exactly sure about 16.0f. I suppose it's a matter of digging deeper into the camera code. It is possible that there is inaccuracy though.

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

Post by chipmasque »

Thanks, BreBraw. The diagram shown on that (out-dated) Wiki page makes not much sense to me; again, there's that constant 16 again for no apparent reason. Also, it seems to indicate that the "Lens" value is NOT intended to be a FL in mm, but some value for camera-to-subject distance, which only equals focal length under very strict conditions in an "ideal lens" solution.

the math in the do_lenstoangleconversion_cb function does seem to resemble to some degree that which I use for the most basic FOV calculations, that of an "ideal thin lens" focused at infinity:

FOV = 2 atan(0.5A/f)

where FOV is the "angle of view" in degrees, A is the image area aperture (which is often defined as the longer of either length or width of the image area), and f is the focal length of the lens. This equation assumes the tan function operates on values in degrees -- if calc'ing in radians (as Blender seems to), multiply the right side of the equation by 180/pi.

The only difference is that mine uses a variable (A) and Blender uses a constant (16). This seems to indicate that for some reason, the Blender camera is hard-coded to an image aperture dimension of A = 32 units. As long as the unit system is consistent, it won't matter what these units actually are, but if we assume the variable lensvalue in the function is intended to be a focal length in mm, then the formula will only work if it's also assumed that the aperture of the imaged area is fixed at 32mm.

But an "ideal thin lens focused at infinity" formula and a hard-coded image aperture (A) value make this a very restrictive approach, unlikely to match real-world camera situations except by serendipity. So trying to use the "Lens" settings as is is going to make matching real-world camera output (film or digital) very difficult. In "all Blender" renderings and animations, it will likely make little difference, and most would not notice (or even care to notice ;) ) any discrepancies. But there are situations where a more accurate correlation between the Lens setting (if assumed to be a FL in mm) and the FOV setting (which in real-world cameras is dependent on a number of specific variables) would be extremely useful.

What is the "usual" procedure for setting up a project to look into implementing an alternate Camera system for Blender? Obviously, the existing code can't be arbitrarily changed (even if it's not very accurate), for fear of breaking a large number of pre-existing scene setups. But perhaps another Camera mode can be implemented? I've done some scripting in various languages, including a smattering of Python, but nothing in C or C++, and so would need some assistance in that regard.

macouno
Posts: 0
Joined: Tue Nov 18, 2003 3:37 am

Post by macouno »

Have a look the BFOV script on this page:

http://www.alienhelpdesk.com/python_scripts/old_scripts

I don't have a page specificly for the script anymore since not a lot of people need it.

Basicly to calculate a 'Lens' value from a real world focal length / film size do this:

LensValue = ((32 * FocalLength) / FilmWidth);

Values in millimeters.

Hope that helps ;)

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

Post by chipmasque »

Thanks, macouno, I'll try out that script and see what results, but from looking over the math it uses, it doesn't seem to do a lot more than what the Blender functions do, and still uses the constants 32 & 16 for some reason. I'd really like to know why that is. My experience is that there are no constants used in correlating real-world FOV and focal length, but Blender's "Lens" setting may not refer to a lens focal length (though a number of sources refer to it as such).


EDIT_______________

I've tried out the script in a couple of test scenes, and though it does a better job than the coded Blender camera, it still doesn't match my real-world setups closely in all situations. From what I can tell, the script does not take into consideration the camera-to-subject distance, which is critical for determining the magnification factor of the camera/lens/subject system. Those constants are still a puzzle. Any idea what these values actually refer to? Constants in these situation usually refer to physical properties (like Earth's g, or the IOR of a material, or pi), but I can't think of anything of that nature that would be required for this calculation.

FURTHER EDIT_________________

After twisting the equations around a bit, what I can see is that the script essentially compensates for Blender's apparent use of a constant for the image aperture (i.e., film width) by using the ratio of this constant (32) and a target film width (32/filmwidth) to modify a target focal length (lens length in mm) proportionally, and using the result in the "Lens" setting.

The reason for the constant(s) still is unclear, and thismethod does ignore magnification, which is actually a very important factor for any lens focused at a distance less than infinity (about 30m in practice).

macouno, are you interested in revising your script at all? I can supply the equations I've used to better effect if you are interested. I'm also researching a method that will make matching digital camera images easier (their sensors are trickier to calc for than film sizes).

macouno
Posts: 0
Joined: Tue Nov 18, 2003 3:37 am

Post by macouno »

Of course... if you have something that will improve the script please let me know. I didn't come up with the original equations myself either. I don't know too much about the math behind it ;)

Phlip is the one that figured out the 32-16 thing when I talked to him in the chat channels. I haven't seen him in a while though. You could try to get in touch with him. (search for his nick on the blenderartists.org forum).

This sort of thing is really interesting for pretty much everyone working with compositing. Perhaps you should try the chat channels as well. 'Keir' is currently working on 'camera tracking' from video sources so I figure he's had a very good look at blender's camera settings.

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

Post by chipmasque »

Sounds good. I'll write up my formulas and some info on how I derived them, so it'll be clearer what the changes will do, and try to find some folks willing to help do real-world test cases (can't really rely on math alone). I'll see if I can get in touch with Phlip & Keir as well. I'm still working on new math for digital cameras, so it may be a while, but I'll post back or PM you when I have something to start coding on.

Thanks for taking the time to discuss this :)

macouno
Posts: 0
Joined: Tue Nov 18, 2003 3:37 am

Post by macouno »

O I forgot to make a point yesterday....

When using the script you must make sure that your render size (render settings/format/sizeX & sizeY) matches the aspect ratio of your film. I kind of assumed you knew that but realised it isn't mentioned anywhere.

Did you have that set properly when testing?

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

Post by chipmasque »

yup, got that covered, but thanks for the heads up.

goodguy20k
Posts: 0
Joined: Fri Aug 31, 2007 6:45 pm

Post by goodguy20k »

Howdy,

A very green programmer here, looking at tweaking a few things on Blender.

Has anything more been cleared up with this?

I've been thinking about a very small mod that will allow for the camera angle settings to be selected from different "profiles" of real cameras, to assist those trying to match up.

Better math calculations for the FOV would be awesome in helping this along, so I'm wondering how far this has gone.

Thanks.

Daniel

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

Post by chipmasque »

I have a first draft of the text done, and need to put together a few explanatory figures and diagrams (some of which I have to find on my old Mac :roll: ). I also want to draft an interface mockup for the scripter/coder to work to, since my implementation requires more input and has more interactivity than the BFOV.py script.

As far as camera profiles are concerned, do you mean only digitals? The basic "profile" of film cameras is their film format, which is independent of the camera make and model, and this data is very well-known and tightly specified. Would that digital camera sensors were so consistent. There seems to be a lot of room for error regarding the actual effective image area of a digital sensor chip, even from as respected a camera mfr. as Nikon, but the FOV calcs need to be as precise as possible. I have a method laid out that I think will provide accurate info based on "in-the-field" tests rather than somewhat vague tech data sheets. It needs to be tested thoroughly, of course, along with the focal length-to-FOV script.

Daniel, if you'd like to help out, could you do some test setups once the project's on a testing basis?

macouno, I PM'd Phlip on the blenderartists.org site, haven't yet received a response, he may not be active there much any more. I'm just assuming that the constants in the BFOV script math reflect those used in the Blender code itself. I'm planning to ignore the default "Lens" setting (the one that may or may not be a focal length in millimeters), which may always be in error because changing it might break so many users' projects. But the FOV (in Blender that's the "Lens" Degrees option) is the fundamental property of the Blender camera, and my tests show it is accurate, and is what's used to produce the other value using the odd math.

goodguy20k
Posts: 0
Joined: Fri Aug 31, 2007 6:45 pm

Post by goodguy20k »

I understand how vast the profile list could be. You're right, it really comes down to "film size", and a couple other factors.

What I'd like is a list of a few of the latest digital profiles, along with some by-gone-days profiles. CinemaScope, Panavision, "widescreen", etc. (And yes, I know Panavision is technically 360 degrees of view angle. That would take more work.)

But sure, I'd love to help test when you're farther along. :) Just let me know!

chipmasque
Posts: 0
Joined: Mon Aug 20, 2007 6:31 pm

Post by chipmasque »

I've completed a beta draft of the website and other info about this project, which I've dubbed "BLenses".

However, it's not yet ready for prime time, as I'd like some private feedback on the concept and the site presentation, especially from macouno, the prospective scripter, but also from any other interested parties (are you listening, Daniel? :) I'd like your input on the "presets" of the proposed interface, where your "profiles" would be listed.)

But this site doesn't allow PMs (why is that btw?), so I need another means of contacting macouno and goodguy20k other than by public post. I'm also available on the blenderartists site, where PMs are allowed, so maybe that'd be a better venue for now?

Post Reply