[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 08:17:00 EST 2017


Hi,

I want to ask how to pass uniform and attribute from host to shader by vtk’s python wrapping.

I follow this instruction <file:///Users/Quentan/Library/Caches/Marked%202/Watchers/[http://noeskasmit.com/shaders-vtk-cel-shaded-skull-example-100-lines-python-code/](http://noeskasmit.com/shaders-vtk-cel-shaded-skull-example-100-lines-python-code/)> to implement this result <file:///Users/Quentan/Library/Caches/Marked%202/Watchers/[https://www.shadertoy.com/view/XsXGRS](https://www.shadertoy.com/view/XsXGRS)>, 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);
}

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


More information about the vtkusers mailing list