Matching Luxcore & Opengl Field Of View

Discussion related to the LuxCore functionality, implementations and API.
Post Reply
User avatar
Continuum
Posts: 30
Joined: Mon Dec 02, 2019 11:16 pm
Location: Zambia
Contact:

Matching Luxcore & Opengl Field Of View

Post by Continuum » Fri Oct 16, 2020 12:57 am

I've been looking through the Blender addon, but I can't seem to find this. Essentially i am trying to draw a rectangle in OpenGl to match Luxcore sensor width and height by using the Field of View. While I am getting the correct ratios, the renders don't match the opengl camera. Some code below to demonstrate, the fov is equal to the luxcore FOV. I tested it with a similar renderer and things were matching up.

Oh and where does the Lens value 74.51911163330078 come from? It is used to calculate lens radius for depth of field. Is it a constant?

Code: Select all

static float proj[16];

glMatrixMode(GL_PROJECTION);
auto _proj = perspective(radians(fov), cam_w / cam_h, 0.05f, 500.0f);
glPushMatrix();
glLoadIdentity();
glMultMatrixf(&_proj[0][0]);
glGetFloatv(GL_PROJECTION_MATRIX, proj);
glPopMatrix();

const double _nearScale = 1.0;
const double _near = proj[11] / (proj[10] - 1.0);

const double nLeft  = _near * _nearScale *(proj[2] - 1.0) / proj[0];
const double nRight = _near * _nearScale *(1.0 + proj[2]) / proj[0];
const double nTop   = _near * _nearScale *(1.0 + proj[6]) / proj[5];
const double nBottom = _near * _nearScale *(proj[6] - 1.0) / proj[5];

glMatrixMode(GL_MODELVIEW);
glLineStipple(1, 0xAAAA);
glLineWidth(2.0);
glPushMatrix();
glMultMatrixf(&inverse(viewMat)[0][0]);
glBegin(GL_LINES);

glColor3f(1.f, 1.f, 1.f);

glVertex3f(nLeft, nBottom, -_near);
glVertex3f(nRight, nBottom, -_near);

glVertex3f(nRight, nTop, -_near);
glVertex3f(nLeft, nTop, -_near);

glVertex3f(nLeft, nTop, -_near);
glVertex3f(nLeft, nBottom, -_near);

glVertex3f(nRight, nTop, -_near);
glVertex3f(nRight, nBottom, -_near);

glEnd();
glPopMatrix();

User avatar
Continuum
Posts: 30
Joined: Mon Dec 02, 2019 11:16 pm
Location: Zambia
Contact:

Re: Matching Luxcore & Opengl Field Of View

Post by Continuum » Fri Oct 16, 2020 1:15 pm

Okay, not familiar with the blender api, just figured out that the Lens value comes from blender. So I am guessing blender is calculating the camera render region automatically. So dived into the Luxcore C++ code, and found this. I believe I am close. I also calculate the perspective matrix and multiply it by the camera world transform. But I am missing a step?

Code: Select all

// identity matrix.
if (orig == target)
	trans->cameraToWorld = Transform();
else {
	const Transform worldToCamera = LookAt(orig, target, up);
	trans->cameraToWorld = Inverse(worldToCamera);
}

// Compute projective camera transformations
trans->screenToCamera = Inverse(Perspective(fieldOfView, clipHither, clipYon));
trans->screenToWorld = trans->cameraToWorld * trans->screenToCamera;
// Compute projective camera screen transformations
trans->rasterToScreen = luxrays::Translate(Vector(screenWindow[0] + screenOffsetX, screenWindow[3] + screenOffsetY, 0.f)) *
	Scale(screenWindow[1] - screenWindow[0], screenWindow[2] - screenWindow[3], 1.f) *
	Scale(1.f / filmWidth, 1.f / filmHeight, 1.f);
trans->rasterToCamera = trans->screenToCamera * trans->rasterToScreen;
trans->rasterToWorld = trans->screenToWorld * trans->rasterToScreen;
And an image to show the problem. I could live without this, since there's interactive rendering but would love to get it to work.
comparision.PNG

User avatar
Continuum
Posts: 30
Joined: Mon Dec 02, 2019 11:16 pm
Location: Zambia
Contact:

Re: Matching Luxcore & Opengl Field Of View

Post by Continuum » Sat Oct 17, 2020 10:50 am

Okay, after looking into the blender code, I see this wasn't the best place to post this problem :D I can follow the threads from there. But if anyone has an alternative solution would love to here it.

Ernest
Posts: 7
Joined: Sun Mar 31, 2019 11:30 am

Re: Matching Luxcore & Opengl Field Of View

Post by Ernest » Sun Nov 15, 2020 1:57 pm

