POV-Ray : Newsgroups : povray.general : Rewrite of Doctor John's FieldCam macro : Rewrite of Doctor John's FieldCam macro Server Time18 May 2024 05:16:35 EDT (-0400)
 Rewrite of Doctor John's FieldCam macro
 From: Tor Olav Kristensen Date: 9 Nov 2021 19:01:06 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#web.6189cefb97b1968629a3960c89db30a9%40news.povray.org",
"headline": "Rewrite of Doctor John's FieldCam macro",
"dateCreated": "2021-11-10T00:01:06+00:00",
"datePublished": "2021-11-10T00:01:06+00:00",
"author": {
"@type": "Person",
"name": "Tor Olav Kristensen"
}
}
Some time ago I had a brief look at the FieldCam macro by Doctor John. At the
time I thought that it looked a bit overcomplicated.

Now I have had some time to try to simplify the mathematical expressions and to
rewrite the macro. I've actually split it into two macros.

(v4.0 of it has an improvement suggested by Bald Eagle.)

There's a long discussion about it here:

As creating and changing global variables inside macros is generally poor
programming practice, I've reintroduced parameter passing - and the macros now
"returns" the calculated transformation. Also I've made the macros fail hard
when the viewing (direction) vector is problematic, so that problems are
discovered early (when they may be easier to find and resolve).

Here's the rewritten macros:

#macro FieldCameraTransform_UpY(pLocation, pLookAt)

#local vDirection = pLookAt - pLocation;
#local vDirXZ = vDirection*(x + z);
#local SqrXZ = vdot(vDirXZ, vDirXZ);
#if (SqrXZ = 0)
#error "The viewing vector is perpendicular to the XZ-plane.\n"
#end // if
#local vS = vDirection.y/SqrXZ*vDirXZ;

transform {
matrix <
1,    0,    0,
vS.x,    1, vS.z,
0,    0,    1,
0,    0,    0
>
translate -pLocation.y*vS
}

#end // FieldCameraTransform_UpY

#macro FieldCameraTransform_UpZ(pLocation, pLookAt)

#local vDirection = pLookAt - pLocation;
#local vDirXY = vDirection*(x + y);
#local SqrXY = vdot(vDirXY, vDirXY);
#if (SqrXY = 0)
#error "The viewing vector is perpendicular to the XY-plane.\n"
#end
#local vS = vDirection.z/SqrXY*vDirXY;

transform {
matrix <
1,    0,    0,
0,    1,    0,
vS.x, vS.y,    1,
0,    0,    0
>
translate -pLocation.z*vS
}

#end // FieldCameraTransform_UpZ

// From the tests I've done so far, this code:

#declare pLocation = <10, 1, -14>;
#declare pLookAt = <-4, 6, 0>;

camera {
perspective
location pLocation
FieldCameraTransform_UpY(pLocation, pLookAt)
look_at pLookAt
}

// - seems to create the same result as this:
// (without the side effect of creating the global NoFall variable)

/*
#include "dj_fieldcam_v4.0.inc"

#declare CamPos = pLocation;
#declare CamLook = pLookAt;

FieldCam()

camera {
perspective
location CamPos
transform { NoFall }
look_at CamLook
}
*/

--
Tor Olav
http://subcube.com
https://github.com/t-o-k
```