SbrRenW.cc 33.6 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*=========================================================================

  Program:   Visualization Library
  Module:    SbrRenW.cc
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

This file is part of the Visualization Library. No part of this file or its
contents may be copied, reproduced or altered in any way without the express
written consent of the authors.

Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994

=========================================================================*/
#include <math.h>
Ken Martin's avatar
Ken Martin committed
17
#include <stdlib.h>
Ken Martin's avatar
Ken Martin committed
18
19
20
21
#include <iostream.h>
#include "SbrRenW.hh"
#include "SbrRen.hh"
#include "SbrProp.hh"
Ken Martin's avatar
Ken Martin committed
22
#include "SbrText.hh"
Ken Martin's avatar
Ken Martin committed
23
24
25
26
27
28
29
30
31
#include "SbrCam.hh"
#include "SbrLgt.hh"

#define MAX_LIGHTS 16

static char *lights[MAX_LIGHTS] =
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

Will Schroeder's avatar
Will Schroeder committed
32
33
34
vlSbrRenderWindow::vlSbrRenderWindow()
{
  this->Fd = -1;
Ken Martin's avatar
Ken Martin committed
35
  this->StereoType = VL_STEREO_CRYSTAL_EYES;
Will Schroeder's avatar
Will Schroeder committed
36
37
38
39
40
  strcpy(this->Name,"Visualization Library - Starbase");
}

// Description:
// Create a starbase-specific actor.
Ken Martin's avatar
Ken Martin committed
41
42
43
44
45
46
47
48
vlActor *vlSbrRenderWindow::MakeActor()
{
  vlActor *actor;
  vlSbrProperty *prop;

  actor = new vlActor;
  prop  = new vlSbrProperty;

49
  actor->SetProperty((vlProperty *)prop);
Ken Martin's avatar
Ken Martin committed
50
51
52
  return (vlActor *)actor;
}

Will Schroeder's avatar
Will Schroeder committed
53
54
// Description:
// Create a starbase specific light.
Ken Martin's avatar
Ken Martin committed
55
56
57
58
59
60
61
62
vlLight *vlSbrRenderWindow::MakeLight()
{
  vlSbrLight *light;

  light = new vlSbrLight;
  return (vlLight *)light;
}

Will Schroeder's avatar
Will Schroeder committed
63
64
// Description:
// Create a starbase specific renderer.
Ken Martin's avatar
Ken Martin committed
65
66
67
68
69
vlRenderer *vlSbrRenderWindow::MakeRenderer()
{
  vlSbrRenderer *ren;

  ren = new vlSbrRenderer;
70
  this->AddRenderers(ren);
Ken Martin's avatar
Ken Martin committed
71
72
73
74
75
76
77

  // by default we are its parent
  ren->SetRenderWindow((vlRenderWindow*)this);
  
  return (vlRenderer *)ren;
}

Will Schroeder's avatar
Will Schroeder committed
78
79
// Description:
// Create a starbase specific camera.
Ken Martin's avatar
Ken Martin committed
80
81
82
83
84
85
86
87
vlCamera *vlSbrRenderWindow::MakeCamera()
{
  vlSbrCamera *camera;

  camera = new vlSbrCamera;
  return (vlCamera *)camera;
}

88
89
90
91
92
93
94
95
96
97
// Description:
// Create a starbase specific property.
vlProperty *vlSbrRenderWindow::MakeProperty()
{
  vlSbrProperty *property;

  property = new vlSbrProperty;
  return (vlProperty *)property;
}

Ken Martin's avatar
Ken Martin committed
98
99
100
101
102
103
104
105
106
107
// Description:
// Create a starbase specific texture.
vlTexture *vlSbrRenderWindow::MakeTexture()
{
  vlSbrTexture *texture;

  texture = new vlSbrTexture;
  return (vlTexture *)texture;
}

