Matching Luxcore & Opengl Field Of View

Discussion related to the LuxCore functionality, implementations and API.
Post Reply
User avatar
Continuum
Posts: 25
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: 25
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: 25
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.

Post Reply