Skip to content

Commit 2cc7894

Browse files
author
Jon Haitz Legarreta Gorroño
committed
ENH: Add DiffuseSpheres example.
Add the DiffuseSpheres.cxx example from the VTK source tree to the VTK wiki example repository.
1 parent 07b8439 commit 2cc7894

4 files changed

Lines changed: 183 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ ADD_SUBDIRECTORY(Cxx/PolyData)
105105
ADD_SUBDIRECTORY(Cxx/Points)
106106
ADD_SUBDIRECTORY(Cxx/RectilinearGrid)
107107
ADD_SUBDIRECTORY(Cxx/Remote)
108+
ADD_SUBDIRECTORY(Cxx/Rendering)
108109
ADD_SUBDIRECTORY(Cxx/RenderMan)
109110
ADD_SUBDIRECTORY(Cxx/SimpleOperations)
110111
ADD_SUBDIRECTORY(Cxx/StructuredGrid)

Cxx/Rendering/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
PROJECT (${WIKI}Rendering)
2+
3+
IF(NOT WikiExamples_BINARY_DIR)
4+
FIND_PACKAGE(VTK REQUIRED)
5+
IF(NOT VTK_USE_RENDERING)
6+
MESSAGE(FATAL_ERROR "Example ${PROJECT_NAME} requires VTK_USE_RENDERING.")
7+
ENDIF(NOT VTK_USE_RENDERING)
8+
INCLUDE(${VTK_USE_FILE})
9+
ENDIF(NOT WikiExamples_BINARY_DIR)
10+
11+
if("${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}" LESS 5.8)
12+
SET(KIT_LIBS vtkHybrid vtkWidgets)
13+
else()
14+
SET(KIT_LIBS ${VTK_LIBRARIES})
15+
endif()
16+
#
17+
# Build all .cxx files in the directory
18+
FILE(GLOB ALL_FILES *.cxx)
19+
foreach(SOURCE_FILE ${ALL_FILES})
20+
STRING(REPLACE ".cxx" "" TMP ${SOURCE_FILE})
21+
STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" EXAMPLE ${TMP})
22+
ADD_EXECUTABLE(${WIKI}${EXAMPLE} ${EXECUTABLE_FLAG} ${EXAMPLE}.cxx)
23+
TARGET_LINK_LIBRARIES(${WIKI}${EXAMPLE} ${KIT_LIBS})
24+
endforeach(SOURCE_FILE)
25+
26+
if (BUILD_TESTING)
27+
# Testing
28+
SET(KIT Rendering)
29+
SET(NEEDS_ARGS
30+
)
31+
INCLUDE(${WikiExamples_SOURCE_DIR}/CMake/ExamplesTesting.cmake)
32+
endif()
File renamed without changes.

