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>
58 #include <OpenGL/glu.h>
106 mVolumeBuffer = buffer;
125 mVolumeBuffer->allocate();
127 mLutBuffer->allocate();
132 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
138 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
147 mVolumeBuffer->bind(mIndex);
149 int texture = 2*mIndex;
150 int lut = 2*mIndex+1;
155 mLutBuffer->bind(mIndex);
156 lutsize = mLutBuffer->getLutSize();
159 vtkUniformVariables* uniforms = shader->GetUniformVariables();
173 hasLoadedExtensions(false)
182 QString TextureSlicePainter::loadShaderFile()
184 QString filepath = mShaderPath +
"/cxOverlay2D_frag.glsl";
188 fp.open(QFile::ReadOnly);
189 QTextStream shaderfile(&fp);
190 return shaderfile.readAll();
194 std::cout <<
"TextureSlicer can't read shaderfile [" << fp.fileName() <<
"]" << std::endl;
201 if (this->LastContext)
209 if (this->Shader != 0)
211 this->Shader->ReleaseGraphicsResources();
216 this->LastContext = 0;
217 this->Superclass::ReleaseGraphicsResources(win);
226 this->LastContext = 0;
227 this->Superclass::PrepareForRendering(renderer, actor);
231 GLint oldTextureUnit;
232 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
234 vtkRenderWindow* renWin = renderer->GetRenderWindow();
235 if (this->LastContext != renWin)
238 hasLoadedExtensions =
false;
240 this->LastContext = renWin;
242 vtkOpenGLRenderWindow *context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
243 if (!hasLoadedExtensions)
249 hasLoadedExtensions =
true;
252 for (
unsigned i = 0; i < this->mElement.size(); ++i)
253 this->mElement[i].initializeRendering();
258 QString shaderSource = this->loadShaderFile();
259 shaderSource = this->replaceShaderSourceMacros(shaderSource);
263 this->buildProgram(shaderSource, context);
266 glPixelStorei(GL_PACK_ALIGNMENT, 1);
267 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
268 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
269 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
271 this->Superclass::PrepareForRendering(renderer, actor);
273 glActiveTexture(oldTextureUnit);
276 QString TextureSlicePainter::replaceShaderSourceMacros(QString shaderSource)
279 int layers = this->mElement.size();
280 shaderSource = shaderSource.replace(
"${LAYERS}", QString(
"%1").arg(layers));
283 QString element =
"\tif (index==%1) return texture1D(lut[%1], idx);\n";
284 QString sampleLutContent;
285 for (
unsigned i=0; i<layers; ++i)
286 sampleLutContent += element.arg(i);
287 shaderSource = shaderSource.replace(
"${SAMPLE_LUT_CONTENT}", sampleLutContent);
294 bool forceCompileOnly)
301 GLint oldTextureUnit;
302 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
304 int layers = this->mElement.size();
305 this->Shader->GetUniformVariables()->SetUniformi(
"layers", 1, &layers);
307 for (
unsigned i = 0; i < this->mElement.size(); ++i)
309 this->mElement[i].eachRenderInternal(this->Shader);
314 if (!this->Shader->IsValid())
316 vtkErrorMacro(<<
" validation of the program failed: "<< this->Shader->GetLastValidateLog());
319 this->Superclass::RenderInternal(renderer, actor, typeflags, forceCompileOnly);
321 this->Shader->Restore();
323 glActiveTexture(oldTextureUnit);
328 return !this->mElement.empty();
333 bool loaded = mgr->LoadSupportedExtension(
cstring_cast(
id));
335 std::cout <<
"TextureSlicePainter Error: GL extension " +
id +
" not found" << std::endl;
348 this->safeIndex(index).
SetBuffer(buffer);
353 this->safeIndex(index).
SetBuffer(buffer);
371 if ((
int)mElement.size() <= index)
373 mElement.resize(index+1);
376 return mElement[index];
379 void TextureSlicePainter::buildProgram(QString shaderSource, vtkOpenGLRenderWindow* renderWindow)
382 pgm->SetContext(renderWindow);
385 s2->SetType(VTK_SHADER_TYPE_FRAGMENT);
387 s2->SetContext(pgm->GetContext());
388 pgm->GetShaders()->AddItem(s2);
392 if (pgm->GetLastBuildStatus() != VTK_SHADER_PROGRAM2_LINK_SUCCEEDED)
394 vtkErrorMacro(
"Pass Two failed.");
403 if (this->Shader != 0)
405 this->Shader->ReleaseGraphicsResources();
415 #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()