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
107 mVolumeBuffer = buffer;
126 mVolumeBuffer->allocate();
128 mLutBuffer->allocate();
133 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
139 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
148 mVolumeBuffer->bind(mIndex);
150 int texture = 2*mIndex;
151 int lut = 2*mIndex+1;
156 mLutBuffer->bind(mIndex);
157 lutsize = mLutBuffer->getLutSize();
160 vtkUniformVariables* uniforms = shader->GetUniformVariables();
174 hasLoadedExtensions(false)
183 QString TextureSlicePainter::loadShaderFile()
185 QString filepath = mShaderPath +
"/cxOverlay2D_frag.glsl";
189 fp.open(QFile::ReadOnly);
190 QTextStream shaderfile(&fp);
191 return shaderfile.readAll();
195 std::cout <<
"TextureSlicer can't read shaderfile [" << fp.fileName() <<
"]" << std::endl;
202 if (this->LastContext)
210 if (this->Shader != 0)
212 this->Shader->ReleaseGraphicsResources();
217 this->LastContext = 0;
218 this->Superclass::ReleaseGraphicsResources(win);
227 this->LastContext = 0;
228 this->Superclass::PrepareForRendering(renderer, actor);
232 GLint oldTextureUnit;
233 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
235 vtkRenderWindow* renWin = renderer->GetRenderWindow();
236 if (this->LastContext != renWin)
239 hasLoadedExtensions =
false;
241 this->LastContext = renWin;
243 vtkOpenGLRenderWindow *context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
244 if (!hasLoadedExtensions)
250 hasLoadedExtensions =
true;
253 for (
unsigned i = 0; i < this->mElement.size(); ++i)
254 this->mElement[i].initializeRendering();
259 QString shaderSource = this->loadShaderFile();
260 shaderSource = this->replaceShaderSourceMacros(shaderSource);
264 this->buildProgram(shaderSource, context);
267 glPixelStorei(GL_PACK_ALIGNMENT, 1);
268 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
269 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
270 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
272 this->Superclass::PrepareForRendering(renderer, actor);
274 glActiveTexture(oldTextureUnit);
277 QString TextureSlicePainter::replaceShaderSourceMacros(QString shaderSource)
280 int layers = this->mElement.size();
281 shaderSource = shaderSource.replace(
"${LAYERS}", QString(
"%1").arg(layers));
284 QString element =
"\tif (index==%1) return texture1D(lut[%1], idx);\n";
285 QString sampleLutContent;
286 for (
unsigned i=0; i<layers; ++i)
287 sampleLutContent += element.arg(i);
288 shaderSource = shaderSource.replace(
"${SAMPLE_LUT_CONTENT}", sampleLutContent);
295 bool forceCompileOnly)
302 GLint oldTextureUnit;
303 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
305 int layers = this->mElement.size();
306 this->Shader->GetUniformVariables()->SetUniformi(
"layers", 1, &layers);
308 for (
unsigned i = 0; i < this->mElement.size(); ++i)
310 this->mElement[i].eachRenderInternal(this->Shader);
315 if (!this->Shader->IsValid())
317 vtkErrorMacro(<<
" validation of the program failed: "<< this->Shader->GetLastValidateLog());
320 this->Superclass::RenderInternal(renderer, actor, typeflags, forceCompileOnly);
322 this->Shader->Restore();
324 glActiveTexture(oldTextureUnit);
329 return !this->mElement.empty();
334 bool loaded = mgr->LoadSupportedExtension(
cstring_cast(
id));
336 std::cout <<
"TextureSlicePainter Error: GL extension " +
id +
" not found" << std::endl;
349 this->safeIndex(index).
SetBuffer(buffer);
354 this->safeIndex(index).
SetBuffer(buffer);
372 if ((
int)mElement.size() <= index)
374 mElement.resize(index+1);
377 return mElement[index];
380 void TextureSlicePainter::buildProgram(QString shaderSource, vtkOpenGLRenderWindow* renderWindow)
383 pgm->SetContext(renderWindow);
386 s2->SetType(VTK_SHADER_TYPE_FRAGMENT);
388 s2->SetContext(pgm->GetContext());
389 pgm->GetShaders()->AddItem(s2);
393 if (pgm->GetLastBuildStatus() != VTK_SHADER_PROGRAM2_LINK_SUCCEEDED)
395 vtkErrorMacro(
"Pass Two failed.");
404 if (this->Shader != 0)
406 this->Shader->ReleaseGraphicsResources();
416 #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()