aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/main/clip.c
blob: acecb60b6bf6ba46e5e84363884880c06e00d253 (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* $Id: clip.c,v 1.5 1999/11/11 01:22:25 brianp Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 * 
 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#ifdef PC_HEADER
#include "all.h"
#else
#include "glheader.h"
#include "clip.h"
#include "context.h"
#include "macros.h"
#include "matrix.h"
#include "mmath.h"
#include "types.h"
#include "vb.h"
#include "xform.h"
#endif




/* Linear interpolation between A and B: */
#define LINTERP( T, A, B )   ( (A) + (T) * ( (B) - (A) ) )



#define INTERP_SZ( t, vec, to, a, b, sz )			\
do {								\
   switch (sz) {						\
   case 4: vec[to][3] = LINTERP( t, vec[a][3], vec[b][3] );	\
   case 3: vec[to][2] = LINTERP( t, vec[a][2], vec[b][2] );	\
   case 2: vec[to][1] = LINTERP( t, vec[a][1], vec[b][1] );	\
   case 1: vec[to][0] = LINTERP( t, vec[a][0], vec[b][0] );	\
   }								\
} while(0)
   



#define CLIP_RGBA0    0x1
#define CLIP_RGBA1    0x2
#define CLIP_TEX0     0x4
#define CLIP_TEX1     0x8
#define CLIP_INDEX0   0x10
#define CLIP_INDEX1   0x20

static clip_interp_func clip_interp_tab[0x40]; 

#define IND 0
#define NAME clip_nil
#include "interp_tmp.h"

#define IND (CLIP_RGBA0)
#define NAME clipRGBA0
#include "interp_tmp.h"

#define IND (CLIP_RGBA0|CLIP_RGBA1)
#define NAME clipRGBA0_RGBA1
#include "interp_tmp.h"

#define IND (CLIP_TEX0|CLIP_RGBA0)
#define NAME clipTEX0_RGBA0
#include "interp_tmp.h"

#define IND (CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1)
#define NAME clipTEX0_RGBA0_RGBA1
#include "interp_tmp.h"

#define IND (CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0)
#define NAME clipTEX1_TEX0_RGBA0
#include "interp_tmp.h"

#define IND (CLIP_TEX0)
#define NAME clipTEX0
#include "interp_tmp.h"

#define IND (CLIP_TEX1|CLIP_TEX0)
#define NAME clipTEX1_TEX0
#include "interp_tmp.h"

#define IND (CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1)
#define NAME clipTEX1_TEX0_RGBA0_RGBA1
#include "interp_tmp.h"

#define IND (CLIP_INDEX0)
#define NAME clipINDEX0
#include "interp_tmp.h"

#define IND (CLIP_INDEX0|CLIP_INDEX1)
#define NAME clipINDEX0_INDEX1
#include "interp_tmp.h"




/**********************************************************************/
/*                     Get/Set User clip-planes.                      */
/**********************************************************************/



void
_mesa_ClipPlane( GLenum plane, const GLdouble *eq )
{
   GET_CURRENT_CONTEXT(ctx);
   GLint p;
   GLfloat equation[4];

   equation[0] = eq[0];
   equation[1] = eq[1];
   equation[2] = eq[2];
   equation[3] = eq[3];

   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glClipPlane");

   p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
   if (p<0 || p>=MAX_CLIP_PLANES) {
      gl_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
      return;
   }

   /*
    * The equation is transformed by the transpose of the inverse of the
    * current modelview matrix and stored in the resulting eye coordinates.
    *
    * KW: Eqn is then transformed to the current clip space, where user
    * clipping now takes place.  The clip-space equations are recalculated
    * whenever the projection matrix changes.
    */
   if (ctx->ModelView.flags & MAT_DIRTY_ALL_OVER) {
      gl_matrix_analyze( &ctx->ModelView );
   }
   gl_transform_vector( ctx->Transform.EyeUserPlane[p], equation,
		        ctx->ModelView.inv );


   if (ctx->Transform.ClipEnabled[p]) {
      ctx->NewState |= NEW_USER_CLIP;

      if (ctx->ProjectionMatrix.flags & MAT_DIRTY_ALL_OVER) {
	 gl_matrix_analyze( &ctx->ProjectionMatrix );
      }
      gl_transform_vector( ctx->Transform.ClipUserPlane[p], 
			   ctx->Transform.EyeUserPlane[p], 
			   ctx->ProjectionMatrix.inv );
   }
}


void gl_update_userclip( GLcontext *ctx )
{
   GLuint p;
   
   for (p = 0 ; p < MAX_CLIP_PLANES ; p++) {
      if (ctx->Transform.ClipEnabled[p]) {
	 gl_transform_vector( ctx->Transform.ClipUserPlane[p],
			      ctx->Transform.EyeUserPlane[p],
			      ctx->ProjectionMatrix.inv );
      }
   }
}

void
_mesa_GetClipPlane( GLenum plane, GLdouble *equation )
{
   GET_CURRENT_CONTEXT(ctx);
   GLint p;

   ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetClipPlane");


   p = (GLint) (plane - GL_CLIP_PLANE0);
   if (p<0 || p>=MAX_CLIP_PLANES) {
      gl_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
      return;
   }

   equation[0] = (GLdouble) ctx->Transform.EyeUserPlane[p][0];
   equation[1] = (GLdouble) ctx->Transform.EyeUserPlane[p][1];
   equation[2] = (GLdouble) ctx->Transform.EyeUserPlane[p][2];
   equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
}




/**********************************************************************/
/*                         View volume clipping.                      */
/**********************************************************************/


/*
 * Clip a point against the view volume.
 * Input:  v - vertex-vector describing the point to clip
 * Return:  0 = outside view volume
 *          1 = inside view volume
 */
GLuint gl_viewclip_point( const GLfloat v[] )
{
   if (   v[0] > v[3] || v[0] < -v[3]
       || v[1] > v[3] || v[1] < -v[3]
       || v[2] > v[3] || v[2] < -v[3] ) {
      return 0;
   }
   else {
      return 1;
   }
}

/*
 * Clip a point against the user clipping planes.
 * Input:  v - vertex-vector describing the point to clip.
 * Return:  0 = point was clipped
 *          1 = point not clipped
 */
GLuint gl_userclip_point( GLcontext* ctx, const GLfloat v[] )
{
   GLuint p;

   for (p=0;p<MAX_CLIP_PLANES;p++) {
      if (ctx->Transform.ClipEnabled[p]) {
	 GLfloat dot = v[0] * ctx->Transform.ClipUserPlane[p][0]
		     + v[1] * ctx->Transform.ClipUserPlane[p][1]
		     + v[2] * ctx->Transform.ClipUserPlane[p][2]
		     + v[3] * ctx->Transform.ClipUserPlane[p][3];
         if (dot < 0.0F) {
            return 0;
         }
      }
   }

   return 1;
}




#if defined(__i386__)
#define NEGATIVE(x) ((*(int *)&x)<0)
#else
#define NEGATIVE(x) (x < 0)
#endif


static clip_poly_func gl_poly_clip_tab[2][5];
static clip_line_func gl_line_clip_tab[2][5];

#define W(i) coord[i][3]
#define Z(i) coord[i][2]
#define Y(i) coord[i][1]
#define X(i) coord[i][0]
#define SIZE 4
#define IND 0
#define TAG(x) x##_4
#include "clip_funcs.h"

#define W(i) 1.0
#define Z(i) coord[i][2]
#define Y(i) coord[i][1]
#define X(i) coord[i][0]
#define SIZE 3
#define IND 0
#define TAG(x) x##_3
#include "clip_funcs.h"

#define W(i) 1.0
#define Z(i) 0.0
#define Y(i) coord[i][1]
#define X(i) coord[i][0]
#define SIZE 2
#define IND 0
#define TAG(x) x##_2
#include "clip_funcs.h"

#define W(i) coord[i][3]
#define Z(i) coord[i][2]
#define Y(i) coord[i][1]
#define X(i) coord[i][0]
#define SIZE 4
#define IND CLIP_TAB_EDGEFLAG
#define TAG(x) x##_4_edgeflag
#include "clip_funcs.h"

#define W(i) 1.0
#define Z(i) coord[i][2]
#define Y(i) coord[i][1]
#define X(i) coord[i][0]
#define SIZE 3
#define IND CLIP_TAB_EDGEFLAG
#define TAG(x) x##_3_edgeflag
#include "clip_funcs.h"

#define W(i) 1.0
#define Z(i) 0.0
#define Y(i) coord[i][1]
#define X(i) coord[i][0]
#define SIZE 2
#define IND CLIP_TAB_EDGEFLAG
#define TAG(x) x##_2_edgeflag
#include "clip_funcs.h"




void gl_update_clipmask( GLcontext *ctx )
{
   GLuint mask = 0;

   if (ctx->Visual->RGBAflag) 
   {
      if (ctx->Light.ShadeModel==GL_SMOOTH) 
      {
	 mask |= CLIP_RGBA0;
      
	 if (ctx->TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_SEPERATE_SPECULAR))
	    mask |= CLIP_RGBA1;
      }

      if (ctx->Texture.ReallyEnabled & 0xf0)
	 mask |= CLIP_TEX1|CLIP_TEX0;

      if (ctx->Texture.ReallyEnabled & 0xf)
	 mask |= CLIP_TEX0;
   }
   else if (ctx->Light.ShadeModel==GL_SMOOTH) 
   {
      mask |= CLIP_INDEX0;
      
      if (ctx->TriangleCaps & DD_TRI_LIGHT_TWOSIDE) 
	 mask |= CLIP_INDEX1;
   }

   
   ctx->ClipInterpFunc = clip_interp_tab[mask];
   ctx->poly_clip_tab = gl_poly_clip_tab[0];
   ctx->line_clip_tab = gl_line_clip_tab[0];

   if (ctx->TriangleCaps & DD_TRI_UNFILLED) {
      ctx->poly_clip_tab = gl_poly_clip_tab[1];
      ctx->line_clip_tab = gl_line_clip_tab[0];
   } 
}


