97 lines
2.9 KiB
C++
97 lines
2.9 KiB
C++
// please do not touch this code.
|
|
// it is a waste of time looking at this code.
|
|
// please, dont. if you do continue, edit the value below
|
|
// as a warning for future you.
|
|
|
|
// hoursWasted = 8
|
|
|
|
#include <SDL3/SDL.h>
|
|
#include <cmath>
|
|
#include <float.h>
|
|
|
|
#include "renderer/camera.hpp"
|
|
#include "globals.hpp"
|
|
|
|
DeltaCamera::DeltaCamera(float x, float y, float z) {
|
|
pos = DeltaVector3(x, y, z);
|
|
angle = DeltaVector2();
|
|
}
|
|
|
|
DeltaVector3 DeltaCamera::translatePoint(const DeltaVector3 &worldPos) {
|
|
DeltaVector3 calculatedPos;
|
|
float sineX = std::sin(angle.x);
|
|
float cosineX = std::cos(angle.x);
|
|
|
|
float sineY = std::sin(angle.y);
|
|
float cosineY = std::cos(angle.y);
|
|
|
|
// yaw translate
|
|
|
|
DeltaVector3 t = worldPos - pos;
|
|
|
|
float rx = t.x * cosineX - t.y * sineX;
|
|
float ry = t.y * cosineX + t.x * sineX;
|
|
float rz = (worldPos.z - pos.z);
|
|
|
|
// pitch translate
|
|
|
|
calculatedPos.x = rx;
|
|
calculatedPos.y = ry * cosineY - t.z * sineY;
|
|
calculatedPos.z = t.z * cosineY + ry * sineY;
|
|
// dont touch calculatedPos.x
|
|
|
|
// if (calculatedPos.y <= 0.1f) return {-FLT_MAX, 0, 0};
|
|
|
|
return calculatedPos;
|
|
}
|
|
|
|
DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) {
|
|
DeltaVector3 calculatedPos; DeltaVector2 projected;
|
|
calculatedPos = worldPos;
|
|
|
|
// calculatedPos.y += 0.00001f;
|
|
|
|
projected.x = calculatedPos.x * FOCAL / calculatedPos.y + SIMULATED_WIDTH / 2.0f;
|
|
projected.y = calculatedPos.z * FOCAL / calculatedPos.y + SIMULATED_HEIGHT / 2.0f;
|
|
|
|
// projected.x = std::min((float)SIMULATED_WIDTH, std::max(0.0f, projected.x));
|
|
// projected.x = std::min((float)SIMULATED_HEIGHT, std::max(0.0f, projected.y));
|
|
|
|
// now gotta convert y to our y
|
|
projected.y = SIMULATED_HEIGHT - projected.y;
|
|
|
|
// we are done =D
|
|
return projected;
|
|
}
|
|
|
|
int DeltaCamera::projectTriangle(const DeltaTriangle &in, DeltaTriangle2D &out1, DeltaTriangle2D &out2) {
|
|
DeltaTriangle translated = { translatePoint(in.a), translatePoint(in.b), translatePoint(in.c) }, clipped1, clipped2;
|
|
|
|
int numClipped = clipTriangleAgainstNearPlane(0.01, translated, clipped1, clipped2);
|
|
if (numClipped == 1) {
|
|
out1.a = projectPointVec(clipped1.a);
|
|
out1.b = projectPointVec(clipped1.b);
|
|
out1.c = projectPointVec(clipped1.c);
|
|
|
|
// out1.a = projectPointVec(translated.a);
|
|
// out1.b = projectPointVec(translated.b);
|
|
// out1.c = projectPointVec(translated.c);
|
|
}
|
|
|
|
if (numClipped == 2) {
|
|
|
|
out1.a = projectPointVec(clipped1.a);
|
|
out1.b = projectPointVec(clipped1.b);
|
|
out1.c = projectPointVec(clipped1.c);
|
|
|
|
out2.a = projectPointVec(clipped2.a);
|
|
out2.b = projectPointVec(clipped2.b);
|
|
out2.c = projectPointVec(clipped2.c);
|
|
}
|
|
return numClipped;
|
|
}
|
|
|
|
DeltaVector2 DeltaCamera::projectPoint(float worldX, float worldY, float worldZ) {
|
|
// simple conversion, dont iterate further
|
|
return projectPointVec({ worldX, worldY, worldZ });
|
|
} |