mesa: Restore 78-column wrapping of license text in C-style comments.
[mesa.git] / src / mesa / tnl / t_vb_cliptmp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29
30 #define CLIP_DOTPROD(K, A, B, C, D) X(K)*A + Y(K)*B + Z(K)*C + W(K)*D
31
32 #define POLY_CLIP( PLANE_BIT, A, B, C, D ) \
33 do { \
34 if (mask & PLANE_BIT) { \
35 GLuint idxPrev = inlist[0]; \
36 GLfloat dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D ); \
37 GLuint outcount = 0; \
38 GLuint i; \
39 \
40 inlist[n] = inlist[0]; /* prevent rotation of vertices */ \
41 for (i = 1; i <= n; i++) { \
42 GLuint idx = inlist[i]; \
43 GLfloat dp = CLIP_DOTPROD(idx, A, B, C, D ); \
44 \
45 if (!IS_NEGATIVE(dpPrev)) { \
46 outlist[outcount++] = idxPrev; \
47 } \
48 \
49 if (DIFFERENT_SIGNS(dp, dpPrev)) { \
50 if (IS_NEGATIVE(dp)) { \
51 /* Going out of bounds. Avoid division by zero as we \
52 * know dp != dpPrev from DIFFERENT_SIGNS, above. \
53 */ \
54 GLfloat t = dp / (dp - dpPrev); \
55 INTERP_4F( t, coord[newvert], coord[idx], coord[idxPrev]); \
56 interp( ctx, t, newvert, idx, idxPrev, GL_TRUE ); \
57 } else { \
58 /* Coming back in. \
59 */ \
60 GLfloat t = dpPrev / (dpPrev - dp); \
61 INTERP_4F( t, coord[newvert], coord[idxPrev], coord[idx]); \
62 interp( ctx, t, newvert, idxPrev, idx, GL_FALSE ); \
63 } \
64 outlist[outcount++] = newvert++; \
65 } \
66 \
67 idxPrev = idx; \
68 dpPrev = dp; \
69 } \
70 \
71 if (outcount < 3) \
72 return; \
73 \
74 { \
75 GLuint *tmp = inlist; \
76 inlist = outlist; \
77 outlist = tmp; \
78 n = outcount; \
79 } \
80 } \
81 } while (0)
82
83
84 #define LINE_CLIP(PLANE_BIT, A, B, C, D ) \
85 do { \
86 if (mask & PLANE_BIT) { \
87 const GLfloat dp0 = CLIP_DOTPROD( v0, A, B, C, D ); \
88 const GLfloat dp1 = CLIP_DOTPROD( v1, A, B, C, D ); \
89 const GLboolean neg_dp0 = IS_NEGATIVE(dp0); \
90 const GLboolean neg_dp1 = IS_NEGATIVE(dp1); \
91 \
92 /* For regular clipping, we know from the clipmask that one \
93 * (or both) of these must be negative (otherwise we wouldn't \
94 * be here). \
95 * For userclip, there is only a single bit for all active \
96 * planes, so we can end up here when there is nothing to do, \
97 * hence the second IS_NEGATIVE() test: \
98 */ \
99 if (neg_dp0 && neg_dp1) \
100 return; /* both vertices outside clip plane: discard */ \
101 \
102 if (neg_dp1) { \
103 GLfloat t = dp1 / (dp1 - dp0); \
104 if (t > t1) t1 = t; \
105 } else if (neg_dp0) { \
106 GLfloat t = dp0 / (dp0 - dp1); \
107 if (t > t0) t0 = t; \
108 } \
109 if (t0 + t1 >= 1.0) \
110 return; /* discard */ \
111 } \
112 } while (0)
113
114
115
116 /* Clip a line against the viewport and user clip planes.
117 */
118 static inline void
119 TAG(clip_line)( struct gl_context *ctx, GLuint v0, GLuint v1, GLubyte mask )
120 {
121 TNLcontext *tnl = TNL_CONTEXT(ctx);
122 struct vertex_buffer *VB = &tnl->vb;
123 tnl_interp_func interp = tnl->Driver.Render.Interp;
124 GLfloat (*coord)[4] = VB->ClipPtr->data;
125 GLuint newvert = VB->Count;
126 GLfloat t0 = 0;
127 GLfloat t1 = 0;
128 GLuint p;
129 const GLuint v0_orig = v0;
130
131 if (mask & CLIP_FRUSTUM_BITS) {
132 LINE_CLIP( CLIP_RIGHT_BIT, -1, 0, 0, 1 );
133 LINE_CLIP( CLIP_LEFT_BIT, 1, 0, 0, 1 );
134 LINE_CLIP( CLIP_TOP_BIT, 0, -1, 0, 1 );
135 LINE_CLIP( CLIP_BOTTOM_BIT, 0, 1, 0, 1 );
136 LINE_CLIP( CLIP_FAR_BIT, 0, 0, -1, 1 );
137 LINE_CLIP( CLIP_NEAR_BIT, 0, 0, 1, 1 );
138 }
139
140 if (mask & CLIP_USER_BIT) {
141 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
142 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
143 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
144 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
145 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
146 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
147 LINE_CLIP( CLIP_USER_BIT, a, b, c, d );
148 }
149 }
150 }
151
152 if (VB->ClipMask[v0]) {
153 INTERP_4F( t0, coord[newvert], coord[v0], coord[v1] );
154 interp( ctx, t0, newvert, v0, v1, GL_FALSE );
155 v0 = newvert;
156 newvert++;
157 }
158 else {
159 ASSERT(t0 == 0.0);
160 }
161
162 /* Note: we need to use vertex v0_orig when computing the new
163 * interpolated/clipped vertex position, not the current v0 which
164 * may have got set when we clipped the other end of the line!
165 */
166 if (VB->ClipMask[v1]) {
167 INTERP_4F( t1, coord[newvert], coord[v1], coord[v0_orig] );
168 interp( ctx, t1, newvert, v1, v0_orig, GL_FALSE );
169
170 if (ctx->Light.ShadeModel == GL_FLAT)
171 tnl->Driver.Render.CopyPV( ctx, newvert, v1 );
172
173 v1 = newvert;
174
175 newvert++;
176 }
177 else {
178 ASSERT(t1 == 0.0);
179 }
180
181 tnl->Driver.Render.ClippedLine( ctx, v0, v1 );
182 }
183
184
185 /* Clip a triangle against the viewport and user clip planes.
186 */
187 static inline void
188 TAG(clip_tri)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLubyte mask )
189 {
190 TNLcontext *tnl = TNL_CONTEXT(ctx);
191 struct vertex_buffer *VB = &tnl->vb;
192 tnl_interp_func interp = tnl->Driver.Render.Interp;
193 GLuint newvert = VB->Count;
194 GLfloat (*coord)[4] = VB->ClipPtr->data;
195 GLuint pv = v2;
196 GLuint vlist[2][MAX_CLIPPED_VERTICES];
197 GLuint *inlist = vlist[0], *outlist = vlist[1];
198 GLuint p;
199 GLuint n = 3;
200
201 ASSIGN_3V(inlist, v2, v0, v1 ); /* pv rotated to slot zero */
202
203 if (0) {
204 /* print pre-clip vertex coords */
205 GLuint i, j;
206 printf("pre clip:\n");
207 for (i = 0; i < n; i++) {
208 j = inlist[i];
209 printf(" %u: %u: %f, %f, %f, %f\n",
210 i, j,
211 coord[j][0], coord[j][1], coord[j][2], coord[j][3]);
212 assert(!IS_INF_OR_NAN(coord[j][0]));
213 assert(!IS_INF_OR_NAN(coord[j][1]));
214 assert(!IS_INF_OR_NAN(coord[j][2]));
215 assert(!IS_INF_OR_NAN(coord[j][3]));
216 }
217 }
218
219
220 if (mask & CLIP_FRUSTUM_BITS) {
221 POLY_CLIP( CLIP_RIGHT_BIT, -1, 0, 0, 1 );
222 POLY_CLIP( CLIP_LEFT_BIT, 1, 0, 0, 1 );
223 POLY_CLIP( CLIP_TOP_BIT, 0, -1, 0, 1 );
224 POLY_CLIP( CLIP_BOTTOM_BIT, 0, 1, 0, 1 );
225 POLY_CLIP( CLIP_FAR_BIT, 0, 0, -1, 1 );
226 POLY_CLIP( CLIP_NEAR_BIT, 0, 0, 1, 1 );
227 }
228
229 if (mask & CLIP_USER_BIT) {
230 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
231 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
232 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
233 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
234 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
235 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
236 POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
237 }
238 }
239 }
240
241 if (ctx->Light.ShadeModel == GL_FLAT) {
242 if (pv != inlist[0]) {
243 ASSERT( inlist[0] >= VB->Count );
244 tnl->Driver.Render.CopyPV( ctx, inlist[0], pv );
245 }
246 }
247
248 if (0) {
249 /* print post-clip vertex coords */
250 GLuint i, j;
251 printf("post clip:\n");
252 for (i = 0; i < n; i++) {
253 j = inlist[i];
254 printf(" %u: %u: %f, %f, %f, %f\n",
255 i, j,
256 coord[j][0], coord[j][1], coord[j][2], coord[j][3]);
257 }
258 }
259
260 tnl->Driver.Render.ClippedPolygon( ctx, inlist, n );
261 }
262
263
264 /* Clip a quad against the viewport and user clip planes.
265 */
266 static inline void
267 TAG(clip_quad)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3,
268 GLubyte mask )
269 {
270 TNLcontext *tnl = TNL_CONTEXT(ctx);
271 struct vertex_buffer *VB = &tnl->vb;
272 tnl_interp_func interp = tnl->Driver.Render.Interp;
273 GLuint newvert = VB->Count;
274 GLfloat (*coord)[4] = VB->ClipPtr->data;
275 GLuint pv = v3;
276 GLuint vlist[2][MAX_CLIPPED_VERTICES];
277 GLuint *inlist = vlist[0], *outlist = vlist[1];
278 GLuint p;
279 GLuint n = 4;
280
281 ASSIGN_4V(inlist, v3, v0, v1, v2 ); /* pv rotated to slot zero */
282
283 if (mask & CLIP_FRUSTUM_BITS) {
284 POLY_CLIP( CLIP_RIGHT_BIT, -1, 0, 0, 1 );
285 POLY_CLIP( CLIP_LEFT_BIT, 1, 0, 0, 1 );
286 POLY_CLIP( CLIP_TOP_BIT, 0, -1, 0, 1 );
287 POLY_CLIP( CLIP_BOTTOM_BIT, 0, 1, 0, 1 );
288 POLY_CLIP( CLIP_FAR_BIT, 0, 0, -1, 1 );
289 POLY_CLIP( CLIP_NEAR_BIT, 0, 0, 1, 1 );
290 }
291
292 if (mask & CLIP_USER_BIT) {
293 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
294 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
295 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
296 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
297 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
298 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
299 POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
300 }
301 }
302 }
303
304 if (ctx->Light.ShadeModel == GL_FLAT) {
305 if (pv != inlist[0]) {
306 ASSERT( inlist[0] >= VB->Count );
307 tnl->Driver.Render.CopyPV( ctx, inlist[0], pv );
308 }
309 }
310
311 tnl->Driver.Render.ClippedPolygon( ctx, inlist, n );
312 }
313
314 #undef W
315 #undef Z
316 #undef Y
317 #undef X
318 #undef SIZE
319 #undef TAG
320 #undef POLY_CLIP
321 #undef LINE_CLIP