• 用vtk画海量的离散点(此方法用点精灵和glsl)


    ttttttttt在无网格法中,画离散点,如果点比较多的话,就不能用小球表示,具我的测试,小球的数量如果超出一百个,速度就慢得无法忍受,解决办法是用点精灵,或者glsl着色语言

    #include "vtkCamera.h"
    #include "vtkRenderWindow.h"
    #include "vtkRenderWindowInteractor.h"
    #include "vtkInteractorStyleSwitch.h"
    #include "vtkPolyData.h"
    #include "vtkPointSource.h"
    #include "vtkRenderer.h"
    #include "vtkElevationFilter.h"
    #include "vtkPointSpriteMapper.h"
    #include "vtkImageGaussianSource.h"
    #include "vtkMath.h"
    #include "vtkActor.h"
    #include "vtkDebugLeaks.h"
    #include "vtkProperty.h"
    #include "vtkSmartPointer.h"
    #include "vtkPolyData.h"
    #include "vtkPoints.h"
    #include "vtkCellArray.h"
    #include "vtkFloatArray.h"
    #include "vtkPointData.h"

    // vtk Testing
    #include "vtkTestUtilities.h"
    #include "vtkRegressionTestImage.h"
    #include "vtkSphereSource.h"
    #include "vtkAppendPolyData.h"

    #include <vtksys/SystemTools.hxx>
    #include <vtkstd/string>

    //#define SPRITES_ON
    //#define SPHERES_ON
    #define use_glsl          0
    #define use_pointsprites  1

    //---------------------------------------------------------------------------
    //bool SetEnv(const char* name, const char *val, bool overwrite);
    //---------------------------------------------------------------------------
    int main( int argc, char *argv[] )
    {
     
      //
      // We are loading Shaders from a custom materials repository, tell
      // the vtkXMLShader loading code to search here.
      //
      //SetEnv("USER_MATERIALS_DIRS", ResourceDir.c_str(), true);

      ////////////////////////////////////////////////
      //
      // An array of point sprite sphere glyphs
      //
      ////////////////////////////////////////////////
      const double rows = 10;
      const double numspheres = rows*rows;
      const double Ascaling = 0.1;
      const double Bscaling = 1.0 - Ascaling;

      vtkPolyData* Sprites = vtkPolyData::New();
      vtkPoints*    points = vtkPoints::New();
      vtkCellArray*  verts = vtkCellArray::New();
      vtkFloatArray *sizes = vtkFloatArray::New();

      points->SetNumberOfPoints(numspheres);
      sizes->SetNumberOfTuples(numspheres);
      sizes->SetNumberOfComponents(1);
      verts->Allocate(numspheres,numspheres);
      Sprites->SetPoints(points);
      Sprites->SetVerts(verts);
      sizes->SetName("PointSizes");
      Sprites->GetPointData()->AddArray(sizes);
      //
      for (int y=0; y<rows; y++) {
        for (int x=0; x<rows; x++) {
          vtkIdType Id = y*rows+x;
          points->SetPoint(Id, x, y, 0.0);
          sizes->SetValue(Id, Ascaling + Bscaling*((double)Id/numspheres));
          verts->InsertNextCell(1,&Id);
        }
      }

      //
      // Colour sprites
      //
      vtkElevationFilter* SpriteColour = vtkElevationFilter::New();
      SpriteColour->SetInput(Sprites);
      SpriteColour->SetLowPoint ( 0.0, 0.0, 0.0);
      SpriteColour->SetHighPoint( rows, rows, 0.0);

      vtkPointSpriteMapper *SpriteMapper = vtkPointSpriteMapper::New();
       
        SpriteMapper->SetRenderModeToPointSprite();
        SpriteMapper->SetBlendModeToOcclude();
        SpriteMapper->SetDefaultPointSize(64);
        SpriteMapper->SetQuadraticPointDistanceAttenuation(1.0, 0.1, 0.0);
     
     SpriteMapper->SetParticleImageFileName("D:/aaa.png");
        SpriteMapper->SetParticleImage(NULL);
     SpriteMapper->SetInputConnection(SpriteColour->GetOutputPort());

      // Create actor and set sprite mapper
      vtkActor *SpriteActor = vtkActor::New();
      SpriteActor->SetMapper(SpriteMapper);


      ////////////////////////////////////////////////
      //
      // An array of Spheres to go alongside sprites
      //
      ////////////////////////////////////////////////
      vtkSphereSource *SphereSource = vtkSphereSource::New();
      vtkAppendPolyData *SphereAppend = vtkAppendPolyData::New();
      for (int y=0; y<rows; y++) {
        for (int x=0; x<rows; x++) {
          int Id = y*rows+x;
          SphereSource->SetCenter(x, y, 1.0);
          SphereSource->SetRadius((Ascaling + Bscaling*((double)Id/numspheres))/2.0);
          SphereSource->SetThetaResolution(32);
          SphereSource->SetPhiResolution(16);
          SphereSource->Update();
          vtkPolyData *poly = vtkPolyData::New();
          poly->DeepCopy(SphereSource->GetOutput());
          SphereAppend->AddInput(poly);
          poly->Delete();
        }
      }

      //
      // Colour spheres
      //
      vtkElevationFilter* SphereColour = vtkElevationFilter::New();
      SphereColour->SetInputConnection(SphereAppend->GetOutputPort());
      SphereColour->SetLowPoint ( 0.0, 0.0, 0.0);
      SphereColour->SetHighPoint( rows, rows, 0.0);

      //
      // Polydata mapper
      //
      vtkPolyDataMapper* SphereMapper = vtkPolyDataMapper::New();
      SphereMapper->SetInputConnection(SphereColour->GetOutputPort());

      //
      // SphereMapper actor
      //
      vtkActor* SphereActor = vtkActor::New();
      SphereActor->SetMapper(SphereMapper);


      ////////////////////////////////////////////////
      // setup renderer etc
      ////////////////////////////////////////////////
      vtkRenderer* ren = vtkRenderer::New();
    //#ifdef SPRITES_ON
      ren->AddActor(SpriteActor);
    //#endif
    //#ifdef SPHERES_ON
     // ren->AddActor(SphereActor);
    //#endif
      const double distance = 1.75;
      ren->SetBackground(0.0,0.0,0.0);
      ren->GetActiveCamera()->SetPosition(distance*(rows+1.0), distance*(rows+1.0), 0.5);
      ren->GetActiveCamera()->SetFocalPoint(rows/2.0, rows/2.0, 0.5);
      ren->GetActiveCamera()->SetRoll(45.0);
      ren->ResetCameraClippingRange();

      vtkRenderWindow* renWin = vtkRenderWindow::New();
      renWin->SetSize(512, 512);
      renWin->AddRenderer(ren);

      vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
      iren->SetRenderWindow(renWin);
      vtkInteractorStyleSwitch *is = vtkInteractorStyleSwitch::New();
      iren->SetInteractorStyle(is);
      is->SetCurrentStyleToTrackballCamera();

      renWin->Render();


        iren->Start();
     

     
      return 1;
    }

  • 相关阅读:
    重新了解Java基础(六)-Java关键字
    重新了解Java基础(五)-Java标识符
    重新了解Java基础(四)-常用开发工具&注释简介
    CentOS 7 安装JDK
    一步一步教你自定义博客园(cnblog)界面
    如何设置文字的阴影
    如何实现2D动画
    跳转九宫格的制作
    风车的动图制作
    心跳的动图制作
  • 原文地址:https://www.cnblogs.com/lizhengjin/p/1536649.html
Copyright © 2020-2023  润新知