Cxx/Rendering/DiffuseSpheres.cxx

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include "vtkSmartPointer.h"
2+
#include "vtkSphereSource.h"
3+
#include "vtkPolyDataMapper.h"
4+
#include "vtkActor.h"
5+
#include "vtkRenderer.h"
6+
#include "vtkRenderWindow.h"
7+
#include "vtkRenderWindowInteractor.h"
8+
#include "vtkProperty.h"
9+
#include "vtkCamera.h"
10+
#include "vtkLight.h"
11+
12+
int main()
13+
{
14+
// The following lines create a sphere represented by polygons.
15+
//
16+
vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
17+
sphere->SetThetaResolution(100);
18+
sphere->SetPhiResolution(50);
19+
20+
// The mapper is responsible for pushing the geometry into the graphics
21+
// library. It may also do color mapping, if scalars or other attributes
22+
// are defined.
23+
//
24+
vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
25+
sphereMapper->SetInputConnection(sphere->GetOutputPort());
26+
27+
// The actor is a grouping mechanism: besides the geometry (mapper), it
28+
// also has a property, transformation matrix, and/or texture map.
29+
// In this example we create eight different spheres (two rows of four
30+
// spheres) and set the specular lighting coefficients. A little ambient
31+
// is turned on so the sphere is not completely black on the back side.
32+
//
33+
vtkSmartPointer<vtkActor> sphere1 = vtkSmartPointer<vtkActor>::New();
34+
sphere1->SetMapper(sphereMapper);
35+
sphere1->GetProperty()->SetColor(1,0,0);
36+
sphere1->GetProperty()->SetAmbient(0.3);
37+
sphere1->GetProperty()->SetDiffuse(0.0);
38+
sphere1->GetProperty()->SetSpecular(0.0);
39+
sphere1->GetProperty()->SetSpecularPower(5.0);
40+
41+
vtkSmartPointer<vtkActor> sphere2 = vtkSmartPointer<vtkActor>::New();
42+
sphere2->SetMapper(sphereMapper);
43+
sphere2->GetProperty()->SetColor(1,0,0);
44+
sphere2->GetProperty()->SetAmbient(0.3);
45+
sphere2->GetProperty()->SetDiffuse(0.125);
46+
sphere2->GetProperty()->SetSpecular(0.0);
47+
sphere2->GetProperty()->SetSpecularPower(10.0);
48+
sphere2->AddPosition(1.25,0,0);
49+
50+
vtkSmartPointer<vtkActor> sphere3 = vtkSmartPointer<vtkActor>::New();
51+
sphere3->SetMapper(sphereMapper);
52+
sphere3->GetProperty()->SetColor(1,0,0);
53+
sphere3->GetProperty()->SetAmbient(0.3);
54+
sphere3->GetProperty()->SetDiffuse(0.25);
55+
sphere3->GetProperty()->SetSpecular(0.0);
56+
sphere3->AddPosition(2.5,0,0);
57+
58+
vtkSmartPointer<vtkActor> sphere4 = vtkSmartPointer<vtkActor>::New();
59+
sphere4->SetMapper(sphereMapper);
60+
sphere4->GetProperty()->SetColor(1,0,0);
61+
sphere4->GetProperty()->SetAmbient(0.3);
62+
sphere4->GetProperty()->SetDiffuse(0.375);
63+
sphere4->GetProperty()->SetSpecular(0.0);
64+
sphere4->AddPosition(3.75,0,0);
65+
66+
vtkSmartPointer<vtkActor> sphere5 = vtkSmartPointer<vtkActor>::New();
67+
sphere5->SetMapper(sphereMapper);
68+
sphere5->GetProperty()->SetColor(1,0,0);
69+
sphere5->GetProperty()->SetAmbient(0.3);
70+
sphere5->GetProperty()->SetDiffuse(0.5);
71+
sphere5->GetProperty()->SetSpecular(0.0);
72+
sphere5->AddPosition(0.0,1.25,0);
73+
74+
vtkSmartPointer<vtkActor> sphere6 = vtkSmartPointer<vtkActor>::New();
75+
sphere6->SetMapper(sphereMapper);
76+
sphere6->GetProperty()->SetColor(1,0,0);
77+
sphere6->GetProperty()->SetAmbient(0.3);
78+
sphere6->GetProperty()->SetDiffuse(0.625);
79+
sphere6->GetProperty()->SetSpecular(0.0);
80+
sphere6->AddPosition(1.25,1.25,0);
81+
82+
vtkSmartPointer<vtkActor> sphere7 = vtkSmartPointer<vtkActor>::New();
83+
sphere7->SetMapper(sphereMapper);
84+
sphere7->GetProperty()->SetColor(1,0,0);
85+
sphere7->GetProperty()->SetAmbient(0.3);
86+
sphere7->GetProperty()->SetDiffuse(0.75);
87+
sphere7->GetProperty()->SetSpecular(0.0);
88+
sphere7->AddPosition(2.5,1.25,0);
89+
90+
vtkSmartPointer<vtkActor> sphere8 = vtkSmartPointer<vtkActor>::New();
91+
sphere8->SetMapper(sphereMapper);
92+
sphere8->GetProperty()->SetColor(1,0,0);
93+
sphere8->GetProperty()->SetAmbient(0.3);
94+
sphere8->GetProperty()->SetDiffuse(0.875);
95+
sphere8->GetProperty()->SetSpecular(0.0);
96+
sphere8->AddPosition(3.75,1.25,0);
97+
98+
// Create the graphics structure. The renderer renders into the
99+
// render window. The render window interactor captures mouse events
100+
// and will perform appropriate camera or actor manipulation
101+
// depending on the nature of the events.
102+
//
103+
vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
104+
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
105+
renWin->AddRenderer(ren1);
106+
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
107+
iren->SetRenderWindow(renWin);
108+
109+
// Add the actors to the renderer, set the background and size.
110+
//
111+
ren1->AddActor(sphere1);
112+
ren1->AddActor(sphere2);
113+
ren1->AddActor(sphere3);
114+
ren1->AddActor(sphere4);
115+
ren1->AddActor(sphere5);
116+
ren1->AddActor(sphere6);
117+
ren1->AddActor(sphere7);
118+
ren1->AddActor(sphere8);
119+
ren1->SetBackground(0.1, 0.2, 0.4);
120+
renWin->SetSize(400, 200);
121+
122+
// Set up the lighting.
123+
//
124+
vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();
125+
light->SetFocalPoint(1.875,0.6125,0);
126+
light->SetPosition(0.875,1.6125,1);
127+
ren1->AddLight(light);
128+
129+
// We want to eliminate perspective effects on the apparent lighting.
130+
// Parallel camera projection will be used. To zoom in parallel projection
131+
// mode, the ParallelScale is set.
132+
//
133+
ren1->GetActiveCamera()->SetFocalPoint(0,0,0);
134+
ren1->GetActiveCamera()->SetPosition(0,0,1);
135+
ren1->GetActiveCamera()->SetViewUp(0,1,0);
136+
ren1->GetActiveCamera()->ParallelProjectionOn();
137+
ren1->ResetCamera();
138+
ren1->GetActiveCamera()->SetParallelScale(1.5);
139+
140+
// This starts the event loop and invokes an initial render.
141+
//
142+
iren->Initialize();
143+
iren->Start();
144+
145+
return EXIT_SUCCESS;
146+
}
147+
148+
149+
150+

0 commit comments

Comments
 (0)