vtkOpenGLHardwareSelector.cxx 10.1 KB
Newer Older
1
2
3
/*=========================================================================

  Program:   Visualization Toolkit
4
  Module:    vtkOpenGLHardwareSelector.cxx
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.

=========================================================================*/
15
#include "vtkOpenGLHardwareSelector.h"
16

Ken Martin's avatar
Ken Martin committed
17
#include "vtk_glew.h"
18

19
#include "vtkObjectFactory.h"
20
#include "vtkDataObject.h"
21
22
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
23
#include "vtkOpenGLRenderWindow.h"
24
25
26

#include "vtkOpenGLError.h"

Ken Martin's avatar
Ken Martin committed
27
28
29
30
31
//#define vtkOpenGLHardwareSelectorDEBUG
#ifdef vtkOpenGLHardwareSelectorDEBUG
#include "vtkPNMWriter.h"
#include "vtkImageImport.h"
#include "vtkNew.h"
32
33
#include <sstream>
#include "vtkWindows.h"  // OK on UNix etc
Ken Martin's avatar
Ken Martin committed
34
35
#endif

36
37
#define ID_OFFSET 1

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Define to print debug statements to the OpenGL CS stream (useful for e.g.
// apitrace debugging):
//#define ANNOTATE_STREAM

namespace
{
void annotate(const std::string &str)
{
#ifdef ANNOTATE_STREAM
  vtkOpenGLStaticCheckErrorMacro("Error before glDebug.")
  glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER,
                       GL_DEBUG_SEVERITY_NOTIFICATION,
                       0, str.size(), str.c_str());
  vtkOpenGLClearErrorMacro();
#else // ANNOTATE_STREAM
  (void)str;
#endif // ANNOTATE_STREAM
}
}

58
59
// Description:
// Internal state and helper methods.
60
class vtkOpenGLHardwareSelector::vtkInternals
61
62
{
public:
63
  vtkOpenGLRenderWindow *Context;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  bool MultisampleSupport;
  bool OriginalMultisample;
  bool OriginalBlending;

  vtkInternals() :
    Context(NULL),
    MultisampleSupport(false),
    OriginalMultisample(false),
    OriginalBlending(false)
    {}

  // Description:
  // Set the rendering context and load the required
  // extensions.
  void SetContext(vtkRenderWindow *context)
79
  {
80
    if (this->Context != context)
81
    {
82
      this->MultisampleSupport = true;
83
    }
84
  }
85
86
87
88

  // Description:
  // Enable/disable multisampling.
  void EnableMultisampling(bool mode)
89
  {
90
    if (this->MultisampleSupport)
91
    {
Ken Martin's avatar
Ken Martin committed
92
#if GL_ES_VERSION_3_0 != 1
93
      if (mode)
94
      {
95
        glEnable(GL_MULTISAMPLE);
96
      }
97
      else
98
      {
99
        glDisable(GL_MULTISAMPLE);
100
      }
101
#endif
102
    }
103
  }
104
105
106
107

  // Description:
  // Check if multisample is enabled.
  bool QueryMultisampling()
108
  {
Ken Martin's avatar
Ken Martin committed
109
#if GL_ES_VERSION_3_0 != 1
110
    if (this->MultisampleSupport && glIsEnabled(GL_MULTISAMPLE))
111
    {
112
      return true;
113
    }
114
    else
115
    {
116
      return false;
117
    }
118
119
120
#else
    return false;
#endif
121
  }
122
123
124
125

  // Description:
  // Enable/Disable blending
  void EnableBlending(bool mode)
126
  {
127
    if (mode)
128
    {
129
      glEnable(GL_BLEND);
130
    }
131
    else
132
    {
133
134
      glDisable(GL_BLEND);
    }
135
  }
136
137
138
139

  // Description:
  // Check if blending is enabled.
  bool QueryBlending()
140
  {
141
    if (glIsEnabled(GL_BLEND))
142
    {
143
      return true;
144
    }
145
    else
146
    {
147
148
      return false;
    }
149
  }
150
151
152
};

//----------------------------------------------------------------------------
153
vtkStandardNewMacro(vtkOpenGLHardwareSelector);
154
155

//----------------------------------------------------------------------------
156
vtkOpenGLHardwareSelector::vtkOpenGLHardwareSelector()
157
{
158
159
  #ifdef vtkOpenGLHardwareSelectorDEBUG
  cerr << "=====vtkOpenGLHardwareSelector::vtkOpenGLHardwareSelector" << endl;
160
161
162
163
164
  #endif
  this->Internals = new vtkInternals;
}