Will Schroeder's avatar
Will Schroeder committed
108
109
// Description:
// Begin the rendering process.
Ken Martin's avatar
Ken Martin committed
110
111
112
113
114
115
116
117
118
void vlSbrRenderWindow::Start(void)
{
  // if the renderer has not been initialized, do so now
  if (this->Fd == -1)
    this->Initialize();

  flush_matrices(this->Fd);
}

Ken Martin's avatar
Ken Martin committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Description:
// Update system if needed due to stereo rendering.
void vlSbrRenderWindow::StereoUpdate(void)
{
  // if stereo is on and it wasn't before
  if (this->StereoRender && (!this->StereoStatus))
    {
    switch (this->StereoType) 
      {
      case VL_STEREO_CRYSTAL_EYES:
	{
	gescape_arg arg1,arg2;
	arg1.i[0] = 1;
	gescape(this->Fd,STEREO,&arg1,&arg2);
	// make sure we are in full screen
        this->StereoStatus = 1;
	this->FullScreenOn();
	}
137
138
139
140
141
	break;
      case VL_STEREO_RED_BLUE:
	{
        this->StereoStatus = 1;
	}
Ken Martin's avatar
Ken Martin committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
      }
    }
  else if ((!this->StereoRender) && this->StereoStatus)
    {
    switch (this->StereoType) 
      {
      case VL_STEREO_CRYSTAL_EYES:
	{
	gescape_arg arg1,arg2;
	arg1.i[0] = 0;
	gescape(this->Fd,STEREO,&arg1,&arg2);
	// make sure we are in full screen
        this->StereoStatus = 0;
	this->FullScreenOff();
	}
157
158
159
160
161
	break;
      case VL_STEREO_RED_BLUE:
	{
        this->StereoStatus = 0;
	}
Ken Martin's avatar
Ken Martin committed
162
163
164
165
      }
    }
}

Will Schroeder's avatar
Will Schroeder committed
166
167
// Description:
// End the rendering process and display the image.
Ken Martin's avatar
Ken Martin committed
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
void vlSbrRenderWindow::Frame(void)
{
  // flush and display the buffer
  if (this->DoubleBuffer) 
    {
    dbuffer_switch(this->Fd, this->Buffer = !(this->Buffer));
    }
}

/*
 * get the visual type which matches the depth argument
 */
static Visual * xlib_getvisual(Display *display,int screen,int depth)
{
    XVisualInfo templ;
    XVisualInfo *visuals, *v;
    Visual	*vis = NULL;
    int		nvisuals;
    int		i;

	templ.screen = screen;
	templ.depth = depth;

	vis = DefaultVisual(display, screen);

	visuals = XGetVisualInfo(display, VisualScreenMask | VisualDepthMask,
			&templ, &nvisuals);

	for (v = visuals, i = 0; i < nvisuals; v++, i++)
		if (v->c_class == vis->c_class) {
			vis = v->visual;
			break;						
		}

	return(vis);
}

/*
 * get a PseudoColor visual
 */
static Visual * xlib_getpseudocolorvisual(Display *display,int screen,
					  int depth)
{
    XVisualInfo templ;
    XVisualInfo *visuals, *v;
    Visual	*vis = NULL;
    int		nvisuals;
    int		i;

	  templ.screen = screen;
	  templ.depth = depth;

	  visuals = XGetVisualInfo(display, VisualScreenMask | VisualDepthMask,
			&templ, &nvisuals);

	  for (v = visuals, i = 0; i < nvisuals; v++, i++)
		if (v->c_class == PseudoColor) {
			vis = v->visual;
			break;						
		}

	return(vis);
}

/*
 * get a TrueColor visual
 */
static Visual * xlib_gettruecolorvisual(Display *display,int screen,int depth)
{
    XVisualInfo templ;
    XVisualInfo *visuals, *v;
    Visual	*vis = NULL;
    int		nvisuals;
    int		i;

	  templ.screen = screen;
	  templ.depth = depth;

	  visuals = XGetVisualInfo(display, VisualScreenMask | VisualDepthMask,
			&templ, &nvisuals);

	  for (v = visuals, i = 0; i < nvisuals; v++, i++)
		if (v->c_class == TrueColor) {
			vis = v->visual;
			break;						
		}

	return(vis);
}

