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 =========================================================================*/
15 // .NAME vtkOpenGLRenderWindow - OpenGL rendering window
16 // .SECTION Description
17 // vtkOpenGLRenderWindow is a concrete implementation of the abstract class
18 // vtkRenderWindow. vtkOpenGLRenderer interfaces to the OpenGL graphics
19 // library. Application programmers should normally use vtkRenderWindow
20 // instead of the OpenGL specific version.
21 
22 #ifndef vtkOpenGLRenderWindow_h
23 #define vtkOpenGLRenderWindow_h
24 
25 #include "vtkRenderingOpenGL2Module.h" // For export macro
26 #include "vtkRenderWindow.h"
27 #include <map> // for ivar
28 
29 #include "vtk_glew.h" // Needed for GLuint.
30 
31 class vtkIdList;
32 class vtkOpenGLHardwareSupport;
33 class vtkTextureUnitManager;
34 class vtkOpenGLShaderCache;
35 class vtkStdString;
36 class vtkTexture;
37 class vtkTextureObject;
38 class vtkShaderProgram;
39 
40 namespace vtkgl
41 {
42 class VertexArrayObject;
43 }
44 
45 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLRenderWindow : public vtkRenderWindow
46 {
47 public:
48   vtkTypeMacro(vtkOpenGLRenderWindow, vtkRenderWindow);
49   void PrintSelf(ostream& os, vtkIndent indent);
50 
51   // Description:
52   // Set/Get the maximum number of multisamples
53   static void SetGlobalMaximumNumberOfMultiSamples(int val);
54   static int  GetGlobalMaximumNumberOfMultiSamples();
55 
56   // Description:
57   // Update system if needed due to stereo rendering.
58   virtual void StereoUpdate();
59 
60   // Description:
61   // Set/Get the pixel data of an image, transmitted as RGBRGB...
62   virtual unsigned char *GetPixelData(int x,int y,int x2,int y2,int front);
63   virtual int GetPixelData(int x,int y,int x2,int y2, int front,
64                            vtkUnsignedCharArray *data);
65   virtual int SetPixelData(int x,int y,int x2,int y2,unsigned char *data,
66                            int front);
67   virtual int SetPixelData(int x,int y,int x2,int y2,
68                            vtkUnsignedCharArray *data, int front);
69 
70   // Description:
71   // Set/Get the pixel data of an image, transmitted as RGBARGBA...
72   virtual float *GetRGBAPixelData(int x,int y,int x2,int y2,int front);
73   virtual int GetRGBAPixelData(int x,int y,int x2,int y2, int front,
74                                vtkFloatArray* data);
75   virtual int SetRGBAPixelData(int x,int y,int x2,int y2, float *data,
76                                int front, int blend=0);
77   virtual int SetRGBAPixelData(int x,int y,int x2,int y2, vtkFloatArray *data,
78                                int front, int blend=0);
79   virtual void ReleaseRGBAPixelData(float *data);
80   virtual unsigned char *GetRGBACharPixelData(int x,int y,int x2,int y2,
81                                               int front);
82   virtual int GetRGBACharPixelData(int x,int y,int x2,int y2, int front,
83                                    vtkUnsignedCharArray *data);
84   virtual int SetRGBACharPixelData(int x, int y, int x2, int y2,
85                                    unsigned char *data, int front,
86                                    int blend=0);
87   virtual int SetRGBACharPixelData(int x,int y,int x2,int y2,
88                                    vtkUnsignedCharArray *data, int front,
89                                    int blend=0);
90 
91   // Description:
92   // Set/Get the zbuffer data from an image
93   virtual float *GetZbufferData( int x1, int y1, int x2, int y2 );
94   virtual int GetZbufferData( int x1, int y1, int x2, int y2, float* z );
95   virtual int GetZbufferData( int x1, int y1, int x2, int y2,
96                               vtkFloatArray* z );
97   virtual int SetZbufferData( int x1, int y1, int x2, int y2, float *buffer );
98   virtual int SetZbufferData( int x1, int y1, int x2, int y2,
99                               vtkFloatArray *buffer );
100 
101 
102   // Description:
103   // Activate a texture unit for this texture
104   void ActivateTexture(vtkTextureObject *);
105 
106   // Description:
107   // Deactive a previously activated texture
108   void DeactivateTexture(vtkTextureObject *);
109 
110   // Description:
111   // Get the texture unit for a given texture object
112   int GetTextureUnitForTexture(vtkTextureObject *);
113 
114   // Description:
115   // Get the size of the depth buffer.
116   int GetDepthBufferSize();
117 
118   // Description:
119   // Get the size of the color buffer.
120   // Returns 0 if not able to determine otherwise sets R G B and A into buffer.
121   int GetColorBufferSizes(int *rgba);
122 
123   // Description:
124   // Initialize OpenGL for this window.
125   virtual void OpenGLInit();
126 
127   // Initialize the state of OpenGL that VTK wants for this window
128   virtual void OpenGLInitState();
129 
130   // Initialize VTK for rendering in a new OpenGL context
131   virtual void OpenGLInitContext();
132 
133   // Description::
134   // Get if the context includes opengl core profile 3.2 support
135   static bool GetContextSupportsOpenGL32();
136   void SetContextSupportsOpenGL32(bool val);
137 
138   // Description:
139   // Return the OpenGL name of the back left buffer.
140   // It is GL_BACK_LEFT if GL is bound to the window-system-provided
141   // framebuffer. It is vtkgl::COLOR_ATTACHMENT0_EXT if GL is bound to an
142   // application-created framebuffer object (GPU-based offscreen rendering)
143   // It is used by vtkOpenGLCamera.
144   unsigned int GetBackLeftBuffer();
145 
146   // Description:
147   // Return the OpenGL name of the back right buffer.
148   // It is GL_BACK_RIGHT if GL is bound to the window-system-provided
149   // framebuffer. It is vtkgl::COLOR_ATTACHMENT0_EXT+1 if GL is bound to an
150   // application-created framebuffer object (GPU-based offscreen rendering)
151   // It is used by vtkOpenGLCamera.
152   unsigned int GetBackRightBuffer();
153 
154   // Description:
155   // Return the OpenGL name of the front left buffer.
156   // It is GL_FRONT_LEFT if GL is bound to the window-system-provided
157   // framebuffer. It is vtkgl::COLOR_ATTACHMENT0_EXT if GL is bound to an
158   // application-created framebuffer object (GPU-based offscreen rendering)
159   // It is used by vtkOpenGLCamera.
160   unsigned int GetFrontLeftBuffer();
161 
162   // Description:
163   // Return the OpenGL name of the front right buffer.
164   // It is GL_FRONT_RIGHT if GL is bound to the window-system-provided
165   // framebuffer. It is vtkgl::COLOR_ATTACHMENT0_EXT+1 if GL is bound to an
166   // application-created framebuffer object (GPU-based offscreen rendering)
167   // It is used by vtkOpenGLCamera.
168   unsigned int GetFrontRightBuffer();
169 
170   // Description:
171   // Return the OpenGL name of the back left buffer.
172   // It is GL_BACK if GL is bound to the window-system-provided
173   // framebuffer. It is vtkgl::COLOR_ATTACHMENT0_EXT if GL is bound to an
174   // application-created framebuffer object (GPU-based offscreen rendering)
175   // It is used by vtkOpenGLCamera.
176   unsigned int GetBackBuffer();
177 
178   // Description:
179   // Return the OpenGL name of the front left buffer.
180   // It is GL_FRONT if GL is bound to the window-system-provided
181   // framebuffer. It is vtkgl::COLOR_ATTACHMENT0_EXT if GL is bound to an
182   // application-created framebuffer object (GPU-based offscreen rendering)
183   // It is used by vtkOpenGLCamera.
184   unsigned int GetFrontBuffer();
185 
186   // Description:
187   // @deprecated Replaced by
188   // vtkOpenGLCheckErrorMacro
189   VTK_LEGACY(virtual void CheckGraphicError());
190   VTK_LEGACY(virtual int HasGraphicError());
191   VTK_LEGACY(virtual const char *GetLastGraphicErrorString());
192 
193   // Description:
194   // Get the time when the OpenGL context was created.
195   virtual unsigned long GetContextCreationTime();
196 
197   // Description:
198   // Returns an Shader Cache object
199   vtkGetObjectMacro(ShaderCache,vtkOpenGLShaderCache);
200 
201   // Description:
202   // Returns its texture unit manager object. A new one will be created if one
203   // hasn't already been set up.
204   vtkTextureUnitManager *GetTextureUnitManager();
205 
206   // Description:
207   // Block the thread until the actual rendering is finished().
208   // Useful for measurement only.
209   virtual void WaitForCompletion();
210 
211   // Description:
212   // Helper function that draws a quad on the screen
213   // at the specified vertex coordinates and if
214   // tcoords are not NULL with the specified
215   // texture coordinates.
216   static void RenderQuad(
217     float *verts, float *tcoords,
218     vtkShaderProgram *program, vtkgl::VertexArrayObject *vao);
219 
220   // Description:
221   // Replacement for the old glDrawPixels function
222   virtual void DrawPixels(int x1, int y1, int x2, int y2,
223               int numComponents, int dataType, void *data);
224 
225   // Description:
226   // Replacement for the old glDrawPixels function, but it allows
227   // for scaling the data and using only part of the texture
228   virtual void DrawPixels(
229     int dstXmin, int dstYmin, int dstXmax, int dstYmax,
230     int srcXmin, int srcYmin, int srcXmax, int srcYmax,
231     int srcWidth, int srcHeight, int numComponents, int dataType, void *data);
232 
233   // Description:
234   // Replacement for the old glDrawPixels function.  This simple version draws all
235   // the data to the entire current viewport scaling as needed.
236   virtual void DrawPixels(
237     int srcWidth, int srcHeight, int numComponents, int dataType, void *data);
238 
239 protected:
240   vtkOpenGLRenderWindow();
241   ~vtkOpenGLRenderWindow();
242 
243   vtkOpenGLShaderCache *ShaderCache;
244 
245   long OldMonitorSetting;
246 
247   std::map<const vtkTextureObject *, int> TextureResourceIds;
248 
249   int GetPixelData(int x, int y, int x2, int y2, int front, unsigned char* data);
250   int GetRGBAPixelData(int x, int y, int x2, int y2, int front, float* data);
251   int GetRGBACharPixelData(int x, int y, int x2, int y2, int front,
252                            unsigned char* data);
253 
254   // Description:
255   // Create an offScreen window based on OpenGL framebuffer extension.
256   // Return if the creation was successful or not.
257   // \pre positive_width: width>0
258   // \pre positive_height: height>0
259   // \pre not_initialized: !OffScreenUseFrameBuffer
260   // \post valid_result: (result==0 || result==1)
261   //                     && (result implies OffScreenUseFrameBuffer)
262   int CreateHardwareOffScreenWindow(int width, int height);
263 
264   // Description:
265   // Destroy an offscreen window based on OpenGL framebuffer extension.
266   // \pre initialized: OffScreenUseFrameBuffer
267   // \post destroyed: !OffScreenUseFrameBuffer
268   void DestroyHardwareOffScreenWindow();
269 
270   // Description:
271   // Flag telling if a framebuffer-based offscreen is currently in use.
272   int OffScreenUseFrameBuffer;
273 
274   // Description:
275   // Variables used by the framebuffer-based offscreen method.
276   int NumberOfFrameBuffers;
277   unsigned int TextureObjects[4]; // really GLuint
278   unsigned int FrameBufferObject; // really GLuint
279   unsigned int DepthRenderBufferObject; // really GLuint
280 
281   // Description:
282   // Create a not-off-screen window.
283   virtual void CreateAWindow() = 0;
284 
285   // Description:
286   // Destroy a not-off-screen window.
287   virtual void DestroyWindow() = 0;
288 
289   // Description:
290   // Free up any graphics resources associated with this window
291   virtual void ReleaseGraphicsResources();
292 
293   // Description:
294   // Set the texture unit manager.
295   void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager);
296 
297   unsigned int BackLeftBuffer;
298   unsigned int BackRightBuffer;
299   unsigned int FrontLeftBuffer;
300   unsigned int FrontRightBuffer;
301   unsigned int FrontBuffer;
302   unsigned int BackBuffer;
303 
304   #ifndef VTK_LEGACY_REMOVE
305   // Description:
306   // @deprecated Replaced by
307   // vtkOpenGLCheckErrorMacro
308   unsigned int LastGraphicError;
309   #endif
310 
311   // Description:
312   // Flag telling if the context has been created here or was inherited.
313   int OwnContext;
314 
315   vtkTimeStamp ContextCreationTime;
316 
317   vtkTextureUnitManager *TextureUnitManager;
318 
319   vtkTextureObject *DrawPixelsTextureObject;
320 
321   bool Initialized; // ensure glewinit has been called
322 
323 private:
324   vtkOpenGLRenderWindow(const vtkOpenGLRenderWindow&);  // Not implemented.
325   void operator=(const vtkOpenGLRenderWindow&);  // Not implemented.
326 };
327 
328 #endif
329