AW: [vtkusers] Using textures

de Boer Ingo I.deBoer at polytec.de
Thu Jan 8 04:46:53 EST 2004


Hi,

> I have to use textures on my 3D models but I don't know how 
> to do that. 
> Could somebody tell me the steps to do this? Could somebody 
> send me a link 
> to some information?
This code should give you some hints...

greets
  Ingo

---
Dr.-Ing. Ingo H. de Boer

Polytec GmbH
Polytec-Platz 1-7, 76337 Waldbronn, Germany
phone: ++49 7243 604 106
fax  : ++49 7243 604 255
 

##########################################
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkPlaneSource.h"
#include "vtkTexture.h"
#include "vtkBMPReader.h"
#include "vtkImageData.h"
#include "vtkLookupTable.h"

int main( int argc, char *argv[] )
{
  int i;
  static float x[8][3]={{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
                        {0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}};
  static vtkIdType pts[6][4]={{0,1,2,3}, {4,5,6,7}, {0,1,5,4},
                        {1,2,6,5}, {2,3,7,6}, {3,0,4,7}};
  
  // Read bitmap from file
  vtkBMPReader *bmpReader = vtkBMPReader::New();
  bmpReader->SetFileName("example.bmp");

  // Create texture
  vtkTexture *atext = vtkTexture::New();
  atext->SetInput(bmpReader->GetOutput());
  atext->InterpolateOn();

  // We'll create the building blocks of polydata including data attributes.
  vtkPolyData *cube = vtkPolyData::New();
  vtkPoints *points = vtkPoints::New();
  vtkCellArray *polys = vtkCellArray::New();
  vtkFloatArray *tCoords = vtkFloatArray::New();
  tCoords->SetNumberOfComponents(2);
  tCoords->InsertTuple2(0, 0, 0);
  tCoords->InsertTuple2(1, 1, 0);
  tCoords->InsertTuple2(2, 1, 1);
  tCoords->InsertTuple2(3, 0, 1);
  tCoords->InsertTuple2(4, 0, 0);
  tCoords->InsertTuple2(5, 1, 0);
  tCoords->InsertTuple2(6, 1, 1);
  tCoords->InsertTuple2(7, 0, 1);

  // Load the point, cell, and data attributes.
  for (i=0; i<8; i++) points->InsertPoint(i,x[i]);
  for (i=0; i<6; i++) polys->InsertNextCell(4,pts[i]);

  // We now assign the pieces to the vtkPolyData.
  cube->SetPoints(points);
  points->Delete();
  cube->SetPolys(polys);
  polys->Delete();
  cube->GetPointData()->SetTCoords(tCoords);
  tCoords->Delete();

  // Now we'll look at it.
  vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
      cubeMapper->SetInput(cube);
  vtkActor *cubeActor = vtkActor::New();
      cubeActor->SetMapper(cubeMapper);
      cubeActor->SetTexture(atext);

  // The usual rendering stuff.
  vtkCamera *camera = vtkCamera::New();
      camera->SetPosition(1,1,1);
      camera->SetFocalPoint(0,0,0);

  vtkRenderer *renderer = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(renderer);

  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

  renderer->AddActor(cubeActor);
      renderer->SetActiveCamera(camera);
      renderer->ResetCamera();
      renderer->SetBackground(1,1,1);
  
  renWin->SetSize(300,300);

  // interact with data
  renWin->Render();
  iren->Start();

  // Clean up
  cube->Delete();
  cubeMapper->Delete();
  cubeActor->Delete();
  camera->Delete();
  renderer->Delete();
  renWin->Delete();
  iren->Delete();
  atext->Delete();
  bmpReader->Delete();

  return 0;
}



More information about the vtkusers mailing list