`
aswang
  • 浏览: 848145 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

vtk学习笔记 --- 基本对象使用

 
阅读更多

1、使用基本的三维对象,如锥体,立方体,柱体以及平面等

2、在同一窗口中同时显示多个渲染结果(设置视口)

3、添加基本的鼠标交互,使用鼠标可以转动三维对象

 

程序运行效果如下图:


 

相关代码如下:

 

public class ConeTest {
    //加载动态链接库
    static {
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
    }
    
    public static void main(String[] args) {
        vtkConeSource cone = new vtkConeSource();
        //设置圆锥的高度和半径
        cone.SetHeight(3);
        cone.SetRadius(1);
        //设置分辨率,值越大,越趋近于圆锥体
        cone.SetResolution(10);
        
        vtkCubeSource cube = new vtkCubeSource();
        //设置立方体的长宽高
        cube.SetXLength(1);
        cube.SetYLength(1.5);
        cube.SetZLength(1);
        
        //创建圆柱体
        vtkCylinderSource cylinder = new vtkCylinderSource();
        //创建平面
        vtkPlaneSource plane = new vtkPlaneSource();
        //创建对应的mapper
        vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
        coneMapper.SetInputConnection(cone.GetOutputPort());
        
        vtkPolyDataMapper cubeMapper = new vtkPolyDataMapper();
        cubeMapper.SetInput(cube.GetOutput());
        
        vtkPolyDataMapper cylinderMapper = new vtkPolyDataMapper();
        cylinderMapper.SetInput(cylinder.GetOutput());
        
        vtkPolyDataMapper planeMapper = new vtkPolyDataMapper();
        planeMapper.SetInput(plane.GetOutput());
        //创建对应的actor,设置mapper
        vtkActor coneActor = new vtkActor();
        coneActor.SetMapper(coneMapper);
        
        vtkActor cubeActor = new vtkActor();
        cubeActor.SetMapper(cubeMapper);
        
        vtkActor cylinderActor = new vtkActor();
        cylinderActor.SetMapper(cylinderMapper);
        
        vtkActor planeActor = new vtkActor();
        planeActor.SetMapper(planeMapper);
        //针对每个actor创建一个vtkRender,添加actor
        vtkRenderer ren1 = new vtkRenderer();
        ren1.AddActor(coneActor);
        //下面的代码就是设置视口的,将屏幕分为4个视口
        //前两个参数是视口左下角点的坐标(xmin,ymin),后两个参数是右上角的坐标(xmax,ymax)
        ren1.SetViewport(0, 0.5, 0.5, 1);
        ren1.SetBackground(0.1, 0.2, 0.4);
        
        vtkRenderer ren2 = new vtkRenderer();
        ren2.AddActor(cubeActor);
        ren2.SetViewport(0.5, 0.5, 1, 1);
        ren2.SetBackground(0, 1, 0);
        
        vtkRenderer ren3 = new vtkRenderer();
        ren3.AddActor(cylinderActor);
        ren3.SetViewport(0, 0, 0.5, 0.5);
        
        vtkRenderer ren4 = new vtkRenderer();
        ren4.AddActor(planeActor);
        ren4.SetViewport(0.5 , 0, 1, 0.5);
        ren4.SetBackground(0, 0, 1);
        
        vtkRenderWindow renWin = new vtkRenderWindow();
        renWin.AddRenderer(ren1);
        renWin.AddRenderer(ren2);
        renWin.AddRenderer(ren3);
        renWin.AddRenderer(ren4);
        renWin.SetSize(500, 400);
        
        //添加鼠标交互
        vtkRenderWindowInteractor interactor = new vtkRenderWindowInteractor();
        interactor.SetRenderWindow(renWin);
        vtkInteractorStyleTrackballActor style = new vtkInteractorStyleTrackballActor();
        interactor.SetInteractorStyle(style);
        //初始化交互器 并开始执行事件循环
        interactor.Initialize();
        interactor.Start();
    }
}
 

 

 

  • 大小: 26.9 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics