diff --git a/GL/clip.c b/GL/clip.c index 5951163..eded420 100644 --- a/GL/clip.c +++ b/GL/clip.c @@ -78,15 +78,18 @@ void clipTriangleStrip(AlignedVector* vertices, AlignedVector* outBuffer) { aligned_vector_at(vertices, i) }; - ClipVertex* v1 = sourceTriangle[0]; - ClipVertex* v2 = sourceTriangle[1]; + /* If we're on an odd vertex, we need to swap the order of the first two vertices, as that's what + * triangle strips do */ + ClipVertex* v1 = (i % 2 == 0) ? sourceTriangle[0] : sourceTriangle[1]; + ClipVertex* v2 = (i % 2 == 0) ? sourceTriangle[1] : sourceTriangle[0]; ClipVertex* v3 = sourceTriangle[2]; uint8_t visible = ((v1->xyz[2] <= 0) ? 4 : 0) | ((v2->xyz[2] <= 0) ? 2 : 0) | ((v3->xyz[2] <= 0) ? 1 : 0); + uint8_t startOfStrip = (i == 2) || (outBuffer->size > 2 && ((ClipVertex*) aligned_vector_back(outBuffer))->flags == VERTEX_CMD_EOL); /* All visible, we're fine! */ if(visible == 0b111) { - if(i == 2) { + if(startOfStrip) { aligned_vector_push_back(outBuffer, v1, 1); aligned_vector_push_back(outBuffer, v2, 1); } diff --git a/containers/aligned_vector.c b/containers/aligned_vector.c index a8310a3..8ad7a80 100644 --- a/containers/aligned_vector.c +++ b/containers/aligned_vector.c @@ -70,6 +70,10 @@ void* aligned_vector_at(const AlignedVector* vector, const unsigned int index) { return &vector->data[index * vector->element_size]; } +void* aligned_vector_back(AlignedVector* vector) { + return aligned_vector_at(vector, vector->size - 1); +} + void* aligned_vector_extend(AlignedVector* vector, const unsigned int additional_count) { const unsigned int current = vector->size; aligned_vector_resize(vector, vector->size + additional_count); diff --git a/containers/aligned_vector.h b/containers/aligned_vector.h index 2ddf5ec..2b710d4 100644 --- a/containers/aligned_vector.h +++ b/containers/aligned_vector.h @@ -23,6 +23,7 @@ void* aligned_vector_extend(AlignedVector* vector, const unsigned int additional void aligned_vector_clear(AlignedVector* vector); void aligned_vector_shrink_to_fit(AlignedVector* vector); void aligned_vector_cleanup(AlignedVector* vector); +void* aligned_vector_back(AlignedVector* vector); #ifdef __cplusplus }