#define USER_CLIPTEST(NAME, SZ)						\
static void NAME( struct vertex_buffer *VB )				\
{									\
   GLcontext *ctx = VB->ctx;						\
   GLubyte *clipMask = VB->ClipMask;					\
   GLubyte *userClipMask = VB->UserClipMask;				\
   GLuint start = VB->Start;						\
   GLuint count = VB->Count;						\
   GLuint p, i;								\
   GLubyte bit;								\
									\
									\
   for (bit = 1, p = 0; p < MAX_CLIP_PLANES ; p++, bit *=2)		\
      if (ctx->Transform.ClipEnabled[p]) {				\
	 GLuint nr = 0;							\
	 const GLfloat a = ctx->Transform.ClipUserPlane[p][0];		\
	 const GLfloat b = ctx->Transform.ClipUserPlane[p][1];		\
	 const GLfloat c = ctx->Transform.ClipUserPlane[p][2];		\
	 const GLfloat d = ctx->Transform.ClipUserPlane[p][3];		\
         GLfloat *coord = VB->ClipPtr->start;				\
         GLuint stride = VB->ClipPtr->stride;				\
									\
	 for (i = start ; i < count ; i++, STRIDE_F(coord, stride)) {	\
	    GLfloat dp = coord[0] * a + coord[1] * b;			\
	    if (SZ > 2) dp += coord[2] * c;				\
	    if (SZ > 3) dp += coord[3] * d; else dp += d;		\
									\
	    if (dp < 0) {						\
	       clipMask[i] |= CLIP_USER_BIT;				\
	       userClipMask[i] |= bit;					\
	       nr++;							\
	    }								\
	 }								\
									\
	 if (nr > 0) {							\
	    VB->ClipOrMask |= CLIP_USER_BIT;				\
	    VB->CullMode |= CLIP_MASK_ACTIVE;				\
	    if (nr == count - start) {					\
	       VB->ClipAndMask |= CLIP_USER_BIT;			\
	       VB->Culled = 1;						\
	       return;							\
	    }								\
	 }								\
      }									\
}


