Skip to content
Snippets Groups Projects
Commit 1c7d1cd0 authored by Sankhesh Jhaveri's avatar Sankhesh Jhaveri :speech_balloon:
Browse files

Added test that demonstrates ability to set shader uniforms from python

parent e1a4d5b9
No related branches found
No related tags found
No related merge requests found
set (RenderingOpenGL2PythonTests
TestUserShader2.py
)
if ("${VTK_RENDERING_BACKEND}" STREQUAL "OpenGL2")
vtk_add_test_python(
${RenderingOpenGL2PythonTests}
)
endif ()
#!/usr/bin/env python
'''
=========================================================================
Program: Visualization Toolkit
Module: TestUserShader2.py
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.
=========================================================================
'''
import sys
import vtk
from vtk.util.misc import vtkGetDataRoot
'''
Prevent .pyc files from being created.
Stops the vtk source being polluted
by .pyc files.
'''
sys.dont_write_bytecode = True
@vtk.calldata_type(vtk.VTK_OBJECT)
def vtkShaderCallback(caller, event, calldata):
program = calldata
if program is not None:
diffuseColor = [0.4, 0.7, 0.6]
program.SetUniform3f("diffuseColorUniform", diffuseColor)
renWin = vtk.vtkRenderWindow()
renWin.SetSize(400, 400)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren = vtk.vtkRenderer()
ren.SetBackground(0.0, 0.0, 0.0)
ren.GradientBackgroundOn()
renWin.AddRenderer(ren)
actor = vtk.vtkActor()
ren.AddActor(actor)
reader = vtk.vtkPLYReader()
reader.SetFileName("" + str(vtkGetDataRoot()) + "/Data/dragon.ply")
norms = vtk.vtkTriangleMeshPointNormals()
norms.SetInputConnection(reader.GetOutputPort())
mapper = vtk.vtkOpenGLPolyDataMapper()
mapper.SetInputConnection(norms.GetOutputPort())
actor.SetMapper(mapper)
actor.GetProperty().SetAmbientColor(0.2, 0.2, 1.0)
actor.GetProperty().SetDiffuseColor(1.0, 0.65, 0.7)
actor.GetProperty().SetSpecularColor(1.0, 1.0, 1.0)
actor.GetProperty().SetSpecular(0.5)
actor.GetProperty().SetDiffuse(0.7)
actor.GetProperty().SetAmbient(0.5)
actor.GetProperty().SetSpecularPower(20.0)
actor.GetProperty().SetOpacity(1.0)
mapper.SetVertexShaderCode(
"//VTK::System::Dec\n"
"attribute vec4 vertexMC;\n"
"//VTK::Normal::Dec\n"
"uniform mat4 MCDCMatrix;\n"
"void main () {\n"
" normalVCVSOutput = normalMatrix * normalMC;\n"
" vec4 tmpPos = MCDCMatrix * vertexMC;\n"
" gl_Position = tmpPos*vec4(0.2+0.8*abs(tmpPos.x),0.2+0.8*abs(tmpPos.y),1.0,1.0);\n"
"}\n"
)
mapper.SetFragmentShaderCode(
"//VTK::System::Dec\n"
"//VTK::Output::Dec\n"
"varying vec3 normalVCVSOutput;\n"
"uniform vec3 diffuseColorUniform;\n"
"void main () {\n"
" float df = max(0.0, normalVCVSOutput.z);\n"
" float sf = pow(df, 20.0);\n"
" vec3 diffuse = df * diffuseColorUniform;\n"
" vec3 specular = sf * vec3(0.4,0.4,0.4);\n"
" gl_FragData[0] = vec4(0.3*abs(normalVCVSOutput) + 0.7*diffuse + specular, 1.0);\n"
"}\n"
)
mapper.AddObserver(vtk.vtkCommand.UpdateShaderEvent, vtkShaderCallback)
renWin.Render()
ren.GetActiveCamera().SetPosition(-0.2, 0.4, 1)
ren.GetActiveCamera().SetFocalPoint(0, 0, 0)
ren.GetActiveCamera().SetViewUp(0, 1, 0)
ren.ResetCamera()
ren.GetActiveCamera().Zoom(2.0)
renWin.Render()
iren.Start()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment