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
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:34
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:32
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:35
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:33
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:30
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:65
virtual void StereoUpdate()
Update the system, if needed, due to stereo rendering.
virtual void PushContext()
Ability to push and pop this window'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.
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.