near plane clipping partial

This commit is contained in:
2026-02-21 16:28:09 +01:00
parent 7c03b1b81d
commit 7ca48a5080
9 changed files with 271 additions and 37 deletions
+44 -4
View File
@@ -3,10 +3,11 @@
// please, dont. if you do continue, edit the value below
// as a warning for future you.
// hoursWasted = 5
// hoursWasted = 8
#include <SDL3/SDL.h>
#include <cmath>
#include <float.h>
#include "renderer/camera.hpp"
#include "globals.hpp"
@@ -16,8 +17,8 @@ DeltaCamera::DeltaCamera(float x, float y, float z) {
angle = DeltaVector2();
}
DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) {
DeltaVector3 calculatedPos; DeltaVector2 projected;
DeltaVector3 DeltaCamera::translatePoint(const DeltaVector3 &worldPos) {
DeltaVector3 calculatedPos;
float sineX = std::sin(angle.x);
float cosineX = std::cos(angle.x);
@@ -39,11 +40,23 @@ DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) {
calculatedPos.z = t.z * cosineY + ry * sineY;
// dont touch calculatedPos.x
// calculatedPos.y = calculatedPos.y * cosineY;
// 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;
@@ -51,6 +64,33 @@ DeltaVector2 DeltaCamera::projectPointVec(const DeltaVector3 &worldPos) {
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 });