14 #include <vtkShaderProgram.h>
15 #include <vtkShader.h>
16 #include <vtkOpenGLHelper.h>
17 #include <vtkOpenGLVertexArrayObject.h>
18 #include <vtkOpenGLBufferObject.h>
19 #include <vtkTextureObject.h>
20 #include <vtkOpenGLIndexBufferObject.h>
21 #include <vtkOpenGLRenderWindow.h>
52 void ShaderCallback::printDebugInfo(vtkOpenGLHelper *OpenGLHelper)
59 vtkShaderProgram *program = OpenGLHelper->Program;
60 std::cout <<
"Program is compiled? " << program->GetCompiled() << std::endl;
61 std::cout <<
"Program is bound? " << program->isBound() << std::endl;
62 std::cout <<
"IBO index count " << OpenGLHelper->IBO->IndexCount << std::endl;
63 std::string vertexshader = program->GetVertexShader()->GetSource();
64 std::cout <<
"Vertexshader:\n " << vertexshader << std::endl;
65 std::string fragmentshader = program->GetFragmentShader()->GetSource();
66 std::cout <<
"Fragmentshader:\n " << fragmentshader << std::endl;
74 vtkOpenGLHelper *OpenGLHelper =
reinterpret_cast<vtkOpenGLHelper*
>(cbo);
76 if(!OpenGLHelper || !OpenGLHelper->VAO || !OpenGLHelper->Program)
82 if(eventId == vtkCommand::UpdateShaderEvent)
91 if(textures_to_add != 0)
93 this->bindFSOutputVariable(OpenGLHelper->Program);
99 OpenGLHelper->VAO->Bind();
102 for(
int i=0; i< textures_to_add; ++i)
108 if(texture_coordinates)
110 if(!texture_coordinates->Bind())
115 this->addArrayToAttributeArray(OpenGLHelper->Program, texture_coordinates, VS_In_Vec3_TextureCoordinate, i);
130 this->addUniformiArray(OpenGLHelper->Program, getVectorNameFromName(FS_Uniform_3DTexture_Volume, i), texture->GetTextureUnit());
145 this->addUniformiArray(OpenGLHelper->Program, getVectorNameFromName(FS_Uniform_1DTexture_LUT, i), lut->GetTextureUnit());
154 this->addUniformfArray(OpenGLHelper->Program, getVectorNameFromName(FS_Uniform_Window,i), shaderItem->mWindow);
155 this->addUniformfArray(OpenGLHelper->Program, getVectorNameFromName(FS_Uniform_Level,i), shaderItem->mLevel);
156 this->addUniformfArray(OpenGLHelper->Program, getVectorNameFromName(FS_Uniform_LLR,i), shaderItem->mLLR);
157 this->addUniformfArray(OpenGLHelper->Program, getVectorNameFromName(FS_Uniform_Alpha,i), shaderItem->mAlpha);
169 for(
int i=0; i<mShaderItems.size(); ++i)
171 item = mShaderItems.at(i);
173 if(item->mTextureUid == image_uid)
185 return mShaderItems.size();
190 mShaderItems.push_back(item);
195 mShaderItems.clear();
206 void ShaderCallback::addArrayToAttributeArray(vtkShaderProgram *program,
vtkOpenGLBufferObjectPtr buffer, std::string name,
int vector_index)
212 size_t stride =
sizeof(float)*vec_size;
213 int elementType = GL_FLOAT;
214 bool normalize =
false;
219 const GLchar *namePtr =
static_cast<const GLchar *
>(name.c_str());
220 GLint start_of_vector_index = glGetAttribLocation(program->GetHandle(), namePtr);
224 if(start_of_vector_index != -1)
226 GLint position_in_vector_index = start_of_vector_index + vector_index;
229 glEnableVertexAttribArray(position_in_vector_index);
231 glVertexAttribPointer(position_in_vector_index,
235 static_cast<GLsizei
>(stride),
236 ((
char *)NULL + (offset))
242 CX_LOG_ERROR() <<
"Error setting attribute " << name <<
" with vector_index " << vector_index;
247 void ShaderCallback::addUniformiArray(vtkShaderProgram *program, std::string name,
int value)
253 if(!program->SetUniform1iv(name.c_str(), 1, &value))
255 CX_LOG_ERROR() <<
"Could not set uniform named " << name;
261 void ShaderCallback::addUniformfArray(vtkShaderProgram *program, std::string name,
float value)
267 if(!program->SetUniform1fv(name.c_str(), 1, &value))
269 CX_LOG_ERROR() <<
"Could not set uniform named " << name;
275 void ShaderCallback::bindFSOutputVariable(vtkShaderProgram *program)
277 GLint color_frag_out_index = glGetFragDataLocation(program->GetHandle(), FS_Out_Vec4_Color.c_str());
279 if(color_frag_out_index != -1)
281 glBindFragDataLocation(program->GetHandle(), color_frag_out_index, FS_Out_Vec4_Color.c_str());
286 CX_LOG_ERROR() <<
"Could not find glGetFragDataLocation for " << FS_Out_Vec4_Color;
290 std::string ShaderCallback::getVectorNameFromName(std::string name,
int index_of_vector)
const
292 QString fullName = QString(
"%1[%2]").arg(QString(name.c_str())).arg(index_of_vector);
293 return fullName.toStdString();