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];

auto _proj = perspective(radians(fov), cam_w / cam_h, 0.05f, 500.0f);
glGetFloatv(GL_PROJECTION_MATRIX, proj);

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];

glLineStipple(1, 0xAAAA);

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);


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.

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.