Hello, I would like to join this discussion. I have a similar issue when trying to map OpenGL's glFrustum parameters to Luxcore perspective camera settings.

Should it be possible to achieve the same results and the same flexibility - especially asymmetric screen sections - with Luxcore as it is possible with glFrustum or is it limited in the same way as gluPerspective is?
I already read the description of the original pbrt source code but I cannot find any clear statement there.

If Luxcore does not have the same flexibility as glFrustum, what would be the best way to add it? All parameters should already exist like screenwindow, focal distance, clip values. Add a new option 'OpenGLMode = true' or a new camera type derived from PerspectiveCamera?

Thnaks for your help or advice.

User avatar
Dade
Developer
Posts: 4817
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: Matching Luxcore & Opengl Field Of View

Post by Dade » Sun Nov 15, 2020 2:08 pm

Ernest wrote:
Sun Nov 15, 2020 1:57 pm
Should it be possible to achieve the same results and the same flexibility - especially asymmetric screen sections - with Luxcore as it is possible with glFrustum or is it limited in the same way as gluPerspective is?
What do you mean exactly with "asymmetric screen sections" ? Can you post some image example ? I have the feeling the same result may be achieved with LuxCore subregion parameter.
Support LuxCoreRender project with salts and bounties

Ernest
Posts: 7
Joined: Sun Mar 31, 2019 11:30 am

Re: Matching Luxcore & Opengl Field Of View

Post by Ernest » Sun Nov 15, 2020 6:27 pm

This is meant by symmetric view:
Symmetric.JPG

and this by asymmetric - camera placement is unchanged in the following image just screenwindow has changed (by using something like a magnifying glass to view a sub-section)
asymmetric.JPG

Even if the screenwindow values are symmetric (e.g -xstart == xend) and xstart == ystart original image and lux image do not fit

Source image:
SourceCamera.GIF
Luxcore image - please ignore the colors - WIP.:
LuxImage.GIF

Stepping through Lux source seems to show that the rasterToScreen matrix works as expected. The screenToCamera part is harder to check.
How to calculate the FOV value for a symmetric squared case seemed obvious to me: 2 * RadToDeg(atan(yend - ystart)/2/FocalDistance)) .
But how is it supposed to work for the general case?

This explains hopefully a bit better what I do not understand.

User avatar
Dade
Developer
Posts: 4817
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: Matching Luxcore & Opengl Field Of View

Post by Dade » Mon Nov 16, 2020 11:17 am

Ernest wrote:
Sun Nov 15, 2020 6:27 pm
This is meant by symmetric view:
Symmetric.JPG


and this by asymmetric - camera placement is unchanged in the following image just screenwindow has changed (by using something like a magnifying glass to view a sub-section)

asymmetric.JPG


Even if the screenwindow values are symmetric (e.g -xstart == xend) and xstart == ystart original image and lux image do not fit
So this is what you mean with symmetric window:

sym.jpg

and this an asymmetric window using "film.subregion = 400 700 300 750" :?:

asym.jpg

"film.subregion" renders a subregion of the original image (with "xstart xstop ystart ystop" in pixels). You may have to crop the black border (or not, depend on the kind of application) but it should be what you are looking for :?:
Support LuxCoreRender project with salts and bounties

User avatar
B.Y.O.B.
Developer
Posts: 3872
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Matching Luxcore & Opengl Field Of View

Post by B.Y.O.B. » Mon Nov 16, 2020 11:34 am

Dade wrote:
Mon Nov 16, 2020 11:17 am
and this an asymmetric window using "film.subregion = 400 700 300 750"
Note that the same can be achieved with the screenwindow properties. This will save memory because the black pixels will not be present in that variant, however it is harder to configure.
https://wiki.luxcorerender.org/LuxCore_ ... 2.5#Camera

Ernest
Posts: 7
Joined: Sun Mar 31, 2019 11:30 am

Re: Matching Luxcore & Opengl Field Of View

Post by Ernest » Mon Nov 16, 2020 11:36 pm

I'm already using the screenwindow properties to setup an image section but as B.Y.O.B.already mentioned I do not understand how exactly these parameters work in Lux. Our camera system uses a similar way to specify the sub-section. The values for the screenwindow are in our case placed on the projection plane which located 'focal distance' units away from the eye point along the view direction.

Can anybody please explain how this is supposed to work in Lux. Playing around with one of the sample programs delivered with lux source code did not reveal any more details.

Thanks in advance.

Ernest
Posts: 7
Joined: Sun Mar 31, 2019 11:30 am

Re: Matching Luxcore & Opengl Field Of View

Post by Ernest » Wed Nov 18, 2020 11:01 pm

Short update - looks like I got the modified sample program to render the intended image section.

Post Reply