-
Notifications
You must be signed in to change notification settings - Fork 1
/
gl-clip.c
310 lines (235 loc) · 11.2 KB
/
gl-clip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* KallistiGL for KallistiOS ##version##
libgl/gl-clip.c
Copyright (C) 2013-2014 Josh Pearson
Near-Z Clipping Algorithm (C) 2013-2014 Josh PH3NOM Pearson
Input Primitive Types Supported:
-GL_TRIANGLES
-GL_TRIANGLE_STRIPS
-GL_QUADS
Outputs a mix of Triangles and Triangle Strips for use with the PVR
*/
#include <stdio.h>
#include <string.h>
#include "gl.h"
#include "gl-api.h"
#include "gl-clip.h"
static float3 CLIP_BUF[1024 * 32];
static inline void _glKosVertexClipZNear(pvr_vertex_t *v1, pvr_vertex_t *v2, float MAG) {
colorui *c1 = (colorui *)&v1->argb;
colorui *c2 = (colorui *)&v2->argb;
v1->x += (v2->x - v1->x) * MAG;
v1->y += (v2->y - v1->y) * MAG;
v1->z += (v2->z - v1->z) * MAG;
v1->u += (v2->u - v1->u) * MAG;
v1->v += (v2->v - v1->v) * MAG;
c1->a += (c2->a - c1->a) * MAG;
c1->r += (c2->r - c1->r) * MAG;
c1->g += (c2->g - c1->g) * MAG;
c1->b += (c2->b - c1->b) * MAG;
}
static GLuint _glKosTransformClip(pvr_vertex_t *v, GLint count) {
pvr_vertex_t *V = v;
float3 *C = CLIP_BUF;
GLuint in = 0;
while(count--) {
mat_trans_single3_nodiv_nomod(V->x, V->y, V->z, C->x, C->y, C->z);
if(C->z < CLIP_NEARZ) ++in;
++C;
++V;
}
return in;
}
GLuint _glKosClipTriangleStrip(pvr_vertex_t *src, pvr_vertex_t *dst, GLuint vertices) {
GLuint i, v = 0, in = _glKosTransformClip(src, vertices);
float3 *C = CLIP_BUF;
if(in == vertices) {
memcpy(dst, src, vertices * 0x20);
pvr_vertex_t *v = dst;
while(--in) {
v->flags = PVR_CMD_VERTEX;
++v;
}
v->flags = PVR_CMD_VERTEX_EOL;
return vertices;
}
else if(in == 0)
return 0;
/* Iterate all Triangles of the Strip - Hence looping vertices-2 times */
for(i = 0; i < ((vertices) - 2); i++) {
GLushort clip = 0; /* Clip Code for current Triangle */
GLubyte verts_in = 0; /* # of Verteices inside clip plane for current Triangle */
C->z >= CLIP_NEARZ ? clip |= FIRST : ++verts_in;
(C + 1)->z >= CLIP_NEARZ ? clip |= SECOND : ++verts_in;
(C + 2)->z >= CLIP_NEARZ ? clip |= THIRD : ++verts_in;
switch(verts_in) { /* Start by examining # of vertices inside clip plane */
case 0: /* All Vertices of Triangle are outside of clip plne */
break;
case 3: /* All Vertices of Triangle are inside of clip plne */
memcpy(&dst[v], &src[i], 96);
dst[v].flags = dst[v + 1].flags = PVR_CMD_VERTEX;
dst[v + 2].flags = PVR_CMD_VERTEX_EOL;
v += 3;
break;
case 1:/* 1 Vertex of Triangle is inside of clip plane = output 1 Triangle */
memcpy(&dst[v], &src[i], 96);
switch(clip) {
case FIRST_TWO_OUT:
_glKosVertexClipZNear(&dst[v], &dst[v + 2], _glKosNearZClipMag(&CLIP_BUF[i], &CLIP_BUF[i + 2]));
_glKosVertexClipZNear(&dst[v + 1], &dst[v + 2], _glKosNearZClipMag(&CLIP_BUF[i + 1], &CLIP_BUF[i + 2]));
break;
case FIRST_AND_LAST_OUT:
_glKosVertexClipZNear(&dst[v], &dst[v + 1], _glKosNearZClipMag(&CLIP_BUF[i], &CLIP_BUF[i + 1]));
_glKosVertexClipZNear(&dst[v + 2], &dst[v + 1], _glKosNearZClipMag(&CLIP_BUF[i + 2], &CLIP_BUF[i + 1]));
break;
case LAST_TWO_OUT:
_glKosVertexClipZNear(&dst[v + 1], &dst[v], _glKosNearZClipMag(&CLIP_BUF[i + 1], &CLIP_BUF[i + 0]));
_glKosVertexClipZNear(&dst[v + 2], &dst[v], _glKosNearZClipMag(&CLIP_BUF[i + 2], &CLIP_BUF[i + 0]));
break;
}
dst[v].flags = dst[v + 1].flags = PVR_CMD_VERTEX;
dst[v + 2].flags = PVR_CMD_VERTEX_EOL;
v += 3;
break;
case 2:/* 2 Vertices of Triangle are inside of clip plane = output 2 Triangles */
switch(clip) {
case FIRST:
_glKosVertexCopyPVR(&src[i + 0], &dst[v + 0]);
_glKosVertexCopyPVR(&src[i + 1], &dst[v + 1]);
_glKosVertexCopyPVR(&src[i + 0], &dst[v + 2]);
_glKosVertexCopyPVR(&src[i + 2], &dst[v + 3]);
_glKosVertexClipZNear(&dst[v + 0], &dst[v + 1], _glKosNearZClipMag(&CLIP_BUF[i], &CLIP_BUF[i + 1]));
_glKosVertexClipZNear(&dst[v + 2], &dst[v + 3], _glKosNearZClipMag(&CLIP_BUF[i], &CLIP_BUF[i + 2]));
break;
case SECOND:
_glKosVertexCopyPVR(&src[i + 1], &dst[v + 0]);
_glKosVertexCopyPVR(&src[i + 0], &dst[v + 1]);
_glKosVertexCopyPVR(&src[i + 1], &dst[v + 2]);
_glKosVertexCopyPVR(&src[i + 2], &dst[v + 3]);
_glKosVertexClipZNear(&dst[v + 0], &dst[v + 1], _glKosNearZClipMag(&CLIP_BUF[i + 1], &CLIP_BUF[i + 0]));
_glKosVertexClipZNear(&dst[v + 2], &dst[v + 3], _glKosNearZClipMag(&CLIP_BUF[i + 1], &CLIP_BUF[i + 2]));
break;
case THIRD:
_glKosVertexCopyPVR(&src[i + 2], &dst[v + 0]);
_glKosVertexCopyPVR(&src[i + 0], &dst[v + 1]);
_glKosVertexCopyPVR(&src[i + 2], &dst[v + 2]);
_glKosVertexCopyPVR(&src[i + 1], &dst[v + 3]);
_glKosVertexClipZNear(&dst[v + 0], &dst[v + 1], _glKosNearZClipMag(&CLIP_BUF[i + 2], &CLIP_BUF[i]));
_glKosVertexClipZNear(&dst[v + 2], &dst[v + 3], _glKosNearZClipMag(&CLIP_BUF[i + 2], &CLIP_BUF[i + 1]));
break;
}
dst[v + 0].flags = dst[v + 1].flags = dst[v + 2].flags = PVR_CMD_VERTEX;
dst[v + 3].flags = PVR_CMD_VERTEX_EOL;
v += 4;
break;
}
C++;
}
return v;
}
static inline unsigned char _glKosClipTri(pvr_vertex_t *src, pvr_vertex_t *dst) {
GLushort clip = 0; /* Clip Code for current Triangle */
GLubyte verts_in = 0; /* # of Vertices inside clip plane for current Triangle */
float3 clip_buf[3]; /* Store the Vertices for each Triangle Translated into Clip Space */
/* Transform all 3 Vertices of Triangle */
{
register float __x __asm__("fr12") = src->x;
register float __y __asm__("fr13") = src->y;
register float __z __asm__("fr14") = src->z;
mat_trans_fv12_nodiv();
clip_buf[0].x = __x;
clip_buf[0].y = __y;
clip_buf[0].z = __z;
__x = (src + 1)->x;
__y = (src + 1)->y;
__z = (src + 1)->z;
mat_trans_fv12_nodiv();
clip_buf[1].x = __x;
clip_buf[1].y = __y;
clip_buf[1].z = __z;
__x = (src + 2)->x;
__y = (src + 2)->y;
__z = (src + 2)->z;
mat_trans_fv12_nodiv();
clip_buf[2].x = __x;
clip_buf[2].y = __y;
clip_buf[2].z = __z;
/* Compute Clip Code for Triangle */
(clip_buf[0].z >= CLIP_NEARZ) ? clip |= FIRST : ++verts_in;
(clip_buf[1].z >= CLIP_NEARZ) ? clip |= SECOND : ++verts_in;
(clip_buf[2].z >= CLIP_NEARZ) ? clip |= THIRD : ++verts_in;
}
switch(verts_in) { /* Start by examining # of vertices inside clip plane */
case 0: /* All Vertices of Triangle are Outside of clip plne */
return 0;
case 3: /* All Vertices of Triangle are inside of clip plne */
memcpy(dst, src, 96);
dst->flags = (dst + 1)->flags = PVR_CMD_VERTEX;
(dst + 2)->flags = PVR_CMD_VERTEX_EOL;
return 3;
case 1:/* 1 Vertex of Triangle is inside of clip plane = output 1 Triangle */
memcpy(dst, src, 96);
switch(clip) {
case FIRST_TWO_OUT:
_glKosVertexClipZNear(dst, dst + 2, _glKosNearZClipMag(&clip_buf[0], &clip_buf[2]));
_glKosVertexClipZNear(dst + 1, dst + 2, _glKosNearZClipMag(&clip_buf[1], &clip_buf[2]));
break;
case FIRST_AND_LAST_OUT:
_glKosVertexClipZNear(dst, dst + 1, _glKosNearZClipMag(&clip_buf[0], &clip_buf[1]));
_glKosVertexClipZNear(dst + 2, dst + 1, _glKosNearZClipMag(&clip_buf[2], &clip_buf[1]));
break;
case LAST_TWO_OUT:
_glKosVertexClipZNear(dst + 1, dst, _glKosNearZClipMag(&clip_buf[1], &clip_buf[0]));
_glKosVertexClipZNear(dst + 2, dst, _glKosNearZClipMag(&clip_buf[2], &clip_buf[0]));
break;
}
dst->flags = (dst + 1)->flags = PVR_CMD_VERTEX;
(dst + 2)->flags = PVR_CMD_VERTEX_EOL;
return 3;
case 2:/* 2 Vertices of Triangle are inside of clip plane = output 2 Triangles */
switch(clip) {
case FIRST:
memcpy(dst, src, 64);
_glKosVertexCopyPVR(src, dst + 2);
_glKosVertexCopyPVR(src + 2, dst + 3);
_glKosVertexClipZNear(dst, dst + 1, _glKosNearZClipMag(&clip_buf[0], &clip_buf[1]));
_glKosVertexClipZNear(dst + 2, dst + 3, _glKosNearZClipMag(&clip_buf[0], &clip_buf[2]));
break;
case SECOND:
_glKosVertexCopyPVR(src + 1, dst);
_glKosVertexCopyPVR(src, dst + 1);
memcpy(dst + 2, src + 1, 64);
_glKosVertexClipZNear(dst, dst + 1, _glKosNearZClipMag(&clip_buf[1], &clip_buf[0]));
_glKosVertexClipZNear(dst + 2, dst + 3, _glKosNearZClipMag(&clip_buf[1], &clip_buf[2]));
break;
case THIRD:
_glKosVertexCopyPVR(src + 2, dst);
_glKosVertexCopyPVR(src, dst + 1);
_glKosVertexCopyPVR(src + 2, dst + 2);
_glKosVertexCopyPVR(src + 1, dst + 3);
_glKosVertexClipZNear(dst, dst + 1, _glKosNearZClipMag(&clip_buf[2], &clip_buf[0]));
_glKosVertexClipZNear(dst + 2, dst + 3, _glKosNearZClipMag(&clip_buf[2], &clip_buf[1]));
break;
}
dst->flags = (dst + 1)->flags = (dst + 2)->flags = PVR_CMD_VERTEX;
(dst + 3)->flags = PVR_CMD_VERTEX_EOL;
return 4;
}
return 0;
}
GLuint _glKosClipTriangles(pvr_vertex_t *src, pvr_vertex_t *dst, GLuint vertices) {
GLuint i, v = 0;
for(i = 0; i < vertices; i += 3) /* Iterate all Triangles */
v += _glKosClipTri(src + i, dst + v);
return v;
}
GLuint _glKosClipQuads(pvr_vertex_t *src, pvr_vertex_t *dst, GLuint vertices) {
GLuint i, v = 0;
pvr_vertex_t qv;
for(i = 0; i < vertices; i += 4) { /* Iterate all Quads, Rearranging into Triangle Strips */
_glKosVertexCopyPVR(src + i + 3, &qv);
_glKosVertexCopyPVR(src + i + 2, src + i + 3);
_glKosVertexCopyPVR(&qv, src + i + 2);
v += _glKosClipTriangleStrip(src + i, dst + v, 4);
}
return v;
}