/*
 * get a DirectColor visual
 */
static Visual * xlib_getdirectcolorvisual(Display *display,
					  int screen,int depth)
{
    XVisualInfo templ;
    XVisualInfo *visuals, *v;
    Visual	*vis = NULL;
    int		nvisuals;
    int		i;

	  templ.screen = screen;
	  templ.depth = depth;

	  visuals = XGetVisualInfo(display, VisualScreenMask | VisualDepthMask,
			&templ, &nvisuals);

	  for (v = visuals, i = 0; i < nvisuals; v++, i++)
		if (v->c_class == DirectColor) {
			vis = v->visual;
			break;						
		}

	return(vis);
}



/*
 * get the best visual for XGL accelerated colors
 * This should be called instead of xglut_argprocess if you
 * don't want your user to be able to select the color type
 * and visual of xglut.
 */
static int xlib_get_best_depth(Display *display)
{
  int depth;
  Visual *vis;

  vis = xlib_gettruecolorvisual(display, DefaultScreen(display), 24);
  if (vis == NULL) 
    {
    vis = xlib_getdirectcolorvisual(display, DefaultScreen(display), 24);
    if (vis == NULL) 
      {
      vis = xlib_getpseudocolorvisual(display, DefaultScreen(display), 8); 
      if (vis == NULL) 
	{
	fprintf(stderr,"can't get visual info\n");
	exit(1);
	}
      else 
	{
	depth = 8;
	}
      }
    else 
      {
      depth = 24;
      }
    }
  else 
    {
    depth = 24;
    }

  return(depth);
}

/*
 * get the best visual for XGL accelerated colors
 * This should be called instead of xglut_argprocess if you
 * don't want your user to be able to select the color type
 * and visual of xglut.
 */
static Visual *xlib_get_best_visual(Display *display)
{
  int depth;
  Visual *vis;

    vis = xlib_getdirectcolorvisual(display, DefaultScreen(display), 24);
    if (vis == NULL) 
      {
      vis = xlib_getpseudocolorvisual(display, DefaultScreen(display), 8); 
      if (vis == NULL) 
	{
	fprintf(stderr,"can't get visual info\n");
	exit(1);
	}
      else 
	{
	depth = 8;
	}
      }
    else 
      {
      depth = 24;
      }

  return (vis);
}


int vlSbrRenderWindow::GetDesiredDepth ()
{
  int depth;

  /* get the default display connection */
  if (!this->DisplayId)
    {
    this->DisplayId = XOpenDisplay((char *)NULL); 
    if (this->DisplayId == NULL) 
      {
      vlErrorMacro(<< "bad X server connection.\n");
      }
    }

  /* get the default depth to use */
  depth = xlib_get_best_depth(this->DisplayId);

  return depth;  
}

