2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "camera.h"
|
|
|
|
#include "scene.h"
|
|
|
|
|
|
|
|
#include "util_vector.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
Camera::Camera()
|
|
|
|
{
|
2012-04-30 12:49:26 +00:00
|
|
|
shuttertime = 1.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-16 13:14:02 +00:00
|
|
|
aperturesize = 0.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
focaldistance = 10.0f;
|
2011-09-16 13:14:02 +00:00
|
|
|
blades = 0;
|
|
|
|
bladesrotation = 0.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
matrix = transform_identity();
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
motion.pre = transform_identity();
|
|
|
|
motion.post = transform_identity();
|
|
|
|
use_motion = false;
|
|
|
|
|
2012-02-28 16:44:54 +00:00
|
|
|
type = CAMERA_PERSPECTIVE;
|
2012-05-04 16:20:51 +00:00
|
|
|
panorama_type = PANORAMA_EQUIRECTANGULAR;
|
|
|
|
fisheye_fov = M_PI_F;
|
|
|
|
fisheye_lens = 10.5f;
|
2011-04-27 11:58:34 +00:00
|
|
|
fov = M_PI_F/4.0f;
|
|
|
|
|
2012-05-04 16:20:51 +00:00
|
|
|
sensorwidth = 0.036;
|
|
|
|
sensorheight = 0.024;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
nearclip = 1e-5f;
|
|
|
|
farclip = 1e5f;
|
|
|
|
|
|
|
|
width = 1024;
|
|
|
|
height = 512;
|
|
|
|
|
|
|
|
left = -((float)width/(float)height);
|
|
|
|
right = (float)width/(float)height;
|
|
|
|
bottom = -1.0f;
|
|
|
|
top = 1.0f;
|
|
|
|
|
|
|
|
screentoworld = transform_identity();
|
|
|
|
rastertoworld = transform_identity();
|
|
|
|
ndctoworld = transform_identity();
|
|
|
|
rastertocamera = transform_identity();
|
|
|
|
cameratoworld = transform_identity();
|
|
|
|
worldtoraster = transform_identity();
|
|
|
|
|
|
|
|
dx = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
dy = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
need_update = true;
|
|
|
|
need_device_update = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera::~Camera()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::update()
|
|
|
|
{
|
|
|
|
if(!need_update)
|
|
|
|
return;
|
|
|
|
|
2011-12-20 12:25:37 +00:00
|
|
|
/* ndc to raster */
|
2011-04-27 11:58:34 +00:00
|
|
|
Transform screentocamera;
|
2011-12-20 12:25:37 +00:00
|
|
|
Transform ndctoraster = transform_scale(width, height, 1.0f);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* raster to screen */
|
2012-02-28 16:44:54 +00:00
|
|
|
Transform screentoraster = ndctoraster;
|
|
|
|
|
|
|
|
screentoraster = ndctoraster *
|
2011-04-27 11:58:34 +00:00
|
|
|
transform_scale(1.0f/(right - left), 1.0f/(top - bottom), 1.0f) *
|
|
|
|
transform_translate(-left, -bottom, 0.0f);
|
|
|
|
|
|
|
|
Transform rastertoscreen = transform_inverse(screentoraster);
|
|
|
|
|
|
|
|
/* screen to camera */
|
2012-02-28 16:44:54 +00:00
|
|
|
if(type == CAMERA_PERSPECTIVE)
|
|
|
|
screentocamera = transform_inverse(transform_perspective(fov, nearclip, farclip));
|
|
|
|
else if(type == CAMERA_ORTHOGRAPHIC)
|
2011-04-27 11:58:34 +00:00
|
|
|
screentocamera = transform_inverse(transform_orthographic(nearclip, farclip));
|
|
|
|
else
|
2012-02-28 16:44:54 +00:00
|
|
|
screentocamera = transform_identity();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
rastertocamera = screentocamera * rastertoscreen;
|
|
|
|
|
|
|
|
cameratoworld = matrix;
|
|
|
|
screentoworld = cameratoworld * screentocamera;
|
|
|
|
rastertoworld = cameratoworld * rastertocamera;
|
|
|
|
ndctoworld = rastertoworld * ndctoraster;
|
|
|
|
worldtoraster = transform_inverse(rastertoworld);
|
|
|
|
|
|
|
|
/* differentials */
|
2012-02-28 16:44:54 +00:00
|
|
|
if(type == CAMERA_ORTHOGRAPHIC) {
|
2011-04-27 11:58:34 +00:00
|
|
|
dx = transform_direction(&rastertocamera, make_float3(1, 0, 0));
|
|
|
|
dy = transform_direction(&rastertocamera, make_float3(0, 1, 0));
|
|
|
|
}
|
2012-02-28 16:44:54 +00:00
|
|
|
else if(type == CAMERA_PERSPECTIVE) {
|
2012-04-16 08:35:21 +00:00
|
|
|
dx = transform_perspective(&rastertocamera, make_float3(1, 0, 0)) -
|
|
|
|
transform_perspective(&rastertocamera, make_float3(0, 0, 0));
|
|
|
|
dy = transform_perspective(&rastertocamera, make_float3(0, 1, 0)) -
|
|
|
|
transform_perspective(&rastertocamera, make_float3(0, 0, 0));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-02-28 16:44:54 +00:00
|
|
|
else {
|
|
|
|
dx = make_float3(0, 0, 0);
|
|
|
|
dy = make_float3(0, 0, 0);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
dx = transform_direction(&cameratoworld, dx);
|
|
|
|
dy = transform_direction(&cameratoworld, dy);
|
|
|
|
|
|
|
|
need_update = false;
|
|
|
|
need_device_update = true;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
void Camera::device_update(Device *device, DeviceScene *dscene, Scene *scene)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
update();
|
|
|
|
|
|
|
|
if(!need_device_update)
|
|
|
|
return;
|
|
|
|
|
|
|
|
KernelCamera *kcam = &dscene->data.cam;
|
|
|
|
|
|
|
|
/* store matrices */
|
|
|
|
kcam->screentoworld = screentoworld;
|
|
|
|
kcam->rastertoworld = rastertoworld;
|
|
|
|
kcam->ndctoworld = ndctoworld;
|
|
|
|
kcam->rastertocamera = rastertocamera;
|
|
|
|
kcam->cameratoworld = cameratoworld;
|
|
|
|
kcam->worldtoscreen = transform_inverse(screentoworld);
|
2012-04-30 12:49:26 +00:00
|
|
|
kcam->worldtoraster = worldtoraster;
|
2011-04-27 11:58:34 +00:00
|
|
|
kcam->worldtondc = transform_inverse(ndctoworld);
|
|
|
|
kcam->worldtocamera = transform_inverse(cameratoworld);
|
|
|
|
|
2012-04-30 12:49:26 +00:00
|
|
|
/* camera motion */
|
|
|
|
Scene::MotionType need_motion = scene->need_motion();
|
2012-05-02 09:33:45 +00:00
|
|
|
kcam->have_motion = 0;
|
2012-04-30 12:49:26 +00:00
|
|
|
|
|
|
|
if(need_motion == Scene::MOTION_PASS) {
|
2012-05-07 10:53:09 +00:00
|
|
|
if(type == CAMERA_PANORAMA) {
|
|
|
|
if(use_motion) {
|
|
|
|
kcam->motion.pre = transform_inverse(motion.pre);
|
|
|
|
kcam->motion.post = transform_inverse(motion.post);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kcam->motion.pre = kcam->worldtocamera;
|
|
|
|
kcam->motion.post = kcam->worldtocamera;
|
|
|
|
}
|
2012-04-30 12:49:26 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-05-07 10:53:09 +00:00
|
|
|
if(use_motion) {
|
|
|
|
kcam->motion.pre = transform_inverse(motion.pre * rastertocamera);
|
|
|
|
kcam->motion.post = transform_inverse(motion.post * rastertocamera);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kcam->motion.pre = worldtoraster;
|
|
|
|
kcam->motion.post = worldtoraster;
|
|
|
|
}
|
2012-04-30 12:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(need_motion == Scene::MOTION_BLUR) {
|
|
|
|
/* todo: exact camera position will not be hit this way */
|
2012-05-02 09:33:45 +00:00
|
|
|
if(use_motion) {
|
|
|
|
transform_motion_decompose(&kcam->motion, &motion);
|
|
|
|
kcam->have_motion = 1;
|
|
|
|
}
|
2012-04-30 12:49:26 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* depth of field */
|
2011-09-16 13:14:02 +00:00
|
|
|
kcam->aperturesize = aperturesize;
|
2011-04-27 11:58:34 +00:00
|
|
|
kcam->focaldistance = focaldistance;
|
2011-09-16 13:14:02 +00:00
|
|
|
kcam->blades = (blades < 3)? 0.0f: blades;
|
|
|
|
kcam->bladesrotation = bladesrotation;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* motion blur */
|
2012-04-30 12:49:26 +00:00
|
|
|
kcam->shuttertime= (need_motion == Scene::MOTION_BLUR)? shuttertime: 0.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* type */
|
2012-02-28 16:44:54 +00:00
|
|
|
kcam->type = type;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-05-04 16:20:51 +00:00
|
|
|
/* panorama */
|
|
|
|
kcam->panorama_type = panorama_type;
|
|
|
|
kcam->fisheye_fov = fisheye_fov;
|
|
|
|
kcam->fisheye_lens = fisheye_lens;
|
|
|
|
|
|
|
|
/* sensor size */
|
|
|
|
kcam->sensorwidth = sensorwidth;
|
|
|
|
kcam->sensorheight = sensorheight;
|
|
|
|
|
2012-05-07 10:53:09 +00:00
|
|
|
/* render size */
|
|
|
|
kcam->width = width;
|
|
|
|
kcam->height = height;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* store differentials */
|
2011-12-20 12:25:45 +00:00
|
|
|
kcam->dx = float3_to_float4(dx);
|
|
|
|
kcam->dy = float3_to_float4(dy);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* clipping */
|
|
|
|
kcam->nearclip = nearclip;
|
|
|
|
kcam->cliplength = (farclip == FLT_MAX)? FLT_MAX: farclip - nearclip;
|
|
|
|
|
|
|
|
need_device_update = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::device_free(Device *device, DeviceScene *dscene)
|
|
|
|
{
|
|
|
|
/* nothing to free, only writing to constant memory */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera::modified(const Camera& cam)
|
|
|
|
{
|
2012-04-30 12:49:26 +00:00
|
|
|
return !((shuttertime== cam.shuttertime) &&
|
2011-09-16 13:14:02 +00:00
|
|
|
(aperturesize == cam.aperturesize) &&
|
|
|
|
(blades == cam.blades) &&
|
|
|
|
(bladesrotation == cam.bladesrotation) &&
|
2011-04-27 11:58:34 +00:00
|
|
|
(focaldistance == cam.focaldistance) &&
|
2012-02-28 16:44:54 +00:00
|
|
|
(type == cam.type) &&
|
2011-04-27 11:58:34 +00:00
|
|
|
(fov == cam.fov) &&
|
|
|
|
(nearclip == cam.nearclip) &&
|
|
|
|
(farclip == cam.farclip) &&
|
2012-05-04 16:20:51 +00:00
|
|
|
(sensorwidth == cam.sensorwidth) &&
|
|
|
|
(sensorheight == cam.sensorheight) &&
|
2011-04-27 11:58:34 +00:00
|
|
|
// modified for progressive render
|
|
|
|
// (width == cam.width) &&
|
|
|
|
// (height == cam.height) &&
|
|
|
|
(left == cam.left) &&
|
|
|
|
(right == cam.right) &&
|
|
|
|
(bottom == cam.bottom) &&
|
|
|
|
(top == cam.top) &&
|
2012-04-30 12:49:26 +00:00
|
|
|
(matrix == cam.matrix) &&
|
|
|
|
(motion == cam.motion) &&
|
2012-05-04 16:20:51 +00:00
|
|
|
(use_motion == cam.use_motion) &&
|
|
|
|
(panorama_type == cam.panorama_type) &&
|
|
|
|
(fisheye_fov == cam.fisheye_fov) &&
|
|
|
|
(fisheye_lens == cam.fisheye_lens));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::tag_update()
|
|
|
|
{
|
|
|
|
need_update = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|