[vtkusers] Help on reading VRML file.

Dongqing Chen dqchen at cvip.louisville.edu
Thu Oct 29 19:12:13 EDT 2009


Dear All:

    I have been stuck in loading in and viewing the VRML files in the past two days. I found some codes online, and wrote my own based on those codes. However, there are some problems, always the access violation problem. I have tested on different files, some created by my collegues before, and some downloaded from the internet. 

   I post the code patch, anyone could help me?

---------------------------------------------------------------------------------------------

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"

#include "vtkVRMLImporter.h"
#include "vtkDataSet.h"
#include "vtkActorCollection.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkPolyDataNormals.h"
#include "vtkActor.h"

#include <iostream>

int main (int argc, char **argv)
{

 // VRML Import
vtkVRMLImporter *importer=vtkVRMLImporter::New();
//importer->SetFileName(argv[1]);
importer->SetFileName(file name);
importer->Read();
importer->Update();

//Convert to vtkDataSet
vtkDataSet *pDataset;
vtkActorCollection *actors = importer->GetRenderer()->GetActors();
actors->InitTraversal();
pDataset = actors->GetNextActor()->GetMapper()->GetInput(); //Problem happened here

//Convert to vtkPolyData
vtkPolyData *polyData = vtkPolyData::SafeDownCast(pDataset);
polyData->Update();


// Create Normal Vectors to enhance smoothness & illumination 
vtkPolyDataNormals *normals = vtkPolyDataNormals::New();
normals->SetInput(polyData);
normals->SetFeatureAngle(60.0);

// Mapper
vtkPolyDataMapper *SolidMapper = vtkPolyDataMapper::New();
SolidMapper->SetInput(normals->GetOutput());
SolidMapper->ScalarVisibilityOff(); 

// Actor
vtkActor *SolidActor = vtkActor::New();
SolidActor->SetMapper(SolidMapper);
//SolidActor->GetProperty()->SetOpacity(1);

// Render
vtkRenderer *ren = vtkRenderer::New();
ren->AddActor(SolidActor); 
ren->SetBackground(1,1,1);

// RenderWindow
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren);

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

renWin->Start(); 
renWin->SetSize(800-5,600-5); // Must be Called after Start() and before Render() 
// interact with data
iren->Initialize();
iren->Start();

// delete 

actors->Delete();
normals->Delete(); 
SolidMapper->Delete(); 
SolidActor->Delete();
ren->Delete(); 
renWin->Delete();
iren->Delete();

return 0;
}

----------------------------------------------------------------------------------------------------------------------------------------------

Best Wishes,
-----------------------------------------------------------------------------
Dongqing Chen, Ph.D.
Computer Vision & Image Processing (CVIP) Lab
Department of Electrical & Computer Engineering
Speed School of Engineering
University of Louisville
Louisville, KY, 40292
U.S.A
email: dqchen at cvip.louisville.edu
phone: 1-502-852-2789 (Lab)
             1-502-852-6130 (Office)
----------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20091029/b0bfff6a/attachment.htm>


More information about the vtkusers mailing list