45 #if !defined(CX_VTK_OPENGL2) && !defined(WIN32)
47 #include <vtkOpenGLExtensionManager.h>
48 #include <vtkRenderer.h>
49 #include <vtkShaderProgram2.h>
50 #include <vtkShader2.h>
51 #include <vtkShader2Collection.h>
52 #include <vtkUniformVariables.h>
53 #include <vtkObjectFactory.h>
54 #include <vtkOpenGLRenderWindow.h>
57 #include <OpenGL/glu.h>
61 #define GL_GLEXT_PROTOTYPES
108 mVolumeBuffer = buffer;
127 mVolumeBuffer->allocate();
129 mLutBuffer->allocate();
134 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
140 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
149 mVolumeBuffer->bind(mIndex);
151 int texture = 2*mIndex;
152 int lut = 2*mIndex+1;
157 mLutBuffer->bind(mIndex);
158 lutsize = mLutBuffer->getLutSize();
161 vtkUniformVariables* uniforms = shader->GetUniformVariables();
175 hasLoadedExtensions(false)
184 QString TextureSlicePainter::loadShaderFile()
186 QString filepath = mShaderPath +
"/cxOverlay2D_frag.glsl";
190 fp.open(QFile::ReadOnly);
191 QTextStream shaderfile(&fp);
192 return shaderfile.readAll();
196 std::cout <<
"TextureSlicer can't read shaderfile [" << fp.fileName() <<
"]" << std::endl;
203 if (this->LastContext)
211 if (this->Shader != 0)
213 this->Shader->ReleaseGraphicsResources();
218 this->LastContext = 0;
219 this->Superclass::ReleaseGraphicsResources(win);
228 this->LastContext = 0;
229 this->Superclass::PrepareForRendering(renderer, actor);
233 GLint oldTextureUnit;
234 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
236 vtkRenderWindow* renWin = renderer->GetRenderWindow();
237 if (this->LastContext != renWin)
240 hasLoadedExtensions =
false;
242 this->LastContext = renWin;
244 vtkOpenGLRenderWindow *context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
245 if (!hasLoadedExtensions)
251 hasLoadedExtensions =
true;
254 for (
unsigned i = 0; i < this->mElement.size(); ++i)
255 this->mElement[i].initializeRendering();
260 QString shaderSource = this->loadShaderFile();
261 shaderSource = this->replaceShaderSourceMacros(shaderSource);
265 this->buildProgram(shaderSource, context);
268 glPixelStorei(GL_PACK_ALIGNMENT, 1);
269 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
270 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
271 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
273 this->Superclass::PrepareForRendering(renderer, actor);
275 glActiveTexture(oldTextureUnit);
278 QString TextureSlicePainter::replaceShaderSourceMacros(QString shaderSource)
281 int layers = this->mElement.size();
282 shaderSource = shaderSource.replace(
"${LAYERS}", QString(
"%1").arg(layers));
285 QString element =
"\tif (index==%1) return texture1D(lut[%1], idx);\n";
286 QString sampleLutContent;
287 for (
unsigned i=0; i<layers; ++i)
288 sampleLutContent += element.arg(i);
289 shaderSource = shaderSource.replace(
"${SAMPLE_LUT_CONTENT}", sampleLutContent);
296 bool forceCompileOnly)
303 GLint oldTextureUnit;
304 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
306 int layers = this->mElement.size();
307 this->Shader->GetUniformVariables()->SetUniformi(
"layers", 1, &layers);
309 for (
unsigned i = 0; i < this->mElement.size(); ++i)
311 this->mElement[i].eachRenderInternal(this->Shader);
316 if (!this->Shader->IsValid())
318 vtkErrorMacro(<<
" validation of the program failed: "<< this->Shader->GetLastValidateLog());
321 this->Superclass::RenderInternal(renderer, actor, typeflags, forceCompileOnly);
323 this->Shader->Restore();
325 glActiveTexture(oldTextureUnit);
330 return !this->mElement.empty();
335 bool loaded = mgr->LoadSupportedExtension(
cstring_cast(
id));
337 std::cout <<
"TextureSlicePainter Error: GL extension " +
id +
" not found" << std::endl;
350 this->safeIndex(index).
SetBuffer(buffer);
355 this->safeIndex(index).
SetBuffer(buffer);
373 if ((
int)mElement.size() <= index)
375 mElement.resize(index+1);
378 return mElement[index];
381 void TextureSlicePainter::buildProgram(QString shaderSource, vtkOpenGLRenderWindow* renderWindow)
384 pgm->SetContext(renderWindow);
387 s2->SetType(VTK_SHADER_TYPE_FRAGMENT);
389 s2->SetContext(pgm->GetContext());
390 pgm->GetShaders()->AddItem(s2);
394 if (pgm->GetLastBuildStatus() != VTK_SHADER_PROGRAM2_LINK_SUCCEEDED)
396 vtkErrorMacro(
"Pass Two failed.");
405 if (this->Shader != 0)
407 this->Shader->ReleaseGraphicsResources();
417 #endif //CX_VTK_OPENGL2
vtkSmartPointer< class vtkShaderProgram2 > vtkShaderProgram2Ptr
bool CanRender(vtkRenderer *, vtkActor *)
void PrintSelf(ostream &os, vtkIndent indent)
void SetLutBuffer(int index, GPUImageLutBufferPtr buffer)
~SingleVolumePainterHelper()
void setUniformiArray(vtkUniformVariables *uniforms, QString name, int val)
void initializeRendering()
void eachRenderInternal(vtkSmartPointer< vtkShaderProgram2 > shader)
void SetColorAttribute(float window, float level, float llr, float alpha)
void ClearGraphicsResources()
cstring_cast_Placeholder cstring_cast(const T &val)
void setShaderPath(QString path)
SingleVolumePainterHelper()
virtual ~TextureSlicePainter()
void releaseGraphicsResources(int index)
virtual void PrepareForRendering(vtkRenderer *, vtkActor *)
static bool LoadRequiredExtension(vtkOpenGLExtensionManager *mgr, QString id)
vtkStandardNewMacro(TextureSlicePainter)
static bool LoadRequiredExtensions(vtkOpenGLExtensionManager *mgr)
void SetVolumeBuffer(int index, GPUImageDataBufferPtr buffer)
void setUniformfArray(vtkUniformVariables *uniforms, QString name, float val)
boost::shared_ptr< class GPUImageDataBuffer > GPUImageDataBufferPtr
void RenderInternal(vtkRenderer *renderer, vtkActor *actor, unsigned long typeflags, bool forceCompileOnly)
boost::shared_ptr< class GPUImageLutBuffer > GPUImageLutBufferPtr
vtkSmartPointer< class vtkShader2 > vtkShader2Ptr
void SetBuffer(GPUImageDataBufferPtr buffer)
void SetColorAttribute(int index, float window, float level, float llr, float alpha)
virtual void ReleaseGraphicsResources(vtkWindow *)
#define report_gl_error()