环境:
操作系统:windows xp
编译环境:visual studio 2008 + jdk 1.6
这里选择的vtk版本为:vtk 5.8.0 ,下载地址:
安装文件:http://www.vtk.org/files/release/5.8/vtk-5.8.0-win32-x86.exe
源码:http://www.vtk.org/files/release/5.8/vtk-5.8.0.zip
还需要另外一个项目配置工具:cmake 2.8.6 ,下载地址:http://www.cmake.org/files/v2.8/cmake-2.8.6-win32-x86.exe
注意事项:操作系统最好选择32位的,之前因为本机操作系统为win7 64位,便尝试着编译了一次,结果失败。也可以在xp虚拟机里面编译。只不过速度稍微慢点。
1、安装vtk 和 cmake,上面选择的是可安装的vtk版本,这样我们直接将其安装在某个目录,如E:\VTK-5.8.0,为了编码出错,安装目录最好不要包含空格。然后在该目录下新建源码目录,vtk,以及用于后续编译的工作目录build,然后将上面的vtk源码解压到vtk目录。 接着安装cmake,这个没有限制,默认即可 。
2、配置项目,打开cmake工具,截图如下:
在上面的source code地址选择vtk源码目录,在下面的build 地址选择build目录,然后点击下面的Configure按钮,
在接下来的窗口,会让你选择编译工具, 选择机器上安装的vs2008 即可。完成以后,cmake开始配置项目,这个过程
可能会需要几分钟时间,如果配置一切正确,那最后会显示Confiure done,否则提示出错。同时在Configure过程结束以后,
在中间的窗口中会显示很多配置项,有的后面会显示复选框,有的后面已经有值了。如果出错,一般都是因为某些配置不正确,
在这里需要选中BUILD SHARED LIBS和VTK_WRAP_JAVA这两个复选框。如果你希望用mfc来开发界面,还需要选择一项GUISUPPORT,重新配置以后,需要再次点击Configure,这个时候,cmake会检查,如果选择了GUISUPPORT,那么后面还需要选择一项VTK_USER_MFC,然后再次Configure,知道没错了,最后点击Generate来生成vs项目。生成的项目在build目录。
3,编译vtk c++库以及java库,找到build目录下的VTK.sln文件,这个是对应的vs工程主文件,打开即可打开整个工程,里面包含的项目很多,大概100多个。然后选择重新生成解决方案,剩下的时间就是等待编译完成,这个过程看个人机器配置,估计需要30分钟以上。如果一切ok的话,那么在build目录下会生成一个bin目录,里面会包含一个Debug或者Release目录,这个取决于你编译的是什么版本,在这个目录里面会包含生成的库文件,以及对应的jar包。我们只需要里面的dll文件和jar包。
4,编译测试demo。这个可以直接使用源码里面example目录中的例子来测试,在example目录中有个tutorial目录,里面包含几个项目,找到Step1项目,把java目录中的java文件复制出来即可。
//
// This example creates a polygonal model of a cone, and then renders it to
// the screen. It will rotate the cone 360 degrees and then exit. The basic
// setup of source -> mapper -> actor -> renderer -> renderwindow is
// typical of most VTK programs.
//
// We import the vtk wrapped classes first.
import vtk.*;
// Then we define our class.
public class Cone {
// In the static contructor we load in the native code.
// The libraries must be in your path to work.
static {
System.loadLibrary("vtkCommonJava");
System.loadLibrary("vtkFilteringJava");
System.loadLibrary("vtkIOJava");
System.loadLibrary("vtkImagingJava");
System.loadLibrary("vtkGraphicsJava");
System.loadLibrary("vtkRenderingJava");
}
// now the main program
public static void main (String []args) {
//
// Next we create an instance of vtkConeSource and set some of its
// properties. The instance of vtkConeSource "cone" is part of a
// visualization pipeline (it is a source process object); it produces data
// (output type is vtkPolyData) which other filters may process.
//
vtkConeSource cone = new vtkConeSource();
cone.SetHeight( 3.0 );
cone.SetRadius( 1.0 );
cone.SetResolution( 10 );
//
// In this example we terminate the pipeline with a mapper process object.
// (Intermediate filters such as vtkShrinkPolyData could be inserted in
// between the source and the mapper.) We create an instance of
// vtkPolyDataMapper to map the polygonal data into graphics primitives. We
// connect the output of the cone souece to the input of this mapper.
//
vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
coneMapper.SetInputConnection( cone.GetOutputPort() );
//
// Create an actor to represent the cone. The actor orchestrates rendering
// of the mapper's graphics primitives. An actor also refers to properties
// via a vtkProperty instance, and includes an internal transformation
// matrix. We set this actor's mapper to be coneMapper which we created
// above.
//
vtkActor coneActor = new vtkActor();
coneActor.SetMapper( coneMapper );
//
// Create the Renderer and assign actors to it. A renderer is like a
// viewport. It is part or all of a window on the screen and it is
// responsible for drawing the actors it has. We also set the background
// color here
//
vtkRenderer ren1 = new vtkRenderer();
ren1.AddActor( coneActor );
ren1.SetBackground( 0.1, 0.2, 0.4 );
//
// Finally we create the render window which will show up on the screen
// We put our renderer into the render window using AddRenderer. We also
// set the size to be 300 pixels by 300
//
vtkRenderWindow renWin = new vtkRenderWindow();
renWin.AddRenderer( ren1 );
renWin.SetSize( 300, 300 );
//
// now we loop over 360 degreeees and render the cone each time
//
int i;
for (i = 0; i < 360; ++i)
{
// render the image
renWin.Render();
// rotate the active camera by one degree
ren1.GetActiveCamera().Azimuth( 1 );
}
}
}
注意,需要将vtk.jar文件加入eclipse的build path,同时把上面拷贝出来的dll文件拷贝到java 安装目录下的bin目录下,即和java.exe文件在同一目录。接下来就是执行程序测试结果了,如果不出问题,那么可以得到以下效果:
至此,编译的java库已经能够正常工作了,后面可以在此基础上开发vtk程序了。