Commit eeaecd61 authored by Utkarsh Ayachit's avatar Utkarsh Ayachit
Browse files

ENH: Adding support for passing generic vertex attributes to the rendering pipeline

parent 0d10087a
......@@ -21,11 +21,11 @@ ENDIF(APPLE AND VTK_WRAP_JAVA)
SET( Kit_SRCS
vtkAbstractMapper3D.cxx
vtkAbstractVolumeMapper.cxx
vtkAbstractPicker.cxx
vtkAbstractPropPicker.cxx
vtkActor.cxx
vtkAbstractVolumeMapper.cxx
vtkActorCollection.cxx
vtkActor.cxx
vtkAreaPicker.cxx
vtkAssembly.cxx
vtkAxisActor2D.cxx
......@@ -36,8 +36,8 @@ SET( Kit_SRCS
vtkChooserPainter.cxx
vtkClipPlanesPainter.cxx
vtkCoincidentTopologyResolutionPainter.cxx
vtkCuller.cxx
vtkCullerCollection.cxx
vtkCuller.cxx
vtkDataSetMapper.cxx
vtkDefaultPainter.cxx
vtkDisplayListPainter.cxx
......@@ -46,14 +46,14 @@ SET( Kit_SRCS
vtkFollower.cxx
vtkFrustumCoverageCuller.cxx
vtkGenericRenderWindowInteractor.cxx
vtkGenericVertexAttributeMapping.cxx
vtkGraphicsFactory.cxx
vtkHierarchicalPolyDataMapper.cxx
vtkIVExporter.cxx
vtkIdentColoredPainter.cxx
vtkImageActor.cxx
vtkImageMapper.cxx
vtkImageViewer.cxx
vtkImageViewer2.cxx
vtkImageViewer.cxx
vtkImagingFactory.cxx
vtkImporter.cxx
vtkInteractorEventRecorder.cxx
......@@ -69,22 +69,23 @@ SET( Kit_SRCS
vtkInteractorStyleRubberBandZoom.cxx
vtkInteractorStyleSwitch.cxx
vtkInteractorStyleTerrain.cxx
vtkInteractorStyleTrackball.cxx
vtkInteractorStyleTrackballActor.cxx
vtkInteractorStyleTrackballCamera.cxx
vtkInteractorStyleTrackball.cxx
vtkInteractorStyleUnicam.cxx
vtkInteractorStyleUser.cxx
vtkLODActor.cxx
vtkLODProp3D.cxx
vtkIVExporter.cxx
vtkLabeledDataMapper.cxx
vtkLeaderActor2D.cxx
vtkLight.cxx
vtkLightCollection.cxx
vtkLightKit.cxx
vtkLight.cxx
vtkLightingPainter.cxx
vtkLightKit.cxx
vtkLinesPainter.cxx
vtkMapper.cxx
vtkLODActor.cxx
vtkLODProp3D.cxx
vtkMapperCollection.cxx
vtkMapper.cxx
vtkMultiGroupPolyDataMapper.cxx
vtkOBJExporter.cxx
vtkObserverMediator.cxx
......@@ -94,25 +95,25 @@ SET( Kit_SRCS
vtkPainterPolyDataMapper.cxx
vtkParallelCoordinatesActor.cxx
vtkPicker.cxx
vtkPointsPainter.cxx
vtkPointPicker.cxx
vtkPolyDataMapper.cxx
vtkPointsPainter.cxx
vtkPolyDataMapper2D.cxx
vtkPolyDataMapper.cxx
vtkPolyDataPainter.cxx
vtkPolygonsPainter.cxx
vtkPrimitivePainter.cxx
vtkProp3D.cxx
vtkProp3DCollection.cxx
vtkPropPicker.cxx
vtkProp3D.cxx
vtkProperty.cxx
vtkPropPicker.cxx
vtkQuaternionInterpolator.cxx
vtkRenderWindow.cxx
vtkRenderWindowCollection.cxx
vtkRenderWindowInteractor.cxx
vtkRenderedAreaPicker.cxx
vtkRenderer.cxx
vtkRendererCollection.cxx
vtkRenderer.cxx
vtkRendererSource.cxx
vtkRenderWindowCollection.cxx
vtkRenderWindow.cxx
vtkRenderWindowInteractor.cxx
vtkRepresentationPainter.cxx
vtkScalarBarActor.cxx
vtkScalarsToColorsPainter.cxx
......@@ -120,24 +121,25 @@ SET( Kit_SRCS
vtkScenePicker.cxx
vtkSelectVisiblePoints.cxx
vtkShader.cxx
vtkShaderDeviceAdapter.cxx
vtkShaderProgram.cxx
vtkStandardPolyDataPainter.cxx
vtkTesting.cxx
vtkTextActor.cxx
# vtkTextActor2D.cxx
vtkTextActor3D.cxx
vtkTextActor.cxx
vtkTextMapper.cxx
vtkTextProperty.cxx
vtkTexture.cxx
vtkTransformInterpolator.cxx
vtkTStripsPainter.cxx
vtkTupleInterpolator.cxx
vtkVisibleCellSelector.cxx
vtkVisibilitySort.cxx
vtkVRMLExporter.cxx
vtkVolume.cxx
vtkVisibleCellSelector.cxx
vtkVolumeCollection.cxx
vtkVolume.cxx
vtkVolumeProperty.cxx
vtkVRMLExporter.cxx
vtkWindowToImageFilter.cxx
vtkWorldPointPicker.cxx
)
......@@ -152,6 +154,7 @@ ENDIF(VTK_USE_GL2PS)
IF(VTK_USE_CG_SHADERS)
SET(Kit_SRCS ${Kit_SRCS}
vtkCgShader.cxx
vtkCgShaderDeviceAdapter.cxx
vtkCgShaderProgram.cxx
)
INCLUDE_DIRECTORIES(${CG_INCLUDE_PATH})
......@@ -184,6 +187,7 @@ SET( KitOpenGL_SRCS
IF(VTK_USE_GLSL_SHADERS)
SET(KitOpenGL_SRCS ${KitOpenGL_SRCS}
vtkGLSLShader.cxx
vtkGLSLShaderDeviceAdapter.cxx
vtkGLSLShaderProgram.cxx
)
ENDIF(VTK_USE_GLSL_SHADERS)
......@@ -204,6 +208,7 @@ SET_SOURCE_FILES_PROPERTIES(
vtkPrimitivePainter
vtkProp3D
vtkShader
vtkShaderDeviceAdapter
vtkShaderProgram
vtkVisibilitySort.cxx
ABSTRACT
......
......@@ -41,6 +41,7 @@ IF (VTK_USE_RENDERING AND VTK_USE_DISPLAY)
# Tests testing Cg Shaders.
SET(tests ${tests}
TestCgShader
TestGenericVertexAttributesCg
)
ENDIF (VTK_USE_CG_SHADERS)
......@@ -48,6 +49,7 @@ IF (VTK_USE_RENDERING AND VTK_USE_DISPLAY)
# Tests testing GLSL Shaders.
SET(tests ${tests}
TestGLSLShader
TestGenericVertexAttributesGLSL
)
ENDIF (VTK_USE_GLSL_SHADERS)
......
# tests the support to pass generic vertex attributes to be used in Cg shaders.
set xmlMaterial {<?xml version="1.0" encoding="UTF-8"?>
<Material name="GenericAttributes1">
<Shader
scope="Vertex"
name="VertexShader"
location="Inline"
language="Cg"
entry="main">
<MatrixUniform name="ModelViewProj"
type="State"
number_of_elements="2"
value="CG_GL_MODELVIEW_PROJECTION_MATRIX CG_GL_MATRIX_IDENTITY" />
<MatrixUniform name="ModelViewIT"
type="State"
number_of_elements="2"
value="CG_GL_MODELVIEW_MATRIX CG_GL_MATRIX_INVERSE_TRANSPOSE" />
struct appin
{
float4 Position : POSITION;
float3 Normal : NORMAL;
};
// define outputs from vertex shader
struct vertout
{
float4 HPosition : POSITION;
float4 Color0 : COLOR0;
};
vertout main(appin IN,
uniform float4x4 ModelViewProj,
uniform float4x4 ModelViewIT)
{
vertout OUT;
// transform vertex position into homogenous clip-space
OUT.HPosition = mul(ModelViewProj, IN.Position);
OUT.Color0.xyz = normalize(IN.Normal);
OUT.Color0.a = 1.0;
return OUT;
}
</Shader>
</Material>
}
package require vtk
package require vtkinteraction
vtkRenderWindow renWin
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
vtkRenderer renderer
renWin AddRenderer renderer
vtkSphereSource src1
src1 SetRadius 5
src1 SetPhiResolution 20
src1 SetThetaResolution 20
vtkBrownianPoints randomVectors
randomVectors SetMinimumSpeed 0
randomVectors SetMaximumSpeed 1
randomVectors SetInputConnection [src1 GetOutputPort]
vtkPolyDataMapper mapper
mapper SetInputConnection [randomVectors GetOutputPort]
vtkActor actor
actor SetMapper mapper
# Load the material. Here, we are loading a material
# defined in the Vtk Library. One can also specify
# a filename to a material description xml.
[actor GetProperty] LoadMaterialFromString $xmlMaterial
# Set red color to show if shading fails.
[actor GetProperty] SetColor 1.0 0 0
# Turn shading on. Otherwise, shaders are not used.
[actor GetProperty] ShadingOn
# Map PointData.BrownianVectors (all 3 components) to IN.Normal
mapper MapDataArrayToVertexAttribute "IN.Normal" "BrownianVectors" 0 -1
renderer AddActor actor
renderer SetBackground 0.5 0.5 0.5
renWin Render
[renderer GetActiveCamera] Azimuth -50
[renderer GetActiveCamera] Roll 70
renWin Render
wm withdraw .
# tests the support to pass generic vertex attributes to be used in Cg shaders.
set xmlMaterial {<?xml version="1.0" encoding="UTF-8"?>
<Material name="GenericAttributes1">
<Shader
scope="Vertex"
name="VertexShader"
location="Inline"
language="GLSL"
entry="main">
attribute vec3 genAttrVector;
varying vec4 color;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix *gl_Vertex;
color = vec4(normalize(genAttrVector), 1.0);
}
</Shader>
<Shader scope="Fragment" name="FragmentShader" location="Inline"
language="GLSL" entry="main">
varying vec4 color;
void main(void)
{
gl_FragColor = color;
}
</Shader>
</Material>
}
package require vtk
package require vtkinteraction
vtkRenderWindow renWin
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
vtkRenderer renderer
renWin AddRenderer renderer
vtkSphereSource src1
src1 SetRadius 5
src1 SetPhiResolution 20
src1 SetThetaResolution 20
vtkBrownianPoints randomVectors
randomVectors SetMinimumSpeed 0
randomVectors SetMaximumSpeed 1
randomVectors SetInputConnection [src1 GetOutputPort]
vtkPolyDataMapper mapper
mapper SetInputConnection [randomVectors GetOutputPort]
vtkActor actor
actor SetMapper mapper
# Load the material. Here, we are loading a material
# defined in the Vtk Library. One can also specify
# a filename to a material description xml.
[actor GetProperty] LoadMaterialFromString $xmlMaterial
# Set red color to show if shading fails.
[actor GetProperty] SetColor 1.0 0 0
# Turn shading on. Otherwise, shaders are not used.
[actor GetProperty] ShadingOn
# Map PointData.BrownianVectors (all 3 components) to genAttrVector
mapper MapDataArrayToVertexAttribute "genAttrVector" "BrownianVectors" 0 -1
renderer AddActor actor
renderer SetBackground 0.5 0.5 0.5
renWin Render
[renderer GetActiveCamera] Azimuth -50
[renderer GetActiveCamera] Roll 70
renWin Render
wm withdraw .
......@@ -138,6 +138,7 @@ public:
// we need to ensure that the actor transformations are pushed before
// state matrix uniform variables are bound.
virtual void PassShaderVariables(vtkActor* actor, vtkRenderer* ren);
//BTX
protected:
vtkCgShader();
~vtkCgShader();
......@@ -160,10 +161,12 @@ protected:
virtual void SetSamplerParameter(const char* name, vtkTexture* texture,
int);
friend class vtkCgShaderDeviceAdapter;
private:
vtkCgShader(const vtkCgShader&); // Not Implemented
void operator=(const vtkCgShader&); // Not Implemented
vtkCgShaderInternals* Internals;
//ETX
};
#endif //_vtkCgShader_h
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCgShaderDeviceAdapter.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 "vtkCgShaderDeviceAdapter.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkShaderProgram.h"
#include "vtkCgShader.h"
#include "vtkCollectionIterator.h"
#include "vtkXMLShader.h"
class vtkCgShaderDeviceAdapter::vtkInternal
{
public:
vtkSmartPointer<vtkCgShader> VertexShader;
};
vtkStandardNewMacro(vtkCgShaderDeviceAdapter);
vtkCxxRevisionMacro(vtkCgShaderDeviceAdapter, "1.1");
//----------------------------------------------------------------------------
vtkCgShaderDeviceAdapter::vtkCgShaderDeviceAdapter()
{
this->Internal = new vtkInternal();
}
//----------------------------------------------------------------------------
vtkCgShaderDeviceAdapter::~vtkCgShaderDeviceAdapter()
{
delete this->Internal;
}
//----------------------------------------------------------------------------
void vtkCgShaderDeviceAdapter::PrepareForRender()
{
// locate the vertex CgShader which can accept varying parameters.
vtkCollectionIterator* shaderIter = this->ShaderProgram->NewShaderIterator();
for (shaderIter->InitTraversal(); !shaderIter->IsDoneWithTraversal();
shaderIter->GoToNextItem())
{
vtkCgShader* shader = vtkCgShader::SafeDownCast(
shaderIter->GetCurrentObject());
if (shader && shader->GetScope() == vtkXMLShader::SCOPE_VERTEX)
{
this->Internal->VertexShader = shader;
break;
}
}
shaderIter->Delete();
}
template <class T>
void vtkCgShaderDeviceAdapterSendAttributeInternal(vtkCgShaderDeviceAdapter* self,
const char* attrname, int components, const T* attribute, unsigned long offset)
{
double converted_value[4];
for (int cc=0; cc < 4 && cc < components; cc++)
{
converted_value[cc] = static_cast<double>((attribute+offset)[cc]);
}
self->SendAttributeInternal(attrname, components, converted_value);
}
VTK_TEMPLATE_SPECIALIZE
void vtkCgShaderDeviceAdapterSendAttributeInternal(vtkCgShaderDeviceAdapter* self,
const char* attrname, int components, const float* attribute, unsigned long offset)
{
self->SendAttributeInternal(attrname, components, (attribute+offset));
}
//----------------------------------------------------------------------------
void vtkCgShaderDeviceAdapter::SendAttributeInternal(
const char* attrname, int components, const double* data)
{
this->Internal->VertexShader->SetUniformParameter(attrname, components, data);
}
//----------------------------------------------------------------------------
void vtkCgShaderDeviceAdapter::SendAttributeInternal(
const char* attrname, int components, const float* data)
{
this->Internal->VertexShader->SetUniformParameter(attrname, components, data);
}
//----------------------------------------------------------------------------
void vtkCgShaderDeviceAdapter::SendAttribute(const char* attrname,
int components, int type,
const void* attribute, unsigned long offset/*=0*/)
{
switch (type)
{
vtkTemplateMacro(
vtkCgShaderDeviceAdapterSendAttributeInternal(this,
attrname, components, static_cast<const VTK_TT*>(attribute), offset));
}
}
//----------------------------------------------------------------------------
void vtkCgShaderDeviceAdapter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCgShaderDeviceAdapter.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 vtkCgShaderDeviceAdapter - adapter to pass generic vertex attributes
// to the rendering pipeline to be used in a Cg shader.
// .SECTION Description
// vtkShaderDeviceAdapter subclass for Cg.
#ifndef __vtkCgShaderDeviceAdapter_h
#define __vtkCgShaderDeviceAdapter_h
#include "vtkShaderDeviceAdapter.h"
class VTK_COMMON_EXPORT vtkCgShaderDeviceAdapter : public vtkShaderDeviceAdapter
{
public:
static vtkCgShaderDeviceAdapter* New();
vtkTypeRevisionMacro(vtkCgShaderDeviceAdapter, vtkShaderDeviceAdapter);
void PrintSelf(ostream& os, vtkIndent indent);
// Descrition:
// This method is called before rendering. This gives the shader device
// adapter an opportunity to collect information, such as attribute indices
// that it will need while rendering.
virtual void PrepareForRender();
// Description:
// Sends a single attribute to the graphics card.
// The attrname parameter identifies the name of attribute.
// The components parameter gives the number of
// components in the attribute. In general, components must be between
// 1-4, but a rendering system may impose even more constraints. The
// type parameter is a VTK type enumeration (VTK_FLOAT, VTK_INT, etc.).
// Again, a rendering system may not support all types for all
// attributes. The attribute parameter is the actual data for the
// attribute.
// If offset is specified, it is added to attribute pointer \c after
// it has been casted to the proper type.
virtual void SendAttribute(const char* attrname,
int components, int type,
const void* attribute, unsigned long offset=0);
//BTX
void SendAttributeInternal(const char* attrname, int components, const double*);
void SendAttributeInternal(const char* attrname, int components, const float*);
protected:
vtkCgShaderDeviceAdapter();
~vtkCgShaderDeviceAdapter();
private:
vtkCgShaderDeviceAdapter(const vtkCgShaderDeviceAdapter&); // Not implemented.
void operator=(const vtkCgShaderDeviceAdapter&); // Not implemented.
class vtkInternal;
vtkInternal* Internal;
//ETX
};
#endif
......@@ -24,26 +24,29 @@
#include "vtkCgShaderProgram.h"
#include "vtkActor.h"
#include "vtkCollectionIterator.h"
#include "vtkCgShaderDeviceAdapter.h"
#include "vtkCgShader.h"
#include "vtkCollectionIterator.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkXMLMaterialReader.h"
vtkCxxRevisionMacro(vtkCgShaderProgram, "1.4");
vtkCxxRevisionMacro(vtkCgShaderProgram, "1.5");
vtkStandardNewMacro(vtkCgShaderProgram);
//----------------------------------------------------------------------------
vtkCgShaderProgram::vtkCgShaderProgram()
{
vtkCgShaderDeviceAdapter* da = vtkCgShaderDeviceAdapter::New();
this->SetShaderDeviceAdapter(da);
da->Delete();
}
//----------------------------------------------------------------------------
vtkCgShaderProgram::~vtkCgShaderProgram()
{
this->SetShaderDeviceAdapter(0);
}
//----------------------------------------------------------------------------
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGLSLShaderDeviceAdapter.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 "vtkGLSLShaderDeviceAdapter.h"
#include "vtkCollection.h"
#include "vtkDataObject.h"
#include "vtkDataSetAttributes.h"
#include "vtkGLSLShaderProgram.h"
#include "vtkObjectFactory.h"
#include <vtkstd/map>
#include <vtkstd/string>
#include "vtkgl.h"
class vtkGLSLShaderDeviceAdapter::vtkInternal
{
public:
typedef vtkstd::map<vtkstd::string, int> MapOfStringToInt;
MapOfStringToInt AttributeIndicesCache;
};
vtkCxxRevisionMacro(vtkGLSLShaderDeviceAdapter, "1.1");
vtkStandardNewMacro(vtkGLSLShaderDeviceAdapter);