VTK中一些功能

vkt中一些功能

获取渲染的shader

    renderWindow->Render();
    vtkOpenGLShaderCache* shadercache = renderWindow->GetShaderCache();
    vtkShaderProgram* shaderprogram = shadercache->GetLastShaderBound();
    std::cout << "FragmentShader:\n" << shaderprogram->GetFragmentShader()->GetSource();

opengl中保存数组为图片

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texturecolor);
    glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_FLOAT,texuturebufcolor);

    static int indexgg = 0;
    std::string filename = "IMGComputeShaderTest_" +
            std::to_string(indexgg) + ".ppm";
    ofstream outfile;
    outfile.open(filename.c_str());
    int nx = resolutionx;
    int ny = resolutiony;
    outfile << "P3\n" << nx << " " << ny << "\n255\n";
    for (int j = ny - 1; j >= 0; j--){
        for (int i = 0; i < nx; i++){
            int pixindex = (j*resolutionx+i)*4;
            int ir = int(255.99*texuturebufcolor[pixindex+0]);
            int ig = int(255.99*texuturebufcolor[pixindex+1]);
            int ib = int(255.99*texuturebufcolor[pixindex+2]);
            outfile << ir << " " << ig << " " << ib << "\n";
        }
    }
    outfile.close();
    indexgg++;

VTK中保存屏幕截图

vtkDemo地址:https://lorensen.github.io/VTKExamples/site/Cxx/Utilities/Screenshot/

关键代码:

将renderwindow中的图像转换为png
  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = 
    vtkSmartPointer<vtkWindowToImageFilter>::New();
  windowToImageFilter->SetInput(renderWindow);
#if VTK_MAJOR_VERSION > 8 || VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 1
  windowToImageFilter->SetScale(2); //image quality
#else
  windowToImageFilter->SetMagnification(2); //image quality
#endif
  windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
  windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
  windowToImageFilter->Update();

  vtkSmartPointer<vtkPNGWriter> writer = 
    vtkSmartPointer<vtkPNGWriter>::New();
  writer->SetFileName("screenshot2.png");
  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
  writer->Write();
将renderwindow中的图像转换为QPixmap或QImage
    /*将renderwindow中的图像转换为QPixmap或QImage*/
    vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
      vtkSmartPointer<vtkWindowToImageFilter>::New();
    windowToImageFilter->SetInput(renderwindow);
  #if VTK_MAJOR_VERSION > 8 || VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 1
    windowToImageFilter->SetScale(2); //image quality
  #else
    windowToImageFilter->SetMagnification(1); //image quality
  #endif
    windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
    windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
    windowToImageFilter->Update();

    vtkImageData* imageData = windowToImageFilter->GetOutput();

    int width = imageData->GetDimensions()[0];
    int height = imageData->GetDimensions()[1];

    qDebug() << width << "   " << height;
    QImage image( width, height, QImage::Format_RGB32 );
    QRgb *rgbPtr =
      reinterpret_cast<QRgb *>( image.bits() ) + width * ( height - 1 );
    unsigned char *colorsPtr =
      reinterpret_cast<unsigned char *>( imageData->GetScalarPointer() );

    // Loop over the vtkImageData contents.
    for ( int row = 0; row < height; row++ )
    {
      for ( int col = 0; col < width; col++ )
      {
        // Swap the vtkImageData RGB values with an equivalent QColor
        *( rgbPtr++ ) = QColor( colorsPtr[0], colorsPtr[1], colorsPtr[2] ).rgb();
        colorsPtr += imageData->GetNumberOfScalarComponents();
      }

      rgbPtr -= width * 2;
    }

    QPixmap pixmapToShow = QPixmap::fromImage( image );

通过相机旋转actor

关键代码

        int dx = nowposx - lastposx;
        int dy = nowposy - lastposy;

        int *size = renderwindow->GetSize();

        double delta_elevation = -20.0 / size[1];
        double delta_azimuth = -20.0 / size[0];

        double rxf = dx * delta_azimuth * 10.0;
        double ryf = dy * delta_elevation * 10.0;

        vtkCamera *camera = renderer->GetActiveCamera();
        camera->Azimuth(rxf);
        camera->Elevation(ryf);
        camera->OrthogonalizeViewUp();
        renderwindow->Render();

构造vtkImageData

    int dims[3] = {512,512,133};
    double spacing[3] = {0.7,0.7,2.5};
    vtkImageData* image = vtkImageData::New();
    image->SetDimensions(dims);
    image->SetSpacing(spacing);
    image->AllocateScalars(VTK_SHORT,1);
    short *ptr = static_cast<short*>(image->GetScalarPointer());
    vtkDataArray* dataArr = image->GetPointData()->GetScalars();
    for(int i=0;i<dataArr->GetNumberOfTuples();i++){
        dataArr->InsertComponent(i,0,0);
    }
    image->Modify();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力减肥的小胖子5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值