[vtkusers] Problems with uniform shader variables in Cg shader
Rocco Gasteiger
post at rocco-gasteiger.de
Thu Oct 15 11:06:14 EDT 2009
Hi Cory,
Thanks a lot! This was my mistake. Now, it works! :-)
Best regards, Rocco
-----Original Message-----
From: cquammen at gmail.com [mailto:cquammen at gmail.com] On Behalf Of Cory
Quammen
Sent: Thursday, October 15, 2009 4:28 PM
To: Rocco Gasteiger
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Problems with uniform shader variables in Cg shader
Rocco,
Try declaring your uniforms as ApplicationUniforms, e.g.:
<ApplicationUniform name="Ka" value="Ambient" />
<ApplicationUniform name="AmbientColor" value="AmbientColor" />
<ApplicationUniform name="Kd" value="Diffuse" />
<ApplicationUniform name="DiffuseColor" value="DiffuseColor" />
<ApplicationUniform name="Ks" value="Specular" />
<ApplicationUniform name="Ksp" value="SpecularPower" />
<ApplicationUniform name="SpecularColor" value="SpecularColor" />
Your light properties can be set in the same way.
This works in an XML-material file I am using.
Best,
Cory
On Thu, Oct 15, 2009 at 9:17 AM, Rocco Gasteiger
<post at rocco-gasteiger.de> wrote:
> Dear vtk community,
>
>
>
> For my research project I want to use the Cg shader support of VTK. I can
> load a VTK included XML-material file into a vtkProperty object, which
reads
> the shader code correctly. However, if I add uniform shader variables,
which
> are used within the shader code, it has no influence for the rendering.
> Below I present the xml-material file (provided by VTK), my vtk-code and
the
> cg-shader code (provided by VTK).
>
>
>
> Does anybody know what I have done wrong or missed?
>
>
>
> Thanks in advanced and best regards,
>
> Rocco Gasteiger
>
>
>
> --------------------------------------------------
>
> Dipl.-Ing. Rocco Gasteiger
>
> Otto-von-Guericke University
> Faculty of Computer Science
> Department of Simulation and Graphics
> Universitätsplatz 2, 39106 Magdeburg, Germany
>
>
>
> Office: G29-223
>
> Phone: +49 391 67 127 59
> Fax: +49 391 67 111 64
>
> Website: http://wwwisg.cs.uni-magdeburg.de/cvcms/
>
> --------------------------------------------------
>
>
>
>
>
> ********************
>
> XML-Material file:
>
> ********************
>
>
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <Material name="CgTestMaterial">
>
> <Property name="Property1">
>
> <Member name="DiffuseColor" number_of_elements="3" type="Float"
> value="1.0 1.0 1.0" />
>
> <Member name="Ambient" number_of_elements="3" type="Float" value="1.0
> 0.0 1.0" />
>
> </Property>
>
>
>
> <Shader scope="Vertex" name="CGVertexLighting" location="Library"
> language="Cg"
>
> entry="main" args="-DVERTEX_PROGRAM">
>
> <MatrixUniform name="ModelViewProj" type="State"
number_of_elements="2"
>
> value="CG_GL_MODELVIEW_PROJECTION_MATRIX CG_GL_MATRIX_IDENTITY">
>
> </MatrixUniform>
>
>
>
> <PropertyUniform name="Ka" value="Ambient" />
>
> <PropertyUniform name="AmbientColor" value="AmbientColor" />
>
> <PropertyUniform name="Kd" value="Diffuse" />
>
> <PropertyUniform name="DiffuseColor" value="DiffuseColor" />
>
> <PropertyUniform name="Ks" value="Specular" />
>
> <PropertyUniform name="Ksp" value="SpecularPower" />
>
> <PropertyUniform name="SpecularColor" value="SpecularColor" />
>
>
>
> <LightUniform name="lightPosition" value="Position" />
>
> <LightUniform name="lightDiffuseColor" value="DiffuseColor" />
>
> <LightUniform name="lightSpecularColor" value="SpecularColor" />
>
> <LightUniform name="lightAmbientColor" value="AmbientColor" />
>
>
>
> <CameraUniform name="eyePosition" value="Position" />
>
> </Shader>
>
> </Material>
>
>
>
>
>
> ********************
>
> My vtk-Code:
>
> ********************
>
>
>
> vtkSphereSource* sphere = vtkSphereSource::New();
>
>
>
> vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
>
> sphereMapper->SetInput(sphere->GetOutput());
>
>
>
> vtkActor *sphereActor = vtkActor::New();
>
> sphereActor->SetMapper(sphereMapper);
>
>
>
sphereActor->GetProperty()->LoadMaterial("D:\\Bibliotheken\\VTK\\VTKSource\\
Utilities\\MaterialLibrary\\Materials\\CgLighting.xml");
>
> sphereActor->GetProperty()->ShadingOn();
>
> // I also tried the different version of AddShaderVariable() but it
> didnt help.
>
> float color = (0.0, 0.0, 0.0);
>
> sphereActor->GetProperty()->AddShaderVariable("DiffuseColor", 3,
&color);
>
>
>
> vtkRenderer *renderer = vtkRenderer::New();
>
> vtkRenderWindow *renWin = vtkRenderWindow::New();
>
> renWin->AddRenderer(renderer);
>
>
>
> vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
>
> iren->SetRenderWindow(renWin);
>
>
>
> renderer->AddActor(sphereActor);
>
> renderer->ResetCamera();
>
>
>
> renWin->Render();
>
> iren->Start();
>
>
>
>
>
> ********************
>
> The Cg-Shader code:
>
> ********************
>
>
>
> struct vertin
>
> {
>
> float4 Position: POSITION;
>
> float3 Normal: NORMAL;
>
> };
>
>
>
> struct vertout
>
> {
>
> float4 Position : POSITION;
>
> float4 Color : COLOR;
>
> };
>
>
>
> vertout main(vertin IN,
>
> uniform float4x4 ModelViewProj,
>
> uniform float Ka,
>
> uniform float3 AmbientColor,
>
> uniform float Kd,
>
> uniform float3 DiffuseColor,
>
> uniform float Ks,
>
> uniform float Ksp,
>
> uniform float3 SpecularColor,
>
>
>
> uniform float3 lightPosition,
>
> uniform float3 eyePosition,
>
> uniform float3 lightDiffuseColor,
>
> uniform float3 lightAmbientColor,
>
> uniform float3 lightSpecularColor
>
> )
>
> {
>
> float3 N = IN.Normal; //invert(IN.Normal);
>
> vertout OUT;
>
> OUT.Position = mul(ModelViewProj, IN.Position);
>
>
>
> // Computer Ambient intensity.
>
> float3 ambient = Ka * AmbientColor * lightAmbientColor;
>
>
>
> // Comput Diffuse intensity.
>
> float3 L = normalize(lightPosition - IN.Position);
>
> float diffuseLight = max(dot(N, L), 0);
>
> float3 diffuse = Kd * DiffuseColor * lightDiffuseColor * diffuseLight;
>
>
>
> // Compute Specular intensity.
>
> float3 V = normalize(eyePosition - IN.Position);
>
> float3 H = normalize(L + V);
>
> float specularLight = pow(max(dot(N, H), 0), Ksp);
>
> if (diffuseLight <= 0) specularLight = 0;
>
> float3 specular = Ks * SpecularColor * lightSpecularColor *
specularLight;
>
>
>
> OUT.Color.xyz = ambient + diffuse + specular;
>
> OUT.Color.w = 1;
>
>
>
> return OUT;
>
> }
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> 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
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
--
Cory Quammen
Center for Computer Integrated Systems for Microscopy and Manipulation
(CISMM)
Department of Computer Science
University of North Carolina at Chapel Hill
http://www.cs.unc.edu/~cquammen
More information about the vtkusers
mailing list