USER_CLIPTEST(userclip2, 2)		       
USER_CLIPTEST(userclip3, 3)		       
USER_CLIPTEST(userclip4, 4)		       

static void (*(usercliptab[5]))( struct vertex_buffer * ) = {
   0,
   0,
   userclip2,
   userclip3,
   userclip4
};

void gl_user_cliptest( struct vertex_buffer *VB )
{
   usercliptab[VB->ClipPtr->size]( VB );
}


void gl_init_clip(void)
{
   init_clip_funcs_4();
   init_clip_funcs_3();
   init_clip_funcs_2();

   init_clip_funcs_4_edgeflag();
   init_clip_funcs_3_edgeflag();
   init_clip_funcs_2_edgeflag();

   clip_interp_tab[0] = clip_nil;
   clip_interp_tab[CLIP_RGBA0] = clipRGBA0;
   clip_interp_tab[CLIP_RGBA0|CLIP_RGBA1] = clipRGBA0_RGBA1;
   clip_interp_tab[CLIP_TEX0|CLIP_RGBA0] = clipTEX0_RGBA0;
   clip_interp_tab[CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1] = clipTEX0_RGBA0_RGBA1;
   clip_interp_tab[CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0] = clipTEX1_TEX0_RGBA0;
   clip_interp_tab[CLIP_TEX1|CLIP_TEX0|CLIP_RGBA0|CLIP_RGBA1] = 
      clipTEX1_TEX0_RGBA0_RGBA1;

   clip_interp_tab[CLIP_TEX0] = clipTEX0;
   clip_interp_tab[CLIP_TEX1|CLIP_TEX0] = clipTEX1_TEX0;

   clip_interp_tab[CLIP_INDEX0] = clipINDEX0;
   clip_interp_tab[CLIP_INDEX0|CLIP_INDEX1] = clipINDEX0_INDEX1;
}