fs2port/port/include/projection.h
2026-05-13 21:32:05 -05:00

39 lines
1.5 KiB
C

// 3D -> 2D projection and frustum classification.
//
// Uses a 90 deg horizontal/vertical FOV (matching the original FS2
// frustum) and the Apple II hires viewport (280 wide, rows 0..98).
// All camera-space coordinates are Q16.16 world-units (matching
// `CameraT::worldX/Y/Z`); screen output is integer pixels.
#ifndef PROJECTION_H
#define PROJECTION_H
#include <stdbool.h>
#include <stdint.h>
#include "types.h"
typedef struct ProjectedT {
int32_t cx; // camera-space X (Q16.16 world units)
int32_t cy; // camera-space Y
int32_t cz; // camera-space Z (positive = in front)
int16_t screenX; // post-projection screen column
int16_t screenY; // post-projection screen row
uint8_t outcode; // OUTCODE_* bits
} ProjectedT;
// Compute frustum outcodes for a camera-space point (Q16.16).
uint8_t projectionOutcode(int32_t cx_q1616, int32_t cy_q1616, int32_t cz_q1616);
// Project a camera-space point onto the screen. Returns true if the
// point is visible (outcode == 0). When false, screenX / screenY are
// undefined.
bool projectionToScreen(int32_t cx_q1616, int32_t cy_q1616, int32_t cz_q1616, int16_t *outX, int16_t *outY);
// Trivial-reject + trivial-accept Cohen-Sutherland clip in 3D.
// On entry both endpoints are camera-space + outcode-stamped. On
// exit, if true, the endpoints have been moved onto the visible
// region of the frustum. Returns false if the line is wholly
// outside.
bool projectionClipLine(ProjectedT *a, ProjectedT *b);
#endif