GlrRenW.cc 17.7 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
5
6
7
8
  Module:    GlrRenW.cc
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

Ken Martin's avatar
Ken Martin committed
9
This file is part of the Visualization Toolkit. No part of this file or its
Ken Martin's avatar
Ken Martin committed
10
11
12
13
14
15
16
17
18
19
20
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>
#include <iostream.h>
#include "GlrRenW.hh"
#include "GlrRen.hh"
#include "GlrProp.hh"
Ken Martin's avatar
Ken Martin committed
21
#include "GlrText.hh"
Ken Martin's avatar
Ken Martin committed
22
23
24
#include "GlrCam.hh"
#include "GlrLgt.hh"
#include "gl/glws.h"
Ken Martin's avatar
Ken Martin committed
25
#include "gl/get.h"
Ken Martin's avatar
Ken Martin committed
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

#define MAX_LIGHTS 8

#ifndef GD_MULTISAMPLE
#define GD_MULTISAMPLE 1000
#define GC_MS_SAMPLES 1000
void mssize() {return;}
long getgconfig() {return 1;}
void zbsize() {return;}
#endif

/* Declare the data structure for the GL rendering configuration needed */
static GLXconfig the_config[] = 
{
  { GLX_NORMAL,	GLX_RGB,	True} ,
  { GLX_NORMAL,	GLX_DOUBLE,	True} ,
  { GLX_NORMAL,	GLX_ZSIZE,	GLX_NOCONFIG} ,
  { GLX_NORMAL,	GLX_MSSAMPLE,	0} ,
  { GLX_NORMAL,	GLX_MSZSIZE,	0} ,
  { 0,		0,		0}
  };

static float tevprops[]
  = {
  TV_MODULATE, TV_NULL
    };

static unsigned long extract_config_value(int buffer,int mode,
					  GLXconfig *conf)
{
    int	i;
    for (i = 0; conf[i].buffer; i++)
	if (conf[i].buffer == buffer && conf[i].mode == mode)
	    return conf[i].arg;
    return 0;
}

static void set_config_value(int buffer,int mode,
			     GLXconfig *conf,unsigned long value)
{
  int	i;
  for (i = 0; conf[i].buffer; i++)
    {
    if (conf[i].buffer == buffer && conf[i].mode == mode)
      {
      conf[i].arg = (int)value;
      return;
      }
    }
}

/* Extract X visual information */
static XVisualInfo *extract_visual(int buffer,GLXconfig *conf,
				   Display *D,int S)
{
  XVisualInfo	templ;
  int n;
  
  templ.screen = S;
  templ.visualid = extract_config_value(buffer, GLX_VISUAL, conf);
  return XGetVisualInfo (D, VisualScreenMask|VisualIDMask, &templ, &n);
}

/* Fill the configuration structure with the appropriately */
/* created window */
static void set_window(int buffer,Window W,GLXconfig *conf)
{
    int	i;

    for (i = 0; conf[i].buffer; i++)
	if (conf[i].buffer == buffer && conf[i].mode == GLX_WINDOW)
	    conf[i].arg = (int)W;
}

Ken Martin's avatar
Ken Martin committed
100
vtkGlrRenderWindow::vtkGlrRenderWindow()
Will Schroeder's avatar
Will Schroeder committed
101
102
{
  this->Gid = -2;
103
  this->MultiSamples = 8;
Will Schroeder's avatar
Will Schroeder committed
104
105
106
107
  this->DisplayId = (Display *)NULL;
  this->WindowId = (Window)NULL;
  this->NextWindowId = (Window)NULL;
  this->ColorMap = (Colormap)0;
Ken Martin's avatar
Ken Martin committed
108
  this->StereoType = VTK_STEREO_CRYSTAL_EYES;
Will Schroeder's avatar
Will Schroeder committed
109

Ken Martin's avatar
Ken Martin committed
110
  strcpy(this->Name,"Visualization Toolkit - GL");
Will Schroeder's avatar
Will Schroeder committed
111
112
113
114
}

