So, I have worked out a basic transform stack with my build of GL, and began some tests with rendering different scenes.

Immediately, I observed some strange behaviour of polygons behind other polygons showing through each other.

This was fixed by setting the poly context depth flag to

**PVR_DEPTHWRITE_ENABLE** for opaque polygons.

This is done in my build by the call

**glEnable(GL_DEPTH_TEST);**
Next, I noticed the PVR does not like receiving vertices that are outside of the view frustum; some very natsy results can occur, up to and including a drop in framerate down to 30fps with graphical glitches on screen.

So that brings me to this topic, Clipping polygons to the view frustum. KGL obviously had some problems with clipping, as seen in several demos ( anyone try that "open dynamics" buggy demo?

http://www.boob.co.uk/devtools.html ) and some testing I did a while back

viewtopic.php?f=29&t=102059
So, I really want to implement a nice but fast clipping algorithm.

I have read a basic outline of the sutherland-hodgeman algorithm, and I have begun my own implementation loosely based on that outline.

My algorithm basically walks the vertices, determining if it is inside the view frustum, and then transforms the vertex if needed.

At first it seems simple, but then you realize vertices may also be added to the new clipped polygon...

For example, I have tested using a bounding box within the view frustum for visual confirmation.

The initial scene, we see a triangle centered within a box

Moving the triangle to the right, without clipping the triange, the geometry continues beyond the wall of the box

Now, with my clip algorithm enabled, the triangle becomes a quadrilateral, and remains inside of the box

I am posting, mainly because I am curious what solutions other devs here have used to manage 3D clipping for the PVR, any thoughts welcome.

Oh yeah, here is the function I wrote to clip a vertex on the x axis, say to a view frustum x min or max

It calculates the point of intersection between the vertices to the frustum, and transforms the vertex accordingly

Code: Select all

```
static matrix_t TM __attribute__((aligned(32))) =
{
{ 1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f }
};
static vector4f dvt; // Displacement Transform Vector
void LineClipFrustumX3fv( vector3f v1, vector3f v2, float fx )
{
/* Calculate Displacement Vector ( |Dv| ) As a Matrix */
TM[0][0] = v2[0]-v1[0];
TM[1][1] = v2[1]-v1[1];
TM[2][2] = v2[2]-v1[2];
TM[3][3] = 1.0f;
mat_load(&TM);
/* Transform Clip Point ( |Dv|*mag ) */
dvt[0] = dvt[1] = dvt[2] = (fx - v1[0])/TM[0][0]; /* Magnitude */
dvt[3] = 1.0f;
mat_trans_nodiv( dvt[0], dvt[1], dvt[2], dvt[3] );
v1[0] += dvt[0]; /* Update the Vertices to Transformed Clip Point */
v1[1] += dvt[1];
v1[2] += dvt[2];
}
```