VTK
vtkOpenGLRenderWindow.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLRenderWindow.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
25 #ifndef vtkOpenGLRenderWindow_h
26 #define vtkOpenGLRenderWindow_h
27 
28 #include "vtkRect.h" // for vtkRecti
29 #include "vtkRenderWindow.h"
30 #include "vtkRenderingOpenGL2Module.h" // For export macro
31 #include "vtkType.h" // for ivar
32 #include <map> // for ivar
33 #include <set> // for ivar
34 #include <string> // for ivar
35 
36 class vtkIdList;
38 class vtkOpenGLHardwareSupport;
42 class vtkShaderProgram;
43 class vtkStdString;
44 class vtkTexture;
45 class vtkTextureObject;
48 class vtkOpenGLState;
49 
50 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLRenderWindow : public vtkRenderWindow
51 {
52 public:
54  void PrintSelf(ostream& os, vtkIndent indent) override;
55 
59  const char *GetRenderingBackend() override;
60 
62 
65  static void SetGlobalMaximumNumberOfMultiSamples(int val);
66  static int GetGlobalMaximumNumberOfMultiSamples();
68 
70 
73  unsigned char *GetPixelData(int x,int y,int x2,int y2,int front,int right)
74  override;
75  int GetPixelData(int x,int y,int x2,int y2, int front,
76  vtkUnsignedCharArray *data, int right) override;
77  int SetPixelData(int x,int y,int x2,int y2,unsigned char *data,
78  int front, int right) override;
79  int SetPixelData(int x,int y,int x2,int y2,
80  vtkUnsignedCharArray *data, int front, int right)
81  override;
83 
85 
88  float *GetRGBAPixelData(int x,int y,int x2,int y2,int front,int right=0)
89  override;
90  int GetRGBAPixelData(int x,int y,int x2,int y2, int front,
91  vtkFloatArray* data, int right=0) override;
92  int SetRGBAPixelData(int x,int y,int x2,int y2, float *data,
93  int front, int blend=0, int right=0) override;
94  int SetRGBAPixelData(int x,int y,int x2,int y2, vtkFloatArray *data,
95  int front, int blend=0, int right=0) override;
96  void ReleaseRGBAPixelData(float *data) override;
97  unsigned char *GetRGBACharPixelData(int x,int y,int x2,int y2,
98  int front, int right=0) override;
99  int GetRGBACharPixelData(int x,int y,int x2,int y2, int front,
100  vtkUnsignedCharArray *data, int right=0)
101  override;
102  int SetRGBACharPixelData(int x, int y, int x2, int y2,
103  unsigned char *data, int front,
104  int blend=0, int right=0) override;
105  int SetRGBACharPixelData(int x,int y,int x2,int y2,
106  vtkUnsignedCharArray *data, int front,
107  int blend=0,int right=0) override;
109 
111 
114  float *GetZbufferData( int x1, int y1, int x2, int y2 ) override;
115  int GetZbufferData( int x1, int y1, int x2, int y2, float* z ) override;
116  int GetZbufferData( int x1, int y1, int x2, int y2,
117  vtkFloatArray* z ) override;
118  int SetZbufferData( int x1, int y1, int x2, int y2, float *buffer ) override;
119  int SetZbufferData( int x1, int y1, int x2, int y2,
120  vtkFloatArray *buffer ) override;
122 
123 
127  void ActivateTexture(vtkTextureObject *);
128 
132  void DeactivateTexture(vtkTextureObject *);
133 
137  int GetTextureUnitForTexture(vtkTextureObject *);
138 
142  int GetDepthBufferSize() override;
143 
147  bool GetUsingSRGBColorSpace();
148 
153  int GetColorBufferSizes(int *rgba) override;
154 
160  int GetColorBufferInternalFormat(int attachmentPoint);
161 
163 
166  void SetSize(int a[2]) override;
167  void SetSize(int,int) override;
169 
173  virtual void OpenGLInit();
174 
175  // Initialize the state of OpenGL that VTK wants for this window
176  virtual void OpenGLInitState();
177 
178  // Initialize VTK for rendering in a new OpenGL context
179  virtual void OpenGLInitContext();
180 
186  void GetOpenGLVersion(int &major, int &minor);
187 
195  unsigned int GetBackLeftBuffer();
196 
204  unsigned int GetBackRightBuffer();
205 
213  unsigned int GetFrontLeftBuffer();
214 
222  unsigned int GetFrontRightBuffer();
223 
231  unsigned int GetBackBuffer();
232 
240  unsigned int GetFrontBuffer();
241 
245  virtual vtkMTimeType GetContextCreationTime();
246 
248 
251  vtkGetObjectMacro(ShaderCache,vtkOpenGLShaderCache);
253 
255 
258  vtkGetObjectMacro(VBOCache,vtkOpenGLVertexBufferObjectCache);
260 
262 
265  vtkGetMacro(FrameBufferObject, unsigned int);
267 
272  vtkTextureUnitManager *GetTextureUnitManager();
273 
278  void WaitForCompletion() override;
279 
283  virtual void DrawPixels(int x1, int y1, int x2, int y2,
284  int numComponents, int dataType, void *data);
285 
290  virtual void DrawPixels(
291  int dstXmin, int dstYmin, int dstXmax, int dstYmax,
292  int srcXmin, int srcYmin, int srcXmax, int srcYmax,
293  int srcWidth, int srcHeight, int numComponents, int dataType, void *data);
294 
299  virtual void DrawPixels(
300  int srcWidth, int srcHeight, int numComponents, int dataType, void *data);
301 
305  virtual float GetMaximumHardwareLineWidth() {
306  return this->MaximumHardwareLineWidth; };
307 
314  virtual bool IsPointSpriteBugPresent()
315  {
316  return 0;
317  }
318 
324  int GetDefaultTextureInternalFormat(
325  int vtktype, int numComponents,
326  bool needInteger, bool needFloat, bool needSRGB);
327 
334  {
335  return this->OpenGLSupportMessage;
336  }
337 
338  // Create and bind offscreen rendering buffers without destroying the current
339  // OpenGL context. This allows to temporary switch to offscreen rendering
340  // (ie. to make a screenshot even if the window is hidden).
341  // Return if the creation was successful (1) or not (0).
342  // Note: This function requires that the device supports OpenGL framebuffer extension.
343  // The function has no effect if OffScreenRendering is ON.
344  int SetUseOffScreenBuffers(bool offScreen) override;
345  bool GetUseOffScreenBuffers() override;
346 
350  int SupportsOpenGL() override;
351 
355  const char *ReportCapabilities() override;
356 
363  virtual void Initialize(void) {};
364 
365  std::set<vtkGenericOpenGLResourceFreeCallback *> Resources;
366 
368  std::set<vtkGenericOpenGLResourceFreeCallback *>::iterator it
369  = this->Resources.find(cb);
370  if (it == this->Resources.end())
371  {
372  this->Resources.insert(cb);
373  }
374  }
375 
377  std::set<vtkGenericOpenGLResourceFreeCallback *>::iterator it
378  = this->Resources.find(cb);
379  if (it != this->Resources.end())
380  {
381  this->Resources.erase(it);
382  }
383  }
384 
394  virtual void PushContext() { this->MakeCurrent(); }
395  virtual void PopContext() {}
396 
401  bool InitializeFromCurrentContext() override;
402 
410  vtkGetMacro(DefaultFrameBufferId, unsigned int);
411 
421  virtual bool SetSwapControl(int ) { return false; }
422 
423  // Get the state object used to keep track of
424  // OpenGL state
426  return this->State; }
427 
428  // Get a VBO that can be shared by many
429  // It consists of normalized display
430  // coordinates for a quad and tcoords
431  vtkOpenGLBufferObject *GetTQuad2DVBO();
432 
433  // Activate and return thje texture unit for a generic 2d 64x64
434  // float greyscale noise texture ranging from 0 to 1. The texture is
435  // generated using PerlinNoise. This textur eunit will automatically
436  // be deactivated at the end of the render process.
437  int GetNoiseTextureUnit();
438 
443  void StereoUpdate() override;
444 
449  void StereoMidpoint() override;
450 
454  void Render() override;
455 
456 protected:
458  ~vtkOpenGLRenderWindow() override;
459 
462 
464 
465  // used in testing for opengl support
466  // in the SupportsOpenGL() method
470 
471  int TextureInternalFormats[VTK_UNICODE_STRING][3][5];
472  void InitializeTextureInternalFormats();
473 
474  std::map<const vtkTextureObject *, int> TextureResourceIds;
475 
476  virtual int ReadPixels(const vtkRecti& rect, int front, int glFormat, int glType, void* data, int right=0);
477 
487  int CreateHardwareOffScreenWindow(int width, int height);
488 
489  int CreateHardwareOffScreenBuffers(int width, int height, bool bind = false);
490  void BindHardwareOffScreenBuffers();
491 
497  void DestroyHardwareOffScreenWindow();
498 
499  void UnbindHardwareOffScreenBuffers();
500  void DestroyHardwareOffScreenBuffers();
501 
506 
508 
512  unsigned int TextureObjects[4]; // really GLuint
513  unsigned int FrameBufferObject; // really GLuint
514  unsigned int DepthRenderBufferObject; // really GLuint
515  int HardwareBufferSize[2];
518 
522  virtual void CreateAWindow() = 0;
523 
527  virtual void DestroyWindow() = 0;
528 
533  virtual void ReleaseGraphicsResources(vtkRenderWindow *);
534 
538  void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager);
539 
540 
544  void SaveGLState();
545 
549  void RestoreGLState();
550 
551  std::map<std::string, int> GLStateIntegers;
552 
553  unsigned int BackLeftBuffer;
554  unsigned int BackRightBuffer;
555  unsigned int FrontLeftBuffer;
556  unsigned int FrontRightBuffer;
557  unsigned int FrontBuffer;
558  unsigned int BackBuffer;
559  unsigned int DefaultFrameBufferId;
560 
565 
567 
569 
571 
572  bool Initialized; // ensure glewinit has been called
573  bool GlewInitValid; // Did glewInit initialize with a valid state?
574 
576 
578 
579  // used for fast quad rendering
581 
582  // noise texture
584 
585 private:
587  void operator=(const vtkOpenGLRenderWindow&) = delete;
588 };
589 
590 #endif
vtkWindow::SetSize
virtual void SetSize(int, int)
vtkOpenGLRenderWindow::GetMaximumHardwareLineWidth
virtual float GetMaximumHardwareLineWidth()
Return the largest line width supported by the hardware.
Definition: vtkOpenGLRenderWindow.h:305
vtkOpenGLRenderWindow::RegisterGraphicsResources
void RegisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
Definition: vtkOpenGLRenderWindow.h:367
vtkOpenGLBufferObject
OpenGL buffer object.
Definition: vtkOpenGLBufferObject.h:33
vtkOpenGLRenderWindow::NumberOfFrameBuffers
int NumberOfFrameBuffers
Variables used by the framebuffer-based offscreen method.
Definition: vtkOpenGLRenderWindow.h:511
vtkOpenGLRenderWindow::DefaultFrameBufferId
unsigned int DefaultFrameBufferId
Definition: vtkOpenGLRenderWindow.h:559
vtkRenderWindow::GetUseOffScreenBuffers
virtual bool GetUseOffScreenBuffers()
Definition: vtkRenderWindow.h:681
vtkOpenGLRenderWindow::MaximumHardwareLineWidth
float MaximumHardwareLineWidth
Definition: vtkOpenGLRenderWindow.h:575
vtkRenderWindow::GetColorBufferSizes
virtual int GetColorBufferSizes(int *rgba)=0
Get the size of the color buffer.
vtkRenderWindow::GetDepthBufferSize
virtual int GetDepthBufferSize()=0
This method should be defined by the subclass.
vtkFloatArray
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:35
vtkOpenGLRenderWindow::SetSwapControl
virtual bool SetSwapControl(int)
Set the number of vertical syncs required between frames.
Definition: vtkOpenGLRenderWindow.h:421
vtkX3D::data
Definition: vtkX3D.h:315
vtkOpenGLRenderWindow::TextureResourceIds
std::map< const vtkTextureObject *, int > TextureResourceIds
Definition: vtkOpenGLRenderWindow.h:474
vtkRenderWindow::GetRGBAPixelData
virtual float * GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right=0)=0
Same as Get/SetPixelData except that the image also contains an alpha component.
vtkOpenGLRenderWindow::GetOpenGLSupportMessage
std::string GetOpenGLSupportMessage()
Return a message profiding additional details about the results of calling SupportsOpenGL() This can ...
Definition: vtkOpenGLRenderWindow.h:333
VTK_UNICODE_STRING
#define VTK_UNICODE_STRING
Definition: vtkType.h:85
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:32
vtkRenderWindow::SetUseOffScreenBuffers
virtual int SetUseOffScreenBuffers(bool)
Create and bind offscreen rendering buffers without destroying the current OpenGL context.
Definition: vtkRenderWindow.h:680
vtkUnsignedCharArray
dynamic, self-adjusting array of unsigned char
Definition: vtkUnsignedCharArray.h:35
vtkRenderWindow.h
vtkOpenGLRenderWindow::UnregisterGraphicsResources
void UnregisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
Definition: vtkOpenGLRenderWindow.h:376
vtkOpenGLRenderWindow::DrawPixelsTextureObject
vtkTextureObject * DrawPixelsTextureObject
Definition: vtkOpenGLRenderWindow.h:570
vtkOpenGLRenderWindow::FrameBufferObject
unsigned int FrameBufferObject
Definition: vtkOpenGLRenderWindow.h:513
vtkOpenGLRenderWindow::TextureUnitManager
vtkTextureUnitManager * TextureUnitManager
Definition: vtkOpenGLRenderWindow.h:568
vtkOpenGLRenderWindow::OffScreenUseFrameBuffer
int OffScreenUseFrameBuffer
Flag telling if a framebuffer-based offscreen is currently in use.
Definition: vtkOpenGLRenderWindow.h:505
vtkOpenGLRenderWindow::FrontRightBuffer
unsigned int FrontRightBuffer
Definition: vtkOpenGLRenderWindow.h:556
vtkOpenGLRenderWindow::OpenGLSupportMessage
std::string OpenGLSupportMessage
Definition: vtkOpenGLRenderWindow.h:469
vtkOpenGLRenderWindow::PopContext
virtual void PopContext()
Definition: vtkOpenGLRenderWindow.h:395
vtkOpenGLRenderWindow::FrontBuffer
unsigned int FrontBuffer
Definition: vtkOpenGLRenderWindow.h:557
vtkOpenGLRenderWindow::GlewInitValid
bool GlewInitValid
Definition: vtkOpenGLRenderWindow.h:573
vtkType.h
vtkOpenGLRenderWindow::ContextCreationTime
vtkTimeStamp ContextCreationTime
Definition: vtkOpenGLRenderWindow.h:566
vtkTexture
handles properties associated with a texture map
Definition: vtkTexture.h:65
vtkOpenGLRenderWindow::OpenGLSupportResult
int OpenGLSupportResult
Definition: vtkOpenGLRenderWindow.h:468
vtkTextureUnitManager
allocate/free texture units.
Definition: vtkTextureUnitManager.h:38
vtkOpenGLRenderWindow::ShaderCache
vtkOpenGLShaderCache * ShaderCache
Definition: vtkOpenGLRenderWindow.h:460
vtkRenderWindow::StereoMidpoint
virtual void StereoMidpoint()
Intermediate method performs operations required between the rendering of the left and right eye.
vtkOpenGLRenderWindow::HardwareOffScreenBuffersBind
bool HardwareOffScreenBuffersBind
Definition: vtkOpenGLRenderWindow.h:516
vtkX3D::height
Definition: vtkX3D.h:254
vtkOpenGLRenderWindow::VBOCache
vtkOpenGLVertexBufferObjectCache * VBOCache
Definition: vtkOpenGLRenderWindow.h:461
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:44
vtkOpenGLRenderWindow::OwnContext
int OwnContext
Flag telling if the context has been created here or was inherited.
Definition: vtkOpenGLRenderWindow.h:564
vtkGenericOpenGLResourceFreeCallback
Definition: vtkOpenGLResourceFreeCallback.h:25
vtkRect.h
vtkRenderWindow::GetZbufferData
virtual float * GetZbufferData(int x, int y, int x2, int y2)=0
Set/Get the zbuffer data from the frame buffer.
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:41
vtkRenderWindow::Render
void Render() override
Ask each renderer owned by this RenderWindow to render its image and synchronize this process.
vtkRenderWindow::SetPixelData
virtual int SetPixelData(int x, int y, int x2, int y2, unsigned char *data, int front, int right=0)=0
Set/Get the pixel data of an image, transmitted as RGBRGBRGB.
vtkOpenGLRenderWindow::Capabilities
char * Capabilities
Definition: vtkOpenGLRenderWindow.h:577
vtkOpenGLRenderWindow::FrontLeftBuffer
unsigned int FrontLeftBuffer
Definition: vtkOpenGLRenderWindow.h:555
vtkOpenGLRenderWindow::NoiseTextureObject
vtkTextureObject * NoiseTextureObject
Definition: vtkOpenGLRenderWindow.h:583
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:30
vtkRenderWindow::WaitForCompletion
virtual void WaitForCompletion()=0
Block the thread until the actual rendering is finished().
vtkOpenGLRenderWindow::GetState
virtual vtkOpenGLState * GetState()
Definition: vtkOpenGLRenderWindow.h:425
vtkRenderWindow::MakeCurrent
void MakeCurrent() override=0
Attempt to make this window the current graphics context for the calling thread.
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:35
vtkRenderWindow::ReportCapabilities
virtual const char * ReportCapabilities()
Get report of capabilities for the render window.
Definition: vtkRenderWindow.h:612
vtkOpenGLRenderWindow::IsPointSpriteBugPresent
virtual bool IsPointSpriteBugPresent()
Returns true if driver has an EGL/OpenGL bug that makes vtkChartsCoreCxx-TestChartDoubleColors and ot...
Definition: vtkOpenGLRenderWindow.h:314
vtkOpenGLRenderWindow::Initialized
bool Initialized
Definition: vtkOpenGLRenderWindow.h:572
vtkX3D::string
Definition: vtkX3D.h:490
vtkRenderWindow::SetZbufferData
virtual int SetZbufferData(int x, int y, int x2, int y2, float *z)=0
vtkRenderWindow::SetRGBAPixelData
virtual int SetRGBAPixelData(int x, int y, int x2, int y2, float *, int front, int blend=0, int right=0)=0
vtkWindow::GetPixelData
virtual unsigned char * GetPixelData(int x, int y, int x2, int y2, int front, int right=0)=0
Get the pixel data of an image, transmitted as RGBRGBRGB.
vtkOpenGLRenderWindow::OpenGLSupportTested
bool OpenGLSupportTested
Definition: vtkOpenGLRenderWindow.h:467
vtkRenderWindow::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkOpenGLRenderWindow::BackRightBuffer
unsigned int BackRightBuffer
Definition: vtkOpenGLRenderWindow.h:554
vtkRenderWindow::GetRGBACharPixelData
virtual unsigned char * GetRGBACharPixelData(int x, int y, int x2, int y2, int front, int right=0)=0
vtkOpenGLVertexBufferObjectCache
manage vertex buffer objects shared within a context
Definition: vtkOpenGLVertexBufferObjectCache.h:38
vtkRenderWindow::StereoUpdate
virtual void StereoUpdate()
Update the system, if needed, due to stereo rendering.
vtkOpenGLRenderWindow::GLStateIntegers
std::map< std::string, int > GLStateIntegers
Definition: vtkOpenGLRenderWindow.h:551
vtkRenderWindow::ReleaseRGBAPixelData
virtual void ReleaseRGBAPixelData(float *data)=0
vtkOpenGLRenderWindow::BackLeftBuffer
unsigned int BackLeftBuffer
Definition: vtkOpenGLRenderWindow.h:553
vtkRenderWindow::SupportsOpenGL
virtual int SupportsOpenGL()
Does this render window support OpenGL? 0-false, 1-true.
Definition: vtkRenderWindow.h:617
vtkRecti
Definition: vtkRect.h:284
vtkRenderWindow::SetRGBACharPixelData
virtual int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char *data, int front, int blend=0, int right=0)=0
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:50
vtkOpenGLRenderWindow::State
vtkOpenGLState * State
Definition: vtkOpenGLRenderWindow.h:463
vtkRenderWindow
create a window for renderers to draw into
Definition: vtkRenderWindow.h:81
vtkRenderWindow::InitializeFromCurrentContext
virtual bool InitializeFromCurrentContext()
Initialize the render window from the information associated with the currently activated OpenGL cont...
Definition: vtkRenderWindow.h:567
vtkRenderWindow::GetRenderingBackend
virtual const char * GetRenderingBackend()
What rendering backend has the user requested.
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:34
vtkOpenGLRenderWindow::TQuad2DVBO
vtkOpenGLBufferObject * TQuad2DVBO
Definition: vtkOpenGLRenderWindow.h:580
vtkOpenGLRenderWindow::DepthRenderBufferObject
unsigned int DepthRenderBufferObject
Definition: vtkOpenGLRenderWindow.h:514
vtkOpenGLRenderWindow::BackBuffer
unsigned int BackBuffer
Definition: vtkOpenGLRenderWindow.h:558
vtkOpenGLState
OpenGL state storage.
Definition: vtkOpenGLState.h:67
vtkOpenGLRenderWindow::PushContext
virtual void PushContext()
Ability to push and pop this window's context as the current context.
Definition: vtkOpenGLRenderWindow.h:394
vtkOpenGLVertexArrayObject
The VertexArrayObject class uses, or emulates, vertex array objects.
Definition: vtkOpenGLVertexArrayObject.h:36
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:302