diff --git a/GL/clip.c b/GL/clip.c index 152baab..d37fb31 100644 --- a/GL/clip.c +++ b/GL/clip.c @@ -1,4 +1,6 @@ #include +#include + #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; diff --git a/GL/clip.h b/GL/clip.h index 1432bc3..d2f582f 100644 --- a/GL/clip.h +++ b/GL/clip.h @@ -19,6 +19,8 @@ typedef struct { uint32 argb; /**< \brief Vertex color */ uint32 oargb; /**< \brief Vertex offset color */ } pvr_vertex_t; +#else +#include #endif typedef enum { diff --git a/GL/draw.c b/GL/draw.c index 8e8cf5c..ff7ca47 100644 --- a/GL/draw.c +++ b/GL/draw.c @@ -1,4 +1,5 @@ #include +#include #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; } diff --git a/samples/Makefile b/samples/Makefile index ca3c239..ed4cf66 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -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