Will Schroeder's avatar
Will Schroeder committed
382
383
// Description:
// Obtain a colormap from windowing system.
Ken Martin's avatar
Ken Martin committed
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
Colormap vlSbrRenderWindow::GetDesiredColormap ()
{
  XVisualInfo *pVisInfo, visInfo;
  Colormap cmapID;
  int depth;
  unsigned int mask;
  int retVal;
  Display *dpy;

  /* get the default depth to use */
  depth = xlib_get_best_depth(this->DisplayId);
  dpy = this->DisplayId;

  visInfo.screen = 0;
  visInfo.depth = depth;

  if(depth == 4)
    visInfo.c_class = PseudoColor;
  if(depth == 8)
    visInfo.c_class = PseudoColor;
  if(depth == 12)
    visInfo.c_class = PseudoColor;
  if(depth == 16)
    visInfo.c_class = PseudoColor;
  
  /* DirectColor visual is used needed for CMAP_FULL */
  if(depth == 24)
    visInfo.c_class = DirectColor;
  
  vlDebugMacro(<< "Starbase: The depth is " << depth << "\n");
  /*
   * First, ask for the desired visual
   */
  mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
  
  pVisInfo = XGetVisualInfo(dpy, mask, &visInfo, &retVal);
  
  if (!retVal) 
    {
    if (depth == 24) 
      {
      /*
       * try again with 16 bits
       */
      visInfo.depth = 16;
      visInfo.c_class = PseudoColor;
      pVisInfo = XGetVisualInfo(dpy, mask, &visInfo, &retVal);
      if (!retVal) 
	{
	fprintf(stderr, "Could not get visual info\n");
	return 0;
	}
      } 
    else 
      {
      fprintf(stderr,"Could not get visual info\n");
      return 0;
      }
    }
  
  if (retVal != 1) 
    {
    fprintf(stderr,"Too many visuals match display+depth+class\n");
    return 0;
    }
  

  /*
   * a ColorMap MUST be created
   */
  if (!this->ColorMap)
    {
    cmapID = 
      XCreateColormap(this->DisplayId,
		      RootWindowOfScreen(ScreenOfDisplay(dpy,0)),
		      pVisInfo->visual, AllocNone);
    if (!cmapID) 
      {
      fprintf(stderr,"Could not create color map\n");
      return 0;
      }
    this->ColorMap = cmapID;
    }

  return this->ColorMap;  
}


Will Schroeder's avatar
Will Schroeder committed
472
473
// Description:
// Get a visual from the windowing system.
Ken Martin's avatar
Ken Martin committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
Visual *vlSbrRenderWindow::GetDesiredVisual ()
{
  Visual *vis;

  /* get the default display connection */
  if (!this->DisplayId)
    {
    this->DisplayId = XOpenDisplay((char *)NULL); 
    if (this->DisplayId == NULL) 
      {
      vlErrorMacro(<< "bad X server connection.\n");
      }
    }

  /* get the default visual to use */
  vis = xlib_get_best_visual(this->DisplayId);

  return vis;  
}


