Skip to content

Commit c00de99

Browse files
authored
Merge pull request #5 from jhlegarreta/AddSpecularSpheresExample
ENH: Add SpecularSpheres example.
2 parents e1663ce + a4fee9f commit c00de99

1 file changed

Lines changed: 170 additions & 0 deletions

File tree

Cxx/Rendering/SpecularSpheres.cxx

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

0 commit comments

Comments
 (0)