// Description:
// Create a gl specific light.
Ken Martin's avatar
Ken Martin committed
115
vtkLightDevice *vtkGlrRenderWindow::MakeLight()
Ken Martin's avatar
Ken Martin committed
116
{
Ken Martin's avatar
Ken Martin committed
117
  vtkGlrLight *light;
Ken Martin's avatar
Ken Martin committed
118

Ken Martin's avatar
Ken Martin committed
119
120
  light = new vtkGlrLight;
  return (vtkLightDevice *)light;
Ken Martin's avatar
Ken Martin committed
121
122
}

Will Schroeder's avatar
Will Schroeder committed
123
124
// Description:
// Create a gl specific renderer.
Ken Martin's avatar
Ken Martin committed
125
vtkRenderer *vtkGlrRenderWindow::MakeRenderer()
Ken Martin's avatar
Ken Martin committed
126
{
Ken Martin's avatar
Ken Martin committed
127
  vtkGlrRenderer *ren;
Ken Martin's avatar
Ken Martin committed
128

Ken Martin's avatar
Ken Martin committed
129
  ren = new vtkGlrRenderer;
130
  this->AddRenderers(ren);
Ken Martin's avatar
Ken Martin committed
131
132

  // by default we are its parent
Ken Martin's avatar
Ken Martin committed
133
  ren->SetRenderWindow((vtkRenderWindow*)this);
Ken Martin's avatar
Ken Martin committed
134
  
Ken Martin's avatar
Ken Martin committed
135
  return (vtkRenderer *)ren;
Ken Martin's avatar
Ken Martin committed
136
137
}

Will Schroeder's avatar
Will Schroeder committed
138
139
// Description:
// Create a gl specific camera.
Ken Martin's avatar
Ken Martin committed
140
vtkCameraDevice *vtkGlrRenderWindow::MakeCamera()
Ken Martin's avatar
Ken Martin committed
141
{
Ken Martin's avatar
Ken Martin committed
142
  vtkGlrCamera *camera;
Ken Martin's avatar
Ken Martin committed
143

Ken Martin's avatar
Ken Martin committed
144
145
  camera = new vtkGlrCamera;
  return (vtkCameraDevice *)camera;
Ken Martin's avatar
Ken Martin committed
146
147
}

148
149
// Description:
// Create a gl specific property.
Ken Martin's avatar
Ken Martin committed
150
vtkPropertyDevice *vtkGlrRenderWindow::MakeProperty()
151
{
Ken Martin's avatar
Ken Martin committed
152
  vtkGlrProperty *property;
153

Ken Martin's avatar
Ken Martin committed
154
155
  property = new vtkGlrProperty;
  return (vtkPropertyDevice *)property;
156
157
}

Ken Martin's avatar
Ken Martin committed
158
159
// Description:
// Create a gl specific texture.
Ken Martin's avatar
Ken Martin committed
160
vtkTextureDevice *vtkGlrRenderWindow::MakeTexture()
Ken Martin's avatar
Ken Martin committed
161
{
Ken Martin's avatar
Ken Martin committed
162
  vtkGlrTexture *texture;
Ken Martin's avatar
Ken Martin committed
163

Ken Martin's avatar
Ken Martin committed
164
165
  texture = new vtkGlrTexture;
  return (vtkTextureDevice *)texture;
Ken Martin's avatar
Ken Martin committed
166
167
}

Will Schroeder's avatar
Will Schroeder committed
168
169
// Description:
// Begin the rendering process.
Ken Martin's avatar
Ken Martin committed
170
void vtkGlrRenderWindow::Start(void)
Ken Martin's avatar
Ken Martin committed
171
172
173
174
175
{
  // if the renderer has not been initialized, do so now
  if (this->Gid < 0)
    this->Initialize();

Ken Martin's avatar
Ken Martin committed
176
177
  // set the current window 
  GLXwinset(this->DisplayId,this->WindowId);
Ken Martin's avatar
Ken Martin committed
178
179
}

