[vtkusers] Texture Mapping

Ye Ning aboycn.mailinglist at gmail.com
Thu Apr 26 02:03:04 EDT 2007


Hi, all,

I want to use texture mapping in VTK, but I do not know how to.
Particularly, I have one mesh (actually a 2D mesh manually picked on the
texture image) and one texture image. And I want to specify the color of
each vertex of the mesh by giving its 2D color index (u,v) on the texture
image. I tried setting the 2D texture index as 2-component scalars (not
normalized to 0~1, should I do that?) of the mesh's point data, but it does
not work. Can any body give me some advices on? Any suggestion will be
appreciated.

Thanks in advance,
----
Ye Ning

Attached here my "NOT-working" code:

#include <fstream>
#include <iostream>

#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkCellArray.h"

#include "vtkDoubleArray.h"
#include "vtkJPEGReader.h"
#include "vtkTexture.h"

#define MeshPts 64
#define MeshTri 111

const char* ptsfile = "facemesh.pts";
const char* trifile = "facemesh.tri";
const char* texfile = "facemesh.jpg";

void LoadShape( vtkPolyData* avatar )
{
    if (!avatar) return;

    vtkPoints* pts = vtkPoints::New();
    vtkCellArray* tri = vtkCellArray::New();
    vtkDoubleArray* scalar = vtkDoubleArray::New();
    scalar->SetNumberOfComponents(2);

    ifstream fin;
    fin.open( ptsfile );
    for ( int i = 0; i < MeshPts; i++ ) {
        double x,y;
        fin >> x; fin >> y;
        pts->InsertNextPoint( x, -y, 0 );
        scalar->InsertTuple2( i, x, y );
    }
    fin.close();
    fin.open( trifile );
    vtkIdType ptslist[3];
    for ( int i = 0; i < MeshTri; i++ ) {
        double t;
        fin >> t; ptslist[0] = vtkIdType(t);
        fin >> t; ptslist[1] = vtkIdType(t);
        fin >> t; ptslist[2] = vtkIdType(t);
        tri->InsertNextCell( 3, ptslist );
    }
    fin.close();

    avatar->SetPoints( pts );
    avatar->SetPolys( tri );
    avatar->GetPointData()->SetScalars( scalar );

    scalar->Delete();
    tri->Delete();
    pts->Delete();
}

void LoadTexture( vtkTexture* texture )
{
    vtkJPEGReader* textureReader = vtkJPEGReader::New();
    textureReader->SetFileName( texfile );

    texture->SetInputConnection( textureReader->GetOutputPort() );
    texture->RepeatOff();
    texture->InterpolateOn();

    textureReader->Delete();
}

void main()
{
    /* + Texture */
    vtkTexture* texture = vtkTexture::New();
    LoadTexture( texture );
    /* o->Poly data source */
    vtkPolyData* avatar = vtkPolyData::New();
    LoadShape( avatar );
    /* -->Mapper */
    vtkPolyDataMapper* avaMapper = vtkPolyDataMapper::New();
    avaMapper->SetInput( avatar );
    /* -->Actor */
    vtkActor* avaActor = vtkActor::New();
    avaActor->SetMapper( avaMapper );
    avaActor->SetTexture( texture );
    /* -->Renderer */
    vtkRenderer* renderer = vtkRenderer::New();
    renderer->AddActor( avaActor );
    renderer->SetBackground(1,1,1);
    /* -->Render Window */
    vtkRenderWindow* renWin = vtkRenderWindow::New();
    renWin->AddRenderer( renderer );
    /* + Interactor */
    vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow( renWin );
    /* Rendering and interacting */
    renWin->Render();
    iren->Start();
    /* Release all */
    iren->Delete();
    renWin->Delete();
    renderer->Delete();
    avaActor->Delete();
    avaMapper->Delete();
    avatar->Delete();
    texture->Delete();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070426/8205f7e4/attachment.htm>


More information about the vtkusers mailing list