# ColorByNormal

VTKExamples/Cxx/Shaders/ColorByNormal

### Description

This example modifies the shader to color based on model normal To do this we have to modify the vertex shader to pass the normal in model coordinates through to the fragment shader. By default the normal is converted to View coordinates and then passed on. We keep that, but add a varying for the original normal. Then we modify the fragment shader to set the diffuse color based on that normal.

Cite

This example was inspired by the test TestUserShader.

### Code

ColorByNormal.cxx

    #include <vtkSmartPointer.h>

    #include <vtkCamera.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkActor.h>
    #include <vtkOpenGLPolyDataMapper.h>
    #include <vtkPLYReader.h>
    #include <vtkProperty.h>
    #include <vtkPolyDataNormals.h>
    #include <vtkTriangleMeshPointNormals.h>
    #include <vtkNamedColors.h>
    #include <vtkRenderWindowInteractor.h>

    #include <vtksys/SystemTools.hxx>
    #include <vtkBYUReader.h>
    #include <vtkOBJReader.h>
    #include <vtkPLYReader.h>
    #include <vtkPolyDataReader.h>
    #include <vtkSTLReader.h>
    #include <vtkXMLPolyDataReader.h>
    #include <vtkSphereSource.h>

    namespace
    {
    vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName);
    }

    //----------------------------------------------------------------------------
    int main(int argc, char *argv[])
    {
      vtkSmartPointer<vtkPolyData> polyData =
        ReadPolyData(argc > 1 ? argv[1] : "");

      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkOpenGLPolyDataMapper> mapper =
        vtkSmartPointer<vtkOpenGLPolyDataMapper>::New();
      renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->SetSize(640, 480);
      renderWindow->AddRenderer(renderer);
      renderer->AddActor(actor);

      vtkSmartPointer<vtkRenderWindowInteractor>  interactor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      interactor->SetRenderWindow(renderWindow);

      vtkSmartPointer<vtkTriangleMeshPointNormals> norms =
        vtkSmartPointer<vtkTriangleMeshPointNormals>::New();
      norms->SetInputData(polyData);

      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);

      // Modify the shader to color based on model normal
      // To do this we have to modify the vertex shader
      // to pass the normal in model coordinates
      // through to the fragment shader. By default the normal
      // is converted to View coordinates and then passed on.
      // We keep that, but add a varying for the original normal.
      // Then we modify the fragment shader to set the diffuse color
      // based on that normal. First lets modify the vertex
      // shader
      mapper->AddShaderReplacement(
        vtkShader::Vertex,
        "//VTK::Normal::Dec", // replace the normal block
        true, // before the standard replacements
        "//VTK::Normal::Dec\n" // we still want the default
        "  varying vec3 myNormalMCVSOutput;\n", //but we add this
        false // only do it once
        );
      mapper->AddShaderReplacement(
        vtkShader::Vertex,
        "//VTK::Normal::Impl", // replace the normal block
        true, // before the standard replacements
        "//VTK::Normal::Impl\n" // we still want the default
        "  myNormalMCVSOutput = normalMC;\n", //but we add this
        false // only do it once
        );
      mapper->AddShaderReplacement(
        vtkShader::Vertex,
        "//VTK::Color::Impl", // dummy replacement for testing clear method
        true,
        "VTK::Color::Impl\n",
        false
        );
      mapper->ClearShaderReplacement(
        vtkShader::Vertex,     // clear our dummy replacement
        "//VTK::Color::Impl",
        true
        );

      // now modify the fragment shader
      mapper->AddShaderReplacement(
        vtkShader::Fragment,  // in the fragment shader
        "//VTK::Normal::Dec", // replace the normal block
        true, // before the standard replacements
        "//VTK::Normal::Dec\n" // we still want the default
        "  varying vec3 myNormalMCVSOutput;\n", //but we add this
        false // only do it once
        );
      mapper->AddShaderReplacement(
        vtkShader::Fragment,  // in the fragment shader
        "//VTK::Normal::Impl", // replace the normal block
        true, // before the standard replacements
        "//VTK::Normal::Impl\n" // we still want the default calc
        "  diffuseColor = abs(myNormalMCVSOutput);\n", //but we add this
        false // only do it once
        );

      renderWindow->Render();
      renderer->GetActiveCamera()->SetPosition(1.0,1.0, -1.0);
      renderer->GetActiveCamera()->SetFocalPoint(0,1,0);
      renderer->GetActiveCamera()->SetViewUp(0,1,0);
      renderer->GetActiveCamera()->Elevation(30.0);
      renderer->ResetCamera();
      renderer->GetActiveCamera()->Zoom(1.3);
      renderWindow->Render();

      interactor->Start();

      return EXIT_SUCCESS;
    }

    namespace
    {
    vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName)
    {
      vtkSmartPointer<vtkPolyData> polyData;
      std::string extension = vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
      if (extension == ".ply")
      {
        vtkSmartPointer<vtkPLYReader> reader =
          vtkSmartPointer<vtkPLYReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".vtp")
      {
        vtkSmartPointer<vtkXMLPolyDataReader> reader =
          vtkSmartPointer<vtkXMLPolyDataReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".obj")
      {
        vtkSmartPointer<vtkOBJReader> reader =
          vtkSmartPointer<vtkOBJReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".stl")
      {
        vtkSmartPointer<vtkSTLReader> reader =
          vtkSmartPointer<vtkSTLReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".vtk")
      {
        vtkSmartPointer<vtkPolyDataReader> reader =
          vtkSmartPointer<vtkPolyDataReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".g")
      {
        vtkSmartPointer<vtkBYUReader> reader =
          vtkSmartPointer<vtkBYUReader>::New();
        reader->SetGeometryFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else
      {
        vtkSmartPointer<vtkSphereSource> source =
          vtkSmartPointer<vtkSphereSource>::New();
        source->Update();
        polyData = source->GetOutput();
      }
      return polyData;
    }
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(ColorByNormal)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(ColorByNormal MACOSX_BUNDLE ColorByNormal.cxx )

    target_link_libraries(ColorByNormal ${VTK_LIBRARIES})

### Download and Build ColorByNormal

Click here to download ColorByNormal and its CMakeLists.txt file. Once the tarball ColorByNormal.tar has been downloaded and extracted,

    cd ColorByNormal/build

If VTK is installed:

    cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

    cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

    make

and run it:

    ./ColorByNormal

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.