Will Schroeder's avatar
Will Schroeder committed
180
181
// Description:
// End the rendering process and display the image.
Ken Martin's avatar
Ken Martin committed
182
void vtkGlrRenderWindow::Frame(void)
Ken Martin's avatar
Ken Martin committed
183
184
185
186
{
  if (this->DoubleBuffer)
    {
    swapbuffers();
Ken Martin's avatar
Ken Martin committed
187
    vtkDebugMacro(<< " GL swapbuffers\n");
Ken Martin's avatar
Ken Martin committed
188
189
190
    }
}
 
Ken Martin's avatar
Ken Martin committed
191
192
193

// Description:
// Update system if needed due to stereo rendering.
Ken Martin's avatar
Ken Martin committed
194
void vtkGlrRenderWindow::StereoUpdate(void)
Ken Martin's avatar
Ken Martin committed
195
196
197
198
199
200
{
  // if stereo is on and it wasn't before
  if (this->StereoRender && (!this->StereoStatus))
    {
    switch (this->StereoType) 
      {
Ken Martin's avatar
Ken Martin committed
201
      case VTK_STEREO_CRYSTAL_EYES:
Ken Martin's avatar
Ken Martin committed
202
203
204
205
206
207
208
209
210
	{
	this->OldMonitorSetting = getmonitor();
	gflush();
	setmonitor(STR_RECT);
	gflush();
	// make sure we are in full screen
        this->StereoStatus = 1;
	this->FullScreenOn();
	}
211
	break;
Ken Martin's avatar
Ken Martin committed
212
      case VTK_STEREO_RED_BLUE:
213
214
215
	{
        this->StereoStatus = 1;
	}
Ken Martin's avatar
Ken Martin committed
216
217
218
219
220
221
      }
    }
  else if ((!this->StereoRender) && this->StereoStatus)
    {
    switch (this->StereoType) 
      {
Ken Martin's avatar
Ken Martin committed
222
      case VTK_STEREO_CRYSTAL_EYES:
Ken Martin's avatar
Ken Martin committed
223
224
225
226
227
228
229
230
231
	{
	/* restore the monitor */
	gflush();
	setmonitor(this->OldMonitorSetting);
	gflush();
	// make sure we are in full screen
        this->StereoStatus = 0;
	this->FullScreenOff();
	}
232
	break;
Ken Martin's avatar
Ken Martin committed
233
      case VTK_STEREO_RED_BLUE:
234
235
236
	{
        this->StereoStatus = 0;
	}
Ken Martin's avatar
Ken Martin committed
237
238
239
240
      }
    }
}

Will Schroeder's avatar
Will Schroeder committed
241
242
// Description:
// Specify various window parameters.
Ken Martin's avatar
Ken Martin committed
243
void vtkGlrRenderWindow::WindowConfigure()
Ken Martin's avatar
Ken Martin committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
{
  if (this->DoubleBuffer)
    {
    set_config_value(GLX_NORMAL,GLX_DOUBLE,the_config,True);
    }
  else
    {
    set_config_value(GLX_NORMAL,GLX_DOUBLE,the_config,False);
    }
  
  if (this->MultiSamples > 1.0)
    {
    set_config_value(GLX_NORMAL,GLX_ZSIZE,the_config,0);
    set_config_value(GLX_NORMAL,GLX_MSSAMPLE,the_config,
		     (unsigned long)this->MultiSamples);
    set_config_value(GLX_NORMAL,GLX_MSZSIZE,the_config,32);
    if (extract_config_value(GLX_NORMAL,GLX_MSSAMPLE,the_config)
	< this->MultiSamples) 
      {
Ken Martin's avatar
Ken Martin committed
263
      vtkDebugMacro(<< " Only got " << 
Ken Martin's avatar
Ken Martin committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
      extract_config_value(GLX_NORMAL,GLX_MSSAMPLE,the_config) 
      << " multisamples\n");
      this->MultiSamples =
	(int)extract_config_value(GLX_NORMAL,GLX_MSSAMPLE,the_config);
      }
    }
  if (this->MultiSamples <= 1.0)
    {
    set_config_value(GLX_NORMAL,GLX_ZSIZE,the_config,GLX_NOCONFIG);
    set_config_value(GLX_NORMAL,GLX_MSSAMPLE,the_config,0);
    set_config_value(GLX_NORMAL,GLX_MSZSIZE,the_config,0);
    }
}


