diff options
| author | John Wilbert M. Villamor <lameguy64@gmail.com> | 2020-04-24 19:01:28 +0800 |
|---|---|---|
| committer | John Wilbert M. Villamor <lameguy64@gmail.com> | 2020-04-24 19:01:28 +0800 |
| commit | 1aa0e17df7c325a41de8cf8a57f52ed853f08bf3 (patch) | |
| tree | 5ec7f69ca0104f2b0a41e2ee7d3cb0cf0c9c54c5 /examples/graphics/fpscam/clip.c | |
| parent | e82da2abe4c264d4b48a48d79cf9b8e4c4fb8ab6 (diff) | |
| download | psn00bsdk-1aa0e17df7c325a41de8cf8a57f52ed853f08bf3.tar.gz | |
Refined toolchain instructions, organized examples, added automatic retry for CdRead(), added FIOCSCAN ioctl in psxsio TTY driver, added tty and console examples.
Diffstat (limited to 'examples/graphics/fpscam/clip.c')
| -rw-r--r-- | examples/graphics/fpscam/clip.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/examples/graphics/fpscam/clip.c b/examples/graphics/fpscam/clip.c new file mode 100644 index 0000000..7f2b780 --- /dev/null +++ b/examples/graphics/fpscam/clip.c @@ -0,0 +1,108 @@ +/* Polygon clip detection code + * + * The polygon clipping logic is based on the Cohen-Sutherland algorithm, but + * only the off-screen detection logic is used to determine which polygon edges + * are off-screen. + * + * In tri_clip, the following edges are checked as follows: + * + * |\ + * | \ + * | \ + * | \ + * |-------- + * + * In quad_clip, the following edges are checked as follows: + * + * |---------| + * | \ / | + * | \ / | + * | / \ | + * | / \ | + * |---------| + * + * The inner portion of the quad is checked, otherwise the quad will be + * culled out if the camera faces right into it, where all four edges + * are off-screen at once. + * + */ + +#include "clip.h" + +#define CLIP_LEFT 1 +#define CLIP_RIGHT 2 +#define CLIP_TOP 4 +#define CLIP_BOTTOM 8 + +int test_clip(RECT *clip, short x, short y) { + + // Tests which corners of the screen a point lies outside of + + int result = 0; + + if ( x < clip->x ) { + result |= CLIP_LEFT; + } + + if ( x >= (clip->x+(clip->w-1)) ) { + result |= CLIP_RIGHT; + } + + if ( y < clip->y ) { + result |= CLIP_TOP; + } + + if ( y >= (clip->y+(clip->h-1)) ) { + result |= CLIP_BOTTOM; + } + + return result; + +} + +int tri_clip(RECT *clip, DVECTOR *v0, DVECTOR *v1, DVECTOR *v2) { + + // Returns non-zero if a triangle is outside the screen boundaries + + short c[3]; + + c[0] = test_clip(clip, v0->vx, v0->vy); + c[1] = test_clip(clip, v1->vx, v1->vy); + c[2] = test_clip(clip, v2->vx, v2->vy); + + if ( ( c[0] & c[1] ) == 0 ) + return 0; + if ( ( c[1] & c[2] ) == 0 ) + return 0; + if ( ( c[2] & c[0] ) == 0 ) + return 0; + + return 1; +} + +int quad_clip(RECT *clip, DVECTOR *v0, DVECTOR *v1, DVECTOR *v2, DVECTOR *v3) { + + // Returns non-zero if a quad is outside the screen boundaries + + short c[4]; + + c[0] = test_clip(clip, v0->vx, v0->vy); + c[1] = test_clip(clip, v1->vx, v1->vy); + c[2] = test_clip(clip, v2->vx, v2->vy); + c[3] = test_clip(clip, v3->vx, v3->vy); + + if ( ( c[0] & c[1] ) == 0 ) + return 0; + if ( ( c[1] & c[2] ) == 0 ) + return 0; + if ( ( c[2] & c[3] ) == 0 ) + return 0; + if ( ( c[3] & c[0] ) == 0 ) + return 0; + if ( ( c[0] & c[2] ) == 0 ) + return 0; + if ( ( c[1] & c[3] ) == 0 ) + return 0; + + return 1; +}
\ No newline at end of file |
