2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 14.01.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Camera.h"
|
|
|
|
#include "utils/Log.h"
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
std::vector<Triangle> &Camera::project(std::shared_ptr<Mesh> mesh) {
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
if(!_ready) {
|
|
|
|
Log::log("Camera::project(): cannot project _tris without camera initialization ( Camera::init() ) ");
|
|
|
|
return this->_triangles;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
if(!mesh->isVisible())
|
|
|
|
return this->_triangles;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
// Model transform matrix: translate _tris in the origin of body.
|
|
|
|
Matrix4x4 M = Matrix4x4::Translation(mesh->position());
|
|
|
|
Matrix4x4 VM = _V * M;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
// We don't want to waste time re-allocating memory every time
|
|
|
|
std::vector<Triangle> clippedTriangles, tempBuffer;
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto& t : mesh->triangles()) {
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
double dot = t.norm().dot((mesh->position() + t[0] - _position).normalized());
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
if(dot > 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Triangle clipped[2];
|
|
|
|
// It needs to be cleared because it's reused through iterations. Usually it doesn't free memory.
|
|
|
|
clippedTriangles.clear();
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
// In the beginning we need to to translate drawTriangle from world coordinate to our camera system:
|
|
|
|
// After that we apply clipping for all planes from _clipPlanes
|
2021-09-13 15:53:43 +03:00
|
|
|
clippedTriangles.emplace_back(t * VM);
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto& plane : _clipPlanes)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
|
|
|
while(!clippedTriangles.empty())
|
|
|
|
{
|
|
|
|
clipped[0] = clippedTriangles.back();
|
|
|
|
clipped[1] = clipped[0];
|
|
|
|
clippedTriangles.pop_back();
|
|
|
|
int additional = plane.clip(clipped[0], clipped[1]);
|
|
|
|
|
|
|
|
for(int i = 0; i < additional; i++)
|
|
|
|
tempBuffer.emplace_back(clipped[i]);
|
|
|
|
}
|
|
|
|
clippedTriangles.swap(tempBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto& clippedTriangle : clippedTriangles) {
|
2021-09-19 11:25:10 +03:00
|
|
|
sf::Color color = clippedTriangle.color();
|
|
|
|
sf::Color ambientColor = sf::Color(color.r * (0.3 * std::abs(dot) + 0.7),
|
|
|
|
color.g * (0.3 * std::abs(dot) + 0.7),
|
|
|
|
color.b * (0.3 * std::abs(dot) + 0.7),
|
|
|
|
color.a);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
// Finally its time to project our clipped colored drawTriangle from 3D -> 2D
|
2021-09-13 15:53:43 +03:00
|
|
|
// and transform it's coordinate to screen space (in pixels):
|
2021-09-19 11:25:10 +03:00
|
|
|
clippedTriangle = clippedTriangle * _SP;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
clippedTriangle = Triangle(clippedTriangle[0] / clippedTriangle[0].w(),
|
|
|
|
clippedTriangle[1] / clippedTriangle[1].w(),
|
|
|
|
clippedTriangle[2] / clippedTriangle[2].w(),
|
|
|
|
ambientColor);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
_triangles.emplace_back(clippedTriangle);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
return this->_triangles;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::init(int width, int height, double fov, double ZNear, double ZFar) {
|
|
|
|
// We need to init camera only after creation or changing width, height, fov, ZNear or ZFar.
|
|
|
|
// Because here we calculate matrix that does not change during the motion of _objects or camera
|
2021-09-19 11:25:10 +03:00
|
|
|
_w = width; _h = height;
|
|
|
|
_aspect = (double)width / (double)height;
|
|
|
|
_P = Matrix4x4::Projection(fov, _aspect, ZNear, ZFar);
|
|
|
|
_S = Matrix4x4::ScreenSpace(width, height);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
_SP = _S * _P; // screen-space-projections matrix
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
// This is planes for clipping _tris.
|
|
|
|
// Motivation: we are not interest in _tris that we cannot see.
|
|
|
|
_clipPlanes.emplace_back(Plane(Point4D{0, 0, 1}, Point4D{0, 0, ZNear})); // near plane
|
|
|
|
_clipPlanes.emplace_back(Plane(Point4D{0, 0, -1}, Point4D{0, 0, ZFar})); // far plane
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
double thetta1 = M_PI*fov*0.5/180.0;
|
2021-09-19 11:25:10 +03:00
|
|
|
double thetta2 = atan(_aspect * tan(thetta1));
|
|
|
|
_clipPlanes.emplace_back(Plane(Point4D{-cos(thetta2), 0, sin(thetta2)}, Point4D{0, 0, 0})); // left plane
|
|
|
|
_clipPlanes.emplace_back(Plane(Point4D{cos(thetta2), 0, sin(thetta2)}, Point4D{0, 0, 0})); // right plane
|
|
|
|
_clipPlanes.emplace_back(Plane(Point4D{0, cos(thetta1), sin(thetta1)}, Point4D{0, 0, 0})); // down plane
|
|
|
|
_clipPlanes.emplace_back(Plane(Point4D{0, -cos(thetta1), sin(thetta1)}, Point4D{0, 0, 0})); // up plane
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
_ready = true;
|
2021-09-13 15:53:43 +03:00
|
|
|
Log::log("Camera::init(): camera successfully initialized.");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Triangle> &Camera::sorted() {
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
// Sort _tris from back to front
|
2021-09-13 15:53:43 +03:00
|
|
|
// This is some replacement for Z-buffer
|
2021-09-19 11:25:10 +03:00
|
|
|
std::sort(_triangles.begin(), _triangles.end(), [](Triangle &t1, Triangle &t2)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
|
|
|
std::vector<double> v_z1({t1[0].z(), t1[1].z(), t1[2].z()});
|
|
|
|
std::vector<double> v_z2({t2[0].z(), t2[1].z(), t2[2].z()});
|
|
|
|
|
|
|
|
std::sort(v_z1.begin(), v_z1.end());
|
|
|
|
std::sort(v_z2.begin(), v_z2.end());
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
double z1 = v_z1[0] + v_z1[1] + v_z1[2];
|
|
|
|
double z2 = v_z2[0] + v_z2[1] + v_z2[2];
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
return z1 > z2;
|
|
|
|
});
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
return _triangles;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
void Camera::clear() {
|
|
|
|
// Cleaning all _tris and recalculation of View matrix
|
2021-09-13 15:53:43 +03:00
|
|
|
// That is like preparation for new camera shot: we need to set
|
|
|
|
// the position of camera and insert new cartridge for photo.
|
2021-09-19 11:25:10 +03:00
|
|
|
_triangles.clear();
|
|
|
|
_V = Matrix4x4::View(_left, _up, _lookAt, _position);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotateX(double rx) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_angle = Point4D{_angle.x() + rx, _angle.y(), _angle.z()};
|
|
|
|
_left = Matrix4x4::RotationX(rx) * _left;
|
|
|
|
_up = Matrix4x4::RotationX(rx) * _up;
|
|
|
|
_lookAt = Matrix4x4::RotationX(rx) * _lookAt;
|
2021-09-14 13:47:53 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
2021-09-14 13:47:53 +03:00
|
|
|
attached->rotateRelativePoint(position(), Point4D{rx, 0, 0});
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotateY(double ry) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_angle = Point4D{_angle.x(), _angle.y() + ry, _angle.z()};
|
|
|
|
_left = Matrix4x4::RotationY(ry) * _left;
|
|
|
|
_up = Matrix4x4::RotationY(ry) * _up;
|
|
|
|
_lookAt = Matrix4x4::RotationY(ry) * _lookAt;
|
2021-09-14 13:47:53 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
2021-09-14 13:47:53 +03:00
|
|
|
attached->rotateRelativePoint(position(), Point4D{0, ry, 0});
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotateZ(double rz) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_angle = Point4D{_angle.x(), _angle.y(), _angle.z() + rz};
|
|
|
|
_left = Matrix4x4::RotationZ(rz) * _left;
|
|
|
|
_up = Matrix4x4::RotationZ(rz) * _up;
|
|
|
|
_lookAt = Matrix4x4::RotationZ(rz) * _lookAt;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
2021-09-14 13:47:53 +03:00
|
|
|
attached->rotateRelativePoint(position(), Point4D{0, 0, rz});
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotate(const Point4D& r) {
|
2021-09-14 13:47:53 +03:00
|
|
|
rotateX(r.x());
|
|
|
|
rotateY(r.y());
|
|
|
|
rotateZ(r.z());
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Camera::rotate(const Point4D& v, double rv) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_left = Matrix4x4::Rotation(v, rv) * _left;
|
|
|
|
_up = Matrix4x4::Rotation(v, rv) * _up;
|
|
|
|
_lookAt = Matrix4x4::Rotation(v, rv) * _lookAt;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
2021-09-13 15:53:43 +03:00
|
|
|
attached->rotateRelativePoint(position(), v, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotateLeft(double rl) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_angleLeftUpLookAt = Point4D{_angleLeftUpLookAt.x() + rl, _angleLeftUpLookAt.y(), _angleLeftUpLookAt.z()};
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
rotate(_left, rl);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
|
|
|
attached->rotateRelativePoint(position(), _left, rl);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotateUp(double ru) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_angleLeftUpLookAt = Point4D{_angleLeftUpLookAt.x(), _angleLeftUpLookAt.y() + ru, _angleLeftUpLookAt.z()};
|
|
|
|
rotate(_up, ru);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
|
|
|
attached->rotateRelativePoint(position(), _up, ru);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotateLookAt(double rlAt) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_angleLeftUpLookAt = Point4D{_angleLeftUpLookAt.x(), _angleLeftUpLookAt.y(), _angleLeftUpLookAt.z() + rlAt};
|
|
|
|
rotate(_lookAt, rlAt);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
|
|
|
attached->rotateRelativePoint(position(), _lookAt, rlAt);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
void Camera::rotateRelativePoint(const Point4D &s, const Point4D &r) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_angle = _angle + r;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
// Translate XYZ by vector r1
|
2021-09-19 11:25:10 +03:00
|
|
|
Point4D r1 = _position - s;
|
2021-09-13 15:53:43 +03:00
|
|
|
// In translated coordinate system we rotate camera and position
|
2021-09-14 13:47:53 +03:00
|
|
|
Point4D r2 = Matrix4x4::Rotation(r)*r1;
|
|
|
|
rotate(r);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
// After rotation we translate XYZ by vector -r2 and recalculate position
|
2021-09-19 11:25:10 +03:00
|
|
|
_position = s + r2;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
2021-09-14 13:47:53 +03:00
|
|
|
attached->rotateRelativePoint(s, r);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::rotateRelativePoint(const Point4D &s, const Point4D &v, double r) {
|
|
|
|
// Translate XYZ by vector r1
|
2021-09-19 11:25:10 +03:00
|
|
|
Point4D r1 = _position - s;
|
2021-09-13 15:53:43 +03:00
|
|
|
// In translated coordinate system we rotate camera and position
|
|
|
|
Point4D r2 = Matrix4x4::Rotation(v, r)*r1;
|
|
|
|
rotate(v, r);
|
|
|
|
|
|
|
|
// After rotation we translate XYZ by vector -r2 and recalculate position
|
2021-09-19 11:25:10 +03:00
|
|
|
_position = s + r2;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
for(auto attached : _attachedObjects)
|
2021-09-13 15:53:43 +03:00
|
|
|
attached->rotateRelativePoint(s, v, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::translateToPoint(const Point4D &point) {
|
2021-09-19 11:25:10 +03:00
|
|
|
translate(point - _position);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|