summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/PEX5/ddpex/mi/level2/miBldXform.c
blob: fd518506bc36ad912bb1aa760fcea628672d1a01 (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/* $XConsortium: miBldXform.c,v 5.7 94/04/17 20:37:01 dpw Exp $ */
/*

Copyright (c) 1989, 1990, 1991  X Consortium

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 THE X CONSORTIUM 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.

Except as contained in this notice, the name of the X Consortium shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the X Consortium.


Copyright 1989, 1990, 1991 by Sun Microsystems, Inc

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name Sun Microsystems not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

*/

#include "X.h"
#include "Xproto.h"
#include "misc.h"
#include "miLUT.h"
#include "PEXErr.h"
#include "pixmap.h"
#include "windowstr.h"
#include "gcstruct.h"
#include "scrnintstr.h"
#include "regionstr.h"
#include "miscstruct.h"
#include "miRender.h"

/* External variables and functions */
extern	void		miMatMult();
extern  void		miMatCopy();
extern  ddpex3rtn	InquireLUTEntryAddress();
extern  void		SetViewportClip();


/*++
 |
 |  Function Name:	miBldViewport_xform
 |
 |  Function Description:
 |	 Computes the viewport_xform with special handling for the ddc.
 |       This is used for setting up for drawing, picking, searching,
 |       and MapDCtoWC/MapWCtoDC
 |
 |  Note(s):
 |
 --*/

ddpex3rtn
miBldViewport_xform(pRend, pDrawable, xform, pddc )
    ddRendererPtr       pRend;	   /* renderer handle */
    DrawablePtr		pDrawable; /* pointer to X drawable */
    ddFLOAT		xform[4][4]; /* returned transform */
    miDDContext		*pddc;	   /* dd context handle */
{

    ddFLOAT	Sx, Sy, Sz;
    ddFLOAT	Sxy;
    ddFLOAT	Tx, Ty, Tz;

    /*
     * Determine the npc -> dc viewport transform. Let's represent
     * this transform as follows :
     *
     *            Sx     0      0      0
     *            0      Sy     0      0
     *            0      0      Sz     0
     *            Tx     Ty     Tz     1
     *
     * There are two components to this transformation.
     *
     * NOTE THAT THE TRANSLATION COMPONENTS DETERMINED IN THE BeginRenderi
ng
     * CODE ARE DIFFERENT FOR THE PICKING CASE. WE USE THE PHIGS DC SPACE
     * TO REPRESENT THE PICK APERTURE AS OPPOSED TO THE X'S SCREEN SPACE.
     *
     *
     * The elements Sx, Sy, Tx, and Ty are computed in two stages as
     * follows :
     *
     * First, the npc subvolume in the renderer determines
     * the subvolume that is projected onto the viewport.
     * This tranformation is implemented by "magnifying" this
     * subvolume to the range (0, 0, 0)-(1,1,1) 
     *
     *         NPC space                "magnified" space
     *
     *          +-----+(c,d)             +--------+(1,1)
     *          |     |                  |        |
     *          |     |      <====>      |        |
     *          |     |                  |        |
     *     (a,b)+-----+             (0,0)+--------+
     *
     *                    1/(c-a)       0      0
     *                      0        1/(d-b)   0
     *                   -a/(c-a)   -b/(d-b)   0
     *
     */
    /*
     * Initialize the scaling components Sx, Sy, and Sz.
     *
     */

    Sx = 1/(pRend->npcSubvolume.maxval.x - pRend->npcSubvolume.minval.x);
    Sy = 1/(pRend->npcSubvolume.maxval.y - pRend->npcSubvolume.minval.y);
    Sz = 1/(pRend->npcSubvolume.maxval.z - pRend->npcSubvolume.minval.z);

    /*
     * Secondly, if the useDrawable flag in the viewport is set, then 
     * get the min and max of the drawable from the pDrawable passed 
     * and store it in the viewport as the default values. Otherwise, 
     * the values are as available in the viewport slot of renderer
     * context already set up. Note that the same aspect ratio limitation
     * used above also applies to the viewport transform.
     */
    memcpy( (char *) xform, (char *) ident4x4, 16 * sizeof(ddFLOAT));

    if (pRend->viewport.useDrawable) { /* Use the drawable width and height */
      /* FOR RENDERING:
       * Note that The origin is always 0,0 since ddx points
       * are specified relative to the window origin
       * Lastly, note that the screen origin is at the upper left
       * corner of the window, the viewing space origin is at the
       * lower left corner. To "flip" the coordinates to match
       * the screen space, apply the following transform:
       *
       *            1	   0	    0	0
       *            0	  -1 	    0	0
       *            0	   0        1	0
       *            0 window_y_max  0	0
       *
       *
       *    ie y' = (window_y_max - y)
       *
       */
      /* FOR PICKING:
       * Note that The origin is always 0,0 since DC points
       * are specified relative to the window origin. The
       * required transform is :
       *
       *            Sx    0     0      0
       *            0    Sy     0      0
       *            0     0     1      0
       *            0     0     0      0
       *
       */
      /*
       * Modify the scaling components Sx and Sy to include the second 
       * transform as follows :
       */

      Sx *= (ddFLOAT)pDrawable->width;
      Sy *= (ddFLOAT)pDrawable->height;

      /* 
       * Initialize the translation components Tx, Ty and Tz
       * Note the difference for picking and searching from
       * drawing. drawing flips the y value. the others don't
       */

      Tx = 0.0;
      if ((pRend->render_mode == MI_REND_PICKING) || (!pddc))
          Ty = 0.0;
      else if ((pRend->render_mode == MI_REND_DRAWING) && (pddc))
          Ty = pDrawable->height;
      Tz = 0.0;

    } else {

      xRectangle viewport;

      /*  FOR RENDERING:
       * Limit the rendering for each of the GC's to the viewport 
       * Note that The origin is always 0,0 since ddx points
       * are specified relative to the window origin
       * Lastly, note that the screen origin is at the upper left
       * corner of the window, the viewing space origin is at the
       * lower left corner. Furthermore, primitives must also be
       * translated from this lower left origin to the viewport origin. 
       * To "flip" the coordinates to match the screen space, apply 
       * the following transform:
       *
       *            1		 	   0			  0    0
       *            0			  -1			  0    0
       *            0		 	   0			  1    0
       *   viewport_origin_x  (window_y_max - viewport_origin_y)  0    0
       *
       *
       *    ie y' = (window_y_max - viewport_origin_y - y)
       *
       * Lastly, note that the origin of the clipping rectangle for
       * ddx is relative to the upper left corner of the rectangle,
       * thus this corner must still be further offset by the viewport
       * height.
       *
       */
      /* FOR PICKING:
       * Limit the picking for each of the primitives to the viewport
       * Note that The origin is always 0,0 since DC points
       * are specified relative to the window origin. The required
       * transform is :
       *
       *            Sx    0     0      0
       *            0    Sy     0      0
       *            0     0     1      0
       *            Tx   Ty     0      0
       *
       */
      /* FOR SEARCHING: (this is called when initializing the renderer)
       * there is no drawable, so work around that.  None of what this
       * procedure does is needed for searching, so there may be a better
       * way to deal with this. 
       */
      viewport.width =  pRend->viewport.maxval.x - pRend->viewport.minval.x;
      viewport.height = pRend->viewport.maxval.y - pRend->viewport.minval.y;

      viewport.x = pRend->viewport.minval.x; 
      if (pDrawable)
          viewport.y = pDrawable->height - 
                   pRend->viewport.minval.y - 
                   viewport.height;
      else
	  viewport.y = 0;

      /*
       * Modify the scaling components Sx and Sy to include the second 
       * transform as follows :
       */

      Sx *= (ddFLOAT)viewport.width;
      Sy *= (ddFLOAT)viewport.height;

      /* 
       * Initialize the translation components Tx, Ty and Tz.
       */

      Tx = (ddFLOAT)pRend->viewport.minval.x;
       
      if ((pRend->render_mode == MI_REND_PICKING) || (!pddc))
          Ty = (ddFLOAT)pRend->viewport.minval.y;
      else if ((pRend->render_mode == MI_REND_DRAWING) && (pddc))
	  if (pDrawable)
              Ty = (ddFLOAT)(pDrawable->height - pRend->viewport.minval.y);
	  else Ty = 0.0;
      Tz = 0.0;

      if ((pRend->render_mode == MI_REND_DRAWING) && pDrawable && pddc) {
	  /* Set the GC clip mask to clip to the viewport */

	  ddLONG numrects = pRend->clipList->numObj;

	  if (numrects > 0) {
	      xRectangle      *xrects, *p;
	      ddDeviceRect    *ddrects;
	      RegionPtr	      clipRegion;
	      RegionRec	      tempRegion;
	      GCPtr	      pGC;
	      BoxRec	      box;
	      int             i;

	      ddrects = (ddDeviceRect *) pRend->clipList->pList;
	      xrects = (xRectangle*) Xalloc (numrects * sizeof(xRectangle));
	      if (!xrects) return BadAlloc;
	      /* Need to convert to XRectangle format */
	      for (i = 0, p = xrects; i < numrects; i++, p++, ddrects++) {
		  p->x = ddrects->xmin;
		  p->y = pRend->pDrawable->height - ddrects->ymax;
		  p->width = ddrects->xmax - ddrects->xmin + 1;
		  p->height = ddrects->ymax - ddrects->ymin + 1;
	      }

	      /*
	       * Compute the intersection of the viewport and the GC's
               * clip region.  Note that there is a GC for each of the
               * primitive types, and we only have to compute the
	       * intersected region for one of them.  This computed region
	       * will be good for all of the GC's.
	       */

	      pGC = pddc->Static.misc.pPolylineGC;

	      clipRegion = RECTS_TO_REGION(pGC->pScreen, numrects,
	          xrects, Unsorted);

	      Xfree((char *) xrects);

	      box.x1 = viewport.x;
	      box.y1 = viewport.y;
	      box.x2 = viewport.x + viewport.width;
	      box.y2 = viewport.y + viewport.height;

	      REGION_INIT(pGC->pScreen, &tempRegion, &box, 1);
	      REGION_INTERSECT(pGC->pScreen, clipRegion, clipRegion, &tempRegion);
	      REGION_UNINIT(pGC->pScreen, &tempRegion);


	      /*
	       * Now set the GC clip regions.
	       */

	      SetViewportClip (pddc->Static.misc.pPolylineGC, clipRegion);
	      SetViewportClip (pddc->Static.misc.pFillAreaGC, clipRegion);
	      SetViewportClip (pddc->Static.misc.pEdgeGC, clipRegion);
	      SetViewportClip (pddc->Static.misc.pPolyMarkerGC, clipRegion);
	      SetViewportClip (pddc->Static.misc.pTextGC, clipRegion);

	      REGION_DESTROY(pGC->pScreen, clipRegion);
	  }
	  else {
	      SetClipRects(pddc->Static.misc.pPolylineGC,
		0, 0, 1, &viewport,YXBanded);
	      SetClipRects(pddc->Static.misc.pFillAreaGC,
		0, 0, 1, &viewport,YXBanded);
	      SetClipRects(pddc->Static.misc.pEdgeGC,
		0, 0, 1, &viewport,YXBanded);
	      SetClipRects(pddc->Static.misc.pPolyMarkerGC,
		0, 0, 1, &viewport,YXBanded);
	      SetClipRects(pddc->Static.misc.pTextGC,
		0, 0, 1, &viewport,YXBanded);
	  }
      }
    }

    /* 
     * Note that Phigs requires that aspect ratio be maintained.
     * Therefore, choose the minimum X or y ratio to represent the
     * x and y view port scaling factor. Note that Z is exempt
     * from this aspect ratio restriction.
     */
    Sxy = (Sx < Sy) ? Sx : Sy;

    /*
     * Finally, set the viewport transform components using Sxy, Sz,
     * and the initial Tx, Ty, Tz modified suitably.
     */

    xform[0][0]= Sxy;
    /* Negate the value to "flip" the screen (see above), IF WE ARE */
    /* RENDERING. On the other hand, since we are PICKING, we need  */
    /* the PHIGS DC, i.e., the lower left is the origin and NO FLIP */
    /* of y is required.                                            */
    /* In case pddc is NULL, this routine is being called from Map- */
    /* DcWc and the situation is similar to PICKING. i.e., No y flip*/
    /* is required.                                                 */
    if ((pRend->render_mode == MI_REND_PICKING) || (!pddc))
        xform[1][1]= Sxy;
    else if ((pRend->render_mode == MI_REND_DRAWING) && (pddc))
        xform[1][1]= -Sxy;
    xform[2][2]= Sz;

    xform[0][3] = Tx - (pRend->npcSubvolume.minval.x * Sxy);

    /* Note, the "+" compensates for negating the [1][1] term above */
    if ((pRend->render_mode == MI_REND_PICKING) || (!pddc))
        xform[1][3]= Ty - (pRend->npcSubvolume.minval.y * Sxy);
    else if ((pRend->render_mode == MI_REND_DRAWING) && (pddc))
        xform[1][3]= Ty + (pRend->npcSubvolume.minval.y * Sxy);

    xform[2][3]= Tz - (pRend->npcSubvolume.minval.z * Sz);

    /* Mark as invalid appropriate inverse transforms in dd context */
    if (pddc)
        pddc->Static.misc.flags |= ( INVTRCCTODCXFRMFLAG );

}

/*++
 |
 |  Function Name:	miBldCC_xform
 |
 |  Function Description:
 |	 Computes the transformation to go from NPC to the PEX-SI's 
 |       clipping space.
 |
 |  Note(s):
 |
 --*/

ddpex3rtn
miBldCC_xform(pRend, pddc)
    ddRendererPtr	pRend;	  /* renderer handle */
    miDDContext		*pddc;	  /* dd context handle */

{

    ddNpcSubvolume	*cliplimits;
    miViewEntry		*viewbundle;
    ddFLOAT		cc_to_npc[4][4];
    ddUSHORT    	status;

    /* 
     * Check if a view LUT is available and accordingly, get the view 
     * matrices either from the view LUT or from the defaults.
     */

    if (!(pRend->lut[PEXViewLUT])) {   /* Use defaults */

	 memcpy( (char *) pddc->Dynamic->npc_to_cc_xform, 
		(char *) ident4x4, 
		16 * sizeof(ddFLOAT));
	 pddc->Dynamic->npc_to_cc_xform[0][0] =  2.0;
	 pddc->Dynamic->npc_to_cc_xform[1][1] =  2.0;
	 pddc->Dynamic->npc_to_cc_xform[2][2] =  2.0;
	 pddc->Dynamic->npc_to_cc_xform[0][3] = -1.0;
	 pddc->Dynamic->npc_to_cc_xform[1][3] = -1.0;
	 pddc->Dynamic->npc_to_cc_xform[2][3] = -1.0;

	 memcpy( (char *) pddc->Dynamic->wc_to_npc_xform,
		(char *) ident4x4,
		16*(sizeof(ddFLOAT)));

	 memcpy( (char *) cc_to_npc,
		(char *) ident4x4, 
		16 * sizeof(ddFLOAT));
	 cc_to_npc[0][0] = 0.5;
	 cc_to_npc[1][1] = 0.5;
	 cc_to_npc[2][2] = 0.5;
	 cc_to_npc[0][3] = 0.5;
	 cc_to_npc[1][3] = 0.5;
	 cc_to_npc[2][3] = 0.5;

/* THIS SHOULD GO AWAY EVENTUALLY... */
pddc->Dynamic->clipFlags = 0;

    } else {

      /* Get the view table entry at current view index first */

      if ((InquireLUTEntryAddress (PEXViewLUT, pRend->lut[PEXViewLUT], 
				   pddc->Dynamic->pPCAttr->viewIndex, 
				   &status, (ddPointer *)(&viewbundle)))
	  == PEXLookupTableError)
	return (PEXLookupTableError);

      miMatCopy(viewbundle->vom, pddc->Dynamic->wc_to_npc_xform);

/* THIS SHOULD GO AWAY EVENTUALLY... */
pddc->Dynamic->clipFlags = viewbundle->entry.clipFlags;

      if (viewbundle->entry.clipFlags)
        {
	/* Use the clip limits as set up in the current view */
 
	 cliplimits  = &viewbundle->entry.clipLimits;
	 /* 
	  * Compute the npc_to_cc & cc_to_npc 
	  * transformations.
	  * The Pex view clipper can only clip against a cube with
	  * corners (-1,-1,-1) (1,1,1).
	  * It is therefore necessary to append an additional 
	  * transformation to the wc_to_cc_xform transform such 
	  * that the view clip volume is transformed to this cube, 
	  * rather than leave the space untouched and clip against actual
	  * clip planes specified in the view lut.
	  * Note that this transformation must be undone by the
	  * subsequent cc_to_dc_xform, so the inverse of this
	  * matrix is pre-pended to this transform.
	  *
	  * two matrices then (note these extend trivially into 3D):
	  *
	  *       NPC subvolume              SI clipping space
	  *
	  *          +-----+(c,d)               +--------+(1,1)
	  *          |     |                    |        |
	  *          |     |      <====>        |        |
	  *          |     |                    |        |
	  *     (a,b)+-----+             (-1,-1)+--------+
	  *
	  *   npc_to_clip:     2/(c-a)       0      0
	  *                      0        2/(d-b)   0
	  *                 (c+a)/(a-c) (d+b)/b-d)  0
	  *
	  *   clip_to_npc:   (c-a)/2         0      0
	  *                     0         (d-b)/2   0
	  *                  (c+a)/2      (d+b)/2   0
	  *
	  * Last, note that the cliplimits from the view table entry are only
	  * used if enabled by the clip flags.
	  */
	 memcpy( (char *) pddc->Dynamic->npc_to_cc_xform,
		(char *) ident4x4, 16 * sizeof(ddFLOAT));

	 memcpy( (char *) cc_to_npc, 
		(char *) ident4x4, 16 * sizeof(ddFLOAT));

	 /* set up X & Y clip limits */

	 if (viewbundle->entry.clipFlags & PEXClipXY) {
	   pddc->Dynamic->npc_to_cc_xform[0][0] = 
			2.0/(cliplimits->maxval.x - cliplimits->minval.x);
	   pddc->Dynamic->npc_to_cc_xform[1][1] = 
			2.0/(cliplimits->maxval.y - cliplimits->minval.y);
	   pddc->Dynamic->npc_to_cc_xform[0][3] = 
		(cliplimits->maxval.x + cliplimits->minval.x) / 
		(cliplimits->minval.x - cliplimits->maxval.x);
	   pddc->Dynamic->npc_to_cc_xform[1][3] = 
		(cliplimits->maxval.y + cliplimits->minval.y) / 
		(cliplimits->minval.y - cliplimits->maxval.y);

	   cc_to_npc[0][0] = 
			(cliplimits->maxval.x - cliplimits->minval.x)/2.0;
	   cc_to_npc[1][1] = 
			(cliplimits->maxval.y - cliplimits->minval.y)/2.0;
	   cc_to_npc[0][3] = 
			(cliplimits->maxval.x + cliplimits->minval.x)/2.0;
	   cc_to_npc[1][3] =
			(cliplimits->maxval.y + cliplimits->minval.y)/2.0;
	 } else {

	   /* Use default NPC clip limits (0.0 <-> 1.0) */

	   pddc->Dynamic->npc_to_cc_xform[0][0] = 2.0/(1.0 - 0.0);
	   pddc->Dynamic->npc_to_cc_xform[1][1] = 2.0/(1.0 - 0.0);
	   pddc->Dynamic->npc_to_cc_xform[0][3] = (1.0 + 0.0)/(0.0 - 1.0);
	   pddc->Dynamic->npc_to_cc_xform[1][3] = (1.0 + 0.0)/(0.0 - 1.0);

	   cc_to_npc[0][0] = (1.0 - 0.0)/2.0;
	   cc_to_npc[1][1] = (1.0 - 0.0)/2.0;
	   cc_to_npc[0][3] = (1.0 + 0.0)/2.0;
	   cc_to_npc[1][3] = (1.0 + 0.0)/2.0;
	 }

	 /* Now, front & back clip limits */

	 if ( (viewbundle->entry.clipFlags & PEXClipFront) &&
	      (viewbundle->entry.clipFlags & PEXClipBack) ) {

	    /* Both front and back clipping on */

	   pddc->Dynamic->npc_to_cc_xform[2][2] = 
			2.0/(cliplimits->maxval.z - cliplimits->minval.z);
	   pddc->Dynamic->npc_to_cc_xform[2][3] = 
		(cliplimits->maxval.z + cliplimits->minval.z) / 
		(cliplimits->minval.z - cliplimits->maxval.z);

	   cc_to_npc[2][2] = 
			(cliplimits->maxval.z - cliplimits->minval.z)/2.0;
	   cc_to_npc[2][3] =
			(cliplimits->maxval.z + cliplimits->minval.z)/2.0;

	 } else if (viewbundle->entry.clipFlags & PEXClipFront) {

	   /* Only front clipping ON; Use default NPC Back value */

	   pddc->Dynamic->npc_to_cc_xform[2][2] = 
			2.0/(1.0 - cliplimits->minval.z);
	   pddc->Dynamic->npc_to_cc_xform[2][3] = 
		(1.0 + cliplimits->minval.z) / 
		(cliplimits->minval.z - 1.0);

	   cc_to_npc[2][2] = 
			(1.0 - cliplimits->minval.z)/2.0;
	   cc_to_npc[2][3] =
			(1.0 + cliplimits->minval.z)/2.0;

	 } else if (viewbundle->entry.clipFlags & PEXClipBack) {

	   /* Only back clipping ON; Use default NPC Front value */

	   pddc->Dynamic->npc_to_cc_xform[2][2] = 2.0/(cliplimits->maxval.z);
	   pddc->Dynamic->npc_to_cc_xform[2][3] = -1.0;

	   cc_to_npc[2][2] = (cliplimits->maxval.z)/2.0;
	   cc_to_npc[2][3] = (cliplimits->maxval.z)/2.0;

	 } else {

	   /* Both front and back clipping OFF; Use default NPC space */

	   pddc->Dynamic->npc_to_cc_xform[2][2] = 2.0/(1.0 - 0.0);
	   pddc->Dynamic->npc_to_cc_xform[2][3] = (1.0 + 0.0) / (0.0 - 1.0);

	   cc_to_npc[2][2] = (1.0 - 0.0)/2.0;
	   cc_to_npc[2][3] = (1.0 + 0.0)/2.0;
	 }

        }
      else  /* All clipping OFF; Use the entire NPC space, i.e., (0,0,0) */
	    /* to (1,1,1)                                                */
        {

	 memcpy( (char *) pddc->Dynamic->npc_to_cc_xform, 
		(char *) ident4x4, 16 * sizeof(ddFLOAT));
	 pddc->Dynamic->npc_to_cc_xform[0][0] =  2.0;
	 pddc->Dynamic->npc_to_cc_xform[1][1] =  2.0;
	 pddc->Dynamic->npc_to_cc_xform[2][2] =  2.0;
	 pddc->Dynamic->npc_to_cc_xform[0][3] = -1.0;
	 pddc->Dynamic->npc_to_cc_xform[1][3] = -1.0;
	 pddc->Dynamic->npc_to_cc_xform[2][3] = -1.0;

	 memcpy( (char *) cc_to_npc,
		(char *) ident4x4, 16 * sizeof(ddFLOAT));
	 cc_to_npc[0][0] = 0.5;
	 cc_to_npc[1][1] = 0.5;
	 cc_to_npc[2][2] = 0.5;
	 cc_to_npc[0][3] = 0.5;
	 cc_to_npc[1][3] = 0.5;
	 cc_to_npc[2][3] = 0.5;

        }
    }

    /*
     * Compute the various composites stored in the
     * dd context.
     */
    /*
     * Compute the composite cc -> dc, i.e., Clip to Device.
     */
    miMatMult ( pddc->Dynamic->cc_to_dc_xform,
		cc_to_npc, 
		pddc->Static.misc.viewport_xform);

    /*
     * Compute the composite wc -> cc, i.e., World to Clip.
     */
    miMatMult ( pddc->Dynamic->wc_to_cc_xform,
		pddc->Dynamic->wc_to_npc_xform, 
		pddc->Dynamic->npc_to_cc_xform);
    /*
     * Compute the composite mc -> wc, i.e. Modelling to World
     */
    miMatMult(  pddc->Dynamic->mc_to_wc_xform,
                pddc->Dynamic->pPCAttr->localMat,
                pddc->Dynamic->pPCAttr->globalMat);

    /*
     * Compute the composite mc -> npc, i.em Modelling to NPC
     */
    miMatMult ( pddc->Dynamic->mc_to_npc_xform,
		pddc->Dynamic->mc_to_wc_xform, 
		pddc->Dynamic->wc_to_npc_xform);

    /* 
     * Compute the composite [VCM], i.e, Modelling to Clip.
     */
    miMatMult ( pddc->Dynamic->mc_to_cc_xform,
		pddc->Dynamic->mc_to_wc_xform, 
		pddc->Dynamic->wc_to_cc_xform);
    /* 
     * Compute the composite mc -> dc transform, i.e., Modelling to Device.
     */
    miMatMult ( pddc->Dynamic->mc_to_dc_xform,
		pddc->Dynamic->mc_to_cc_xform, 
		pddc->Dynamic->cc_to_dc_xform);

    /* Mark as invalid appropriate inverse transforms in dd context */
    pddc->Static.misc.flags |= (INVTRWCTOCCXFRMFLAG | INVTRMCTOCCXFRMFLAG | 
				INVVIEWXFRMFLAG | CC_DCUEVERSION);

}


void SetViewportClip(pGC, clipRegion)
    GCPtr pGC;
    RegionPtr clipRegion;
{
    /*
     * When we call the GC's ChangeClip function, it destroys
     * the region we pass to it.  Since this function is called
     * for each of the GC's, we copy clipRegion into tempRegion
     * and use tempRegion when calling ChangeClip.
     */

    RegionPtr tempRegion = REGION_CREATE(pGC->pScreen, NullBox, 0);

    REGION_COPY(pGC->pScreen, tempRegion, clipRegion);

    pGC->serialNumber |= GC_CHANGE_SERIAL_BIT;
    pGC->clipOrg.x = 0;
    pGC->clipOrg.y = 0;
    pGC->stateChanges |= GCClipXOrigin|GCClipYOrigin;
    (*pGC->funcs->ChangeClip)(pGC, CT_REGION, tempRegion, 0);
    if (pGC->funcs->ChangeGC)
	(*pGC->funcs->ChangeGC) (pGC, GCClipXOrigin|GCClipYOrigin|GCClipMask);
}