Will Schroeder's avatar
Will Schroeder committed
279
280
// Description:
// Initialize the window for rendering.
Ken Martin's avatar
Ken Martin committed
281
void vtkGlrRenderWindow::WindowInitialize (void)
Ken Martin's avatar
Ken Martin committed
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
{
  GLXconfig  *conf;
  XVisualInfo  *v;
  XSetWindowAttributes	attr;
  Window  wins[2];
  int x,y,width,height;
  XWindowAttributes winattr;
  XSizeHints xsh;

  xsh.flags = USSize;
  if ((this->Position[0] >= 0)&&(this->Position[1] >= 0))
    {
    xsh.flags |= USPosition;
    xsh.x =  (int)(this->Position[0]);
    xsh.y =  (int)(this->Position[1]);
    }
  
  x = ((this->Position[0] >= 0) ? this->Position[0] : 5);
  y = ((this->Position[1] >= 0) ? this->Position[1] : 5);
  width = ((this->Size[0] > 0) ? this->Size[0] : 300);
  height = ((this->Size[1] > 0) ? this->Size[1] : 300);

  xsh.width  = width;
  xsh.height = height;

  // get the default display connection 
  if (!this->DisplayId)
    {
    this->DisplayId = XOpenDisplay((char *)NULL); 
    if (this->DisplayId == NULL) 
      {
Ken Martin's avatar
Ken Martin committed
313
      vtkErrorMacro(<< "bad X server connection.\n");
Ken Martin's avatar
Ken Martin committed
314
315
316
317
318
319
320
321
      }
    }

  // test for overlay planes 
  if ((conf = GLXgetconfig(this->DisplayId, 
			   DefaultScreen(this->DisplayId),
			   the_config)) == 0) 
    {
Ken Martin's avatar
Ken Martin committed
322
    vtkErrorMacro(<< "GL: getconfig failed\n");
Ken Martin's avatar
Ken Martin committed
323
324
325
    exit(1);
    }

Ken Martin's avatar
Ken Martin committed
326
327
328
329
330

  attr.override_redirect = False;
  if (this->Borders == 0.0)
    attr.override_redirect = True;
  
Ken Martin's avatar
Ken Martin committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  /* create our own window ? */
  this->OwnWindow = 0;
  if (!this->WindowId)
    {
    v = extract_visual(GLX_NORMAL,conf,this->DisplayId,
		       DefaultScreen(this->DisplayId));
    
    attr.colormap = extract_config_value(GLX_NORMAL, GLX_COLORMAP, conf);
    this->ColorMap = attr.colormap;

    attr.border_pixel = 0;
    this->WindowId = 
      XCreateWindow(this->DisplayId,
		    RootWindow(this->DisplayId,
			       DefaultScreen(this->DisplayId)), 
		    x, y, width, height, 0, v->depth, InputOutput, v->visual,
		    CWBorderPixel|CWColormap|CWOverrideRedirect, &attr);
    XStoreName(this->DisplayId, this->WindowId, this->Name);
    XSetNormalHints(this->DisplayId,this->WindowId,&xsh);
    this->OwnWindow = 1;
    }
Ken Martin's avatar
Ken Martin committed
352
353
354
355
356
357
358
  else
    {
    XChangeWindowAttributes(this->DisplayId,this->WindowId,
			    CWOverrideRedirect, &attr);
    }

  // RESIZE THE WINDOW TO THE DESIRED SIZE
Ken Martin's avatar
Ken Martin committed
359
  vtkDebugMacro(<< "Resizing the xwindow\n");
Ken Martin's avatar
Ken Martin committed
360
361
362
363
364
365
  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);