Will Schroeder's avatar
Will Schroeder committed
495
496
// Description:
// Create a window for starbase output.
Ken Martin's avatar
Ken Martin committed
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
int vlSbrRenderWindow::CreateXWindow(Display *dpy,int xpos,int ypos, 
				     int width,int height,int depth, 
				     char name[80])
{
  Window win;
  XEvent event;
  XVisualInfo *pVisInfo,visInfo;
  Colormap cmapID;
  XColor c0, c1; 
  XSetWindowAttributes winattr;
  char *window_name;
  Pixmap icon_pixmap;
  int screen;
  unsigned int mask;
  int retVal;
  XSizeHints xsh;
  
  visInfo.screen = 0;
  visInfo.depth = depth;
  icon_pixmap = 0;
  
  /*
   * PseudoColor visual is used needed for CMAP_NORMAL
   */
  if(depth == 4)
    visInfo.c_class = PseudoColor;
  
  if(depth == 8)
    visInfo.c_class = PseudoColor;
  
  if(depth == 12)
    visInfo.c_class = PseudoColor;
  
  if(depth == 16)
    visInfo.c_class = PseudoColor;
  
  /*
   * DirectColor visual is used needed for CMAP_FULL
   */
  if(depth == 24)
    visInfo.c_class = DirectColor;
  
  vlDebugMacro(<< "Starbase: The depth is " << depth << "\n");
  /*
   * First, ask for the desired visual
   */
  mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
  
  pVisInfo = XGetVisualInfo(dpy, mask, &visInfo, &retVal);
  
  if (!retVal) {
    if (depth == 24) {
      /*
       * try again with 16 bits
       */
      visInfo.depth = 16;
      visInfo.c_class = PseudoColor;
      pVisInfo = XGetVisualInfo(dpy, mask, &visInfo, &retVal);
      if (!retVal) {
	fprintf(stderr, "Could not get visual info\n");
	return 0;
      }
    } else {
      fprintf(stderr,"Could not get visual info\n");
        return 0;
    }
  }
  
  if (retVal != 1) {
    fprintf(stderr,"Too many visuals match display+depth+class\n");
    return 0;
  }
  
  // a ColorMap MUST be created
  if (!this->ColorMap)
    {
    cmapID = XCreateColormap(dpy,
			     RootWindowOfScreen(ScreenOfDisplay(dpy,0)),
			     pVisInfo->visual, AllocNone);
    if (!cmapID) 
      {
      fprintf(stderr,"Could not create color map\n");
      return 0;
      }
    this->ColorMap = cmapID;
    }

  
  // Border and background info MUST be passed in also
  winattr.event_mask = 0;
  winattr.border_pixel = 1;
  winattr.background_pixel = 0;
  winattr.colormap = this->ColorMap;
  if ((xpos >= 0)&&(ypos >= 0))
    {
    xsh.flags = USPosition | USSize;
    }
  else
    {
    xsh.flags = PPosition | PSize;
    }
  xsh.x = ((xpos >= 0) ? xpos : 5);
  xsh.y = ((ypos >= 0) ? ypos : 5);
  xsh.width = WidthOfScreen(ScreenOfDisplay(dpy,0));
  xsh.height = HeightOfScreen(ScreenOfDisplay(dpy,0));
  
  // if both the position and size have been set, override the window
  // manager
  winattr.override_redirect = False;
  if ((width > 0) && (xpos >= 0) && (!this->Borders))
    winattr.override_redirect = True;

  XFlush(dpy);
  
  /*
   * create the parent X11 Window
   */
  
  win = XCreateWindow(dpy, RootWindowOfScreen(ScreenOfDisplay(dpy,0)),
                      xsh.x, xsh.y, xsh.width, xsh.height, 0, depth,
                      InputOutput, pVisInfo->visual,
                      CWColormap | CWBorderPixel | CWBackPixel |
			CWEventMask | CWOverrideRedirect , &winattr);
  if(! win) 
    {
    fprintf(stderr,"Could not create window\n");
    return 0;
    }

  /*
   * Give the window a name
   */
  XSetStandardProperties(dpy, win, name, name, icon_pixmap, NULL, 0, &xsh);
  XSelectInput(dpy, win, KeyPressMask|ExposureMask|StructureNotifyMask);

  /*
   * set the default window
   */
  this->WindowId = win;  
  this->DisplayId = dpy;
  XSync(dpy, False );

  return 1;
}
 
