Slicer  4.10
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkOpenGLShaderComputation.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: $RCSfile: vtkOpenGLShaderComputation.h,v $
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 vtkOpenGLShaderComputation - OpenGL actor
16 // .SECTION Description
17 // vtkOpenGLShaderComputation is a way to perform GPU computations on vtk data.
18 // vtkOpenGLShaderComputation interfaces to the OpenGL rendering library.
19 
20 #ifndef __vtkOpenGLShaderComputation_h
21 #define __vtkOpenGLShaderComputation_h
22 
23 // VTK includes
24 //#include "vtk_glew.h"
25 #include "vtkAddon.h"
26 #include "vtkImageData.h"
27 #include "vtkRenderWindow.h"
28 #include "vtkVariant.h"
29 
30 // STD includes
31 #include <map>
32 
33 class VTK_ADDON_EXPORT vtkOpenGLShaderComputation : public vtkObject
34 {
35 protected:
36 
37 public:
38  static vtkOpenGLShaderComputation *New();
39  vtkTypeMacro(vtkOpenGLShaderComputation,vtkObject);
40  virtual void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
41 
42  // Description:
43  // Loads the required extensions
44  void Initialize(vtkRenderWindow *renderWindow);
45 
46  // Description:
47  // Checks the framebuffer and prints error if bad.
48  // Return true if framebuffer is complete.
49  bool FramebufferComplete();
50 
51  // Description:
52  // Make this the target for OpenGL operations
53  void MakeCurrent();
54 
55  // Description:
56  // Rebuild the shader program if needed
57  bool UpdateProgram();
58 
59  // Description:
60  // Manage the OpenGL offscreen rendering framebuffer for computing
61  // Select this mode to render into a buffer that matches the ResultImageData
62  // and can be read back with ReadResult. Otherwise use
63  // vtkOpenGLTextureImage::AttachAsDrawTarget to set a texture
64  // as the draw target.
65  bool AcquireResultRenderbuffer();
66  void ReleaseResultRenderbuffer();
67 
68  // Description:
69  // Perform the actual computation
70  // Updates the program if needed and then
71  // renders to the current framebuffer configuration
72  // Slice will be passed as a uniform float
73  void Compute(float slice=0.);
74 
75  // Description:
76  // Add a uniform value TODO: support types other than float
77  void SetUniform(std::string name, float uniform);
78 
79  // Description:
80  // Copy the framebuffer pixels into the result image
81  void ReadResult();
82 
83  // Description:
84  // The strings defining the shaders
85  vtkGetStringMacro(VertexShaderSource);
86  vtkSetStringMacro(VertexShaderSource);
87  vtkGetStringMacro(FragmentShaderSource);
88  vtkSetStringMacro(FragmentShaderSource);
89 
90  // Description:
91  // The results of the computation.
92  // Must be set with the desired dimensions before calling Compute.
93  vtkGetObjectMacro(ResultImageData, vtkImageData);
94  vtkSetObjectMacro(ResultImageData, vtkImageData);
95 
96  // Description:
97  // Used internally to manage OpenGL context and extensions
98  vtkGetObjectMacro(RenderWindow, vtkRenderWindow);
99  vtkSetObjectMacro(RenderWindow, vtkRenderWindow);
100 
101  // Description:
102  // Has the context been set up with a render window?
103  vtkGetMacro(Initialized, bool);
104 
105  // Description:
106  // Has there been an error since the class was created.
107  // After an error, generally the cleanest thing to do is delete the class,
108  // and create a new instance.
109  vtkGetMacro(ErrorOccurred, bool);
110 
111 protected:
114 
115 private:
116  vtkOpenGLShaderComputation(const vtkOpenGLShaderComputation&); // Not implemented.
117  void operator=(const vtkOpenGLShaderComputation&); // Not implemented.
118 
119  bool Initialized;
120  bool ErrorOccurred;
121  char *VertexShaderSource;
122  char *FragmentShaderSource;
123  vtkImageData *ResultImageData;
124 
125  vtkTypeUInt32 ProgramObject; // vtkTypeUInt32 same as GLuint: https://www.opengl.org/wiki/OpenGL_Type
126  unsigned long ProgramObjectMTime;
127  vtkTypeUInt32 FramebufferID;
128  vtkTypeUInt32 ColorRenderbufferID;
129  vtkTypeUInt32 DepthRenderbufferID;
130 
131  vtkRenderWindow *RenderWindow;
132 
133  std::map<std::string, vtkVariant> Uniforms;
134 };
135 
136 #endif