Ken Martin's avatar
Ken Martin committed
366
367
368
369
370
371

  set_window(GLX_NORMAL, this->WindowId, conf);
  
  // Bind the GL to the created windows 
  if (GLXlink(this->DisplayId, conf) < 0) 
    {
Ken Martin's avatar
Ken Martin committed
372
    vtkErrorMacro("GL: Bind failed\n");
Ken Martin's avatar
Ken Martin committed
373
374
375
    exit(1);
    }

Ken Martin's avatar
Ken Martin committed
376
  vtkDebugMacro(" Mapping the xwindow\n");
Ken Martin's avatar
Ken Martin committed
377
378
379
380
381
382
383
384
385
386
387
388
  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);
    };
  
  if (GLXwinset(this->DisplayId,this->WindowId) < 0)
    {
Ken Martin's avatar
Ken Martin committed
389
    vtkErrorMacro(<< "GL: winset failed\n");
Ken Martin's avatar
Ken Martin committed
390
391
392
    exit(1);
    }

Ken Martin's avatar
Ken Martin committed
393
  vtkDebugMacro(" mmode(MVIEWING)\n");
Ken Martin's avatar
Ken Martin committed
394
395
  mmode(MVIEWING);

Ken Martin's avatar
Ken Martin committed
396
  vtkDebugMacro(" zbuff stuff\n");
Ken Martin's avatar
Ken Martin committed
397
  zbuffer(TRUE);
Ken Martin's avatar
Ken Martin committed
398

Ken Martin's avatar
Ken Martin committed
399
  vtkDebugMacro(" subpixel stuff\n");
Ken Martin's avatar
Ken Martin committed
400
  subpixel(TRUE);
Ken Martin's avatar
Ken Martin committed
401
 
Ken Martin's avatar
Ken Martin committed
402
  vtkDebugMacro(" texture stuff\n");
Ken Martin's avatar
Ken Martin committed
403
404
405
406
407
408
  if (getgdesc(GD_TEXTURE))
    {
    tevdef(1,0,tevprops);
    tevbind(TV_ENV0,1);
    }

Ken Martin's avatar
Ken Martin committed
409
  vtkDebugMacro("% alpha stuff\n");
Ken Martin's avatar
Ken Martin committed
410
411
412
413
414
415
416
417
  if (getgdesc(GD_AFUNCTION))
    {
    afunction(0,AF_NOTEQUAL);
    }

  /*
   * initialize blending for transparency
   */
Ken Martin's avatar
Ken Martin committed
418
  vtkDebugMacro(" blend func stuff\n");
Ken Martin's avatar
Ken Martin committed
419
420
421
422
423
  blendfunction(BF_SA, BF_MSA);

  this->Mapped = 1;
}

Will Schroeder's avatar
Will Schroeder committed
424
425
// Description:
// Initialize the rendering window.
Ken Martin's avatar
Ken Martin committed
426
void vtkGlrRenderWindow::Initialize (void)
Ken Martin's avatar
Ken Martin committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
{
  // make sure we havent already been initialized 
  if (this->Gid >= 0)
    return;

  this->Connect();

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

  this->Gid = 1;
}

