Skip to content
Snippets Groups Projects
Commit a09874d8 authored by Bill Lorensen's avatar Bill Lorensen
Browse files

ENH: Add CubeMap and SphereMap shader examples.

parent 9d120791
No related branches found
No related tags found
No related merge requests found
......@@ -842,6 +842,8 @@ This section includes vtkUnstructuredGrid.
| Example Name | Classes Demonstrated | Description | Image |
| -------------- | ---------------------- | ------------- | ------- |
[ColorByNormal](/Cxx/Shaders/ColorByNormal) | vtkOpenGLPolyDataMapper vtkTriangleMeshPointNormals | Modify the shader to color based on model normal.
[CubeMap](/Cxx/Shaders/CubeMap) | vtkShaderProperty vtkOpenGLPolyDataMapper | Modify the Vertex and Frament shaders to apply a 6-sided cube map.
[SphereMap](/Cxx/Shaders/SphereMap) | vtkShaderProperty vtkOpenGLPolyDataMapper | Modify the Vertex and Frament shaders to apply a spherical map.
## Texture Mapping
......
......@@ -20,10 +20,19 @@ endforeach()
set(KIT Shaders)
set(NEEDS_ARGS
ColorByNormal
CubeMap
SphereMap
)
set(DATA ${WikiExamples_SOURCE_DIR}/src/Testing/Data)
add_test(${KIT}-ColorByNormal ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${KIT}CxxTests
TestColorByNormal ${DATA}/Armadillo.ply)
add_test(${KIT}-CubeMap ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${KIT}CxxTests
TestCubeMap ${DATA}/horse.ply ${DATA}/skybox-px.jpg ${DATA}/skybox-nx.jpg ${DATA}/skybox-py.jpg ${DATA}/skybox-ny.jpg ${DATA}/skybox-pz.jpg ${DATA}/skybox-nz.jpg)
add_test(${KIT}-SphereMap ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${KIT}CxxTests
TestSphereMap ${DATA}/Bunny.vtp ${DATA}/wintersun.jpg)
include(${WikiExamples_SOURCE_DIR}/CMake/ExamplesTesting.cmake)
// Inspired by the VTK test Rendering/OpenGL2/Testing/Cxx/TesCubeMap.cxx
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkImageFlip.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkJPEGReader.h>
#include <vtkOpenGLPolyDataMapper.h>
#include <vtkOpenGLRenderWindow.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataNormals.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkShaderProgram.h>
#include <vtkTexture.h>
#include <vtkNamedColors.h>
//----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
if (argc < 8)
{
std::cout << "Usage: " << argv[0]
<< " horse.ply skybox-px.jpg skybox-nx.jpg skybox-py.jpg skybox-ny.jpg skybox-pz.jpg skybox-nz.jpg" << std::endl;
return EXIT_FAILURE;
}
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(640,480);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkTexture> texture =
vtkSmartPointer<vtkTexture>::New();
texture->CubeMapOn();
vtkSmartPointer<vtkPLYReader> reader =
vtkSmartPointer<vtkPLYReader>::New();
reader->SetFileName(argv[1]);
vtkSmartPointer<vtkPolyDataNormals> norms =
vtkSmartPointer<vtkPolyDataNormals>::New();
norms->SetInputConnection(reader->GetOutputPort());
for (int i = 0; i < 6; i++)
{
vtkSmartPointer<vtkJPEGReader> imgReader =
vtkSmartPointer<vtkJPEGReader>::New();
imgReader->SetFileName( argv[i + 2] );
vtkSmartPointer<vtkImageFlip> flip =
vtkSmartPointer<vtkImageFlip>::New();
flip->SetInputConnection(imgReader->GetOutputPort());
flip->SetFilteredAxis(1); // flip y axis
texture->SetInputConnection(i, flip->GetOutputPort(0));
}
vtkSmartPointer<vtkOpenGLPolyDataMapper> mapper =
vtkSmartPointer<vtkOpenGLPolyDataMapper>::New();
mapper->SetInputConnection(norms->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
renderer->AddActor(actor);
actor->SetTexture(texture);
actor->SetMapper(mapper);
// Add new code in default VTK vertex shader
mapper->AddShaderReplacement(
vtkShader::Vertex,
"//VTK::PositionVC::Dec", // replace the normal block
true, // before the standard replacements
"//VTK::PositionVC::Dec\n" // we still want the default
"out vec3 TexCoords;\n",
false // only do it once
);
mapper->AddShaderReplacement(
vtkShader::Vertex,
"//VTK::PositionVC::Impl", // replace the normal block
true, // before the standard replacements
"//VTK::PositionVC::Impl\n" // we still want the default
"vec3 camPos = -MCVCMatrix[3].xyz * mat3(MCVCMatrix);\n"
"TexCoords.xyz = reflect(vertexMC.xyz - camPos, normalize(normalMC));\n",
false // only do it once
);
// Replace VTK fragment shader
mapper->SetFragmentShaderCode(
"//VTK::System::Dec\n" // always start with this line
"//VTK::Output::Dec\n" // always have this line in your FS
"in vec3 TexCoords;\n"
"uniform samplerCube texture_0;\n"
"void main () {\n"
" gl_FragData[0] = texture(texture_0, TexCoords);\n"
"}\n"
);
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
renderer->GetActiveCamera()->SetPosition(-.4, .2, .15);
renderer->GetActiveCamera()->SetViewUp(.3, -.1, .9);
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.4);
renderWindow->Render();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindow->GetInteractor()->SetInteractorStyle(style);
interactor->Start();
return EXIT_SUCCESS;
}
/*=========================================================================
Program: Visualization Toolkit
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkImageData.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkJPEGReader.h>
#include <vtkOpenGLPolyDataMapper.h>
#include <vtkOpenGLRenderWindow.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkPolyDataNormals.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkShaderProgram.h>
#include <vtkSkybox.h>
#include <vtkTexture.h>
#include <vtkNamedColors.h>
#include <vtkLight.h>
//----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(0.0, 0.0, 0.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(640, 480);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkLight> light =
vtkSmartPointer<vtkLight>::New();
light->SetLightTypeToSceneLight();
light->SetPosition(1.0,7.0,1.0);
renderer->AddLight(light);
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(argv[1]);
vtkSmartPointer<vtkPolyDataNormals> norms =
vtkSmartPointer<vtkPolyDataNormals>::New();
norms->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkTexture> texture =
vtkSmartPointer<vtkTexture>::New();
texture->InterpolateOn();
// mipmapping works on many systems but is not
// core 3.2 for cube maps. VTK will silently
// ignore it if it is not supported. We commented it
// out here to make valid images easier
// texture->MipmapOn();
vtkSmartPointer<vtkJPEGReader> imgReader =
vtkSmartPointer<vtkJPEGReader>::New();
imgReader->SetFileName(argv[2]);
texture->SetInputConnection(imgReader->GetOutputPort());
vtkSmartPointer<vtkOpenGLPolyDataMapper> mapper =
vtkSmartPointer<vtkOpenGLPolyDataMapper>::New();
mapper->SetInputConnection(norms->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetPosition(0, 0, 0);
actor->SetScale(6.0, 6.0, 6.0);
actor->GetProperty()->SetSpecular(0.8);
actor->GetProperty()->SetSpecularPower(20);
actor->GetProperty()->SetDiffuse(0.1);
actor->GetProperty()->SetAmbient(0.1);
actor->GetProperty()->SetDiffuseColor(1.0,0.0,0.4);
actor->GetProperty()->SetAmbientColor(0.4,0.0,1.0);
renderer->AddActor(actor);
actor->SetTexture(texture);
actor->SetMapper(mapper);
mapper->AddShaderReplacement(
vtkShader::Vertex,
"//VTK::PositionVC::Dec", // replace
true, // before the standard replacements
"//VTK::PositionVC::Dec\n" // we still want the default
"out vec3 TexCoords;\n",
false // only do it once
);
mapper->AddShaderReplacement(
vtkShader::Vertex,
"//VTK::PositionVC::Impl", // replace
true, // before the standard replacements
"//VTK::PositionVC::Impl\n" // we still want the default
"vec3 camPos = -MCVCMatrix[3].xyz * mat3(MCVCMatrix);\n"
"TexCoords.xyz = reflect(vertexMC.xyz - camPos, normalize(normalMC));\n",
false // only do it once
);
mapper->AddShaderReplacement(
vtkShader::Fragment,
"//VTK::Light::Dec", // replace
true, // before the standard replacements
"//VTK::Light::Dec\n" // we still want the default
"in vec3 TexCoords;\n",
false // only do it once
);
mapper->AddShaderReplacement(
vtkShader::Fragment,
"//VTK::Light::Impl", // replace
true, // before the standard replacements
"//VTK::Light::Impl\n"
" float phix = length(vec2(TexCoords.x, TexCoords.z));\n"
" vec3 skyColor = texture(actortexture, vec2(0.5*atan(TexCoords.z, TexCoords.x)/3.1415927 + 0.5, atan(TexCoords.y,phix)/3.1415927 + 0.5)).xyz;\n"
" gl_FragData[0] = vec4(ambientColor + diffuse + specular + specularColor*skyColor, opacity);\n"
, // we still want the default
false // only do it once
);
vtkSmartPointer<vtkSkybox> world =
vtkSmartPointer<vtkSkybox>::New();
world->SetProjectionToSphere();
world->SetTexture(texture);
renderer->AddActor(world);
renderer->GetActiveCamera()->SetPosition(0.0, 0.55, 2.0);
renderer->GetActiveCamera()->SetFocalPoint(0.0, 0.55, 0.0);
renderer->GetActiveCamera()->SetViewAngle(60.0);
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.1);
renderer->GetActiveCamera()->Azimuth(0);
renderer->GetActiveCamera()->Elevation(5);
renderer->ResetCameraClippingRange();
renderWindow->Render();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindow->GetInteractor()->SetInteractorStyle(style);
interactor->Start();
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment