VTK中一些功能

VTK中一些功能vkt 中一些功能 获取渲染的 shader renderWindow gt Render vtkOpenGLSha shadercache renderWindow gt GetShaderCac vtkShaderPro shaderprogra

大家好,我是讯享网,很高兴认识大家。

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(); 
小讯
上一篇 2025-03-14 20:10
下一篇 2025-02-05 14:34

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/23385.html