diff --git a/Modules/MitkIOExt/CMakeLists.txt b/Modules/MitkIOExt/CMakeLists.txt index 49c65975ff..8421b87f38 100644 --- a/Modules/MitkIOExt/CMakeLists.txt +++ b/Modules/MitkIOExt/CMakeLists.txt @@ -1,5 +1,5 @@ MITK_CREATE_MODULE(MitkIOExt - DEPENDS MitkDataTypesExt MitkMapperExt VtkShaders + DEPENDS MitkDataTypesExt MitkMapperExt WARNINGS_AS_ERRORS AUTOLOAD_WITH Mitk ) diff --git a/Modules/MitkMapperExt/CMakeLists.txt b/Modules/MitkMapperExt/CMakeLists.txt index 4a3c21c06c..4a4e3f9e07 100644 --- a/Modules/MitkMapperExt/CMakeLists.txt +++ b/Modules/MitkMapperExt/CMakeLists.txt @@ -1,9 +1,9 @@ MITK_CREATE_MODULE(MitkMapperExt - DEPENDS MitkDataTypesExt VtkShaders + DEPENDS MitkDataTypesExt PACKAGE_DEPENDS VTK|vtkRenderingVolumeOpenGL WARNINGS_AS_ERRORS ) if(BUILD_TESTING) #add_subdirectory(Testing) endif() diff --git a/Modules/VtkShaders/CMakeLists.txt b/Modules/VtkShaders/CMakeLists.txt index 854a597168..740e6c529c 100644 --- a/Modules/VtkShaders/CMakeLists.txt +++ b/Modules/VtkShaders/CMakeLists.txt @@ -1,4 +1,5 @@ MITK_CREATE_MODULE(VtkShaders - DEPENDS Mitk + DEPENDS Mitk + AUTOLOAD_WITH Mitk # WARNINGS_AS_ERRORS ) diff --git a/Modules/VtkShaders/files.cmake b/Modules/VtkShaders/files.cmake index a87fa683e7..108dba95f3 100644 --- a/Modules/VtkShaders/files.cmake +++ b/Modules/VtkShaders/files.cmake @@ -1,20 +1,20 @@ SET(CPP_FILES - vtkXMLMaterial.cxx - vtkXMLMaterialParser.cxx - vtkXMLShader.cxx + vtkXMLMaterial.cpp + vtkXMLMaterialParser.cpp + vtkXMLShader.cpp mitkVtkShaderRepository.cpp mitkVtkShadersActivator.cpp mitkVtkShaderProgram.cpp ) SET(H_FILES vtkXMLMaterial.h vtkXMLMaterialParser.h vtkXMLShader.h mitkVtkShaderRepository.h - mitkVtkShaderProgram + mitkVtkShaderProgram.h ) set(RESOURCE_FILES Shaders/mitkShaderLighting.xml ) diff --git a/Modules/VtkShaders/mitkVtkShaderProgram.h b/Modules/VtkShaders/mitkVtkShaderProgram.h index 043d5c33fd..492b5760a0 100644 --- a/Modules/VtkShaders/mitkVtkShaderProgram.h +++ b/Modules/VtkShaders/mitkVtkShaderProgram.h @@ -1,75 +1,75 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef _MITKVTKSHADERPROGRAM_H_ #define _MITKVTKSHADERPROGRAM_H_ #include #include #include #include namespace mitk { /** * \brief SHADERTODO */ -class VtkShaders_EXPORT VtkShaderProgram : public IShaderRepository::ShaderProgram +class VtkShaderProgram : public IShaderRepository::ShaderProgram { public: mitkClassMacro( VtkShaderProgram, IShaderRepository::ShaderProgram ) itkFactorylessNewMacro( Self ) /** * Constructor */ VtkShaderProgram(); /** * Destructor */ virtual ~VtkShaderProgram(); virtual void Activate() { if(m_VtkShaderProgram) { m_VtkShaderProgram->UseProgram(); } } virtual void Deactivate() { if(m_VtkShaderProgram) { m_VtkShaderProgram->UnuseProgram(); } } void SetVtkShaderProgram( vtkShaderProgram2 * p ); vtkShaderProgram2 * GetVtkShaderProgram() const; itk::TimeStamp& GetShaderTimestampUpdate(); private: vtkShaderProgram2* m_VtkShaderProgram; itk::TimeStamp m_ShaderTimestampUpdate; }; } //end of namespace mitk #endif diff --git a/Modules/VtkShaders/mitkVtkShadersActivator.cpp b/Modules/VtkShaders/mitkVtkShadersActivator.cpp index 9d2ada3789..706da19679 100644 --- a/Modules/VtkShaders/mitkVtkShadersActivator.cpp +++ b/Modules/VtkShaders/mitkVtkShadersActivator.cpp @@ -1,101 +1,59 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include #include namespace mitk { class VtkShadersActivator : public us::ModuleActivator { public: VtkShadersActivator() { } ~VtkShadersActivator() { } void Load(us::ModuleContext* context) { m_VtkShaderRepository.reset(new VtkShaderRepository); context->RegisterService(m_VtkShaderRepository.get()); - context->AddModuleListener(this, &VtkShadersActivator::HandleModuleEvent); } void Unload(us::ModuleContext*) { m_VtkShaderRepository.reset(NULL); } private: VtkShadersActivator(const VtkShadersActivator&); VtkShadersActivator& operator=(const VtkShadersActivator&); - void HandleModuleEvent(const us::ModuleEvent moduleEvent); - std::map > moduleIdToShaderIds; - std::auto_ptr m_VtkShaderRepository; }; - void VtkShadersActivator::HandleModuleEvent(const us::ModuleEvent moduleEvent) - { - if (moduleEvent.GetType() == us::ModuleEvent::LOADED) - { - // search and load shader files - std::vector shaderResoruces = - moduleEvent.GetModule()->FindResources("Shaders", "*.xml", true); - for (std::vector::iterator i = shaderResoruces.begin(); - i != shaderResoruces.end(); ++i) - { - if (*i) - { - us::ModuleResourceStream rs(*i); - int id = m_VtkShaderRepository->LoadShader(rs, i->GetBaseName()); - if (id >= 0) - { - moduleIdToShaderIds[moduleEvent.GetModule()->GetModuleId()].push_back(id); - } - } - } - } - else if (moduleEvent.GetType() == us::ModuleEvent::UNLOADED) - { - std::map >::iterator shaderIdsIter = - moduleIdToShaderIds.find(moduleEvent.GetModule()->GetModuleId()); - if (shaderIdsIter != moduleIdToShaderIds.end()) - { - for (std::vector::iterator idIter = shaderIdsIter->second.begin(); - idIter != shaderIdsIter->second.end(); ++idIter) - { - m_VtkShaderRepository->UnloadShader(*idIter); - } - moduleIdToShaderIds.erase(shaderIdsIter); - } - } - - - } } US_EXPORT_MODULE_ACTIVATOR(VtkShaders, mitk::VtkShadersActivator); diff --git a/Modules/VtkShaders/vtkXMLMaterial.cxx b/Modules/VtkShaders/vtkXMLMaterial.cpp similarity index 93% rename from Modules/VtkShaders/vtkXMLMaterial.cxx rename to Modules/VtkShaders/vtkXMLMaterial.cpp index 6855dac6dc..8baa6c615b 100644 --- a/Modules/VtkShaders/vtkXMLMaterial.cxx +++ b/Modules/VtkShaders/vtkXMLMaterial.cpp @@ -1,264 +1,280 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + /*========================================================================= Program: Visualization Toolkit Module: vtkXMLMaterial.cxx Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ #include "vtkXMLMaterial.h" #include "vtkObjectFactory.h" #include "vtkSmartPointer.h" #include "vtkXMLDataElement.h" #include "vtkXMLMaterialParser.h" #include "vtkXMLShader.h" #include #include class vtkXMLMaterialInternals { public: typedef std::vector VectorOfElements; typedef std::vector > VectorOfShaders; VectorOfElements Properties; VectorOfShaders VertexShaders; VectorOfShaders FragmentShaders; VectorOfElements Textures; void Initialize() { this->Properties.clear(); this->VertexShaders.clear(); this->FragmentShaders.clear(); this->Textures.clear(); } }; vtkStandardNewMacro(vtkXMLMaterial); //----------------------------------------------------------------------------- vtkXMLMaterial::vtkXMLMaterial() { this->RootElement = 0; this->Internals = new vtkXMLMaterialInternals; } //----------------------------------------------------------------------------- vtkXMLMaterial::~vtkXMLMaterial() { this->SetRootElement(0); delete this->Internals; } vtkXMLMaterial* vtkXMLMaterial::CreateInstance(const char* name) { if (!name) { return 0; } vtkXMLMaterialParser* parser = vtkXMLMaterialParser::New(); vtkXMLMaterial* material = vtkXMLMaterial::New(); parser->SetMaterial(material); // First, look for material library files. // Then, look for Repository files. char* filename = vtkXMLShader::LocateFile(name); if (filename) { parser->SetFileName( filename ); delete [] filename; parser->Parse(); parser->Delete(); return material; } parser->Delete(); material->Delete(); return NULL; } //----------------------------------------------------------------------------- void vtkXMLMaterial::SetRootElement(vtkXMLDataElement* root) { this->Internals->Initialize(); vtkSetObjectBodyMacro(RootElement, vtkXMLDataElement, root); if (this->RootElement) { // Update the internal data structure to // avoid repeated searches. int numElems = this->RootElement->GetNumberOfNestedElements(); for (int i=0; iRootElement->GetNestedElement(i); const char* name = elem->GetName(); if (!name) { continue; } if (strcmp(name, "Property") == 0) { this->Internals->Properties.push_back(elem); } else if (strcmp(name, "Shader") == 0) { vtkXMLShader* shader = vtkXMLShader::New(); shader->SetRootElement(elem); switch (shader->GetScope()) { case vtkXMLShader::SCOPE_VERTEX: this->Internals->VertexShaders.push_back(shader); break; case vtkXMLShader::SCOPE_FRAGMENT: this->Internals->FragmentShaders.push_back(shader); break; default: vtkErrorMacro("Invalid scope for shader: " << shader->GetName()); } shader->Delete(); } else if (strcmp(name, "Texture") == 0) { this->Internals->Textures.push_back(elem); } } } } //----------------------------------------------------------------------------- int vtkXMLMaterial::GetNumberOfProperties() { return static_cast(this->Internals->Properties.size()); } //----------------------------------------------------------------------------- int vtkXMLMaterial::GetNumberOfTextures() { return static_cast(this->Internals->Textures.size()); } //----------------------------------------------------------------------------- int vtkXMLMaterial::GetNumberOfVertexShaders() { return static_cast(this->Internals->VertexShaders.size()); } //----------------------------------------------------------------------------- int vtkXMLMaterial::GetNumberOfFragmentShaders() { return static_cast(this->Internals->FragmentShaders.size()); } //----------------------------------------------------------------------------- vtkXMLDataElement* vtkXMLMaterial::GetProperty(int id) { if (id < this->GetNumberOfProperties()) { return this->Internals->Properties[id]; } return NULL; } //----------------------------------------------------------------------------- vtkXMLDataElement* vtkXMLMaterial::GetTexture(int index) { if (index < this->GetNumberOfTextures()) { return this->Internals->Textures[index]; } return NULL; } //----------------------------------------------------------------------------- vtkXMLShader* vtkXMLMaterial::GetVertexShader(int id) { if (id < this->GetNumberOfVertexShaders()) { return this->Internals->VertexShaders[id].GetPointer(); } return NULL; } //----------------------------------------------------------------------------- vtkXMLShader* vtkXMLMaterial::GetFragmentShader(int id) { if (id < this->GetNumberOfFragmentShaders()) { return this->Internals->FragmentShaders[id].GetPointer(); } return NULL; } //---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // Description: // Get the style the shaders. // \post valid_result: result==1 || result==2 int vtkXMLMaterial::GetShaderStyle() { int result = 1; int vStyle = 0; if (this->GetVertexShader()) { vStyle = this->GetVertexShader()->GetStyle(); } int fStyle = 0; if (this->GetFragmentShader()) { fStyle=this->GetFragmentShader()->GetStyle(); } if (vStyle!=0 && fStyle!=0 && vStyle!=fStyle) { vtkErrorMacro(<<"vertex shader and fragment shader style differ."); } else { if (vStyle!=0) { result = vStyle; } else { result = fStyle; } } assert("post: valid_result" && (result==1 || result==2) ); return result; } //----------------------------------------------------------------------------- void vtkXMLMaterial::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os, indent); os << indent << "Number of Properties: " << this->GetNumberOfProperties() << endl; os << indent << "Number of Vertex Shaders: " << this->GetNumberOfVertexShaders() << endl; os << indent << "Number of Fragment Shaders: " << this->GetNumberOfFragmentShaders() << endl; os << indent << "RootElement: "; if (this->RootElement) { os << endl; this->RootElement->PrintSelf(os, indent.GetNextIndent()); } else { os << "(null)" << endl; } } diff --git a/Modules/VtkShaders/vtkXMLMaterial.h b/Modules/VtkShaders/vtkXMLMaterial.h index 0b2dcb7e0d..6cf834632d 100644 --- a/Modules/VtkShaders/vtkXMLMaterial.h +++ b/Modules/VtkShaders/vtkXMLMaterial.h @@ -1,103 +1,119 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + /*========================================================================= Program: Visualization Toolkit Module: vtkXMLMaterial.h Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ // .NAME vtkXMLMaterial - encapsulates a VTK Material description. // .SECTION Description // vtkXMLMaterial encapsulates VTK Material description. It keeps a pointer // to vtkXMLDataElement that defines the material and provides // access to Shaders/Properties defined in it. // .SECTION Thanks // Shader support in VTK includes key contributions by Gary Templet at // Sandia National Labs. #ifndef __vtkXMLMaterial_h #define __vtkXMLMaterial_h #include "vtkRenderingCoreModule.h" // For export macro #include "vtkObject.h" class vtkXMLDataElement; class vtkXMLMaterialInternals; class vtkXMLShader; class vtkXMLMaterial : public vtkObject { public: static vtkXMLMaterial* New(); vtkTypeMacro(vtkXMLMaterial, vtkObject); void PrintSelf(ostream& os, vtkIndent indent); // Description: // Create a new instance. It searches for the material // using the following order: first, check the MaterialLibrary; second, // treat the name as an absolute path and try to locate it; third, // search the Material repository. Returns null is it fails to // locate the material. static vtkXMLMaterial* CreateInstance(const char* name); // Description: // Get number of elements of type Property. int GetNumberOfProperties(); // Description: // Get number of elements of type Texture. int GetNumberOfTextures(); // Description: // Get number of Vertex shaders. int GetNumberOfVertexShaders(); // Description: // Get number of fragment shaders. int GetNumberOfFragmentShaders(); // Description: // Get the ith vtkXMLDataElement of type . vtkXMLDataElement* GetProperty(int id=0); // Description: // Get the ith vtkXMLDataElement of type . vtkXMLDataElement* GetTexture(int id=0); // Description: // Get the ith vtkXMLDataElement of type . vtkXMLShader* GetVertexShader(int id=0); // Description: // Get the ith vtkXMLDataElement of type . vtkXMLShader* GetFragmentShader(int id=0); // Description: // Get/Set the XML root element that describes this material. vtkGetObjectMacro(RootElement, vtkXMLDataElement); void SetRootElement(vtkXMLDataElement*); // Description: // Get the style the shaders. // \post valid_result: result==1 || result==2 int GetShaderStyle(); protected: vtkXMLMaterial(); ~vtkXMLMaterial(); vtkXMLDataElement* RootElement; vtkXMLMaterialInternals* Internals; private: vtkXMLMaterial(const vtkXMLMaterial&); // Not implemented. void operator=(const vtkXMLMaterial&); // Not implemented. }; #endif diff --git a/Modules/VtkShaders/vtkXMLMaterialParser.cxx b/Modules/VtkShaders/vtkXMLMaterialParser.cpp similarity index 90% rename from Modules/VtkShaders/vtkXMLMaterialParser.cxx rename to Modules/VtkShaders/vtkXMLMaterialParser.cpp index 6b9f04d87d..ea88241911 100644 --- a/Modules/VtkShaders/vtkXMLMaterialParser.cxx +++ b/Modules/VtkShaders/vtkXMLMaterialParser.cpp @@ -1,152 +1,168 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + /*========================================================================= Program: Visualization Toolkit Module: vtkXMLMaterialParser.cxx Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ /* * Copyright 2003 Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the * U.S. Government. Redistribution and use in source and binary forms, with * or without modification, are permitted provided that this Notice and any * statement of authorship are reproduced on all copies. */ #include "vtkXMLMaterialParser.h" #include "vtkXMLMaterial.h" #include "vtkObjectFactory.h" #include "vtkSmartPointer.h" #include "vtkXMLDataElement.h" #include "vtkXMLUtilities.h" #include //----------------------------------------------------------------------------- class vtkXMLMaterialParserInternals { public: typedef std::vector > VectorOfElements; VectorOfElements Stack; }; //----------------------------------------------------------------------------- vtkStandardNewMacro(vtkXMLMaterialParser); vtkCxxSetObjectMacro(vtkXMLMaterialParser, Material, vtkXMLMaterial); //----------------------------------------------------------------------------- vtkXMLMaterialParser::vtkXMLMaterialParser() { this->Material = vtkXMLMaterial::New(); this->Material->Register(this); this->Material->Delete(); this->Internals = new vtkXMLMaterialParserInternals; } //----------------------------------------------------------------------------- vtkXMLMaterialParser::~vtkXMLMaterialParser() { delete this->Internals; this->SetMaterial(0); } //----------------------------------------------------------------------------- int vtkXMLMaterialParser::Parse(const char* str) { return this->Superclass::Parse(str); } //----------------------------------------------------------------------------- int vtkXMLMaterialParser::Parse(const char* str, unsigned int length) { return this->Superclass::Parse(str, length); } //----------------------------------------------------------------------------- int vtkXMLMaterialParser::Parse() { this->Internals->Stack.clear(); return this->Superclass::Parse(); } //----------------------------------------------------------------------------- int vtkXMLMaterialParser::InitializeParser() { int ret = this->Superclass::InitializeParser(); if (ret) { this->Internals->Stack.clear(); } return ret; } //----------------------------------------------------------------------------- void vtkXMLMaterialParser::StartElement(const char* name, const char** atts) { vtkXMLDataElement* element = vtkXMLDataElement::New(); element->SetName(name); element->SetXMLByteIndex(this->GetXMLByteIndex()); vtkXMLUtilities::ReadElementFromAttributeArray(element, atts, VTK_ENCODING_NONE); const char* id = element->GetAttribute("id"); if (id) { element->SetId(id); } this->Internals->Stack.push_back(element); element->Delete(); } //----------------------------------------------------------------------------- void vtkXMLMaterialParser::EndElement(const char* vtkNotUsed(name)) { vtkXMLDataElement* finished = this->Internals->Stack.back().GetPointer(); int prev_pos = static_cast(this->Internals->Stack.size()) - 2; if (prev_pos >= 0) { this->Internals->Stack[prev_pos].GetPointer()->AddNestedElement(finished); } else { this->Material->SetRootElement(finished); } this->Internals->Stack.pop_back(); } //----------------------------------------------------------------------------- void vtkXMLMaterialParser::CharacterDataHandler( const char* inData, int inLength ) { if (this->Internals->Stack.size() > 0) { vtkXMLDataElement* elem = this->Internals->Stack.back().GetPointer(); elem->AddCharacterData(inData, inLength); } /* // this wont happen as the XML parser will flag it as an error. else { vtkErrorMacro("Character data not enclosed in XML tags"); } */ } //----------------------------------------------------------------------------- void vtkXMLMaterialParser::PrintSelf(ostream &os, vtkIndent indent) { this->Superclass::PrintSelf(os, indent); os << indent << "Material: " ; this->Material->PrintSelf(os, indent.GetNextIndent()); } diff --git a/Modules/VtkShaders/vtkXMLMaterialParser.h b/Modules/VtkShaders/vtkXMLMaterialParser.h index b49656d0e5..8975f4b0ea 100644 --- a/Modules/VtkShaders/vtkXMLMaterialParser.h +++ b/Modules/VtkShaders/vtkXMLMaterialParser.h @@ -1,123 +1,139 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + /*========================================================================= Program: Visualization Toolkit Module: vtkXMLMaterialParser.h Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ /* * Copyright 2004 Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the * U.S. Government. Redistribution and use in source and binary forms, with * or without modification, are permitted provided that this Notice and any * statement of authorship are reproduced on all copies. */ // .NAME vtkXMLMaterialParser - Parses VTK Material file // // .SECTION Description // vtkXMLMaterialParser parses a VTK Material file and provides that file's // description of a number of vertex and fragment shaders along with data // values specified for data members of vtkProperty. This material is to be // applied to an actor through it's vtkProperty and augments VTK's concept // of a vtkProperty to include explicitly include vertex and fragment shaders // and parameter settings for those shaders. This effectively makes reflectance // models and other shaders a material property. If no shaders are specified // VTK should default to standard rendering. // // .SECTION Design // vtkXMLMaterialParser provides access to 3 distinct types of first-level // vtkXMLDataElements that describe a VTK material. These elements are as // follows: // // vtkProperty - describe values for vtkProperty data members // // vtkVertexShader - a vertex shader and enough information to // install it into the hardware rendering pipeline including values for // specific shader parameters and structures. // // vtkFragmentShader - a fragment shader and enough information to // install it into the hardware rendering pipeline including values for // specific shader parameters and structures. // // The design of the material file closely follows that of vtk's xml // descriptions of it's data sets. This allows use of the very handy // vtkXMLDataElement which provides easy access to an xml element's // attribute values. Inlined data is currently not handled. // // Ideally this class would be a Facade to a DOM parser, but VTK only // provides access to expat, a SAX parser. Other vtk classes that parse // xml files are tuned to read vtkDataSets and don't provide the functionality // to handle generic xml data. As such they are of little use here. // // This class may be extended for better data handling or may become a // Facade to a DOM parser should on become part of the VTK code base. // .SECTION Thanks // Shader support in VTK includes key contributions by Gary Templet at // Sandia National Labs. #ifndef __vtkXMLMaterialParser_h #define __vtkXMLMaterialParser_h #include "vtkRenderingCoreModule.h" // For export macro #include "vtkXMLParser.h" class vtkXMLMaterial; class vtkXMLMaterialParserInternals; class vtkXMLMaterialParser : public vtkXMLParser { public: static vtkXMLMaterialParser *New(); vtkTypeMacro(vtkXMLMaterialParser,vtkXMLParser); void PrintSelf(ostream& os, vtkIndent indent); // Description: // Set/Get the vtkXMLMaterial representation of the parsed material. vtkGetObjectMacro(Material, vtkXMLMaterial); void SetMaterial(vtkXMLMaterial*); // Description: // Overridden to initialize the internal structures before // the parsing begins. virtual int Parse(); virtual int Parse(const char* inputString); virtual int Parse(const char* inputString, unsigned int length); // Description: // Overridden to clean up internal structures before the chunk-parsing // begins. virtual int InitializeParser(); protected: vtkXMLMaterialParser(); ~vtkXMLMaterialParser(); // Description: // Event for handling the start of an element virtual void StartElement(const char* name, const char** atts); // Description: // Event for handling the end of an element virtual void EndElement(const char*); // Description: // Handle character data, not yet implemented virtual void CharacterDataHandler( const char* data, int length ); vtkXMLMaterial* Material; vtkXMLMaterialParserInternals* Internals; private: vtkXMLMaterialParser(const vtkXMLMaterialParser&); // Not implemented void operator=(const vtkXMLMaterialParser&); // Not implemented }; #endif diff --git a/Modules/VtkShaders/vtkXMLShader.cxx b/Modules/VtkShaders/vtkXMLShader.cpp similarity index 93% rename from Modules/VtkShaders/vtkXMLShader.cxx rename to Modules/VtkShaders/vtkXMLShader.cpp index 67a0647b27..fcf7a1cd84 100644 --- a/Modules/VtkShaders/vtkXMLShader.cxx +++ b/Modules/VtkShaders/vtkXMLShader.cpp @@ -1,277 +1,293 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + /*========================================================================= Program: Visualization Toolkit Module: vtkXMLShader.cxx Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ #include "vtkXMLShader.h" #include "vtkObjectFactory.h" #include "vtkXMLDataElement.h" #include #include vtkStandardNewMacro(vtkXMLShader); vtkCxxSetObjectMacro(vtkXMLShader, SourceLibraryElement, vtkXMLDataElement); //----------------------------------------------------------------------------- vtkXMLShader::vtkXMLShader() : Code(NULL), RootElement(NULL), SourceLibraryElement(NULL), Args(NULL) { } //----------------------------------------------------------------------------- vtkXMLShader::~vtkXMLShader() { if (this->RootElement) { this->RootElement->UnRegister(this); this->RootElement = 0; } this->SetSourceLibraryElement(0); this->SetCode(0); this->CleanupArgs(); } //----------------------------------------------------------------------------- void vtkXMLShader::SetRootElement(vtkXMLDataElement* root) { vtkSetObjectBodyMacro(RootElement, vtkXMLDataElement, root); this->SetCode(0); this->SetSourceLibraryElement(0); // release the SourceLibrary element. } //----------------------------------------------------------------------------- // Note that this method allocates a new string which must be deleted by // the caller. char* vtkXMLShader::LocateFile(const char* filename) { if(!filename) { return NULL; } // if filename is absolute path, return the same. if (vtksys::SystemTools::FileExists(filename)) { return vtksys::SystemTools::DuplicateString(filename); } // Fetch any runtime defined user paths for materials std::vector paths; std::string userpaths; vtksys::SystemTools::GetEnv("USER_MATERIALS_DIRS", userpaths); if (userpaths.size()>0) { vtksys::SystemTools::Split(userpaths.c_str(), paths, ';'); } #ifdef VTK_MATERIALS_DIRS // search thru default paths to locate file. vtksys::SystemTools::Split(VTK_MATERIALS_DIRS, paths, ';'); #endif for (unsigned int i =0; i < paths.size(); i++) { std::string path = paths[i]; if (path.size() == 0) { continue; } vtksys::SystemTools::ConvertToUnixSlashes(path); if (path[path.size()-1] != '/') { path += "/"; } path += filename; if (vtksys::SystemTools::FileExists(path.c_str())) { return vtksys::SystemTools::DuplicateString(path.c_str()); } } return NULL; } //----------------------------------------------------------------------------- int vtkXMLShader::GetScope() { if (this->RootElement) { const char* scope = this->RootElement->GetAttribute("scope"); if (!scope) { vtkErrorMacro("Shader description missing \"scope\" attribute."); } else if (strcmp(scope, "Vertex") == 0) { return vtkXMLShader::SCOPE_VERTEX; } else if (strcmp(scope, "Fragment") == 0) { return vtkXMLShader::SCOPE_FRAGMENT; } } return vtkXMLShader::SCOPE_NONE; } // ---------------------------------------------------------------------------- // \post valid_result: result==1 || result==2 int vtkXMLShader::GetStyle() { int result=1; if(this->RootElement) { const char *loc=this->RootElement->GetAttribute("style"); if(loc==0) { // fine. this attribute is optional. } else { if(strcmp(loc,"1")==0) { // fine. default value. } else { if(strcmp(loc,"2")==0) { result=2; // new style } else { vtkErrorMacro(<<"style number not supported. Expect 1 or 2. We force it to be 1."); } } } } assert("post valid_result" && (result==1 || result==2) ); return result; } //----------------------------------------------------------------------------- const char* vtkXMLShader::GetName() { return (this->RootElement)? this->RootElement->GetAttribute("name") : 0; } //----------------------------------------------------------------------------- const char* vtkXMLShader::GetEntry() { return (this->RootElement)? this->RootElement->GetAttribute("entry") : 0; } //----------------------------------------------------------------------------- const char** vtkXMLShader::GetArgs() { this->CleanupArgs(); if (!this->RootElement || !this->RootElement->GetAttribute("args")) { return 0; } std::vector args; vtksys::SystemTools::Split(this->RootElement->GetAttribute("args"), args, ' '); int i; int size = static_cast(args.size()); if (size == 0) { return 0; } this->Args = new char*[size+1]; for (i=0; i < size; i++) { this->Args[i] = vtksys::SystemTools::DuplicateString(args[i].c_str()); } this->Args[size] = 0; return const_cast(this->Args); } //----------------------------------------------------------------------------- const char* vtkXMLShader::GetCode() { return this->RootElement->GetCharacterData(); } //----------------------------------------------------------------------------- void vtkXMLShader::CleanupArgs() { if (this->Args) { char** a = this->Args; while (*a) { delete [] (*a); a++; } delete [] this->Args; this->Args = 0; } } //----------------------------------------------------------------------------- void vtkXMLShader::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os, indent); os << indent << "Name: " << (this->GetName()? this->GetName() : "(none)") << endl; os << indent << "Scope: "; switch(this->GetScope()) { case SCOPE_NONE: os << "None"; break; case SCOPE_MIXED: os << "Mixed"; break; case SCOPE_VERTEX: os << "Vertex"; break; case SCOPE_FRAGMENT: os << "Fragment"; break; } os << endl; os << indent << "Entry: " << (this->GetEntry()? this->GetEntry() : "(none)") << endl; os << indent << "Args: "; const char** args = this->GetArgs(); if (!args) { os << "(none)" << endl; } else { while (*args) { os << indent << *args << " "; args++; } os << endl; } os << indent << "RootElement: "; if (this->RootElement) { os << endl; this->RootElement->PrintSelf(os, indent.GetNextIndent()); } else { os << "(none)" << endl; } } diff --git a/Modules/VtkShaders/vtkXMLShader.h b/Modules/VtkShaders/vtkXMLShader.h index 1b6d9725f1..e828343a56 100644 --- a/Modules/VtkShaders/vtkXMLShader.h +++ b/Modules/VtkShaders/vtkXMLShader.h @@ -1,112 +1,128 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + /*========================================================================= Program: Visualization Toolkit Module: vtkXMLShader.h Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ // .NAME vtkXMLShader - encapsulates a Shader XML description. // .SECTION Description // vtkXMLShader encapsulates the XML description for a Shader. // It provides convenient access to various attributes/properties // of a shader. // .SECTION Thanks // Shader support in VTK includes key contributions by Gary Templet at // Sandia National Labs. #ifndef __vtkXMLShader_h #define __vtkXMLShader_h #include "vtkRenderingCoreModule.h" // For export macro #include "vtkObject.h" class vtkXMLDataElement; class vtkXMLShader : public vtkObject { public: static vtkXMLShader* New(); vtkTypeMacro(vtkXMLShader, vtkObject); void PrintSelf(ostream& os, vtkIndent indent); // Description: // Get/Set the XML root element that describes this shader. vtkGetObjectMacro(RootElement, vtkXMLDataElement); void SetRootElement(vtkXMLDataElement*); // Description: // Returns the type of the shader as defined in the XML description. int GetScope(); // Description: // Returns the style of the shader as optionaly defined in the XML // description. If not present, default style is 1. "style=2" means it is // a shader without a main(). In style 2, the "main" function for the vertex // shader part is void propFuncVS(void), the main function for the fragment // shader part is void propFuncFS(). This is useful when combining a shader // at the actor level and a shader defines at the renderer level, like // the depth peeling pass. // \post valid_result: result==1 || result==2 int GetStyle(); // Description: // Get the name of the Shader. const char* GetName(); // Description: // Get the entry point to the shader code as defined in the XML. const char* GetEntry(); // Description: // Get the shader code. const char* GetCode(); // Description: // Returns an null terminate array of the pointers to space sepatared Args // defined in the XML description. const char** GetArgs(); // Description: // Searches the file in the VTK_MATERIALS_DIRS. // Note that this allocates new memory for the string. // The caller must delete it. static char* LocateFile(const char* filename); //BTX enum ScopeCodes { SCOPE_NONE=0, SCOPE_MIXED, SCOPE_VERTEX, SCOPE_FRAGMENT }; //ETX protected: vtkXMLShader(); ~vtkXMLShader(); char* Code; // cache for the code. vtkSetStringMacro(Code); vtkXMLDataElement* RootElement; vtkXMLDataElement* SourceLibraryElement; void SetSourceLibraryElement(vtkXMLDataElement*); char** Args; void CleanupArgs(); private: vtkXMLShader(const vtkXMLShader&); // Not implemented. void operator=(const vtkXMLShader&); // Not implemented. }; #endif