Page 1 of 1

Matching Luxcore & Opengl Field Of View

Posted: Fri Oct 16, 2020 12:57 am
by Continuum
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();

Re: Matching Luxcore & Opengl Field Of View

Posted: Fri Oct 16, 2020 1:15 pm
by Continuum
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

Re: Matching Luxcore & Opengl Field Of View

Posted: Sat Oct 17, 2020 10:50 am
by Continuum
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.

Re: Matching Luxcore & Opengl Field Of View

Posted: Sun Nov 15, 2020 1:57 pm
by Ernest
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.

Re: Matching Luxcore & Opengl Field Of View

Posted: Sun Nov 15, 2020 2:08 pm
by Dade
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.

Re: Matching Luxcore & Opengl Field Of View

Posted: Sun Nov 15, 2020 6:27 pm
by Ernest
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.

Re: Matching Luxcore & Opengl Field Of View

Posted: Mon Nov 16, 2020 11:17 am
by Dade
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 :?:

Re: Matching Luxcore & Opengl Field Of View

Posted: Mon Nov 16, 2020 11:34 am
by B.Y.O.B.
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

Re: Matching Luxcore & Opengl Field Of View

Posted: Mon Nov 16, 2020 11:36 pm
by Ernest
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.

Re: Matching Luxcore & Opengl Field Of View

Posted: Wed Nov 18, 2020 11:01 pm
by Ernest
Short update - looks like I got the modified sample program to render the intended image section.