VTK初学一,动画加AVI录制终于做出来了

时间:2021-05-03 16:00:21

VTK初学一,动画加AVI录制终于做出来了

 

 

 

 
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
#endif
#include <iostream>

using namespace std;
#include <vtkSmartPointer.h>
//#include <vtkJPEGReader.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkAVIWriter.h>
#include<vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkImageViewer2.h>
#include <vtkCommand.h>
#include <vtkWindowToImageFilter.h>

int main()
{
    vtkSmartPointer<vtkImageCanvasSource2D> source =vtkSmartPointer<vtkImageCanvasSource2D>::New();
    source->SetScalarTypeToUnsignedChar();
    source->SetNumberOfScalarComponents(3);
    source->SetExtent(0, 250, 0, 150, 0, 0);

    vtkSmartPointer<vtkAVIWriter> writer =vtkSmartPointer<vtkAVIWriter>::New();
    vtkSmartPointer<vtkRenderWindowInteractor> iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkImageViewer2>imageViewer=vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputConnection(source->GetOutputPort());
    imageViewer->SetupInteractor(iren);
    imageViewer->GetRenderer()->ResetCamera();
    imageViewer->GetRenderer()->SetBackground(1,1,1);
    imageViewer->SetSize(800,600);
    iren->Initialize();
//    iren->Start();
    vtkSmartPointer<vtkWindowToImageFilter> filter=vtkSmartPointer<vtkWindowToImageFilter>::New();
    filter->SetInput(imageViewer->GetRenderWindow());
    writer->SetInputConnection(filter->GetOutputPort());
    writer->SetFileName("test.avi");
    writer->Start();

    for(unsigned int i = 0; i < 250; i++)
    {
        source->SetDrawColor(0,0,0,1); //black
        source->FillBox(0, 300, 0,300); //clear image
        source->SetDrawColor(255,0,0,1); //red
        source->FillBox(i,230,80,100);
        source->Update();
        imageViewer->Render();
        for(int i=0;i<5;i++)
        {
            filter->Modified();
            writer->Write();
        }

    }
    writer->End();
    iren->Start();
    return 0;
}