[vtkusers] texture in VTK - apply texture in DEM

Alain SIMAC alain.simac at visioterra.com
Tue Jul 5 04:15:28 EDT 2005


Hi,

 

I wish to apply a texture (an image) to a digital elevation model
(fichier.vtk). Is what somebody could help me because I do not find an
example.

I use VTK-4.4 and C++.

 

Actual code is :

 

#include "vtkPolyDataMapper.h"

#include "vtkActor.h"

#include "vtkRenderWindow.h"

#include "vtkRenderer.h"

#include "vtkRenderWindowInteractor.h"

#include "vtkDataSetReader.h"

#include "vtkDoubleArray.h"

#include "vtkStructuredPoints.h"

#include "vtkPoints.h"

#include "vtkPointData.h"

#include "vtkStructuredGrid.h"

#include "vtkCellData.h"

#include "vtkStructuredGridGeometryFilter.h"

#include "vtkPolyDataNormals.h"

 

#include "vtkTexture.h"

#include "vtkJPEGReader.h"

#include "vtkLookupTable.h"

#include "vtkImageData.h"

 

int main()

{

  //Setup parameters

  const char* m_filename = "C:/fichier.vtk";

  double m_maxvizu = 400;

  double m_minvizu = -4000;

  double m_elevation = 30000;

 

  //Setup texture

  const char* m_image = "C:/image.jpg";

  vtkJPEGReader *reader = vtkJPEGReader::New();

 

  reader->SetFileName(m_image);

 

  vtkLookupTable* VTKtable = vtkLookupTable::New();

  VTKtable->SetNumberOfColors(1000);

  VTKtable->SetTableRange(0,1000);

  VTKtable->SetSaturationRange(0,0);

  VTKtable->SetHueRange(0,1);

  VTKtable->SetValueRange(0,1);

  VTKtable->SetAlphaRange(1,1);

  VTKtable->Build();

 

  vtkImageData* ima = reader->GetOutput();

 

  vtkTexture* VTKtexture = vtkTexture::New();

  VTKtexture->SetInput(ima);

  VTKtexture->InterpolateOn();

  VTKtexture->SetLookupTable(VTKtable);

 

 

  // Create VTK renderer

  vtkRenderer* Ren1 = vtkRenderer::New();

  vtkRenderWindow* RenWin1 = vtkRenderWindow::New();

  RenWin1->AddRenderer(Ren1);

  RenWin1->SetSize(1200,1000);

 

  // Create VTK interactor

  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

  iren->SetRenderWindow(RenWin1);

 

  // Create VTK DataSetReader

  vtkDataSetReader* m_datasetreader;

  m_datasetreader = vtkDataSetReader::New();

  m_datasetreader->SetFileName(m_filename);

  m_datasetreader->Update();

 

  // Get Dataset dimensions

  vtkDataSet* m_points = m_datasetreader->GetOutput();

  int dimx =
m_datasetreader->GetStructuredPointsOutput()->GetDimensions()[0];

  int dimy =
m_datasetreader->GetStructuredPointsOutput()->GetDimensions()[1];

 

  // Allocate vtkPoint structure

  vtkPoints* m_point = vtkPoints::New();

  m_point->SetNumberOfPoints(dimx*dimy);

 

  // Allocate scalar array

  vtkDoubleArray* m_array = vtkDoubleArray::New();

  m_array->SetNumberOfComponents(1);

  m_array->SetNumberOfTuples(dimx*dimy);

 

  // Fill point and scalar array

  for (int y=0;y<dimy;y++)  

    for (int x=0;x<dimx;x++)

    {

      int offset = x+y*dimx;

      double m_coord[3];

      m_points->GetPoint (offset,m_coord);

    

      int offset2 = x+y*(dimx-1);

      double* m_value = m_points->GetPointData()->GetTuple(offset);  

 

      if (m_value[0] > m_maxvizu)

        m_value[0] = m_maxvizu;

 

      if (m_value[0] < m_minvizu)

        m_value[0] = m_minvizu;

 

 
m_array->SetTuple1(offset2,100-((((double)m_value[0]-m_minvizu)/(m_maxvizu-m
_minvizu))*100));

 
m_point->InsertPoint(offset,m_coord[0],m_coord[1],(((double)m_value[0]-m_min
vizu)/(m_maxvizu-m_minvizu))*m_elevation);

    }

 

  // Create vtkStructuredGrid for vizualisation

  vtkStructuredGrid* m_grid = vtkStructuredGrid::New();

  m_grid->SetDimensions(dimx,dimy,1);

  //m_grid->GetCellData()->SetScalars(m_array);        // affichage des
gradients de couleurs

  m_grid->SetPoints(m_point);

 

  vtkStructuredGridGeometryFilter*  m_gridgeometry =
vtkStructuredGridGeometryFilter::New();

  m_gridgeometry->SetInput(m_grid);

 

  // Create normals for better vizualisation

  vtkPolyDataNormals *normals = vtkPolyDataNormals::New();

  normals->SetInput(m_gridgeometry->GetOutput());

  normals->ComputeCellNormalsOn();

  normals->Update();

 

  // Create mapper

  vtkPolyDataMapper* m_mapper = vtkPolyDataMapper::New();

  m_mapper->SetInput(normals->GetOutput());

  m_mapper->SetScalarRange(0,100);

 

  // Create actor

  vtkActor*   m_actor = vtkActor::New();

  m_actor->SetTexture(VTKtexture);

  m_actor->SetMapper(m_mapper);

  //m_actor->PickableOn();

 

 

 

  // Rendering

  Ren1->AddActor(m_actor);

  Ren1->SetBackground(0.5,0.5,0.5); 

  RenWin1->Render();

 

  iren->Start();

 

  return 0;

}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20050705/eac81f28/attachment.htm>


More information about the vtkusers mailing list