More clipping work

This commit is contained in:
Luke Benstead 2018-06-06 19:58:59 +01:00
parent b2a26e58b3
commit 4d3858d0f0
4 changed files with 27 additions and 14 deletions

View File

@ -1,4 +1,6 @@
#include <float.h>
#include <stdio.h>
#include "clip.h"
ClipResult clipLineToNearZ(const float* v1, const float* v2, const float dist, float* vout, float* t) {
@ -45,7 +47,7 @@ TriangleClipResult clipTriangleToNearZ(
*/
typedef unsigned char uint8;
uint8 visible = ((uint8) v1->z < plane_dist) | ((uint8) v2->z < plane_dist) << 1 | ((uint8) v3->z < plane_dist) << 2;
uint8 visible = (v1->z >= plane_dist) ? 1 : 0 | (v2->z >= plane_dist) ? 2 : 0 | (v3->z >= plane_dist) ? 4 : 0;
switch(visible) {
case 0b000:
@ -67,27 +69,31 @@ TriangleClipResult clipTriangleToNearZ(
default: {
/* One vertex is visible */
/* This is the "easy" case, we simply find the vertex which is visible, and clip the lines to the other 2 against the plane */
fprintf(stderr, "Clipping triangle\n");
pvr_vertex_t tmp1, tmp2;
float t1, t2;
if(visible == 0b001) {
ClipResult l1 = clipLineToNearZ(&v1->x, &v2->x, plane_dist, &tmp1, &t1);
ClipResult l2 = clipLineToNearZ(&v1->x, &v3->x, plane_dist, &tmp2, &t2);
ClipResult l1 = clipLineToNearZ(&v1->x, &v2->x, plane_dist, &tmp1.x, &t1);
ClipResult l2 = 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;
*v2out = tmp1;
*v3out = tmp2;
} else if(visible == 0b010) {
ClipResult l1 = clipLineToNearZ(&v2->x, &v1->x, plane_dist, &tmp1, &t1);
ClipResult l2 = clipLineToNearZ(&v2->x, &v3->x, plane_dist, &tmp2, &t2);
ClipResult l1 = clipLineToNearZ(&v2->x, &v1->x, plane_dist, &tmp1.x, &t1);
ClipResult l2 = clipLineToNearZ(&v2->x, &v3->x, plane_dist, &tmp2.x, &t2);
*v1out = tmp1;
*v2out = *v2;
*v3out = tmp2;
} else {
ClipResult l1 = clipLineToNearZ(&v3->x, &v1->x, plane_dist, &tmp1, &t1);
ClipResult l2 = clipLineToNearZ(&v3->x, &v2->x, plane_dist, &tmp2, &t2);
ClipResult l1 = clipLineToNearZ(&v3->x, &v1->x, plane_dist, &tmp1.x, &t1);
ClipResult l2 = clipLineToNearZ(&v3->x, &v2->x, plane_dist, &tmp2.x, &t2);
*v1out = tmp1;
*v2out = tmp2;

View File

@ -19,6 +19,8 @@ typedef struct {
uint32 argb; /**< \brief Vertex color */
uint32 oargb; /**< \brief Vertex offset color */
} pvr_vertex_t;
#else
#include <dc/pvr.h>
#endif
typedef enum {

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <string.h>
#include "../include/gl.h"
#include "../include/glext.h"
@ -190,8 +191,8 @@ inline void transformNormalToEyeSpace(GLfloat* normal) {
mat_trans_normal3(normal[0], normal[1], normal[2]);
}
/* If this has a value other than zero, it must be negative! */
#define NEAR_DEPTH 0.0f
/* If this has a value other than zero, it must be positive! */
#define NEAR_DEPTH 0.0001f
static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum type, const GLvoid* indices) {
static GLfloat normal[3] = {0.0f, 0.0f, -1.0f};
@ -322,6 +323,8 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ
_applyRenderMatrix(); /* Apply the Render Matrix Stack */
float W_stash[3];
// FIXME: Don't perspective divide!
transformVertex(&vertex->x, &vertex->x, &vertex->y, &vertex->z);
@ -343,7 +346,7 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ
// Store this for the clip stash
pvr_vertex_t original_vertex = *((pvr_vertex_t*) dst);
if(rel >= 2) {
if(rel >= 2) {
/* We have at least one complete triangle, let's start clipping! */
pvr_vertex_t* v1 = (pvr_vertex_t*) &clip_stash[0];
pvr_vertex_t* v2 = (pvr_vertex_t*) &clip_stash[1];
@ -380,7 +383,6 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ
/*
* Two vertices were behind the clip plane, we just manipulated them.
we have to end the triangle strip here and pick up next vertex */
v3out->flags = PVR_CMD_VERTEX_EOL;
/* Now we push back the original 2 vertices, so that the next triangle strip will be properly
@ -390,8 +392,9 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ
activePolyList()->vector.size + 2
);
*(++dst) = (PVRCommand) clip_stash[1];
*(++dst) = (PVRCommand) original_vertex;
/* clip_stash[1] holds the previous vertex, original_vertex holds the current one */
memcpy(++dst, &clip_stash[1], sizeof(PVRCommand));
memcpy(++dst, &original_vertex, sizeof(PVRCommand));
} else if(ret == TRIANGLE_CLIP_RESULT_ALTERED_AND_CREATED_VERTEX) {
/* One vertex was behind the clip plane, we need to create another triangle */
@ -407,7 +410,7 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ
clip_stash[0] = clip_stash[1];
clip_stash[1] = original_vertex;
//FIXME: Peform perspective division
//FIXME: Perform perspective division
++dst;
}

View File

@ -14,3 +14,5 @@ all:
$(KOS_MAKE) -C ortho2d all
$(KOS_MAKE) -C lerabot01 all
$(KOS_MAKE) -C zclip all
$(KOS_MAKE) -C zclip1 all
$(KOS_MAKE) -C zclip2 all