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 =========================================================================*/
28 #ifndef vtkOpenGLRenderWindow_h
29 #define vtkOpenGLRenderWindow_h
30 
31 #include "vtkRect.h" // for vtkRecti
32 #include "vtkRenderWindow.h"
33 #include "vtkRenderingOpenGL2Module.h" // For export macro
34 #include "vtkType.h" // for ivar
35 #include <map> // for ivar
36 #include <set> // for ivar
37 #include <string> // for ivar
38 
39 class vtkIdList;
41 class vtkOpenGLHardwareSupport;
45 class vtkShaderProgram;
46 class vtkStdString;
47 class vtkTexture;
48 class vtkTextureObject;
51 class vtkOpenGLState;
52 
53 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLRenderWindow : public vtkRenderWindow
54 {
55 public:
57  void PrintSelf(ostream& os, vtkIndent indent) override;
58 
62  const char *GetRenderingBackend() override;
63 
65 
68  static void SetGlobalMaximumNumberOfMultiSamples(int val);
69  static int GetGlobalMaximumNumberOfMultiSamples();
71 
73 
76  unsigned char *GetPixelData(int x,int y,int x2,int y2,int front,int right)
77  override;
78  int GetPixelData(int x,int y,int x2,int y2, int front,
79  vtkUnsignedCharArray *data, int right) override;
80  int SetPixelData(int x,int y,int x2,int y2,unsigned char *data,
81  int front, int right) override;
82  int SetPixelData(int x,int y,int x2,int y2,
83  vtkUnsignedCharArray *data, int front, int right)
84  override;
86 
88 
91  float *GetRGBAPixelData(int x,int y,int x2,int y2,int front,int right=0)
92  override;
93  int GetRGBAPixelData(int x,int y,int x2,int y2, int front,
94  vtkFloatArray* data, int right=0) override;
95  int SetRGBAPixelData(int x,int y,int x2,int y2, float *data,
96  int front, int blend=0, int right=0) override;
97  int SetRGBAPixelData(int x,int y,int x2,int y2, vtkFloatArray *data,
98  int front, int blend=0, int right=0) override;
99  void ReleaseRGBAPixelData(float *data) override;
100  unsigned char *GetRGBACharPixelData(int x,int y,int x2,int y2,
101  int front, int right=0) override;
102  int GetRGBACharPixelData(int x,int y,int x2,int y2, int front,
103  vtkUnsignedCharArray *data, int right=0)
104  override;
105  int SetRGBACharPixelData(int x, int y, int x2, int y2,
106  unsigned char *data, int front,
107  int blend=0, int right=0) override;
108  int SetRGBACharPixelData(int x,int y,int x2,int y2,
109  vtkUnsignedCharArray *data, int front,
110  int blend=0,int right=0) override;
112 
114 
117  float *GetZbufferData( int x1, int y1, int x2, int y2 ) override;
118  int GetZbufferData( int x1, int y1, int x2, int y2, float* z ) override;
119  int GetZbufferData( int x1, int y1, int x2, int y2,
120  vtkFloatArray* z ) override;
121  int SetZbufferData( int x1, int y1, int x2, int y2, float *buffer ) override;
122  int SetZbufferData( int x1, int y1, int x2, int y2,
123  vtkFloatArray *buffer ) override;
125 
126 
130  void ActivateTexture(vtkTextureObject *);
131 
135  void DeactivateTexture(vtkTextureObject *);
136 
140  int GetTextureUnitForTexture(vtkTextureObject *);
141 
145  int GetDepthBufferSize() override;
146 
150  bool GetUsingSRGBColorSpace();
151 
156  int GetColorBufferSizes(int *rgba) override;
157 
163  int GetColorBufferInternalFormat(int attachmentPoint);
164 
166 
169  void SetSize(int a[2]) override;
170  void SetSize(int,int) override;
172 
176  virtual void OpenGLInit();
177 
178  // Initialize the state of OpenGL that VTK wants for this window
179  virtual void OpenGLInitState();
180 
181  // Initialize VTK for rendering in a new OpenGL context
182  virtual void OpenGLInitContext();
183 
189  void GetOpenGLVersion(int &major, int &minor);
190 
198  unsigned int GetBackLeftBuffer();
199 
207  unsigned int GetBackRightBuffer();
208 
216  unsigned int GetFrontLeftBuffer();
217 
225  unsigned int GetFrontRightBuffer();
226 
234  unsigned int GetBackBuffer();
235 
243  unsigned int GetFrontBuffer();
244 
248  virtual vtkMTimeType GetContextCreationTime();
249 
251 
254  vtkGetObjectMacro(ShaderCache,vtkOpenGLShaderCache);
256 
258 
261  vtkGetObjectMacro(VBOCache,vtkOpenGLVertexBufferObjectCache);
263 
265 
268  vtkGetMacro(FrameBufferObject, unsigned int);
270 
275  vtkTextureUnitManager *GetTextureUnitManager();
276 
281  void WaitForCompletion() override;
282 
286  virtual void DrawPixels(int x1, int y1, int x2, int y2,
287  int numComponents, int dataType, void *data);
288 
293  virtual void DrawPixels(
294  int dstXmin, int dstYmin, int dstXmax, int dstYmax,
295  int srcXmin, int srcYmin, int srcXmax, int srcYmax,
296  int srcWidth, int srcHeight, int numComponents, int dataType, void *data);
297 
302  virtual void DrawPixels(
303  int srcWidth, int srcHeight, int numComponents, int dataType, void *data);
304 
308  virtual float GetMaximumHardwareLineWidth() {
309  return this->MaximumHardwareLineWidth; };
310 
317  virtual bool IsPointSpriteBugPresent()
318  {
319  return 0;
320  }
321 
327  int GetDefaultTextureInternalFormat(
328  int vtktype, int numComponents,
329  bool needInteger, bool needFloat, bool needSRGB);
330 
337  {
338  return this->OpenGLSupportMessage;
339  }
340 
341  // Create and bind offscreen rendering buffers without destroying the current
342  // OpenGL context. This allows to temporary switch to offscreen rendering
343  // (ie. to make a screenshot even if the window is hidden).
344  // Return if the creation was successful (1) or not (0).
345  // Note: This function requires that the device supports OpenGL framebuffer extension.
346  // The function has no effect if OffScreenRendering is ON.
347  int SetUseOffScreenBuffers(bool offScreen) override;
348  bool GetUseOffScreenBuffers() override;
349 
353  int SupportsOpenGL() override;
354 
358  const char *ReportCapabilities() override;
359 
366  virtual void Initialize(void) {};
367 
368  std::set<vtkGenericOpenGLResourceFreeCallback *> Resources;
369 
371  std::set<vtkGenericOpenGLResourceFreeCallback *>::iterator it
372  = this->Resources.find(cb);
373  if (it == this->Resources.end())
374  {
375  this->Resources.insert(cb);
376  }
377  }
378 
380  std::set<vtkGenericOpenGLResourceFreeCallback *>::iterator it
381  = this->Resources.find(cb);
382  if (it != this->Resources.end())
383  {
384  this->Resources.erase(it);
385  }
386  }
387 
397  virtual void PushContext() { this->MakeCurrent(); }
398  virtual void PopContext() {}
399 
404  bool InitializeFromCurrentContext() override;
405 
413  vtkGetMacro(DefaultFrameBufferId, unsigned int);
414 
424  virtual bool SetSwapControl(int ) { return false; }
425 
426  // Get the state object used to keep track of
427  // OpenGL state
429  return this->State; }
430 
431  // Get a VBO that can be shared by many
432  // It consists of normalized display
433  // coordinates for a quad and tcoords
434  vtkOpenGLBufferObject *GetTQuad2DVBO();
435 
436  // Activate and return thje texture unit for a generic 2d 64x64
437  // float greyscale noise texture ranging from 0 to 1. The texture is
438  // generated using PerlinNoise. This textur eunit will automatically
439  // be deactivated at the end of the render process.
440  int GetNoiseTextureUnit();
441 
446  void StereoUpdate() override;
447 
452  void StereoMidpoint() override;
453 
457  void Render() override;
458 
459 protected:
461  ~vtkOpenGLRenderWindow() override;
462 
465 
467 
468  // used in testing for opengl support
469  // in the SupportsOpenGL() method
473 
474  int TextureInternalFormats[VTK_UNICODE_STRING][3][5];
475  void InitializeTextureInternalFormats();
476 
477  std::map<const vtkTextureObject *, int> TextureResourceIds;
478 
479  virtual int ReadPixels(const vtkRecti& rect, int front, int glFormat, int glType, void* data, int right=0);
480 
490  int CreateHardwareOffScreenWindow(int width, int height);
491 
492  int CreateHardwareOffScreenBuffers(int width, int height, bool bind = false);
493  void BindHardwareOffScreenBuffers();
494 
500  void DestroyHardwareOffScreenWindow();
501 
502  void UnbindHardwareOffScreenBuffers();
503  void DestroyHardwareOffScreenBuffers();
504 
509 
511 
515  unsigned int TextureObjects[4]; // really GLuint
516  unsigned int FrameBufferObject; // really GLuint
517  unsigned int DepthRenderBufferObject; // really GLuint
518  int HardwareBufferSize[2];
521 
525  virtual void CreateAWindow() = 0;
526 
530  virtual void DestroyWindow() = 0;
531 
536  virtual void ReleaseGraphicsResources(vtkRenderWindow *);
537 
541  void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager);
542 
543 
547  void SaveGLState();
548 
552  void RestoreGLState();
553 
554  std::map<std::string, int> GLStateIntegers;
555 
556  unsigned int BackLeftBuffer;
557  unsigned int BackRightBuffer;
558  unsigned int FrontLeftBuffer;
559  unsigned int FrontRightBuffer;
560  unsigned int FrontBuffer;
561  unsigned int BackBuffer;
562  unsigned int DefaultFrameBufferId;
563 
568 
570 
572 
574 
575  bool Initialized; // ensure glewinit has been called
576  bool GlewInitValid; // Did glewInit initialize with a valid state?
577 
579 
581 
582  // used for fast quad rendering
584 
585  // noise texture
587 
588 private:
590  void operator=(const vtkOpenGLRenderWindow&) = delete;
591 };
592 
593 #endif
OpenGL rendering window.
int OwnContext
Flag telling if the context has been created here or was inherited.
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:40
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.
int OffScreenUseFrameBuffer
Flag telling if a framebuffer-based offscreen is currently in use.
virtual int SetUseOffScreenBuffers(bool)
Create and bind offscreen rendering buffers without destroying the current OpenGL context...
virtual int SetRGBAPixelData(int x, int y, int x2, int y2, float *, int front, int blend=0, int right=0)=0
Same as Get/SetPixelData except that the image also contains an alpha component.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:302
manage Shader Programs within a context
int NumberOfFrameBuffers
Variables used by the framebuffer-based offscreen method.
void UnregisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
bool HardwareOffScreenBuffersBind
Variables used by the framebuffer-based offscreen method.
record modification and/or execution time
Definition: vtkTimeStamp.h:35
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:41
vtkTextureUnitManager * TextureUnitManager
std::string GetOpenGLSupportMessage()
Return a message profiding additional details about the results of calling SupportsOpenGL() This can ...
manage vertex buffer objects shared within a context
virtual int GetColorBufferSizes(int *rgba)=0
Get the size of the color buffer.
void Render() override
Ask each renderer owned by this RenderWindow to render its image and synchronize this process...
#define VTK_UNICODE_STRING
Definition: vtkType.h:85
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.
virtual void SetSize(int, int)
Set/Get the size of the window in screen coordinates in pixels.
virtual const char * ReportCapabilities()
Get report of capabilities for the render window.
virtual int SupportsOpenGL()
Does this render window support OpenGL? 0-false, 1-true.
virtual int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char *data, int front, int blend=0, int right=0)=0
Same as Get/SetPixelData except that the image also contains an alpha component.
std::map< const vtkTextureObject *, int > TextureResourceIds
OpenGL state storage.
a simple class to control print indentation
Definition: vtkIndent.h:39
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.
The VertexArrayObject class uses, or emulates, vertex array objects.
virtual void ReleaseRGBAPixelData(float *data)=0
Same as Get/SetPixelData except that the image also contains an alpha component.
vtkTextureObject * DrawPixelsTextureObject
unsigned int FrameBufferObject
Variables used by the framebuffer-based offscreen method.
list of point or cell ids
Definition: vtkIdList.h:36
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
virtual int GetDepthBufferSize()=0
This method should be defined by the subclass.
void RegisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
virtual int SetZbufferData(int x, int y, int x2, int y2, float *z)=0
Set/Get the zbuffer data from the frame buffer.
handles properties associated with a texture map
Definition: vtkTexture.h:71
virtual void StereoUpdate()
Update the system, if needed, due to stereo rendering.
virtual void PushContext()
Ability to push and pop this window&#39;s context as the current context.
virtual unsigned char * GetRGBACharPixelData(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.
vtkTextureObject * NoiseTextureObject
dynamic, self-adjusting array of unsigned char
virtual bool SetSwapControl(int)
Set the number of vertical syncs required between frames.
allocate/free texture units.
vtkOpenGLShaderCache * ShaderCache
abstracts an OpenGL texture object.
create a window for renderers to draw into
virtual bool InitializeFromCurrentContext()
Initialize the render window from the information associated with the currently activated OpenGL cont...
virtual const char * GetRenderingBackend()
What rendering backend has the user requested.
std::set< vtkGenericOpenGLResourceFreeCallback * > Resources
virtual float GetMaximumHardwareLineWidth()
Return the largest line width supported by the hardware.
virtual void StereoMidpoint()
Intermediate method performs operations required between the rendering of the left and right eye...
virtual float * GetZbufferData(int x, int y, int x2, int y2)=0
Set/Get the zbuffer data from the frame buffer.
vtkOpenGLBufferObject * TQuad2DVBO
unsigned int DepthRenderBufferObject
Variables used by the framebuffer-based offscreen method.
OpenGL buffer object.
virtual vtkOpenGLState * GetState()
virtual bool GetUseOffScreenBuffers()
virtual void WaitForCompletion()=0
Block the thread until the actual rendering is finished().
virtual bool IsPointSpriteBugPresent()
Returns true if driver has an EGL/OpenGL bug that makes vtkChartsCoreCxx-TestChartDoubleColors and ot...
std::map< std::string, int > GLStateIntegers
void MakeCurrent() override=0
Attempt to make this window the current graphics context for the calling thread.
vtkOpenGLVertexBufferObjectCache * VBOCache
The ShaderProgram uses one or more Shader objects.