Start cleaning up w handling

This commit is contained in:
Luke Benstead 2018-06-26 08:31:31 +01:00
parent 09789c9adc
commit 46e1a9bcc5
3 changed files with 48 additions and 23 deletions

View File

@ -38,9 +38,29 @@ ClipResult clipLineToNearZ(const float* v1, const float* v2, const float dist, f
TriangleClipResult clipTriangleToNearZ( TriangleClipResult clipTriangleToNearZ(
const float plane_dist, const float plane_dist,
const unsigned short triangle_n, const pvr_vertex_t* v1, const pvr_vertex_t* v2, const pvr_vertex_t *v3, const pvr_vertex_t** vertices_in,
pvr_vertex_t* v1out, pvr_vertex_t* v2out, pvr_vertex_t* v3out, pvr_vertex_t* v4out, unsigned char* visible const float* w_coordinates_in,
pvr_vertex_t** vertices_out,
float* w_coordinates_out,
unsigned char* visible
) { ) {
const pvr_vertex_t* v1 = vertices_in[0];
const pvr_vertex_t* v2 = vertices_in[1];
const pvr_vertex_t* v3 = vertices_in[2];
const float w1 = w_coordinates_in[0];
const float w2 = w_coordinates_in[1];
const float w3 = w_coordinates_in[2];
pvr_vertex_t* v1out = vertices_out[0];
pvr_vertex_t* v2out = vertices_out[1];
pvr_vertex_t* v3out = vertices_out[2];
pvr_vertex_t* v4out = vertices_out[3];
float* w1out = w_coordinates_out[0];
float* w2out = w_coordinates_out[1];
float* w3out = w_coordinates_out[2];
float* w4out = w_coordinates_out[3];
/* Fast out. Let's just see if everything is in front of the clip plane (and as in OpenGL Z comes out of the screen /* Fast out. Let's just see if everything is in front of the clip plane (and as in OpenGL Z comes out of the screen
* we check to see if they are all < -dist * we check to see if they are all < -dist
@ -73,29 +93,38 @@ TriangleClipResult clipTriangleToNearZ(
float t1, t2; float t1, t2;
if(visible == 0b001) { if(visible == 0b001) {
ClipResult l1 = clipLineToNearZ(&v1->x, &v2->x, plane_dist, &tmp1.x, &t1); clipLineToNearZ(&v1->x, &v2->x, plane_dist, &tmp1.x, &t1);
ClipResult l2 = clipLineToNearZ(&v1->x, &v3->x, plane_dist, &tmp2.x, &t2); clipLineToNearZ(&v1->x, &v3->x, plane_dist, &tmp2.x, &t2);
fprintf(stderr, "New V2: %f, %f, %f. T: %f\n", tmp1.x, tmp1.y, tmp1.z, t1);
fprintf(stderr, "New V3: %f, %f, %f. T: %f\n", tmp2.x, tmp2.y, tmp2.z, t2);
*v1out = *v1; *v1out = *v1;
*v2out = tmp1; *v2out = tmp1;
*v3out = tmp2; *v3out = tmp2;
*w1out = w1;
*w2out = w1 + ((w2 - w1) * t1);
*w3out = w1 + ((w3 - w1) * t2);
} else if(visible == 0b010) { } else if(visible == 0b010) {
ClipResult l1 = clipLineToNearZ(&v2->x, &v1->x, plane_dist, &tmp1.x, &t1); clipLineToNearZ(&v2->x, &v1->x, plane_dist, &tmp1.x, &t1);
ClipResult l2 = clipLineToNearZ(&v2->x, &v3->x, plane_dist, &tmp2.x, &t2); clipLineToNearZ(&v2->x, &v3->x, plane_dist, &tmp2.x, &t2);
*v1out = tmp1; *v1out = tmp1;
*v2out = *v2; *v2out = *v2;
*v3out = tmp2; *v3out = tmp2;
*w1out = w2 + ((w1 - w2) * t1);
*w2out = w2;
*w3out = w2 + ((w3 - w2) * t2);
} else { } else {
ClipResult l1 = clipLineToNearZ(&v3->x, &v1->x, plane_dist, &tmp1.x, &t1); clipLineToNearZ(&v3->x, &v1->x, plane_dist, &tmp1.x, &t1);
ClipResult l2 = clipLineToNearZ(&v3->x, &v2->x, plane_dist, &tmp2.x, &t2); clipLineToNearZ(&v3->x, &v2->x, plane_dist, &tmp2.x, &t2);
*v1out = tmp1; *v1out = tmp1;
*v2out = tmp2; *v2out = tmp2;
*v3out = *v3; *v3out = *v3;
*w1out = w3 + ((w3 - w1) * t1);
*w2out = w3 + ((w3 - w2) * t2);
*w3out = w3;
} }
return TRIANGLE_CLIP_RESULT_ALTERED_VERTICES; return TRIANGLE_CLIP_RESULT_ALTERED_VERTICES;

View File

@ -57,9 +57,9 @@ typedef enum {
* The outputs can be the same as the inputs. * The outputs can be the same as the inputs.
*/ */
TriangleClipResult clipTriangleToNearZ( TriangleClipResult clipTriangleToNearZ(
const float plane_dist, const float plane_dist, const pvr_vertex_t** vertices_in, const float* w_coordinates_in,
const unsigned short triangle_n, const pvr_vertex_t* v1, const pvr_vertex_t* v2, const pvr_vertex_t *v3, pvr_vertex_t* vertices_out, float* w_coordinates_out,
pvr_vertex_t* v1out, pvr_vertex_t* v2out, pvr_vertex_t* v3out, pvr_vertex_t* v4out, unsigned char* visible unsigned char* visible
); );
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -343,26 +343,22 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ
/* Perform transformation without perspective division. Perspective divide will occur /* Perform transformation without perspective division. Perspective divide will occur
* per-triangle after clipping */ * per-triangle after clipping */
GLfloat W = transformVertexWithoutPerspectiveDivide(&vertex->x, &vertex->x, &vertex->y, &vertex->z); GLfloat W = transformVertexWithoutPerspectiveDivide(&vertex->x, &vertex->x, &vertex->y, &vertex->z);
aligned_vector_push_back(&w_coordinates, &W, 1);
Triangle.w[Triangle.vcount] = W; Triangle.w[Triangle.vcount] = W;
Triangle.vin[Triangle.vcount] = vertex; Triangle.vin[Triangle.vcount] = vertex;
Triangle.vcount++; Triangle.vcount++;
if(Triangle.vcount == 3) { if(Triangle.vcount == 3) {
pvr_vertex_t clipped[4]; pvr_vertex_t clipped[4];
float clipped_w[4];
GLubyte visible; /* Bitmask of which of the 3 input vertices are visible */ GLubyte visible; /* Bitmask of which of the 3 input vertices are visible */
/* OK we have a whole triangle, we may have to clip */ /* OK we have a whole triangle, we may have to clip */
TriangleClipResult tri_result = clipTriangleToNearZ( TriangleClipResult tri_result = clipTriangleToNearZ(
NEAR_DEPTH, NEAR_DEPTH,
(rel - 2), Triangle.vin,
Triangle.vin[0], Triangle.w,
Triangle.vin[1], clipped,
Triangle.vin[2], clipped_w,
&clipped[0],
&clipped[1],
&clipped[2],
&clipped[3],
&visible &visible
); );