Will Schroeder's avatar
Will Schroeder committed
642
643
// Description:
// Initialize the rendering window.
Ken Martin's avatar
Ken Martin committed
644
645
void vlSbrRenderWindow::WindowInitialize (void)
{
Ken Martin's avatar
Ken Martin committed
646
  char *device, *driver, *str;
Ken Martin's avatar
Ken Martin committed
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
  int planes, depth, mode;
  int create_xwindow();
  int cmap_size;
  XSizeHints *size_hints;
  XClassHint *class_hint;
  XWMHints *wm_hints;
  XTextProperty window_name, icon_name;
  char *list[1];
  XEvent event;
  XWindowAttributes winattr;

  // get the default depth to use
  depth = this->GetDesiredDepth();

  mode = OUTDEV;
  if (this->WindowId == 0) 
    {
    if (! 
	this->CreateXWindow(this->DisplayId, this->Position[0],
			    this->Position[1],
			    this->Size[0],
			    this->Size[1],
			    depth, this->Name)) 
      {
      vlErrorMacro(<< "Couldn't create window\n");
      return;
      }
    this->OwnWindow = 1;
    }
  else
    {
Ken Martin's avatar
Ken Martin committed
678
679
    XSetWindowAttributes xswattr;

Ken Martin's avatar
Ken Martin committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
    this->OwnWindow = 0;
    /* make sure the window is unmapped */
    XUnmapWindow(this->DisplayId, this->WindowId);
    XSync(this->DisplayId,False);
    vlDebugMacro(<< "Unmapping the xwindow\n");
    XGetWindowAttributes(this->DisplayId,
			 this->WindowId,&winattr);
    while (winattr.map_state != IsUnmapped)
      {
      XNextEvent(this->DisplayId, &event);
      XGetWindowAttributes(this->DisplayId,
			   this->WindowId,&winattr);
      }; 
    
    /* make sure the window is full screen */
    vlDebugMacro( << "Resizing the xwindow\n");
Ken Martin's avatar
Ken Martin committed
696
697
    XSelectInput(this->DisplayId, this->WindowId, 
		 KeyPressMask|ExposureMask);
Ken Martin's avatar
Ken Martin committed
698
699
700
701
702
703
704
705

    xswattr.override_redirect = False;
    if ((!this->Borders))
      xswattr.override_redirect = True;

    XChangeWindowAttributes(this->DisplayId,this->WindowId,
			    CWOverrideRedirect, &xswattr);

Ken Martin's avatar
Ken Martin committed
706
707
708
709
710
711
712
713
714
    XResizeWindow(this->DisplayId,this->WindowId,
		  WidthOfScreen(ScreenOfDisplay(this->DisplayId,0)),
		  HeightOfScreen(ScreenOfDisplay(this->DisplayId,0)));
    XSync(this->DisplayId,False);
    XGetWindowAttributes(this->DisplayId,
			 this->WindowId,&winattr);
    while (winattr.width != 
	   WidthOfScreen(ScreenOfDisplay(this->DisplayId,0)))
      {
Ken Martin's avatar
Ken Martin committed
715
/*      XNextEvent(this->DisplayId, &event); */
Ken Martin's avatar
Ken Martin committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
      XGetWindowAttributes(this->DisplayId,
			   this->WindowId,&winattr);
      } 
    }

  // convert window id to something Starbase can open
  device = make_X11_gopen_string(this->DisplayId, (Window)this->WindowId);
  if (!device) 
    {
    vlErrorMacro(<< "Could not create device file for window.\n");
    device = "/dev/crt";
    }
  
  driver = getenv("SB_OUTDRIVER");
  if ((this->Fd = 
       gopen(device, mode, driver, 
	     RESET_DEVICE | INIT | THREE_D | MODEL_XFORM)) == -1) 
    {
    vlErrorMacro(<< "cannot open starbase driver error number= " 
    << errno << "\n");
    return;
    }

  // RESIZE THE WINDOW TO THE DESIRED SIZE
  vlDebugMacro(<< "Resizing the xwindow\n");
  XResizeWindow(this->DisplayId,this->WindowId,
		((this->Size[0] > 0) ? 
		 (int)(this->Size[0]) : 256),
		((this->Size[1] > 0) ? 
		 (int)(this->Size[1]) : 256));
  XSync(this->DisplayId,False);

  list[0] = this->Name;
  XStringListToTextProperty( list, 1, &window_name );
  list[0] = this->Name;
  XStringListToTextProperty( list, 1, &icon_name );
    
  size_hints = XAllocSizeHints();
  size_hints->flags = USSize;
  if ((this->Position[0] >= 0)&&(this->Position[1] >= 0))
    {
    size_hints->flags |= USPosition;
    size_hints->x =  (int)(this->Position[0]);
    size_hints->y =  (int)(this->Position[1]);
    }
  
  size_hints->width  = 
    ((this->Size[0] > 0) ? (int)(this->Size[0]) : 256);
  size_hints->height = 
    ((this->Size[1] > 0) ?  (int)(this->Size[1]) : 256);
  
  wm_hints = XAllocWMHints();

  class_hint = XAllocClassHint();
  class_hint->res_name = this->Name;
  class_hint->res_class = this->Name;
  
  XSetWMProperties(this->DisplayId, 
		   this->WindowId, &window_name, &icon_name,
		   NULL, 0, size_hints, wm_hints, class_hint );

  /* Finally -- we can map the window!  We won't actually render anything
     to the window until the expose event happens later. */
  vlDebugMacro(<< "Mapping the xwindow\n");
  XMapWindow(this->DisplayId, this->WindowId);
  XSync(this->DisplayId,False);
  XGetWindowAttributes(this->DisplayId,
		       this->WindowId,&winattr);
  while (winattr.map_state == IsUnmapped)
    {
    XGetWindowAttributes(this->DisplayId,
			 this->WindowId,&winattr);
    };
  
  // free up the memory allocated above 
  free(device);

  set_p1_p2(this->Fd, FRACTIONAL, 0.0, 
	    0.0,
	    0.0, 1.0, 1.0, 1.0);

  mapping_mode(this->Fd, DISTORT);
  vlDebugMacro(<< "SB_mapping_mode: DISTORT\n");
  
  // set clipping
  clip_rectangle(this->Fd, 0.0, 1.0, 0.0, 1.0);
  clip_depth(this->Fd, 0.0, 1.0);
  clip_indicator(this->Fd, CLIP_TO_VIEWPORT);
  depth_indicator(this->Fd, TRUE, TRUE);
  
  // use the full color map, initialize it and turn shading on 
  shade_mode(this->Fd, CMAP_FULL | INIT, TRUE); 
  
  // set Fd update state - reset viewport and buffer commands
810
  this->NumPlanes = depth;
Ken Martin's avatar
Ken Martin committed
811
812
813
814
815
816
817
818
  if (this->DoubleBuffer > 0.0) 
    {
    if ((planes = double_buffer(this->Fd, 
				TRUE | INIT | SUPPRESS_CLEAR,
				depth)) != depth)
      {
      vlDebugMacro(<< "Only " << planes <<
      " planes available for double buffering\n");
819
      this->NumPlanes = planes;
Ken Martin's avatar
Ken Martin committed
820
821
822
823
824
825
826
827
828
829
830
      }
    dbuffer_switch(this->Fd,this->Buffer); 
    buffer_mode(this->Fd, TRUE);
    }


  // turn on z buffering and disable backface culling 
  hidden_surface(this->Fd, TRUE, FALSE);
  clear_control(this->Fd, CLEAR_DISPLAY_SURFACE | CLEAR_ZBUFFER);
  
  // set back faces of polygons to be rendered same as front 
Ken Martin's avatar
Ken Martin committed
831
  bf_control(this->Fd, TRUE, FALSE);
Ken Martin's avatar
Ken Martin committed
832
833
834
835
836
837
838
839
840
841
842
843
844
  // make default polymarker a dot (pixel) 
  marker_type(this->Fd, 0);

  // clear the display 
  clear_view_surface(this->Fd);

  clear_control(this->Fd, CLEAR_VIEWPORT | CLEAR_ZBUFFER);

  // ignore errors 
  gerr_print_control(NO_ERROR_PRINTING);
  this->Mapped = 1;
}

