|
| 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 = 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.125); |
| 37 | + sphere1->GetProperty()->SetDiffuse(0.0); |
| 38 | + sphere1->GetProperty()->SetSpecular(0.0); |
| 39 | + |
| 40 | + vtkSmartPointer<vtkActor> sphere2 = vtkSmartPointer<vtkActor>::New(); |
| 41 | + sphere2->SetMapper(sphereMapper); |
| 42 | + sphere2->GetProperty()->SetColor(1,0,0); |
| 43 | + sphere2->GetProperty()->SetAmbient(0.25); |
| 44 | + sphere2->GetProperty()->SetDiffuse(0.0); |
| 45 | + sphere2->GetProperty()->SetSpecular(0.0); |
| 46 | + sphere2->AddPosition(1.25,0,0); |
| 47 | + |
| 48 | + vtkSmartPointer<vtkActor> sphere3 = vtkSmartPointer<vtkActor>::New(); |
| 49 | + sphere3->SetMapper(sphereMapper); |
| 50 | + sphere3->GetProperty()->SetColor(1,0,0); |
| 51 | + sphere3->GetProperty()->SetAmbient(0.375); |
| 52 | + sphere3->GetProperty()->SetDiffuse(0.0); |
| 53 | + sphere3->GetProperty()->SetSpecular(0.0); |
| 54 | + sphere3->AddPosition(2.5,0,0); |
| 55 | + |
| 56 | + vtkSmartPointer<vtkActor> sphere4 = vtkSmartPointer<vtkActor>::New(); |
| 57 | + sphere4->SetMapper(sphereMapper); |
| 58 | + sphere4->GetProperty()->SetColor(1,0,0); |
| 59 | + sphere4->GetProperty()->SetAmbient(0.5); |
| 60 | + sphere4->GetProperty()->SetDiffuse(0.0); |
| 61 | + sphere4->GetProperty()->SetSpecular(0.0); |
| 62 | + sphere4->AddPosition(3.75,0,0); |
| 63 | + |
| 64 | + vtkSmartPointer<vtkActor> sphere5 = vtkSmartPointer<vtkActor>::New(); |
| 65 | + sphere5->SetMapper(sphereMapper); |
| 66 | + sphere5->GetProperty()->SetColor(1,0,0); |
| 67 | + sphere5->GetProperty()->SetAmbient(0.625); |
| 68 | + sphere5->GetProperty()->SetDiffuse(0.0); |
| 69 | + sphere5->GetProperty()->SetSpecular(0.0); |
| 70 | + sphere5->AddPosition(0.0,1.25,0); |
| 71 | + |
| 72 | + vtkSmartPointer<vtkActor> sphere6 = vtkSmartPointer<vtkActor>::New(); |
| 73 | + sphere6->SetMapper(sphereMapper); |
| 74 | + sphere6->GetProperty()->SetColor(1,0,0); |
| 75 | + sphere6->GetProperty()->SetAmbient(0.75); |
| 76 | + sphere6->GetProperty()->SetDiffuse(0.0); |
| 77 | + sphere6->GetProperty()->SetSpecular(0.0); |
| 78 | + sphere6->AddPosition(1.25,1.25,0); |
| 79 | + |
| 80 | + vtkSmartPointer<vtkActor> sphere7 = vtkSmartPointer<vtkActor>::New(); |
| 81 | + sphere7->SetMapper(sphereMapper); |
| 82 | + sphere7->GetProperty()->SetColor(1,0,0); |
| 83 | + sphere7->GetProperty()->SetAmbient(0.875); |
| 84 | + sphere7->GetProperty()->SetDiffuse(0.0); |
| 85 | + sphere7->GetProperty()->SetSpecular(0.0); |
| 86 | + sphere7->AddPosition(2.5,1.25,0); |
| 87 | + |
| 88 | + vtkSmartPointer<vtkActor> sphere8 = vtkSmartPointer<vtkActor>::New(); |
| 89 | + sphere8->SetMapper(sphereMapper); |
| 90 | + sphere8->GetProperty()->SetColor(1,0,0); |
| 91 | + sphere8->GetProperty()->SetAmbient(1.0); |
| 92 | + sphere8->GetProperty()->SetDiffuse(0.0); |
| 93 | + sphere8->GetProperty()->SetSpecular(0.0); |
| 94 | + sphere8->AddPosition(3.75,1.25,0); |
| 95 | + |
| 96 | + // Create the graphics structure. The renderer renders into the |
| 97 | + // render window. The render window interactor captures mouse events |
| 98 | + // and will perform appropriate camera or actor manipulation |
| 99 | + // depending on the nature of the events. |
| 100 | + // |
| 101 | + vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New(); |
| 102 | + vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New(); |
| 103 | + renWin->AddRenderer(ren1); |
| 104 | + vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New(); |
| 105 | + iren->SetRenderWindow(renWin); |
| 106 | + |
| 107 | + // Add the actors to the renderer, set the background and size. |
| 108 | + // |
| 109 | + ren1->AddActor(sphere1); |
| 110 | + ren1->AddActor(sphere2); |
| 111 | + ren1->AddActor(sphere3); |
| 112 | + ren1->AddActor(sphere4); |
| 113 | + ren1->AddActor(sphere5); |
| 114 | + ren1->AddActor(sphere6); |
| 115 | + ren1->AddActor(sphere7); |
| 116 | + ren1->AddActor(sphere8); |
| 117 | + ren1->SetBackground(0.1, 0.2, 0.4); |
| 118 | + renWin->SetSize(400, 200); |
| 119 | + |
| 120 | + // Set up the lighting. |
| 121 | + // |
| 122 | + vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New(); |
| 123 | + light->SetFocalPoint(1.875,0.6125,0); |
| 124 | + light->SetPosition(0.875,1.6125,1); |
| 125 | + ren1->AddLight(light); |
| 126 | + |
| 127 | + // We want to eliminate perspective effects on the apparent lighting. |
| 128 | + // Parallel camera projection will be used. To zoom in parallel projection |
| 129 | + // mode, the ParallelScale is set. |
| 130 | + // |
| 131 | + ren1->GetActiveCamera()->SetFocalPoint(0,0,0); |
| 132 | + ren1->GetActiveCamera()->SetPosition(0,0,1); |
| 133 | + ren1->GetActiveCamera()->SetViewUp(0,1,0); |
| 134 | + ren1->GetActiveCamera()->ParallelProjectionOn(); |
| 135 | + ren1->ResetCamera(); |
| 136 | + ren1->GetActiveCamera()->SetParallelScale(1.5); |
| 137 | + |
| 138 | + // This starts the event loop and invokes an initial render. |
| 139 | + // |
| 140 | + iren->Initialize(); |
| 141 | + iren->Start(); |
| 142 | + |
| 143 | + return EXIT_SUCCESS; |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
0 commit comments