//----------------------------------------------------------------------------
165
vtkOpenGLHardwareSelector::~vtkOpenGLHardwareSelector()
166
{
167
168
  #ifdef vtkOpenGLHardwareSelectorDEBUG
  cerr << "=====vtkOpenGLHardwareSelector::~vtkOpenGLHardwareSelector" << endl;
169
170
171
172
  #endif
  delete this->Internals;
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
//----------------------------------------------------------------------------
void vtkOpenGLHardwareSelector::PreCapturePass(int pass)
{
  annotate(std::string("Starting pass: ") +
           this->PassTypeToString(static_cast<PassTypes>(pass)));
}

//----------------------------------------------------------------------------
void vtkOpenGLHardwareSelector::PostCapturePass(int pass)
{
  annotate(std::string("Pass complete: ") +
           this->PassTypeToString(static_cast<PassTypes>(pass)));
}

187
//----------------------------------------------------------------------------
188
void vtkOpenGLHardwareSelector::BeginSelection()
189
{
190
  // render normally to set the zbuffer
191
  if (this->FieldAssociation == vtkDataObject::FIELD_ASSOCIATION_POINTS)
192
  {
193
194
195
196
    vtkRenderWindow *rwin = this->Renderer->GetRenderWindow();
    rwin->SwapBuffersOff();
    rwin->Render();
    this->Renderer->PreserveDepthBufferOn();
197
  }
198

199
  return this->Superclass::BeginSelection();
200
201
}

Ken Martin's avatar
Ken Martin committed
202
203
204
205
206
207
208
209
210
211
212
213
214
//----------------------------------------------------------------------------
// just add debug output if compiled with vtkOpenGLHardwareSelectorDEBUG
void vtkOpenGLHardwareSelector::SavePixelBuffer(int passNo)
{
  this->Superclass::SavePixelBuffer(passNo);

#ifdef vtkOpenGLHardwareSelectorDEBUG

  vtkNew<vtkImageImport> ii;
  ii->SetImportVoidPointer(this->PixBuffer[passNo],1);
  ii->SetDataScalarTypeToUnsignedChar();
  ii->SetNumberOfScalarComponents(3);
  ii->SetDataExtent(this->Area[0], this->Area[2], this->Area[1], this->Area[3], 0, 0);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  ii->SetWholeExtent(this->Area[0], this->Area[2], this->Area[1], this->Area[3], 0, 0);

  // change this to somewhere on your system
  // hardcoded as with MPI/parallel/client server it can be hard to
  // find these images sometimes.
  std::string fname = "C:/Users/ken.martin/Documents/pickbuffer_";

#if defined(_WIN32)
  std::ostringstream toString;
  toString.str("");
  toString.clear();
  toString << GetCurrentProcessId();
  fname += toString.str();
  fname += "_";
#endif
Ken Martin's avatar
Ken Martin committed
230
231
232
233
234
235
  fname += ('0'+passNo);
  fname += ".pnm";
  vtkNew<vtkPNMWriter> pw;
  pw->SetInputConnection(ii->GetOutputPort());
  pw->SetFileName(fname.c_str());
  pw->Write();
236
  cerr << "=====vtkOpenGLHardwareSelector wrote " << fname << "\n";
Ken Martin's avatar
Ken Martin committed
237
238
239
#endif
}

240
//----------------------------------------------------------------------------
241
void vtkOpenGLHardwareSelector::BeginRenderProp(vtkRenderWindow *context)
242
{
243
244
  #ifdef vtkOpenGLHardwareSelectorDEBUG
  cerr << "=====vtkOpenGLHardwareSelector::BeginRenderProp" << endl;
245
246
247
248
  #endif

  this->Internals->SetContext(context);

249
  // Disable multisample, and blending.
250
251
252
253
254
255
256
257
  this->Internals->OriginalMultisample = this->Internals->QueryMultisampling();
  this->Internals->EnableMultisampling(false);

  this->Internals->OriginalBlending = this->Internals->QueryBlending();
  this->Internals->EnableBlending(false);
}

//----------------------------------------------------------------------------
258
void vtkOpenGLHardwareSelector::EndRenderProp(vtkRenderWindow *)
259
{
260
261
  #ifdef vtkOpenGLHardwareSelectorDEBUG
  cerr << "=====vtkOpenGLHardwareSelector::EndRenderProp" << endl;
262
263
  #endif

264
  // Restore multisample, and blending.
265
266
267
268
269
  this->Internals->EnableMultisampling(this->Internals->OriginalMultisample);
  this->Internals->EnableBlending(this->Internals->OriginalBlending);
}

//----------------------------------------------------------------------------
270
void vtkOpenGLHardwareSelector::BeginRenderProp()
271
272
273
{
  this->InPropRender++;
  if (this->InPropRender != 1)
274
  {
275
    return;
276
  }
277
278
279
280
281
282
283

  // device specific prep
  vtkRenderWindow *renWin = this->Renderer->GetRenderWindow();
  this->BeginRenderProp(renWin);

  //cout << "In BeginRenderProp" << endl;
  if (this->CurrentPass == ACTOR_PASS)
284
  {
285
286
    int propid = this->PropID;
    if (propid >= 0xfffffe)
287
    {
288
289
290
      vtkErrorMacro("Too many props. Currently only " << 0xfffffe
        << " props are supported.");
      return;
291
    }
292
293
294
295
296
    float color[3];
    // Since 0 is reserved for nothing selected, we offset propid by 1.
    propid = propid + 1;
    vtkHardwareSelector::Convert(propid, color);
    this->SetPropColorValue(color);
297
  }
298
  else if (this->CurrentPass == PROCESS_PASS)
299
  {
300
301
302
303
    float color[3];
    // Since 0 is reserved for nothing selected, we offset propid by 1.
    vtkHardwareSelector::Convert(this->ProcessID + 1, color);
    this->SetPropColorValue(color);
304
  }
305
306
307
}

//----------------------------------------------------------------------------
308
void vtkOpenGLHardwareSelector::RenderCompositeIndex(unsigned int index)
309
310
311
{

  if (index > 0xffffff)
312
  {
313
314
    vtkErrorMacro("Indices > 0xffffff are not supported.");
    return;
315
  }
316
317
318
319

  index += ID_OFFSET;

  if (this->CurrentPass == COMPOSITE_INDEX_PASS)
320
  {
321
322
323
    float color[3];
    vtkHardwareSelector::Convert(static_cast<int>(0xffffff & index), color);
    this->SetPropColorValue(color);
324
  }
325
326
327
328
}

//----------------------------------------------------------------------------
// TODO: make inline
329
void vtkOpenGLHardwareSelector::RenderAttributeId(vtkIdType attribid)
330
331
{
  if (attribid < 0)
332
  {
333
334
    vtkErrorMacro("Invalid id: " << attribid);
    return;
335
  }
336
337
338
339
340

  this->MaxAttributeId = (attribid > this->MaxAttributeId)? attribid :
    this->MaxAttributeId;

  if (this->CurrentPass < ID_LOW24 || this->CurrentPass > ID_HIGH16)
341
  {
342
    return;
343
  }
344
345
346
347
348

  // 0 is reserved.
  attribid += ID_OFFSET;

  for (int cc=0; cc < 3; cc++)
349
  {
350
351
352
    int words24 = (0xffffff & attribid);
    attribid = attribid >> 24;
    if ((this->CurrentPass - ID_LOW24) == cc)
353
    {
354
355
356
357
358
      float color[3];
      vtkHardwareSelector::Convert(words24, color);
      this->SetPropColorValue(color);
      break;
    }
359
  }
360
361
362
}

//----------------------------------------------------------------------------
363
void vtkOpenGLHardwareSelector::RenderProcessId(unsigned int processid)
364
365
{
  if (this->CurrentPass == PROCESS_PASS && this->UseProcessIdFromData)
366
  {
367
    if (processid >= 0xffffff)
368
    {
369
370
      vtkErrorMacro("Invalid id: " << processid);
      return;
371
    }
372
373
374
375
376

    float color[3];
    vtkHardwareSelector::Convert(
      static_cast<int>(processid + 1), color);
    this->SetPropColorValue(color);
377
  }
378
379
380
381
382
}



//----------------------------------------------------------------------------
383
void vtkOpenGLHardwareSelector::PrintSelf(ostream& os, vtkIndent indent)
384
385
386
387
388
389
390
{
  this->Superclass::PrintSelf(os, indent);
  os
   << indent << "MultisampleSupport: "
   << this->Internals->MultisampleSupport
   << endl;
}