Will Schroeder's avatar
Will Schroeder committed
845
846
// Description:
// Initialize the rendering window.
Ken Martin's avatar
Ken Martin committed
847
848
void vlSbrRenderWindow::Initialize (void)
{
Will Schroeder's avatar
Will Schroeder committed
849
  // make sure we haven't already been initialized 
Ken Martin's avatar
Ken Martin committed
850
851
852
853
854
855
856
  if (this->Fd != -1) return;

  // now initialize the window 
  this->WindowInitialize();
}


Will Schroeder's avatar
Will Schroeder committed
857
858
// Description:
// Change the window to fill the entire screen.
Ken Martin's avatar
Ken Martin committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
void vlSbrRenderWindow::SetFullScreen(int arg)
{
  int *temp;

  if (this->FullScreen == arg) return;
  
  if (!this->Mapped)
    {
    this->PrefFullScreen();
    return;
    }

  // set the mode 
  this->FullScreen = arg;
  if (this->FullScreen <= 0)
    {
    this->Position[0] = this->OldScreen[0];
    this->Position[1] = this->OldScreen[1];
    this->Size[0] = this->OldScreen[2]; 
    this->Size[1] = this->OldScreen[3];
    this->Borders = this->OldScreen[4];
    }
  else
    {
    // if window already up get its values 
    if (this->WindowId)
      {
      XWindowAttributes attribs;
      
      //  Find the current window size 
      XGetWindowAttributes(this->DisplayId, 
			   this->WindowId, &attribs);
      
      this->OldScreen[2] = attribs.width;
      this->OldScreen[3] = attribs.height;;

      temp = this->GetPosition();      
      this->OldScreen[0] = temp[0];
      this->OldScreen[1] = temp[1];

      this->OldScreen[4] = this->Borders;
      this->PrefFullScreen();
      }
    }
  
  // remap the window 
  this->WindowRemap();

  // if full screen then grab the keyboard 
  if (this->FullScreen)
    {
    XGrabKeyboard(this->DisplayId,this->WindowId,
		  False,GrabModeAsync,GrabModeAsync,CurrentTime);
    }
  this->Modified();
}

