vtkOpenGLRenderWindow.cxx 57.3 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
3
/*=========================================================================

  Program:   Visualization Toolkit
4
  Module:    vtkOpenGLRenderWindow.cxx
Ken Martin's avatar
Ken Martin committed
5
6
7
8
9
10
11
12
13
14

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
Ken Martin's avatar
Ken Martin committed
15
#include "vtk_glew.h"
16
#include "vtkOpenGLRenderWindow.h"
17

Ken Martin's avatar
Ken Martin committed
18
#include "vtkOpenGLHelper.h"
19

Ken Martin's avatar
Ken Martin committed
20
#include <cassert>
Ken Martin's avatar
Ken Martin committed
21

Ken Martin's avatar
Ken Martin committed
22
#include "vtkFloatArray.h"
23
#include "vtkNew.h"
Ken Martin's avatar
Ken Martin committed
24
#include "vtkObjectFactory.h"
25
#include "vtkOpenGLActor.h"
Ken Martin's avatar
Ken Martin committed
26
#include "vtkOpenGLBufferObject.h"
27
#include "vtkOpenGLCamera.h"
Ken Martin's avatar
Ken Martin committed
28
#include "vtkOpenGLError.h"
29
30
31
32
#include "vtkOpenGLLight.h"
#include "vtkOpenGLProperty.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLShaderCache.h"
Ken Martin's avatar
Ken Martin committed
33
34
#include "vtkOpenGLVertexArrayObject.h"
#include "vtkRendererCollection.h"
35
#include "vtkShaderProgram.h"
Ken Martin's avatar
Ken Martin committed
36
#include "vtkStdString.h"
37
#include "vtkTextureObject.h"
38
#include "vtkTextureUnitManager.h"
Ken Martin's avatar
Ken Martin committed
39
#include "vtkUnsignedCharArray.h"
40

41
42
#include "vtkTextureObjectVS.h"  // a pass through shader

Ken Martin's avatar
Ken Martin committed
43
44
45
#include <sstream>
using std::ostringstream;

46
vtkCxxSetObjectMacro(vtkOpenGLRenderWindow, TextureUnitManager, vtkTextureUnitManager);
Ken Martin's avatar
Ken Martin committed
47
48
49
50

// Initialize static member that controls global maximum number of multisamples
// (off by default on Apple because it causes problems on some Mac models).
#if defined(__APPLE__)
51
static int vtkOpenGLRenderWindowGlobalMaximumNumberOfMultiSamples = 0;
Ken Martin's avatar
Ken Martin committed
52
#else
53
static int vtkOpenGLRenderWindowGlobalMaximumNumberOfMultiSamples = 8;
Ken Martin's avatar
Ken Martin committed
54
55
#endif

56
57
const char* defaultWindowName = "Visualization Toolkit - OpenGL";

Ken Martin's avatar
Ken Martin committed
58
// ----------------------------------------------------------------------------
59
void vtkOpenGLRenderWindow::SetGlobalMaximumNumberOfMultiSamples(int val)
Ken Martin's avatar
Ken Martin committed
60
{
Ken Martin's avatar
Ken Martin committed
61
62
63
64
  if (val == vtkOpenGLRenderWindowGlobalMaximumNumberOfMultiSamples)
    {
    return;
    }
65
  vtkOpenGLRenderWindowGlobalMaximumNumberOfMultiSamples = val;
Ken Martin's avatar
Ken Martin committed
66
67
68
}

// ----------------------------------------------------------------------------
69
int vtkOpenGLRenderWindow::GetGlobalMaximumNumberOfMultiSamples()
Ken Martin's avatar
Ken Martin committed
70
{
71
  return vtkOpenGLRenderWindowGlobalMaximumNumberOfMultiSamples;
Ken Martin's avatar
Ken Martin committed
72
73
}

Ken Martin's avatar
Ken Martin committed
74
75
76
77
78
79
80
81
82
83
84
// used for OpenGL32 Support
static bool vtkOpenGLRenderWindowContextSupportsOpenGL32 = false;

bool vtkOpenGLRenderWindow::GetContextSupportsOpenGL32()
{
  return vtkOpenGLRenderWindowContextSupportsOpenGL32;
}

// ----------------------------------------------------------------------------
void vtkOpenGLRenderWindow::SetContextSupportsOpenGL32(bool val)
{
Ken Martin's avatar
Ken Martin committed
85
86
87
88
  if (val == vtkOpenGLRenderWindowContextSupportsOpenGL32)
    {
    return;
    }
Ken Martin's avatar
Ken Martin committed
89
90
91
  vtkOpenGLRenderWindowContextSupportsOpenGL32 = val;
}

92
//----------------------------------------------------------------------------
93
const char *vtkOpenGLRenderWindow::GetRenderingBackend()
94
{
95
  return "OpenGL2";
96
}
Ken Martin's avatar
Ken Martin committed
97

Ken Martin's avatar
Ken Martin committed
98
// ----------------------------------------------------------------------------
99
vtkOpenGLRenderWindow::vtkOpenGLRenderWindow()
Ken Martin's avatar
Ken Martin committed
100
{
101
102
  this->Initialized = false;

103
  this->ShaderCache = vtkOpenGLShaderCache::New();
104

105
  this->TextureUnitManager = 0;
Ken Martin's avatar
Ken Martin committed
106

107
  this->MultiSamples = vtkOpenGLRenderWindowGlobalMaximumNumberOfMultiSamples;
Ken Martin's avatar
Ken Martin committed
108
  delete [] this->WindowName;
109
110
  this->WindowName = new char[strlen(defaultWindowName) + 1];
  strcpy(this->WindowName, defaultWindowName);
Ken Martin's avatar
Ken Martin committed
111

112
  this->OffScreenUseFrameBuffer = 0;
113
114
115
116
  this->FrameBufferObject = 0;
  this->HardwareBufferSize[0] = 0;
  this->HardwareBufferSize[1] = 0;
  this->HardwareOffScreenBuffersBind = false;
Ken Martin's avatar
Ken Martin committed
117

118
119
120
121
122
123
  this->BackLeftBuffer = static_cast<unsigned int>(GL_BACK_LEFT);
  this->BackRightBuffer = static_cast<unsigned int>(GL_BACK_RIGHT);
  this->FrontLeftBuffer = static_cast<unsigned int>(GL_FRONT_LEFT);
  this->FrontRightBuffer = static_cast<unsigned int>(GL_FRONT_RIGHT);
  this->BackBuffer = static_cast<unsigned int>(GL_BACK);
  this->FrontBuffer = static_cast<unsigned int>(GL_FRONT);
Ken Martin's avatar
Ken Martin committed
124
125

  #ifndef VTK_LEGACY_REMOVE
126
  this->LastGraphicError = static_cast<unsigned int>(GL_NO_ERROR);
Ken Martin's avatar
Ken Martin committed
127
128
  #endif

129
  this->DrawPixelsTextureObject = NULL;
130

131
  this->OwnContext = 1;
132
  this->MaximumHardwareLineWidth = 1.0;
133
134
135
136

  this->OpenGLSupportTested = false;
  this->OpenGLSupportResult = 0;
  this->OpenGLSupportMessage = "Not tested yet";
137
138
139

  this->NumberOfFrameBuffers = 0;
  this->DepthRenderBufferObject = 0;
Ken Martin's avatar
Ken Martin committed
140
141
142
143
}

// free up memory & close the window
// ----------------------------------------------------------------------------
144
vtkOpenGLRenderWindow::~vtkOpenGLRenderWindow()
Ken Martin's avatar
Ken Martin committed
145
{
146
  if(this->DrawPixelsTextureObject != 0)
147
    {
148
149
    this->DrawPixelsTextureObject->UnRegister(this);
    this->DrawPixelsTextureObject = NULL;
150
    }
Ken Martin's avatar
Ken Martin committed
151
  this->TextureResourceIds.clear();
Ken Martin's avatar
Ken Martin committed
152
153
154
155
156
157
  if(this->TextureUnitManager!=0)
    {
    this->TextureUnitManager->SetContext(0);
    }

  this->SetTextureUnitManager(0);
158
159
160

  this->GLStateIntegers.clear();

161
  this->ShaderCache->UnRegister(this);
Ken Martin's avatar
Ken Martin committed
162
163
}

164
// ----------------------------------------------------------------------------
165
void vtkOpenGLRenderWindow::ReleaseGraphicsResources(vtkRenderWindow *renWin)
166
167
168
169
170
171
{
  vtkCollectionSimpleIterator rsit;
  this->Renderers->InitTraversal(rsit);
  vtkRenderer *aren;
  while ( (aren = this->Renderers->GetNextRenderer(rsit)) )
    {
172
173
    if (aren->GetRenderWindow() == this)
      {
174
      aren->ReleaseGraphicsResources(renWin);
175
      }
176
    }
177

178
179
  if(this->DrawPixelsTextureObject != 0)
     {
180
     this->DrawPixelsTextureObject->ReleaseGraphicsResources(renWin);
181
182
     }

183
  this->ShaderCache->ReleaseGraphicsResources(renWin);
184
185
186
187
188
189
190
191
192
193
194

  if (this->TextureResourceIds.size())
    {
    vtkErrorMacro("There are still active textures when there should not be.");
    typedef std::map<const vtkTextureObject *, int>::const_iterator TRIter;
    TRIter found = this->TextureResourceIds.begin();
    for ( ; found != this->TextureResourceIds.end(); found++)
      {
      vtkErrorMacro("Leaked for texture object: " << const_cast<vtkTextureObject *>(found->first));
      }
    }
Ken Martin's avatar
Ken Martin committed
195
  this->Initialized = false;
196
197
}

Ken Martin's avatar
Ken Martin committed
198
// ----------------------------------------------------------------------------
199
unsigned long vtkOpenGLRenderWindow::GetContextCreationTime()
Ken Martin's avatar
Ken Martin committed
200
201
202
203
204
205
206
207
{
  return this->ContextCreationTime.GetMTime();
}

// ----------------------------------------------------------------------------
// Description:
// Return the OpenGL name of the back left buffer.
// It is GL_BACK_LEFT if GL is bound to the window-system-provided
208
// framebuffer. It is GL_COLOR_ATTACHMENT0_EXT if GL is bound to an
Ken Martin's avatar
Ken Martin committed
209
// application-created framebuffer object (GPU-based offscreen rendering)
210
211
// It is used by vtkOpenGLCamera.
unsigned int vtkOpenGLRenderWindow::GetBackLeftBuffer()
Ken Martin's avatar
Ken Martin committed
212
213
214
215
216
217
218
219
{
  return this->BackLeftBuffer;
}

// ----------------------------------------------------------------------------
// Description:
// Return the OpenGL name of the back right buffer.
// It is GL_BACK_RIGHT if GL is bound to the window-system-provided
220
// framebuffer. It is GL_COLOR_ATTACHMENT0_EXT+1 if GL is bound to an
Ken Martin's avatar
Ken Martin committed
221
// application-created framebuffer object (GPU-based offscreen rendering)
222
223
// It is used by vtkOpenGLCamera.
unsigned int vtkOpenGLRenderWindow::GetBackRightBuffer()
Ken Martin's avatar
Ken Martin committed
224
225
226
227
228
229
230
231
{
  return this->BackRightBuffer;
}

// ----------------------------------------------------------------------------
// Description:
// Return the OpenGL name of the front left buffer.
// It is GL_FRONT_LEFT if GL is bound to the window-system-provided
232
// framebuffer. It is GL_COLOR_ATTACHMENT0_EXT if GL is bound to an
Ken Martin's avatar
Ken Martin committed
233
// application-created framebuffer object (GPU-based offscreen rendering)
234
235
// It is used by vtkOpenGLCamera.
unsigned int vtkOpenGLRenderWindow::GetFrontLeftBuffer()
Ken Martin's avatar
Ken Martin committed
236
237
238
239
240
241
242
243
{
  return this->FrontLeftBuffer;
}

// ----------------------------------------------------------------------------
// Description:
// Return the OpenGL name of the front right buffer.
// It is GL_FRONT_RIGHT if GL is bound to the window-system-provided
244
// framebuffer. It is GL_COLOR_ATTACHMENT0_EXT+1 if GL is bound to an
Ken Martin's avatar
Ken Martin committed
245
// application-created framebuffer object (GPU-based offscreen rendering)
246
247
// It is used by vtkOpenGLCamera.
unsigned int vtkOpenGLRenderWindow::GetFrontRightBuffer()
Ken Martin's avatar
Ken Martin committed
248
249
250
251
252
253
254
255
{
  return this->FrontRightBuffer;
}

// ----------------------------------------------------------------------------
// Description:
// Return the OpenGL name of the back left buffer.
// It is GL_BACK if GL is bound to the window-system-provided
256
// framebuffer. It is GL_COLOR_ATTACHMENT0_EXT if GL is bound to an
Ken Martin's avatar
Ken Martin committed
257
// application-created framebuffer object (GPU-based offscreen rendering)
258
259
// It is used by vtkOpenGLCamera.
unsigned int vtkOpenGLRenderWindow::GetBackBuffer()
Ken Martin's avatar
Ken Martin committed
260
261
262
263
264
265
266
267
{
  return this->BackBuffer;
}

// ----------------------------------------------------------------------------
// Description:
// Return the OpenGL name of the front left buffer.
// It is GL_FRONT if GL is bound to the window-system-provided
268
// framebuffer. It is GL_COLOR_ATTACHMENT0_EXT if GL is bound to an
Ken Martin's avatar
Ken Martin committed
269
// application-created framebuffer object (GPU-based offscreen rendering)
270
271
// It is used by vtkOpenGLCamera.
unsigned int vtkOpenGLRenderWindow::GetFrontBuffer()
Ken Martin's avatar
Ken Martin committed
272
273
274
275
276
{
  return this->FrontBuffer;
}

// Update system if needed due to stereo rendering.
277
void vtkOpenGLRenderWindow::StereoUpdate(void)
Ken Martin's avatar
Ken Martin committed
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
{
  // if stereo is on and it wasn't before
  if (this->StereoRender && (!this->StereoStatus))
    {
    switch (this->StereoType)
      {
      case VTK_STEREO_CRYSTAL_EYES:
        // not clear this is supposed to be empty,
        // but it has been that way forever.
        break;
      case VTK_STEREO_RED_BLUE:
        this->StereoStatus = 1;
        break;
      case VTK_STEREO_ANAGLYPH:
        this->StereoStatus = 1;
        break;
      case VTK_STEREO_DRESDEN:
        this->StereoStatus = 1;
        break;
      case VTK_STEREO_INTERLACED:
        this->StereoStatus = 1;
        break;
      case VTK_STEREO_CHECKERBOARD:
        this->StereoStatus = 1;
        break;
      case VTK_STEREO_SPLITVIEWPORT_HORIZONTAL:
        this->StereoStatus = 1;
        break;
      }
    }
  else if ((!this->StereoRender) && this->StereoStatus)
    {
    switch (this->StereoType)
      {
      case VTK_STEREO_CRYSTAL_EYES:
        this->StereoStatus = 0;
        break;
      case VTK_STEREO_RED_BLUE:
        this->StereoStatus = 0;
        break;
      case VTK_STEREO_ANAGLYPH:
        this->StereoStatus = 0;
        break;
      case VTK_STEREO_DRESDEN:
        this->StereoStatus = 0;
        break;
      case VTK_STEREO_INTERLACED:
        this->StereoStatus = 0;
        break;
      case VTK_STEREO_CHECKERBOARD:
        this->StereoStatus = 0;
        break;
      case VTK_STEREO_SPLITVIEWPORT_HORIZONTAL:
        this->StereoStatus = 0;
        break;
      }
    }
}

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
void vtkOpenGLRenderWindow::SetSize(int a[2])
{
  this->SetSize(a[0], a[1]);
}

void vtkOpenGLRenderWindow::SetSize(int x, int y)
{
  if (this->Size[0] == x
    && this->Size[1] == y)
    {
    // Nothing should happend in the superclass but never knows...
    this->Superclass::SetSize(x, y);
    return;
    }

  this->Superclass::SetSize(x, y);
  if (this->HardwareOffScreenBuffersBind)
    {
    // We activate the offscreen buffers again so they will be
    // recreated at the new window size
    this->SetUseOffScreenBuffers(true);
    }
}

361
void vtkOpenGLRenderWindow::OpenGLInit()
Ken Martin's avatar
Ken Martin committed
362
363
364
365
366
{
  OpenGLInitContext();
  OpenGLInitState();
}

367
void vtkOpenGLRenderWindow::OpenGLInitState()
Ken Martin's avatar
Ken Martin committed
368
369
370
371
372
{
  glDepthFunc( GL_LEQUAL );
  glEnable( GL_DEPTH_TEST );

  // initialize blending for transparency
373
374
  glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
                      GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
Ken Martin's avatar
Ken Martin committed
375
376
377
378
  glEnable(GL_BLEND);

  if (this->LineSmoothing)
    {
379
#ifdef GL_LINE_SMOOTH
Ken Martin's avatar
Ken Martin committed
380
    glEnable(GL_LINE_SMOOTH);
381
#endif
Ken Martin's avatar
Ken Martin committed
382
383
384
    }
  else
    {
385
#ifdef GL_LINE_SMOOTH
Ken Martin's avatar
Ken Martin committed
386
    glDisable(GL_LINE_SMOOTH);
387
#endif
Ken Martin's avatar
Ken Martin committed
388
389
390
391
    }

  if (this->PolygonSmoothing)
    {
392
#ifdef GL_POLYGON_SMOOTH
Ken Martin's avatar
Ken Martin committed
393
    glEnable(GL_POLYGON_SMOOTH);
394
#endif
Ken Martin's avatar
Ken Martin committed
395
396
397
    }
  else
    {
398
#ifdef GL_POLYGON_SMOOTH
Ken Martin's avatar
Ken Martin committed
399
    glDisable(GL_POLYGON_SMOOTH);
400
#endif
Ken Martin's avatar
Ken Martin committed
401
402
403
404
405
406
407
408
409
    }

  // Default OpenGL is 4 bytes but it is only safe with RGBA format.
  // If format is RGB, row alignment is 4 bytes only if the width is divisible
  // by 4. Let's do it the safe way: 1-byte alignment.
  // If an algorithm really need 4 bytes alignment, it should set it itself,
  // this is the recommended way in "Avoiding 16 Common OpenGL Pitfalls",
  // section 7:
  // http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
410
#ifdef GL_UNPACK_ALIGNMENT
Ken Martin's avatar
Ken Martin committed
411
  glPixelStorei(GL_UNPACK_ALIGNMENT,1);
412
#endif
Ken Martin's avatar
Ken Martin committed
413
414
415
416
417
  glPixelStorei(GL_PACK_ALIGNMENT,1);
  // Set the number of alpha bit planes used by the window
  int rgba[4];
  this->GetColorBufferSizes(rgba);
  this->SetAlphaBitPlanes(rgba[3]);
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

  this->InitializeTextureInternalFormats();
}

int vtkOpenGLRenderWindow::GetDefaultTextureInternalFormat(
  int vtktype, int numComponents,
  bool needInt, bool needFloat)
{
  // 0 = none
  // 1 = float
  // 2 = int
  if (vtktype >= VTK_UNICODE_STRING)
    {
    return 0;
    }
  if (needInt)
    {
    return this->TextureInternalFormats[vtktype][2][numComponents];
    }
  if (needFloat)
    {
    return this->TextureInternalFormats[vtktype][1][numComponents];
    }
  return this->TextureInternalFormats[vtktype][0][numComponents];
}

void vtkOpenGLRenderWindow::InitializeTextureInternalFormats()
{
  // 0 = none
  // 1 = float
  // 2 = int

  // initialize to zero
  for (int dtype = 0; dtype < VTK_UNICODE_STRING; dtype++)
    {
    for (int ctype = 0; ctype < 3; ctype++)
      {
      for (int comp = 0; comp <= 4; comp++)
        {
        this->TextureInternalFormats[dtype][ctype][comp] = 0;
        }
      }
    }

  this->TextureInternalFormats[VTK_VOID][0][1] = GL_DEPTH_COMPONENT;

#ifdef GL_R8
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][1] = GL_R8;
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][2] = GL_RG8;
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][3] = GL_RGB8;
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][4] = GL_RGBA8;
#else
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][1] = GL_LUMINANCE;
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][2] = GL_LUMINANCE_ALPHA;
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][3] = GL_RGB;
  this->TextureInternalFormats[VTK_UNSIGNED_CHAR][0][4] = GL_RGBA;
#endif

#ifdef GL_R16
  this->TextureInternalFormats[VTK_UNSIGNED_SHORT][0][1] = GL_R16;
  this->TextureInternalFormats[VTK_UNSIGNED_SHORT][0][2] = GL_RG16;
  this->TextureInternalFormats[VTK_UNSIGNED_SHORT][0][3] = GL_RGB16;
  this->TextureInternalFormats[VTK_UNSIGNED_SHORT][0][4] = GL_RGBA16;
#endif

#ifdef GL_R8_SNORM
  this->TextureInternalFormats[VTK_SIGNED_CHAR][0][1] = GL_R8_SNORM;
  this->TextureInternalFormats[VTK_SIGNED_CHAR][0][2] = GL_RG8_SNORM;
  this->TextureInternalFormats[VTK_SIGNED_CHAR][0][3] = GL_RGB8_SNORM;
  this->TextureInternalFormats[VTK_SIGNED_CHAR][0][4] = GL_RGBA8_SNORM;
#endif

#ifdef GL_R16_SNORM
  this->TextureInternalFormats[VTK_SHORT][0][1] = GL_R16_SNORM;
  this->TextureInternalFormats[VTK_SHORT][0][2] = GL_RG16_SNORM;
  this->TextureInternalFormats[VTK_SHORT][0][3] = GL_RGB16_SNORM;
  this->TextureInternalFormats[VTK_SHORT][0][4] = GL_RGBA16_SNORM;
#endif

  bool haveFloatTextures = false;
  bool haveIntTextures = false;
#if GL_ES_VERSION_2_0 != 1
  if (vtkOpenGLRenderWindow::GetContextSupportsOpenGL32())
    {
    haveFloatTextures = true;
    haveIntTextures = true;
    }
  else
    {
    haveFloatTextures= (glewIsSupported("GL_ARB_texture_float") != 0
     && glewIsSupported("GL_ARB_texture_rg") != 0);
    haveIntTextures= (glewIsSupported("GL_EXT_texture_integer") != 0);
    }
#else
  haveFloatTextures = true;
#if GL_ES_VERSION_3_0 == 1
  haveIntTextures = true;
#endif
#endif

  if (haveIntTextures)
    {
#ifdef GL_R8I
    this->TextureInternalFormats[VTK_SIGNED_CHAR][2][1] = GL_R8I;
    this->TextureInternalFormats[VTK_SIGNED_CHAR][2][2] = GL_RG8I;
    this->TextureInternalFormats[VTK_SIGNED_CHAR][2][3] = GL_RGB8I;
    this->TextureInternalFormats[VTK_SIGNED_CHAR][2][4] = GL_RGBA8I;
    this->TextureInternalFormats[VTK_UNSIGNED_CHAR][2][1] = GL_R8UI;
    this->TextureInternalFormats[VTK_UNSIGNED_CHAR][2][2] = GL_RG8UI;
    this->TextureInternalFormats[VTK_UNSIGNED_CHAR][2][3] = GL_RGB8UI;
    this->TextureInternalFormats[VTK_UNSIGNED_CHAR][2][4] = GL_RGBA8UI;

    this->TextureInternalFormats[VTK_SHORT][2][1] = GL_R16I;
    this->TextureInternalFormats[VTK_SHORT][2][2] = GL_RG16I;
    this->TextureInternalFormats[VTK_SHORT][2][3] = GL_RGB16I;
    this->TextureInternalFormats[VTK_SHORT][2][4] = GL_RGBA16I;
    this->TextureInternalFormats[VTK_UNSIGNED_SHORT][2][1] = GL_R16UI;
    this->TextureInternalFormats[VTK_UNSIGNED_SHORT][2][2] = GL_RG16UI;
    this->TextureInternalFormats[VTK_UNSIGNED_SHORT][2][3] = GL_RGB16UI;
    this->TextureInternalFormats[VTK_UNSIGNED_SHORT][2][4] = GL_RGBA16UI;

    this->TextureInternalFormats[VTK_INT][2][1] = GL_R32I;
    this->TextureInternalFormats[VTK_INT][2][2] = GL_RG32I;
    this->TextureInternalFormats[VTK_INT][2][3] = GL_RGB32I;
    this->TextureInternalFormats[VTK_INT][2][4] = GL_RGBA32I;
    this->TextureInternalFormats[VTK_UNSIGNED_INT][2][1] = GL_R32UI;
    this->TextureInternalFormats[VTK_UNSIGNED_INT][2][2] = GL_RG32UI;
    this->TextureInternalFormats[VTK_UNSIGNED_INT][2][3] = GL_RGB32UI;
    this->TextureInternalFormats[VTK_UNSIGNED_INT][2][4] = GL_RGBA32UI;
#endif
    }

  if (haveFloatTextures)
    {
#ifdef GL_R32F
    this->TextureInternalFormats[VTK_FLOAT][1][1] = GL_R32F;
    this->TextureInternalFormats[VTK_FLOAT][1][2] = GL_RG32F;
    this->TextureInternalFormats[VTK_FLOAT][1][3] = GL_RGB32F;
    this->TextureInternalFormats[VTK_FLOAT][1][4] = GL_RGBA32F;

    this->TextureInternalFormats[VTK_SHORT][1][1] = GL_R32F;
    this->TextureInternalFormats[VTK_SHORT][1][2] = GL_RG32F;
    this->TextureInternalFormats[VTK_SHORT][1][3] = GL_RGB32F;
    this->TextureInternalFormats[VTK_SHORT][1][4] = GL_RGBA32F;
#endif
    }
Ken Martin's avatar
Ken Martin committed
564
565
}

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
void vtkOpenGLRenderWindow::GetOpenGLVersion(int &major, int &minor)
{
  int glMajorVersion = 0;
  int glMinorVersion = 0;

  if (this->Initialized)
    {
    glGetIntegerv(GL_MAJOR_VERSION, & glMajorVersion);
    glGetIntegerv(GL_MINOR_VERSION, & glMinorVersion);
    }

  major = glMinorVersion;
  minor = glMinorVersion;
}

581
void vtkOpenGLRenderWindow::OpenGLInitContext()
Ken Martin's avatar
Ken Martin committed
582
583
584
{
  this->ContextCreationTime.Modified();

Ken Martin's avatar
Ken Martin committed
585
586
587
  // When a new OpenGL context is created, force an update
  if (!this->Initialized)
    {
588
#ifdef GLEW_OK
Ken Martin's avatar
Ken Martin committed
589
590
591
592
593
594
595
596
    GLenum result = glewInit();
    bool m_valid = (result == GLEW_OK);
    if (!m_valid)
      {
      vtkErrorMacro("GLEW could not be initialized.");
      return;
      }

597
    if (!GLEW_VERSION_3_2)
Ken Martin's avatar
Ken Martin committed
598
      {
599
      if (!GLEW_VERSION_2_1 || !GLEW_EXT_gpu_shader4)
600
        {
601
602
603
604
605
        vtkErrorMacro("GL version 2.1 with the gpu_shader4 extension is not "
        "supported by your graphics driver but is required for the new "
        "OpenGL rendering backend. Please update your OpenGL driver. "
        "If you are using Mesa please make sure you have version 10.6.5 or "
        "later and make sure your driver in Mesa supports OpenGL 3.2.");
606
607
608
609
610
611
612
613
614
615
616
        return;
        }
      vtkWarningMacro(
        "VTK is designed to work with OpenGL version 3.2 but it appears "
        "it has been given a context that does not support 3.2. VTK will "
        "run in a compatibility mode designed to work with OpenGL 2.1 but "
        "some features may not work.");
      }
    else
      {
      this->SetContextSupportsOpenGL32(true);
Ken Martin's avatar
Ken Martin committed
617
      }
618
#endif
Ken Martin's avatar
Ken Martin committed
619
    this->Initialized = true;
620
621
622
623
624
625

    // get this system's supported maximum line width
    // we do it here and store it to avoid repeated glGet
    // calls when the result should not change
    GLfloat lineWidthRange[2];
    this->MaximumHardwareLineWidth = 1.0;
Ken Martin's avatar
Ken Martin committed
626
#if defined(GL_SMOOTH_LINE_WIDTH_RANGE) && defined(GL_ALIASED_LINE_WIDTH_RANGE)
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
    if (this->LineSmoothing)
      {
      glGetFloatv(GL_SMOOTH_LINE_WIDTH_RANGE,lineWidthRange);
      if (glGetError() == GL_NO_ERROR)
        {
        this->MaximumHardwareLineWidth = lineWidthRange[1];
        }
      }
    else
      {
      glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE,lineWidthRange);
      if (glGetError() == GL_NO_ERROR)
        {
        this->MaximumHardwareLineWidth = lineWidthRange[1];
        }
      }
Ken Martin's avatar
Ken Martin committed
643
#endif
Ken Martin's avatar
Ken Martin committed
644
    }
Ken Martin's avatar
Ken Martin committed
645
646
}

647
void vtkOpenGLRenderWindow::PrintSelf(ostream& os, vtkIndent indent)
Ken Martin's avatar
Ken Martin committed
648
649
650
651
{
  this->Superclass::PrintSelf(os,indent);
}

652
653
void vtkOpenGLRenderWindow::Render()
{
654
655
656
  // Query current GL state and store them
  this->SaveGLState();

657
658
  this->Superclass::Render();

659
660
  // Restore state to previous known value
  this->RestoreGLState();
661
662
}

663
int vtkOpenGLRenderWindow::GetDepthBufferSize()
Ken Martin's avatar
Ken Martin committed
664
665
666
667
668
669
670
{
  GLint size;

  if ( this->Mapped )
    {
    this->MakeCurrent();
    size = 0;
Ken Martin's avatar
Ken Martin committed
671
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
672
673
674
675
676
677
678
    if (vtkOpenGLRenderWindow::GetContextSupportsOpenGL32())
      {
      glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
        GL_DEPTH,
        GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE, &size);
      }
    else
Ken Martin's avatar
Ken Martin committed
679
#endif
680
681
682
      {
      glGetIntegerv( GL_DEPTH_BITS, &size );
      }
Ken Martin's avatar
Ken Martin committed
683
684
685
686
687
688
689
690
691
    return static_cast<int>(size);
    }
  else
    {
    vtkDebugMacro(<< "Window is not mapped yet!" );
    return 24;
    }
}

692
int vtkOpenGLRenderWindow::GetColorBufferSizes(int *rgba)
Ken Martin's avatar
Ken Martin committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
{
  GLint size;

  if (rgba==NULL)
    {
    return 0;
    }
  rgba[0] = 0;
  rgba[1] = 0;
  rgba[2] = 0;
  rgba[3] = 0;

  if ( this->Mapped)
    {
    this->MakeCurrent();
708
709
710
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
    if (vtkOpenGLRenderWindow::GetContextSupportsOpenGL32())
      {
Ken Martin's avatar
Ken Martin committed
711
712
      GLint fboBind = 0;
      glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fboBind);
713
      GLint attachment = GL_BACK_LEFT;
Ken Martin's avatar
Ken Martin committed
714
#ifdef GL_DRAW_BUFFER
715
      glGetIntegerv(GL_DRAW_BUFFER, &attachment);
Ken Martin's avatar
Ken Martin committed
716
#endif
717
718
719
720
721
      // GL seems odd with its handling of left/right.
      // if it says we are using GL_FRONT or GL_BACK
      // then convert those to GL_FRONT_LEFT and
      // GL_BACK_LEFT.
      if (attachment == GL_FRONT)
Ken Martin's avatar
Ken Martin committed
722
        {
723
724
725
726
727
        attachment = GL_FRONT_LEFT;
        }
      if (attachment == GL_BACK)
        {
        attachment = GL_BACK_LEFT;
Ken Martin's avatar
Ken Martin committed
728
        }
729
      glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
Ken Martin's avatar
Ken Martin committed
730
        attachment,
731
        GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &size);
732
733
734
735
      if (glGetError() == GL_NO_ERROR)
        {
        rgba[0] = static_cast<int>(size);
        }
736
      glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
Ken Martin's avatar
Ken Martin committed
737
        attachment,
738
        GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &size);
739
740
741
742
      if (glGetError() == GL_NO_ERROR)
        {
        rgba[1] = static_cast<int>(size);
        }
743
      glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
Ken Martin's avatar
Ken Martin committed
744
        attachment,
745
        GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &size);
746
747
748
749
      if (glGetError() == GL_NO_ERROR)
        {
        rgba[2] = static_cast<int>(size);
        }
750
      glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
Ken Martin's avatar
Ken Martin committed
751
        attachment,
752
        GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &size);
753
754
755
756
      if (glGetError() == GL_NO_ERROR)
        {
        rgba[3] = static_cast<int>(size);
        }
757
758
759
760
761
762
763
764
765
766
767
768
769
      }
    else
#endif
      {
      glGetIntegerv( GL_RED_BITS, &size );
      rgba[0] = static_cast<int>(size);
      glGetIntegerv( GL_GREEN_BITS, &size  );
      rgba[1] = static_cast<int>(size);
      glGetIntegerv( GL_BLUE_BITS, &size );
      rgba[2] = static_cast<int>(size);
      glGetIntegerv( GL_ALPHA_BITS, &size );
      rgba[3] = static_cast<int>(size);
      }
Ken Martin's avatar
Ken Martin committed
770
771
772
773
774
775
776
777
778
779
780
781
782
    return rgba[0]+rgba[1]+rgba[2]+rgba[3];
    }
  else
    {
    vtkDebugMacro(<< "Window is not mapped yet!" );
    rgba[0] = 8;
    rgba[1] = 8;
    rgba[2] = 8;
    rgba[3] = 8;
    return 32;
    }
}

783
unsigned char* vtkOpenGLRenderWindow::GetPixelData(int x1, int y1,
Ken Martin's avatar
Ken Martin committed
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
810
811
812
813
814
815
816
817
                                                   int x2, int y2,
                                                   int front)
{
  int     y_low, y_hi;
  int     x_low, x_hi;

  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;
    }

  unsigned char *data =
    new unsigned char[(x_hi - x_low + 1)*(y_hi - y_low + 1)*3];
  this->GetPixelData(x1, y1, x2, y2, front, data);
  return data;
}

818
int vtkOpenGLRenderWindow::GetPixelData(int x1, int y1,
Ken Martin's avatar
Ken Martin committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
                                        int x2, int y2,
                                        int front,
                                        vtkUnsignedCharArray* data)
{
  int     y_low, y_hi;
  int     x_low, x_hi;

  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;
    }

  int width  = abs(x_hi - x_low) + 1;
  int height = abs(y_hi - y_low) + 1;
  int size = 3*width*height;

  if ( data->GetMaxId()+1 != size)
    {
    vtkDebugMacro("Resizing array.");
    data->SetNumberOfComponents(3);
    data->SetNumberOfValues(size);
    }
  return this->GetPixelData(x1, y1, x2, y2, front, data->GetPointer(0));

}

862
int vtkOpenGLRenderWindow::GetPixelData(int x1, int y1,
Ken Martin's avatar
Ken Martin committed
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
916
917
918
919
920
921
922
923
924
925
926
                                        int x2, int y2,
                                        int front, unsigned char* data)
{
  int     y_low, y_hi;
  int     x_low, x_hi;

  // set the current window
  this->MakeCurrent();

  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;
    }

  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
    {
    ;
    }

  if (front)
    {
    glReadBuffer(static_cast<GLenum>(this->GetFrontLeftBuffer()));
    }
  else
    {
    glReadBuffer(static_cast<GLenum>(this->GetBackLeftBuffer()));
    }

  glDisable( GL_SCISSOR_TEST );

  // Calling pack alignment ensures that we can grab the any size window
  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
  glReadPixels(x_low, y_low, x_hi-x_low+1, y_hi-y_low+1, GL_RGB,
               GL_UNSIGNED_BYTE, data);

  if (glGetError() != GL_NO_ERROR)
    {
    return VTK_ERROR;
    }
  else
    {
    return VTK_OK;
    }

}

927
int vtkOpenGLRenderWindow::SetPixelData(int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
                                        vtkUnsignedCharArray *data, int front)
{
  int     y_low, y_hi;
  int     x_low, x_hi;

  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;
    }

  int width  = abs(x_hi - x_low) + 1;
  int height = abs(y_hi - y_low) + 1;
  int size = 3*width*height;

  if ( data->GetMaxId()+1 != size)
    {
    vtkErrorMacro("Buffer is of wrong size.");
    return VTK_ERROR;
    }
  return this->SetPixelData(x1, y1, x2, y2, data->GetPointer(0), front);

}

969

970
971
972
973
974
// draw (and stretch as needed) the data to the current viewport
void vtkOpenGLRenderWindow::DrawPixels(
  int srcWidth, int srcHeight, int numComponents, int dataType, void *data)
{
  glDisable( GL_SCISSOR_TEST );
975
  glDisable(GL_DEPTH_TEST);
976
977
978
979
  if (!this->DrawPixelsTextureObject)
    {
    this->DrawPixelsTextureObject = vtkTextureObject::New();
    }
980
981
982
983
  else
    {
    this->DrawPixelsTextureObject->ReleaseGraphicsResources(this);
    }
984
985
986
987
988
989
990
991
992
993
994
995
996
  this->DrawPixelsTextureObject->SetContext(this);
  this->DrawPixelsTextureObject->Create2DFromRaw(srcWidth, srcHeight,
        numComponents, dataType, data);
  this->DrawPixelsTextureObject->CopyToFrameBuffer(NULL, NULL);
}

// very generic call to draw pixel data to a region of the window
void vtkOpenGLRenderWindow::DrawPixels(
  int dstXmin, int dstYmin, int dstXmax, int dstYmax,
  int srcXmin, int srcYmin, int srcXmax, int srcYmax,
  int srcWidth, int srcHeight, int numComponents, int dataType, void *data)
{
  glDisable( GL_SCISSOR_TEST );
997
  glDisable(GL_DEPTH_TEST);
998
999
1000
1001
  if (!this->DrawPixelsTextureObject)
    {
    this->DrawPixelsTextureObject = vtkTextureObject::New();
    }
1002
1003
1004
1005
  else
    {
    this->DrawPixelsTextureObject->ReleaseGraphicsResources(this);
    }
1006
1007
1008
1009
  this->DrawPixelsTextureObject->SetContext(this);
  this->DrawPixelsTextureObject->Create2DFromRaw(srcWidth, srcHeight,
        numComponents, dataType, data);
  this->DrawPixelsTextureObject->CopyToFrameBuffer(
1010
      srcXmin, srcYmin, srcXmax, srcYmax,
1011
1012
1013
1014
1015
1016
      dstXmin, dstYmin, dstXmax, dstYmax,
      this->GetSize()[0], this->GetSize()[1],
      NULL, NULL);
}

// less generic verison, old API
1017
void vtkOpenGLRenderWindow::DrawPixels(int x1, int y1, int x2, int y2, int numComponents, int dataType, void *data)
Ken Martin's avatar
Ken Martin committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
{
  int     y_low, y_hi;
  int     x_low, x_hi;

  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;
    }

1044
1045
  int width = x_hi-x_low+1;
  int height = y_hi-y_low+1;
1046

1047
1048
1049
  // call the more generic version
  this->DrawPixels(x_low, y_low, x_hi, y_hi,
    0, 0, width-1, height-1, width, height, numComponents, dataType, data);
1050
1051
}

1052
int vtkOpenGLRenderWindow::SetPixelData(int x1, int y1, int x2, int y2,
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
                                        unsigned char *data, int front)
{
  // set the current window
  this->MakeCurrent();

  // Error checking
  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
    {
    ;
    }

  GLint buffer;
1066
1067

#ifdef GL_DRAW_BUFFER
1068
  glGetIntegerv(GL_DRAW_BUFFER, &buffer);
1069
#endif
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080

  if (front)
    {
    glDrawBuffer(this->GetFrontBuffer());
    }
  else
    {
    glDrawBuffer(this->GetBackBuffer());
    }

  this->DrawPixels(x1, y1, x2, y2, 3, VTK_UNSIGNED_CHAR, data);
Ken Martin's avatar
Ken Martin committed
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093

  glDrawBuffer(buffer);

  if (glGetError() != GL_NO_ERROR)
    {
    return VTK_ERROR;
    }
  else
    {
    return VTK_OK;
    }
}

1094
float* vtkOpenGLRenderWindow::GetRGBAPixelData(int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
                                               int front)
{

  int     y_low, y_hi;
  int     x_low, x_hi;
  int     width, height;

  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;
    }

  width  = abs(x_hi - x_low) + 1;
  height = abs(y_hi - y_low) + 1;

  float *data = new float[ (width*height*4) ];
  this->GetRGBAPixelData(x1, y1, x2, y2, front, data);

  return data;

}

1134
int vtkOpenGLRenderWindow::GetRGBAPixelData(int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
                                            int front, vtkFloatArray* data)
{
  int     y_low, y_hi;
  int     x_low, x_hi;
  int     width, height;

  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;
    }

  width  = abs(x_hi - x_low) + 1;
  height = abs(y_hi - y_low) + 1;

  int size = 4*width*height;

  if ( data->GetMaxId()+1 != size)
    {
    vtkDebugMacro("Resizing array.");
    data->SetNumberOfComponents(4);
    data->SetNumberOfValues(size);
    }
  return this->GetRGBAPixelData(x1, y1, x2, y2, front, data->GetPointer(0));

}

1178
int vtkOpenGLRenderWindow::GetRGBAPixelData(int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
                                            int front, float* data)
{
  int     y_low, y_hi;
  int     x_low, x_hi;
  int     width, height;

  // set the current window
  this->MakeCurrent();

  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;
    }

  // Error checking
  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
    {
    ;
    }

  if (front)
    {
    glReadBuffer(static_cast<GLenum>(this->GetFrontLeftBuffer()));
    }
  else
    {
    glReadBuffer(static_cast<GLenum>(this->GetBackLeftBuffer()));
    }

  width  = abs(x_hi - x_low) + 1;
  height = abs(y_hi - y_low) + 1;


  // Turn of texturing in case it is on - some drivers have a problem
  // getting / setting pixels with texturing enabled.
  glDisable( GL_TEXTURE_2D );

  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
  glReadPixels( x_low, y_low, width, height, GL_RGBA, GL_FLOAT, data);

  if (glGetError() != GL_NO_ERROR)
    {
    return VTK_ERROR;
    }
  else
    {
    return VTK_OK;
    }
}

1247
void vtkOpenGLRenderWindow::ReleaseRGBAPixelData(float *data)
Ken Martin's avatar
Ken Martin committed
1248
1249
1250
1251
{
  delete[] data;
}

1252
int vtkOpenGLRenderWindow::SetRGBAPixelData(int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
                                            vtkFloatArray *data, int front,
                                            int blend)
{
  int     y_low, y_hi;
  int     x_low, x_hi;
  int     width, height;

  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;
    }

  width  = abs(x_hi-x_low) + 1;
  height = abs(y_hi-y_low) + 1;

  int size = 4*width*height;
  if ( data->GetMaxId()+1 != size )
    {
    vtkErrorMacro("Buffer is of wrong size.");
    return VTK_ERROR;
    }

  return this->SetRGBAPixelData(x1, y1, x2, y2, data->GetPointer(0), front,
                                blend);
}

1296
int vtkOpenGLRenderWindow::SetRGBAPixelData(int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
                                            float *data, int front, int blend)
{
  // set the current window
  this->MakeCurrent();

  // Error checking
  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
    {
    ;
    }

  GLint buffer;
1310
#ifdef GL_DRAW_BUFFER
Ken Martin's avatar
Ken Martin committed
1311
  glGetIntegerv(GL_DRAW_BUFFER, &buffer);
1312
#endif
Ken Martin's avatar
Ken Martin committed
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325

  if (front)
    {
    glDrawBuffer(this->GetFrontBuffer());
    }
  else
    {
    glDrawBuffer(this->GetBackBuffer());
    }

  if (!blend)
    {
    glDisable(GL_BLEND);
1326
    this->DrawPixels(x1, y1, x2, y2, 4, VTK_FLOAT, data); // TODO replace dprecated function
Ken Martin's avatar
Ken Martin committed
1327
1328
1329
1330
    glEnable(GL_BLEND);
    }
  else
    {
1331
    this->DrawPixels(x1, y1, x2, y2, 4, VTK_FLOAT, data);
Ken Martin's avatar
Ken Martin committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
    }

  glDrawBuffer(buffer);

  if (glGetError() != GL_NO_ERROR)
    {
    return VTK_ERROR;
    }
  else
    {
    return VTK_OK;
    }
}

1346
unsigned char *vtkOpenGLRenderWindow::GetRGBACharPixelData(int x1, int y1,
Ken Martin's avatar
Ken Martin committed
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
                                                           int x2, int y2,
                                                           int front)
{
  int     y_low, y_hi;
  int     x_low, x_hi;
  int     width, height;

  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;
    }

  width  = abs(x_hi - x_low) + 1;
  height = abs(y_hi - y_low) + 1;

  unsigned char *data = new unsigned char[ (width*height)*4 ];
  this->GetRGBACharPixelData(x1, y1, x2, y2, front, data);

  return data;
}

1386
int vtkOpenGLRenderWindow::GetRGBACharPixelData(int x1, int y1,
Ken Martin's avatar
Ken Martin committed
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
                                                int x2, int y2,
                                                int front,
                                                vtkUnsignedCharArray* data)
{
  int     y_low, y_hi;
  int     x_low, x_hi;

  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;
    }

  int width  = abs(x_hi - x_low) + 1;
  int height = abs(y_hi - y_low) + 1;
  int size = 4*width*height;

  if ( data->GetMaxId()+1 != size)
    {
    vtkDebugMacro("Resizing array.");
    data->SetNumberOfComponents(4);
    data->SetNumberOfValues(size);
    }
  return this->GetRGBACharPixelData(x1, y1, x2, y2, front,
                                    data->GetPointer(0));
}

1430
int vtkOpenGLRenderWindow::GetRGBACharPixelData(int x1, int y1,
Ken Martin's avatar
Ken Martin committed
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
                                                int x2, int y2,
                                                int front,
                                                unsigned char* data)
{
  int     y_low, y_hi;
  int     x_low, x_hi;
  int     width, height;


  // set the current window
  this->MakeCurrent();


  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;
    }


  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
    {
    ;
    }

  if (front)
    {
    glReadBuffer(static_cast<GLenum>(this->GetFrontLeftBuffer()));
    }
  else
    {
    glReadBuffer(static_cast<GLenum>(this->GetBackLeftBuffer()));
    }

  width  = abs(x_hi - x_low) + 1;
  height = abs(y_hi - y_low) + 1;

  glDisable( GL_SCISSOR_TEST );

  glReadPixels( x_low, y_low, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
                data);

  if (glGetError() != GL_NO_ERROR)
    {
    return VTK_ERROR;
    }
  else
    {
    return VTK_OK;
    }

}


1503
int vtkOpenGLRenderWindow::SetRGBACharPixelData(int x1,int y1,int x2,int y2,
Ken Martin's avatar
Ken Martin committed
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
                                                vtkUnsignedCharArray *data,
                                                int front, int blend)
{
  int     y_low, y_hi;
  int     x_low, x_hi;
  int     width, height;

  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;
    }

  width  = abs(x_hi-x_low) + 1;
  height = abs(y_hi-y_low) + 1;

  int size = 4*width*height;
  if ( data->GetMaxId()+1 != size )
    {
    vtkErrorMacro("Buffer is of wrong size. It is " << data->GetMaxId()+1
                  << ", it should be: " << size);
    return VTK_ERROR;
    }

  return this->SetRGBACharPixelData(x1, y1, x2, y2, data->GetPointer(0),
                                    front, blend);

}

1549
int vtkOpenGLRenderWindow::SetRGBACharPixelData(int x1, int y1, int x2,
Ken Martin's avatar
Ken Martin committed
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
                                                int y2, unsigned char *data,
                                                int front, int blend)
{
  // set the current window
  this->MakeCurrent();


  // Error checking
  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
    {
    ;
    }

  GLint buffer;
1565
#ifdef GL_DRAW_BUFFER
Ken Martin's avatar
Ken Martin committed
1566
  glGetIntegerv(GL_DRAW_BUFFER, &buffer);
1567
#endif
Ken Martin's avatar
Ken Martin committed
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585

  if (front)
    {
    glDrawBuffer(this->GetFrontBuffer());
    }
  else
    {
    glDrawBuffer(this->GetBackBuffer());
    }


  // Disable writing on the z-buffer.
  glDepthMask(GL_FALSE);
  glDisable(GL_DEPTH_TEST);

  if (!blend)
    {
    glDisable(GL_BLEND);
1586
    this->DrawPixels(x1,y1,x2,y2,4, VTK_UNSIGNED_CHAR, data);
Ken Martin's avatar
Ken Martin committed
1587
1588
1589
1590
    glEnable(GL_BLEND);
    }
  else
    {
1591
    this->DrawPixels(x1,y1,x2,y2,4, VTK_UNSIGNED_CHAR, data);
Ken Martin's avatar
Ken Martin committed
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
    }

  // Renenable writing on the z-buffer.
  glDepthMask(GL_TRUE);
  glEnable(GL_DEPTH_TEST);

  glDrawBuffer(buffer);

  if (glGetError() != GL_NO_ERROR)
    {
    return VTK_ERROR;
    }
  else
    {
    return VTK_OK;
    }
}


1611
int vtkOpenGLRenderWindow::GetZbufferData( int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
                                           float* z_data )
{
  int             y_low;
  int             x_low;
  int             width, height;

  // set the current window
  this->MakeCurrent();

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

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

  width =  abs(x2 - x1)+1;
  height = abs(y2 - y1)+1;

  // Error checking
  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
    {
    ;
    }

  // Turn of texturing in case it is on - some drivers have a problem
  // getting / setting pixels with texturing enabled.
  glDisable( GL_SCISSOR_TEST );
  glPixelStorei( GL_PACK_ALIGNMENT, 1 );

  glReadPixels( x_low, y_low,
                width, height,
                GL_DEPTH_COMPONENT, GL_FLOAT,
                z_data );

  if (glGetError() != GL_NO_ERROR)
    {
    return VTK_ERROR;
    }
  else
    {
    return VTK_OK;
    }
}

1669
float *vtkOpenGLRenderWindow::GetZbufferData( int x1, int y1, int x2, int y2  )
Ken Martin's avatar
Ken Martin committed
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
{
  float           *z_data;

  int             width, height;
  width =  abs(x2 - x1)+1;
  height = abs(y2 - y1)+1;

  z_data = new float[width*height];
  this->GetZbufferData(x1, y1, x2, y2, z_data);

  return z_data;
}

1683
int vtkOpenGLRenderWindow::GetZbufferData( int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
                                           vtkFloatArray *buffer )
{
  int  width, height;
  width =  abs(x2 - x1)+1;
  height = abs(y2 - y1)+1;
  int size = width*height;
  if ( buffer->GetMaxId()+1 != size)
    {
    vtkDebugMacro("Resizing array.");
    buffer->SetNumberOfComponents(1);
    buffer->SetNumberOfValues(size);
    }
  return this->GetZbufferData(x1, y1, x2, y2, buffer->GetPointer(0));
}

1699
int vtkOpenGLRenderWindow::SetZbufferData( int x1, int y1, int x2, int y2,
Ken Martin's avatar
Ken Martin committed
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709