Will Schroeder's avatar
Will Schroeder committed
441
442
// Description:
// Make the connection to the window manager.
Ken Martin's avatar
Ken Martin committed
443
void vtkGlrRenderWindow::Connect()
Ken Martin's avatar
Ken Martin committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
{
  int status = -1;

  // make sure we haven't already opened 
  if (this->Gid > -2)
    {
    return;
    }

  /* get the default display connection */
  if (!this->DisplayId)
    {
    this->DisplayId = XOpenDisplay((char *)NULL); 
    if (this->DisplayId == NULL) 
      {
Ken Martin's avatar
Ken Martin committed
459
      vtkErrorMacro(<< "bad X server connection.\n");
Ken Martin's avatar
Ken Martin committed
460
461
462
463
464
465
466
467
468
      }
    }
  else
    {
    if ((status = (int)dglopen (DisplayString(this->DisplayId), DGLLOCAL)) < 0)
      {
      /* try local host */
      if ((status = (int)dglopen ("localhost:0.0", DGLLOCAL)) < 0)
	{
Ken Martin's avatar
Ken Martin committed
469
	vtkErrorMacro(<< " error from glopen : " << status << endl);
Ken Martin's avatar
Ken Martin committed
470
471
472
473
474
	exit(-1);
	}
      else
	{
	/* this is recoverable */
Ken Martin's avatar
Ken Martin committed
475
	vtkErrorMacro(<< " error2 from glopen : " << status << endl);
Ken Martin's avatar
Ken Martin committed
476
477
478
479
480
481
482
483
	exit(-1);
	}
      }
    }

  this->Gid = -1;
}