Will Schroeder's avatar
Will Schroeder committed
916
917
// Description:
// Set the preferred window size to full screen.
Ken Martin's avatar
Ken Martin committed
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
void vlSbrRenderWindow::PrefFullScreen()
{
  int *size;

  size = this->GetScreenSize();

  // use full screen 
  this->Position[0] = 0;
  this->Position[1] = 0;
  this->Size[0] = size[0];
  this->Size[1] = size[1];

  // don't show borders 
  this->Borders = 0;
}


Will Schroeder's avatar
Will Schroeder committed
935
936
// Description:
// Resize the window.
Ken Martin's avatar
Ken Martin committed
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
void vlSbrRenderWindow::WindowRemap()
{
  // close the starbase window 
  if (this->Fd)
    {
    gclose(this->Fd);
    }
  this->Fd = -1;
  
  /* free the Xwindow we created no need to free the colormap */
  if (this->OwnWindow)
    {
    XDestroyWindow(this->DisplayId,this->WindowId);
    }
  XSync(this->DisplayId,0);
  this->WindowId = this->NextWindowId;
  this->NextWindowId = 0;

  // configure the window 
  this->WindowInitialize();
}


Will Schroeder's avatar
Will Schroeder committed
960
961
// Description:
// Specify the size of the rendering window.
Ken Martin's avatar
Ken Martin committed
962
963
964
965
966
967
968
969
970
971
972
973
974
void vlSbrRenderWindow::SetSize(int x,int y)
{
  // if we arent mappen then just set the ivars 
  if (!this->Mapped)
    {
    if ((this->Size[0] != x)||(this->Size[1] != y))
      {
      this->Modified();
      }
    this->Size[0] = x;
    this->Size[1] = y;
    return;
    }
Ken Martin's avatar
Ken Martin committed
975
976
977
978
979
980
981
982
  
  if ((this->Size[0] != x)||(this->Size[1] != y))
    {
    this->Modified();
    }
  this->Size[0] = x;
  this->Size[1] = y;
  
Ken Martin's avatar
Ken Martin committed
983
984
985
986
987
988
  XResizeWindow(this->DisplayId,this->WindowId,x,y);
  XSync(this->DisplayId,False);
}

void vlSbrRenderWindow::PrintSelf(ostream& os, vlIndent indent)
{
989
  this->vlXRenderWindow::PrintSelf(os,indent);
Ken Martin's avatar
Ken Martin committed
990

991
  os << indent << "Fd: " << this->Fd << "\n";
Ken Martin's avatar
Ken Martin committed
992
}
993
994
995
996
997
998
999
1000

#define RGB_TO_332( r, g, b ) ( (  r       & 0xe0 ) + \
				( (g >> 3) & 0x1c ) + \
				( (b >> 6) & 0x03 ) )
#define RGB_TO_666_FACTOR 0.0196078
#define RGB_TO_666( r, g, b ) ( 40 + \
(unsigned char)(r * RGB_TO_666_FACTOR) * 36 + \
(unsigned char)(g * RGB_TO_666_FACTOR) * 6 + \