Add a near-z line clipping function in preparation for implementing near-z clipping

This commit is contained in:
Luke Benstead 2018-05-31 09:38:34 +01:00
parent 9167869633
commit 4a154d9c4e
4 changed files with 58 additions and 2 deletions

34
GL/clip.c Normal file
View File

@ -0,0 +1,34 @@
#include <float.h>
#include "clip.h"
ClipResult clipLineToNearZ(const float* v1, const float* v2, const float dist, float* vout, float* t) {
if(v1[2] < dist && v2[2] < dist) {
// Both behind, no clipping
return CLIP_RESULT_ALL_BEHIND;
}
if(v1[2] > dist && v2[2] > dist) {
return CLIP_RESULT_ALL_IN_FRONT;
}
float vec [] = {v2[0] - v1[0], v2[1] - v1[1], v2[2] - v1[2]};
/*
* The plane normal will always be pointing down the negative Z so we can simplify the dot products as x and y will always be zero
* the resulting calculation will result in simply -z of the vector
*/
float vecDotP = -vec[2];
/* If the dot product is zero there is no intersection */
if(vecDotP > FLT_MIN || vecDotP < -FLT_MIN) {
*t = (-(dist - v1[2])) / vecDotP;
vout[0] = v1[0] + (vec[0] * (*t));
vout[1] = v1[1] + (vec[1] * (*t));
vout[2] = v1[2] + (vec[2] * (*t));
return (v1[2] >= dist) ? CLIP_RESULT_FRONT_TO_BACK : CLIP_RESULT_BACK_TO_FRONT;
} else {
return CLIP_RESULT_ALL_ON_PLANE;
}
}

22
GL/clip.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef CLIP_H
#define CLIP_H
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
CLIP_RESULT_ALL_IN_FRONT,
CLIP_RESULT_ALL_BEHIND,
CLIP_RESULT_ALL_ON_PLANE,
CLIP_RESULT_FRONT_TO_BACK,
CLIP_RESULT_BACK_TO_FRONT
} ClipResult;
ClipResult clipLineToNearZ(const float* v1, const float* v2, const float dist, float* vout, float* t);
#ifdef __cplusplus
}
#endif
#endif // CLIP_H

View File

@ -4,7 +4,7 @@
#include "../include/gl.h"
#include "../containers/aligned_vector.h"
#include "../containers/named_array.h"
#include "./clip.h"
#define TRACE_ENABLED 0
#define TRACE() if(TRACE_ENABLED) {fprintf(stderr, "%s\n", __func__);}

View File

@ -7,7 +7,7 @@
TARGET = libGLdc.a
OBJS = GL/draw.o GL/flush.o GL/framebuffer.o GL/immediate.o GL/lighting.o GL/state.o GL/texture.o GL/glu.o
OBJS += GL/matrix.o GL/fog.o GL/error.o containers/stack.o containers/named_array.o containers/aligned_vector.o
OBJS += GL/matrix.o GL/fog.o GL/error.o GL/clip.o containers/stack.o containers/named_array.o containers/aligned_vector.o
SUBDIRS =