Will Schroeder's avatar
Will Schroeder committed
484
485
// Description:
// Change the window to fill the entire screen.
Ken Martin's avatar
Ken Martin committed
486
void vtkGlrRenderWindow::SetFullScreen(int arg)
Ken Martin's avatar
Ken Martin committed
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
{
  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
543
544
// Description:
// Set the preferred window size to full screen.
Ken Martin's avatar
Ken Martin committed
545
void vtkGlrRenderWindow::PrefFullScreen()
Ken Martin's avatar
Ken Martin committed
546
547
548
549
550
551
552
553
554
555
556
557
558
{
  this->Connect();

  // use full screen 
  this->Position[0] = 0;
  this->Position[1] = 0;
  this->Size[0] = (int)getgdesc(GD_XPMAX);
  this->Size[1] = (int)getgdesc(GD_YPMAX);

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

Will Schroeder's avatar
Will Schroeder committed
559
560
// Description:
// Resize the window.
Ken Martin's avatar
Ken Martin committed
561
void vtkGlrRenderWindow::WindowRemap()
Ken Martin's avatar
Ken Martin committed
562
563
564
565
566
567
568
569
570
571
{
  short cur_light;

  /* first delete all the old lights */
  for (cur_light = LIGHT0; cur_light < LIGHT0+MAX_LIGHTS; cur_light++)
    {
    lmbind(cur_light,0);
    }

  // then close the old window 
Ken Martin's avatar
Ken Martin committed
572
573
574
575
  if (this->OwnWindow)
    {
    XDestroyWindow(this->DisplayId,this->WindowId);
    }
Ken Martin's avatar
Ken Martin committed
576
577
578
  GLXunlink(this->DisplayId,this->WindowId);
  // set the default windowid 
  this->WindowId = this->NextWindowId;
579
  this->NextWindowId = (Window)NULL;
Ken Martin's avatar
Ken Martin committed
580
581
582
583
584
585

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

Will Schroeder's avatar
Will Schroeder committed
586
587
// Description:
// Specify the size of the rendering window.
Ken Martin's avatar
Ken Martin committed
588
void vtkGlrRenderWindow::SetSize(int x,int y)
Ken Martin's avatar
Ken Martin committed
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
{
  // 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;
    }

  XResizeWindow(this->DisplayId,this->WindowId,x,y);
  XSync(this->DisplayId,False);
}


Ken Martin's avatar
Ken Martin committed
607

Ken Martin's avatar
Ken Martin committed
608
int vtkGlrRenderWindow::GetDesiredDepth()
Ken Martin's avatar
Ken Martin committed
609
{
Ken Martin's avatar
Ken Martin committed
610
611
  GLXconfig *conf;
  XVisualInfo *v;
Ken Martin's avatar
Ken Martin committed
612

Ken Martin's avatar
Ken Martin committed
613
  this->Connect();
Ken Martin's avatar
Ken Martin committed
614

Ken Martin's avatar
Ken Martin committed
615
616
617
618
  if ((conf = GLXgetconfig(this->DisplayId, 
			   DefaultScreen(this->DisplayId),
			   the_config)) == 0) 
    {
Ken Martin's avatar
Ken Martin committed
619
    vtkErrorMacro(<< "GL: getconfig failed\n");
Ken Martin's avatar
Ken Martin committed
620
621
622
623
624
625
    exit(1);
    }

  /* get the default visual to use */
  v = extract_visual(GLX_NORMAL,conf,this->DisplayId,
		     DefaultScreen(this->DisplayId));
Ken Martin's avatar
Ken Martin committed
626

Ken Martin's avatar
Ken Martin committed
627
  return v->depth;  
Ken Martin's avatar
Ken Martin committed
628
629
}

Will Schroeder's avatar
Will Schroeder committed
630
// Description:
Ken Martin's avatar
Ken Martin committed
631
// Get a visual from the windowing system.
Ken Martin's avatar
Ken Martin committed
632
Visual *vtkGlrRenderWindow::GetDesiredVisual ()
Ken Martin's avatar
Ken Martin committed
633
{
Ken Martin's avatar
Ken Martin committed
634
635
636
637
638
639
640
641
642
  XVisualInfo  *v;
  GLXconfig *conf;

  this->Connect();

  if ((conf = GLXgetconfig(this->DisplayId, 
			   DefaultScreen(this->DisplayId),
			   the_config)) == 0) 
    {
Ken Martin's avatar
Ken Martin committed
643
    vtkErrorMacro(<< "GL: getconfig failed\n");
Ken Martin's avatar
Ken Martin committed
644
645
646
647
648
649
    exit(1);
    }

  /* get the default visual to use */
  v = extract_visual(GLX_NORMAL,conf,this->DisplayId,
		     DefaultScreen(this->DisplayId));
Ken Martin's avatar
Ken Martin committed
650

Ken Martin's avatar
Ken Martin committed
651
  return v->visual;  
Ken Martin's avatar
Ken Martin committed
652
653
}

Ken Martin's avatar
Ken Martin committed
654

Will Schroeder's avatar
Will Schroeder committed
655
// Description:
Ken Martin's avatar
Ken Martin committed
656
// Get a colormap from the windowing system.
Ken Martin's avatar
Ken Martin committed
657
Colormap vtkGlrRenderWindow::GetDesiredColormap ()
Ken Martin's avatar
Ken Martin committed
658
{
Ken Martin's avatar
Ken Martin committed
659
660
661
662
663
664
665
666
667
668
669
  GLXconfig *conf;
  XVisualInfo *v;

  if (this->ColorMap) return this->ColorMap;

  this->Connect();

  if ((conf = GLXgetconfig(this->DisplayId, 
			   DefaultScreen(this->DisplayId),
			   the_config)) == 0) 
    {
Ken Martin's avatar
Ken Martin committed
670
    vtkErrorMacro(<< "GL: getconfig failed\n");
Ken Martin's avatar
Ken Martin committed
671
672
673
674
675
    exit(1);
    }

  /* get the default colormap to use */
  this->ColorMap = extract_config_value(GLX_NORMAL, GLX_COLORMAP, conf);
Ken Martin's avatar
Ken Martin committed
676

Ken Martin's avatar
Ken Martin committed
677
  return this->ColorMap;  
Ken Martin's avatar
Ken Martin committed
678
679
}

Ken Martin's avatar
Ken Martin committed
680
void vtkGlrRenderWindow::PrintSelf(ostream& os, vtkIndent indent)
Ken Martin's avatar
Ken Martin committed
681
{
Ken Martin's avatar
Ken Martin committed
682
  this->vtkXRenderWindow::PrintSelf(os,indent);
Ken Martin's avatar
Ken Martin committed
683

684
685
  os << indent << "Gid: " << this->Gid << "\n";
  os << indent << "MultiSamples: " << this->MultiSamples << "\n";
Ken Martin's avatar
Ken Martin committed
686
}
687
688


689
690
unsigned char *vtkGlrRenderWindow::GetPixelData(int x1, int y1, int x2, int y2,
						int front)
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
{
  long     xloop,yloop;
  int     y_low, y_hi;
  int     x_low, x_hi;
  unsigned long   *buffer;
  unsigned char   *data = NULL;
  unsigned char   *p_data = NULL;

  /* set the current window */
  GLXwinset(this->DisplayId,this->WindowId);

  buffer = new unsigned long[abs(x2 - x1)+1];
  data = new unsigned char[(abs(x2 - x1) + 1)*(abs(y2 - y1) + 1)*3];

  if (y1 < y2)
    {
    y_low = y1; 
    y_hi  = y2;
    }
  else
    {
    y_low = y2; 
    y_hi  = y1;
    }

  if (x1 < x2)
    {
    x_low = x1; 
    x_hi  = x2;
    }
  else
    {
    x_low = x2; 
    x_hi  = x1;
    }

727
728
729
730
731
732
733
734
  if (front)
    {
    readsource(SRC_FRONT);
    }
  else
    { 
    readsource(SRC_BACK);
    }
735
  p_data = data;
736
  for (yloop = y_hi; yloop >= y_low; yloop--)
737
738
739
740
741
742
743
744
745
746
747
    {
    // read in a row of pixels 
    lrectread(x_low,yloop,x_hi,yloop,buffer);
    for (xloop = 0; xloop <= (abs(x2-x1)); xloop++)
      {
      *p_data = buffer[xloop] & (0x000000ff); p_data++;
      *p_data = (buffer[xloop] & (0x0000ff00)) >> 8; p_data++;
      *p_data = (buffer[xloop] & (0x00ff0000)) >> 16; p_data++;
      }
    }
  
Ken Martin's avatar
Ken Martin committed
748
  delete [] buffer;
749
750
751
752

  return data;
}

Ken Martin's avatar
Ken Martin committed
753
void vtkGlrRenderWindow::SetPixelData(int x1, int y1, int x2, int y2,
754
755
756
757
758
759
760
761
762
763
764
765
766
				     unsigned char *data)
{
  int     y_low, y_hi;
  int     x_low, x_hi;
  int     xloop,yloop;
  unsigned long   *buffer;
  unsigned char   *p_data = NULL;

  // set the current window 
  GLXwinset(this->DisplayId,this->WindowId);

  if (this->DoubleBuffer)
    {
767
768
769
770
771
772
773
774
    if (front)
      {
      frontbuffer(TRUE);
      }
    else
      {
      backbuffer(FALSE);
      }
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
    }
  dither(DT_OFF);

  buffer = new unsigned long[4*(abs(x2 - x1)+1)];

  if (y1 < y2)
    {
    y_low = y1; 
    y_hi  = y2;
    }
  else
    {
    y_low = y2; 
    y_hi  = y1;
    }
  
  if (x1 < x2)
    {
    x_low = x1; 
    x_hi  = x2;
    }
  else
    {
    x_low = x2; 
    x_hi  = x1;
    }
  
  viewport(x_low,x_hi,y_low,y_hi);

  /* now write the binary info one row at a time */
  p_data = data;
806
  for (yloop = y_hi; yloop >= y_low; yloop--)
807
808
809
810
811
812
813
814
815
816
817
    {
    for (xloop = 0; xloop <= (abs(x2-x1)); xloop++)
      {
      buffer[xloop] = 0xff000000 + *p_data; p_data++; 
      buffer[xloop] += (*p_data) << 8; p_data++;
      buffer[xloop] += (*p_data) << 16; p_data++;
      }
    /* write out a row of pixels */
    lrectwrite(x_low,yloop,x_hi,yloop,buffer);
    }
  
Ken Martin's avatar
Ken Martin committed
818
  delete [] buffer;
819
820
821

  dither(DT_ON);
}