[vtkusers] Problems with uniform shader variables in Cg shader

Rocco Gasteiger post at rocco-gasteiger.de
Thu Oct 15 09:17:22 EDT 2009


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
didn’t 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;

}

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20091015/f30dcb47/attachment.htm>


More information about the vtkusers mailing list