2013-11-14 17:47:03 +00:00
|
|
|
#include "CameraDefinition.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
2015-12-30 23:36:42 +00:00
|
|
|
#include "Maths.h"
|
2013-11-14 17:47:03 +00:00
|
|
|
#include "PackStream.h"
|
|
|
|
#include "BoundingBox.h"
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
CameraDefinition::CameraDefinition(DefinitionNode *parent) : DefinitionNode(parent, "camera", "camera") {
|
2013-11-14 17:47:03 +00:00
|
|
|
location.x = 0.0;
|
|
|
|
location.y = 0.0;
|
|
|
|
location.z = 0.0;
|
|
|
|
direction.phi = 0.0;
|
|
|
|
direction.theta = 0.0;
|
|
|
|
direction.r = 1.0;
|
|
|
|
roll = 0.0;
|
|
|
|
|
|
|
|
width = 1.0;
|
|
|
|
height = 1.0;
|
2013-12-30 13:59:42 +00:00
|
|
|
perspective.yfov = 1.0;
|
2013-11-14 17:47:03 +00:00
|
|
|
perspective.xratio = 1.0;
|
2015-09-13 18:08:14 +00:00
|
|
|
perspective.znear = 0.5;
|
|
|
|
perspective.zfar = 20000.0;
|
2013-11-14 17:47:03 +00:00
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::save(PackStream *stream) const {
|
2013-11-14 17:47:03 +00:00
|
|
|
location.save(stream);
|
|
|
|
stream->write(&direction.r);
|
|
|
|
stream->write(&direction.phi);
|
|
|
|
stream->write(&direction.theta);
|
|
|
|
stream->write(&roll);
|
2013-12-30 13:59:42 +00:00
|
|
|
stream->write(&perspective.yfov);
|
2013-11-14 17:47:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::load(PackStream *stream) {
|
2013-11-14 17:47:03 +00:00
|
|
|
location.load(stream);
|
|
|
|
stream->read(&direction.r);
|
|
|
|
stream->read(&direction.phi);
|
|
|
|
stream->read(&direction.theta);
|
|
|
|
stream->read(&roll);
|
2013-12-30 13:59:42 +00:00
|
|
|
stream->read(&perspective.yfov);
|
2013-11-14 17:47:03 +00:00
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::copy(DefinitionNode *_destination) const {
|
|
|
|
CameraDefinition *destination = (CameraDefinition *)_destination;
|
2013-11-14 17:47:03 +00:00
|
|
|
|
|
|
|
destination->location = location;
|
|
|
|
destination->direction = direction;
|
|
|
|
destination->roll = roll;
|
|
|
|
|
2013-12-30 13:59:42 +00:00
|
|
|
destination->perspective = perspective;
|
|
|
|
|
2013-11-14 17:47:03 +00:00
|
|
|
destination->validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::validate() {
|
|
|
|
if (location.y > 300.0) {
|
2013-11-14 17:47:03 +00:00
|
|
|
location.y = 300.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
forward.x = 1.0;
|
|
|
|
forward.y = 0.0;
|
|
|
|
forward.z = 0.0;
|
|
|
|
right.x = 0.0;
|
|
|
|
right.y = 0.0;
|
|
|
|
right.z = 1.0;
|
|
|
|
up.x = 0.0;
|
|
|
|
up.y = 1.0;
|
|
|
|
up.z = 0.0;
|
|
|
|
|
2013-12-10 22:41:33 +00:00
|
|
|
Matrix4 rotation = Matrix4::newRotateEuler(direction.phi, direction.theta, roll);
|
2013-11-14 17:47:03 +00:00
|
|
|
|
|
|
|
forward = rotation.multPoint(forward);
|
|
|
|
right = rotation.multPoint(right);
|
|
|
|
up = rotation.multPoint(up);
|
|
|
|
|
|
|
|
target = location.add(direction);
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Matrix4 mperspective =
|
|
|
|
Matrix4::newPerspective(perspective.yfov, perspective.xratio, perspective.znear, perspective.zfar);
|
2013-12-10 22:41:33 +00:00
|
|
|
unperspective = mperspective.inversed();
|
|
|
|
|
|
|
|
projector = mperspective.mult(Matrix4::newLookAt(location, target, up));
|
2013-11-14 17:47:03 +00:00
|
|
|
unprojector = projector.inversed();
|
2014-08-20 14:45:45 +00:00
|
|
|
|
|
|
|
inv_x_factor = 1.0 / (0.5 * width);
|
|
|
|
inv_y_factor = 1.0 / (0.5 * height);
|
2013-11-14 17:47:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
double CameraDefinition::getRealDepth(const Vector3 &projected) const {
|
2014-08-20 14:45:45 +00:00
|
|
|
Vector3 v(projected.x * inv_x_factor - 1.0, -(projected.y * inv_x_factor - 1.0), projected.z);
|
2013-12-10 22:41:33 +00:00
|
|
|
return unperspective.transform(v).z;
|
2013-11-14 17:47:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setLocation(const Vector3 &location) {
|
2013-11-14 17:47:03 +00:00
|
|
|
this->location = location;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setLocationCoords(double x, double y, double z) {
|
2013-11-14 17:47:03 +00:00
|
|
|
location = Vector3(x, y, z);
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setTarget(const Vector3 &target) {
|
2013-11-14 17:47:03 +00:00
|
|
|
Vector3 forward;
|
|
|
|
|
|
|
|
forward = target.sub(location);
|
2015-11-09 21:30:46 +00:00
|
|
|
if (forward.getNorm() < 0.0000001) {
|
2013-11-14 17:47:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
direction = forward.toSpherical();
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setTargetCoords(double x, double y, double z) {
|
2013-11-14 17:47:03 +00:00
|
|
|
setTarget(Vector3(x, y, z));
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setRoll(double angle) {
|
2013-11-14 17:47:03 +00:00
|
|
|
roll = angle;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setZoomToTarget(double zoom) {
|
2013-11-14 17:47:03 +00:00
|
|
|
direction.r = zoom;
|
|
|
|
location = target.add(Vector3(direction).scale(-1.0));
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setFov(double fov) {
|
2013-12-30 13:59:42 +00:00
|
|
|
perspective.yfov = fov;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::strafeForward(double value) {
|
2013-11-14 17:47:03 +00:00
|
|
|
location = location.add(forward.scale(value));
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::strafeRight(double value) {
|
2013-11-14 17:47:03 +00:00
|
|
|
location = location.add(right.scale(value));
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::strafeUp(double value) {
|
2013-11-14 17:47:03 +00:00
|
|
|
location = location.add(up.scale(value));
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::rotateYaw(double value) {
|
2013-11-14 17:47:03 +00:00
|
|
|
direction.phi += value;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::rotatePitch(double value) {
|
2013-11-14 17:47:03 +00:00
|
|
|
direction.theta += value;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::rotateRoll(double value) {
|
2013-11-14 17:47:03 +00:00
|
|
|
roll += value;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void CameraDefinition::setRenderSize(int width, int height) {
|
2015-12-17 00:13:20 +00:00
|
|
|
this->width = to_double(width);
|
|
|
|
this->height = to_double(height);
|
2013-11-14 17:47:03 +00:00
|
|
|
perspective.xratio = this->width / this->height;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Vector3 CameraDefinition::project(const Vector3 &point) const {
|
2013-11-14 17:47:03 +00:00
|
|
|
Vector3 tpoint = projector.transform(point);
|
2015-11-09 21:30:46 +00:00
|
|
|
if (tpoint.z < 1.0) {
|
2013-11-14 17:47:03 +00:00
|
|
|
tpoint.x = -tpoint.x;
|
|
|
|
tpoint.y = -tpoint.y;
|
|
|
|
}
|
|
|
|
tpoint.x = (tpoint.x + 1.0) * 0.5 * width;
|
|
|
|
tpoint.y = (-tpoint.y + 1.0) * 0.5 * height;
|
|
|
|
return tpoint;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Vector3 CameraDefinition::unproject(const Vector3 &point) const {
|
2013-11-14 17:47:03 +00:00
|
|
|
Vector3 tpoint(point.x / (0.5 * width) - 1.0, -(point.y / (0.5 * height) - 1.0), point.z);
|
2015-11-09 21:30:46 +00:00
|
|
|
if (tpoint.z < 1.0) {
|
2013-11-14 17:47:03 +00:00
|
|
|
tpoint.x = -tpoint.x;
|
|
|
|
tpoint.y = -tpoint.y;
|
|
|
|
}
|
|
|
|
return unprojector.transform(tpoint);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
int CameraDefinition::isBoxInView(const Vector3 ¢er, double xsize, double ysize, double zsize) const {
|
2013-11-14 17:47:03 +00:00
|
|
|
BoundingBox box;
|
|
|
|
|
2013-12-11 10:32:10 +00:00
|
|
|
box.pushPoint(center.add(Vector3(-xsize, -ysize, -zsize)));
|
|
|
|
box.pushPoint(center.add(Vector3(xsize, ysize, zsize)));
|
2013-11-14 17:47:03 +00:00
|
|
|
|
|
|
|
return isUnprojectedBoxInView(box);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
int CameraDefinition::isUnprojectedBoxInView(const BoundingBox &box) const {
|
2013-11-14 17:47:03 +00:00
|
|
|
BoundingBox projected;
|
|
|
|
|
|
|
|
projected.pushPoint(project(Vector3(box.xmin, box.ymin, box.zmin)));
|
|
|
|
projected.pushPoint(project(Vector3(box.xmax, box.ymin, box.zmin)));
|
|
|
|
projected.pushPoint(project(Vector3(box.xmin, box.ymax, box.zmin)));
|
|
|
|
projected.pushPoint(project(Vector3(box.xmax, box.ymax, box.zmin)));
|
|
|
|
projected.pushPoint(project(Vector3(box.xmin, box.ymin, box.zmax)));
|
|
|
|
projected.pushPoint(project(Vector3(box.xmax, box.ymin, box.zmax)));
|
|
|
|
projected.pushPoint(project(Vector3(box.xmin, box.ymax, box.zmax)));
|
|
|
|
projected.pushPoint(project(Vector3(box.xmax, box.ymax, box.zmax)));
|
|
|
|
|
|
|
|
return isProjectedBoxInView(projected);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
int CameraDefinition::isProjectedBoxInView(const BoundingBox &box) const {
|
|
|
|
if (box.xmin <= width && box.xmax >= 0.0 && box.ymin <= height && box.ymax >= 0.0 &&
|
|
|
|
box.zmax >= perspective.znear) {
|
2013-11-14 17:47:03 +00:00
|
|
|
double dx = box.xmax - box.xmin;
|
|
|
|
double dy = box.ymax - box.ymin;
|
|
|
|
|
2015-12-17 00:13:20 +00:00
|
|
|
return ceil_to_int(dx) * ceil_to_int(dy);
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2014-01-02 14:08:00 +00:00
|
|
|
return 0;
|
2013-11-14 17:47:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
bool CameraDefinition::transitionToAnother(const CameraDefinition *wanted, double factor) {
|
2013-11-14 17:47:03 +00:00
|
|
|
double dx, dy, dz, dr, dphi, dtheta, droll;
|
|
|
|
|
|
|
|
dx = wanted->location.x - location.x;
|
|
|
|
dy = wanted->location.y - location.y;
|
|
|
|
dz = wanted->location.z - location.z;
|
|
|
|
dr = wanted->direction.r - direction.r;
|
2015-12-30 23:36:42 +00:00
|
|
|
dphi = Maths::modInRange(wanted->direction.phi - direction.phi, -Maths::PI, Maths::PI);
|
|
|
|
dtheta = Maths::modInRange(wanted->direction.theta - direction.theta, -Maths::PI, Maths::PI);
|
|
|
|
droll = Maths::modInRange(wanted->roll - roll, -Maths::PI, Maths::PI);
|
2013-11-14 17:47:03 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (fabs(dx) < 0.000001 && fabs(dy) < 0.000001 && fabs(dz) < 0.000001 && fabs(dr) < 0.000001 &&
|
|
|
|
fabs(dphi) < 0.000001 && fabs(dtheta) < 0.000001 && fabs(droll) < 0.000001) {
|
2013-11-14 17:47:03 +00:00
|
|
|
return false;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2013-11-14 17:47:03 +00:00
|
|
|
location.x += dx * factor;
|
|
|
|
location.y += dy * factor;
|
|
|
|
location.z += dz * factor;
|
|
|
|
direction.r += dr * factor;
|
|
|
|
direction.phi += dphi * factor;
|
|
|
|
direction.theta += dtheta * factor;
|
|
|
|
roll += droll * factor;
|
|
|
|
|
|
|
|
validate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|