24 #if !defined(CX_VTK_OPENGL2) && !defined(WIN32)
26 #include <vtkOpenGLExtensionManager.h>
27 #include <vtkRenderer.h>
28 #include <vtkShaderProgram2.h>
29 #include <vtkShader2.h>
30 #include <vtkShader2Collection.h>
31 #include <vtkUniformVariables.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkOpenGLRenderWindow.h>
36 #include <OpenGL/glu.h>
87 mVolumeBuffer = buffer;
106 mVolumeBuffer->allocate();
108 mLutBuffer->allocate();
113 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
119 QString fullName = QString(
"%1[%2]").arg(name).arg(mIndex);
128 mVolumeBuffer->bind(mIndex);
130 int texture = 2*mIndex;
131 int lut = 2*mIndex+1;
136 mLutBuffer->bind(mIndex);
137 lutsize = mLutBuffer->getLutSize();
140 vtkUniformVariables* uniforms = shader->GetUniformVariables();
154 hasLoadedExtensions(false)
163 QString TextureSlicePainter::loadShaderFile()
165 QString filepath = mShaderPath +
"/cxOverlay2D_frag.glsl";
169 fp.open(QFile::ReadOnly);
170 QTextStream shaderfile(&fp);
171 return shaderfile.readAll();
175 std::cout <<
"TextureSlicer can't read shaderfile [" << fp.fileName() <<
"]" << std::endl;
182 if (this->LastContext)
190 if (this->Shader != 0)
192 this->Shader->ReleaseGraphicsResources();
197 this->LastContext = 0;
198 this->Superclass::ReleaseGraphicsResources(win);
207 this->LastContext = 0;
208 this->Superclass::PrepareForRendering(renderer, actor);
212 GLint oldTextureUnit;
213 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
215 vtkRenderWindow* renWin = renderer->GetRenderWindow();
216 if (this->LastContext != renWin)
219 hasLoadedExtensions =
false;
221 this->LastContext = renWin;
223 vtkOpenGLRenderWindow *context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
224 if (!hasLoadedExtensions)
230 hasLoadedExtensions =
true;
233 for (
unsigned i = 0; i < this->mElement.size(); ++i)
234 this->mElement[i].initializeRendering();
239 QString shaderSource = this->loadShaderFile();
240 shaderSource = this->replaceShaderSourceMacros(shaderSource);
244 this->buildProgram(shaderSource, context);
247 glPixelStorei(GL_PACK_ALIGNMENT, 1);
248 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
249 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
250 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
252 this->Superclass::PrepareForRendering(renderer, actor);
254 glActiveTexture(oldTextureUnit);
257 QString TextureSlicePainter::replaceShaderSourceMacros(QString shaderSource)
260 int layers = this->mElement.size();
261 shaderSource = shaderSource.replace(
"${LAYERS}", QString(
"%1").arg(layers));
264 QString element =
"\tif (index==%1) return texture1D(lut[%1], idx);\n";
265 QString sampleLutContent;
266 for (
unsigned i=0; i<layers; ++i)
267 sampleLutContent += element.arg(i);
268 shaderSource = shaderSource.replace(
"${SAMPLE_LUT_CONTENT}", sampleLutContent);
275 bool forceCompileOnly)
282 GLint oldTextureUnit;
283 glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);
285 int layers = this->mElement.size();
286 this->Shader->GetUniformVariables()->SetUniformi(
"layers", 1, &layers);
288 for (
unsigned i = 0; i < this->mElement.size(); ++i)
290 this->mElement[i].eachRenderInternal(this->Shader);
295 if (!this->Shader->IsValid())
297 vtkErrorMacro(<<
" validation of the program failed: "<< this->Shader->GetLastValidateLog());
300 this->Superclass::RenderInternal(renderer, actor, typeflags, forceCompileOnly);
302 this->Shader->Restore();
304 glActiveTexture(oldTextureUnit);
309 return !this->mElement.empty();
314 bool loaded = mgr->LoadSupportedExtension(
cstring_cast(
id));
316 std::cout <<
"TextureSlicePainter Error: GL extension " +
id +
" not found" << std::endl;
329 this->safeIndex(index).
SetBuffer(buffer);
334 this->safeIndex(index).
SetBuffer(buffer);
352 if ((
int)mElement.size() <= index)
354 mElement.resize(index+1);
357 return mElement[index];
360 void TextureSlicePainter::buildProgram(QString shaderSource, vtkOpenGLRenderWindow* renderWindow)
363 pgm->SetContext(renderWindow);
366 s2->SetType(VTK_SHADER_TYPE_FRAGMENT);
368 s2->SetContext(pgm->GetContext());
369 pgm->GetShaders()->AddItem(s2);
373 if (pgm->GetLastBuildStatus() != VTK_SHADER_PROGRAM2_LINK_SUCCEEDED)
375 vtkErrorMacro(
"Pass Two failed.");
384 if (this->Shader != 0)
386 this->Shader->ReleaseGraphicsResources();
396 #endif //CX_VTK_OPENGL2