[vtkusers] How to pass uniform and attribute from host to shader by vtk’s python wrapping.

Quentan Qi quentan at gmail.com
Mon Feb 20 10:14:10 EST 2017


Thanks Ken. 

I am using vtk 7.1.0

But the python wrapping of `vtkOpenGLPolyDataMapper` has no attribute `AddShaderReplacement`. Is there any way to do this with python wrapping?

Cheers,
Quentan


> On 20 Feb 2017, at 13:34, Ken Martin <ken.martin at kitware.com> wrote:
> 
> If using the new OpenGL rendering backend end see 
> 
> http://www.vtk.org/Wiki/Shader_In_VTK <http://www.vtk.org/Wiki/Shader_In_VTK>
> 
> 
> On Mon, Feb 20, 2017 at 8:17 AM, Quentan Qi <quentan at gmail.com <mailto:quentan at gmail.com>> wrote:
> Hi,
> 
> I want to ask how to pass uniform and attribute from host to shader by vtk’s python wrapping.
> 
> I follow this instruction <> to implement this result <>, but don’t know how to pass uniform variables from host to fragment shader. 
> 
> Could anybody tell me how to do this?
> 
> Cheers,
> Quentan
> 
> Shader02.py
> import vtk
> import time
> 
> timer = [time.clock()]
> resolution = [600, 480]
> 
> file = open('Shader02.vert', 'r')
> vert = file.read()
> file.close()
> 
> file = open('Shader02.frag', 'r')
> frag = file.read()
> file.close()
> 
> # Plane. Is there better way to show ShaderToy's effect without use vtkPlaneSource?
> plane = vtk.vtkPlaneSource()
> plane.SetNormal([0, 0, 1])
> plane.SetResolution(100, 100)
> plane.SetOrigin(-1, -1, 0)
> plane.SetPoint1(1, -1, 1)
> plane.SetPoint2(-1, 1, 0)
> 
> mapper = vtk.vtkPolyDataMapper()
> mapper.SetInputConnection(plane.GetOutputPort())
> 
> actor = vtk.vtkActor()
> actor.SetMapper(mapper)
> actor.GetProperty().SetOpacity(1)
> 
> # Handle the rendering and interaction
> ren = vtk.vtkRenderer()
> renWin = vtk.vtkRenderWindow()
> renWin.AddRenderer(ren)
> iren = vtk.vtkRenderWindowInteractor()
> iren.SetRenderWindow(renWin)
> 
> resolutionUniform = vtk.vtkUniformVariables()
> resolutionUniform.SetUniformi('iResolution', 2, resolution)
> 
> timeUniform = vtk.vtkUniformVariables()
> timeUniform.SetUniformf('iGlobalTime', 1, timer)
> 
> # Now let's get down to shader-business...
> # First we make a ShaderProgram2 and set it up on a date with the RenderWindow
> pgm = vtk.vtkShaderProgram2()
> pgm.SetContext(renWin)
> 
> # For both the vertex and fragment shader, we need to make a Shader2
> # Also set them up with the RenderWindow, by asking the ShaderProgram2 for
> # an introduction
> shaderv = vtk.vtkShader2()
> shaderv.SetType(vtk.VTK_SHADER_TYPE_VERTEX)
> shaderv.SetSourceCode(vert)
> shaderv.SetContext(pgm.GetContext())
> 
> shaderf = vtk.vtkShader2()
> shaderf.SetType(vtk.VTK_SHADER_TYPE_FRAGMENT)
> shaderf.SetSourceCode(frag)
> shaderf.SetContext(pgm.GetContext())
> shaderf.SetUniformVariables(resolutionUniform)  # is this correct?
> # print shaderf.GetUniformVariables()
> shaderf.SetUniformVariables(timeUniform)
> # print shaderf.GetUniformVariables()
> 
> # Now we add the shaders to the program
> pgm.GetShaders().AddItem(shaderv)
> pgm.GetShaders().AddItem(shaderf)
> 
> # And tell the actor property that it should totally use this cool program
> openGLproperty = actor.GetProperty()
> openGLproperty.SetPropProgram(pgm)
> openGLproperty.ShadingOn()
> 
> # openGLproperty.AddShaderVariable('iGlobalTime', 1, timer)  # is this correct?
> # openGLproperty.AddShaderVariable('iResolution', 2, resolution)
> 
> # Add the actor and set a nice bg color
> ren.AddActor(actor)
> ren.SetBackground(0.3, 0, 0.4)
> ren.SetBackground2(0.1, 0, 0.2)
> ren.SetGradientBackground(1)
> renWin.SetSize(resolution)
> 
> iren.Initialize()
> ren.GetActiveCamera().SetPosition(0, -1, 0)
> ren.GetActiveCamera().SetViewUp(0, 0, 1)
> ren.ResetCamera()
> renWin.Render()
> iren.Start()
> Shader02.vert
> void propFuncVS(void) {
>   gl_Position = vec4((gl_Vertex.xy), 0.0, 1.0);
> }
> Shader02.frag
> // ShaderToy: The Blob
> // https://www.shadertoy.com/view/XsXGRS <https://www.shadertoy.com/view/XsXGRS>
> 
> uniform float iGlobalTime;  // not work. uniform or varying?
> uniform vec2 iResolution;
> // float iGlobalTime = 1.0;
> // vec2 iResolution = vec2(600, 480);  // This works, but not from host
> 
> #ifdef GL_ES
> precision highp float;
> #endif
> 
> #define AA 4.
> 
> #define CI vec3(.3,.5,.6)
> #define CO vec3(.2)
> #define CM vec3(.0)
> #define CE vec3(.8,.7,.5)
> 
> float metaball(vec2 p, float r)
> {
>     return r / dot(p, p);
> }
> 
> vec3 samplef(in vec2 uv)
> {
> #if 1
>     float t0 = sin(iGlobalTime * 1.9) * .46;
>     float t1 = sin(iGlobalTime * 2.4) * .49;
>     float t2 = cos(iGlobalTime * 1.4) * .57;
> 
>     float r = metaball(uv + vec2(t0, t2), .33) *
>              metaball(uv - vec2(t0, t1), .27) *
>              metaball(uv + vec2(t1, t2), .59);
> 
> #else
>   float r = length(uv);  // test for time-independent
> 
> #endif
>     vec3 c = (r > .4 && r < .7)
>              ? (vec3(step(.1, r*r*r)) * CE)
>              : (r < .9 ? (r < .7 ? CO: CM) : CI);
> 
>     return c;
> }
> 
> void propFuncFS(void)
> {
>     vec2 uv = (gl_FragCoord.xy / iResolution.xy * 2. - 1.)
>             * vec2(iResolution.x / iResolution.y, 1) * 1.25;
> 
>     vec3 col = vec3(0);
> 
> #ifdef AA
>     // Antialiasing via supersampling
>     float e = 1. / min(iResolution.y , iResolution.x);
>     for (float i = -AA; i < AA; ++i) {
>         for (float j = -AA; j < AA; ++j) {
>     		col += samplef(uv + vec2(i, j) * (e/AA)) / (4.*AA*AA);
>         }
>     }
> #else
>     col += sample(uv);
> #endif /* AA */
> 
>     gl_FragColor = vec4(clamp(col, 0., 1.), 1);
> }
> 
> 
> _______________________________________________
> Powered by www.kitware.com <http://www.kitware.com/>
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html <http://www.kitware.com/opensource/opensource.html>
> 
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ <http://www.vtk.org/Wiki/VTK_FAQ>
> 
> Search the list archives at: http://markmail.org/search/?q=vtkusers <http://markmail.org/search/?q=vtkusers>
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers <http://public.kitware.com/mailman/listinfo/vtkusers>
> 
> 
> 
> 
> -- 
> Ken Martin PhD
> Chairman & CFO
> Kitware Inc.
> 28 Corporate Drive
> Clifton Park NY 12065
> 518 371 3971
> 
> This communication, including all attachments, contains confidential and legally privileged information, and it is intended only for the use of the addressee.  Access to this email by anyone else is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution or any action taken in reliance on it is prohibited and may be unlawful. If you received this communication in error please notify us immediately and destroy the original message.  Thank you.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170220/5ab9ee28/attachment.html>


More information about the vtkusers mailing list