30 lines
1.2 KiB
C++
30 lines
1.2 KiB
C++
|
#include "Camera.h"
|
||
|
#include <numbers>
|
||
|
|
||
|
void raytry::Camera::for_each_ray(const QImage &image, const std::function<void(int, int, raytry::Ray)> &consumer) {
|
||
|
const auto widthf = static_cast<float>(image.width());
|
||
|
const auto heightf = static_cast<float>(image.height());
|
||
|
const float widthToHeight = widthf / heightf;
|
||
|
const float tan = std::tan(std::max(1.0f, std::min(hfov, 179.0f)) * std::numbers::pi_v<float> / 360);
|
||
|
const float xblub = 2 * planeDistance * tan / widthf;
|
||
|
QVector3D zeroOrigin{
|
||
|
positionVec.x() - (xblub * (widthf / 2)),
|
||
|
positionVec.y(),
|
||
|
positionVec.z() + (xblub * widthToHeight * (heightf / 2))};
|
||
|
zeroOrigin += forwardVec * planeDistance;
|
||
|
for (int y = 0; y < image.height(); ++y) {
|
||
|
for (int x = 0; x < image.width(); ++x) {
|
||
|
const QVector3D &directionVec = zeroOrigin + QVector3D(static_cast<float>(x) * xblub, 0.0f, -static_cast<float>(y) * xblub * widthToHeight);
|
||
|
consumer(x, y, Ray(positionVec, (directionVec - positionVec).normalized()));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const QVector3D &raytry::Camera::pos() const {
|
||
|
return positionVec;
|
||
|
}
|
||
|
|
||
|
const QVector3D &raytry::Camera::fwd() const {
|
||
|
return forwardVec;
|
||
|
}
|