光源有位置光源和平行光源。
代码:
1 //tips:如果不是用cmake编译,而是用vs编译的,需要加这三行。 2 #include "vtkAutoInit.h" 3 VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2 4 VTK_MODULE_INIT(vtkInteractionStyle); 5 6 #include <vtkConeSource.h> 7 #include <vtkRenderer.h> 8 #include <vtkPolyDataMapper.h> 9 #include <vtkRenderWindow.h> 10 #include <vtkActor.h> 11 #include <vtkCamera.h> 12 #include <vtkRenderWindowInteractor.h> 13 #include <vtkInteractorStyleTrackballCamera.h> 14 #include <vtkInteractorStyleTrackballActor.h> 15 #include <vtkProperty.h> 16 #include <vtkLight.h> 17 18 int main() 19 { 20 vtkConeSource *cone = vtkConeSource::New(); 21 cone->SetResolution(8); 22 cone->SetHeight(5); 23 cone->SetRadius(6); 24 25 vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New(); 26 coneMapper->SetInputData(cone->GetOutput()); 27 coneMapper->SetInputConnection(cone->GetOutputPort()); 28 29 vtkActor *vtkactor = vtkActor::New(); 30 vtkactor->SetMapper(coneMapper); 31 //vtkactor->GetProperty()->SetColor(1, 0, 0); 32 //不用GetProperty()的方法,用实例化Property的方法也可以。 33 vtkProperty *coneProperty = vtkProperty::New(); 34 coneProperty->SetColor(1.0, 0, 0.0); //可以自己设置颜色,非必须,默认银色 35 vtkactor->SetProperty(coneProperty); 36 37 vtkRenderer *render = vtkRenderer::New(); 38 render->AddActor(vtkactor); 39 render->SetBackground(0.1, 0.2, 0.3); 40 41 //自己设置light 42 vtkLight *light = vtkLight::New(); 43 //light->SetSpecularColor(1.0, 0.0, 0.0); //设置地面光的颜色 44 //light->SetDiffuseColor(0.0, 1.0, 0.0);//设置反射光的颜色 45 light->SetColor(1, 0, 0);//设置光照颜色 46 render->AddLight(light); //有了light,需要渲染render才行 47 48 49 50 vtkRenderWindow *window = vtkRenderWindow::New(); 51 window->SetSize(500, 500); 52 window->AddRenderer(render); 53 54 vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New(); 55 interactor->SetRenderWindow(window);//关联要交互的窗口 56 57 //现在设置控制的是相机,也可以实现相同的效果。 58 //vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New(); 59 //interactor->SetInteractorStyle(style); 60 61 //用这个就是设置控制演员 62 vtkInteractorStyleTrackballActor *style2 = vtkInteractorStyleTrackballActor::New(); 63 interactor->SetInteractorStyle(style2); 64 65 //旋转代码 66 /*for (int i = 0; i < 1800; i++) 67 { 68 window->Render(); 69 render->GetActiveCamera()->Azimuth(1); 70 }*/ 71 72 interactor->Initialize(); 73 interactor->Start(); 74 75 76 cone->Delete(); 77 coneMapper->Delete(); 78 vtkactor->Delete(); 79 render->Delete(); 80 window->Delete(); 81 interactor->Delete(); 82 //style->Delete(); 83 return 0; 84 }
注释:有时候设置光照颜色后,结果是黑色的。比如下图,那是因为光照和本身的颜色混合后变成了黑色。可以改变颜色参数来解决问题。
改变颜色参数,就可以显示了。