[vtkusers] Multithreaded rendering problem

Guerrero juliang at ece.ubc.ca
Wed May 15 18:00:23 EDT 2002


Hello,

I am receiving images through a socket connection (one thread), and saving
them in a shared memory. Afterwards, I read this data, and create a
vtkImageData object, into which I put my received image (640X480, unsigned
char). Then I take this vtkImageData object and use it as a texture map
for a plane, to render afterwards. But when I call render(), I only see my
plane, without the texture.
After several tests, I am able to put other info (another image) into the
vtkImageData object within the same thread, and it renders properly, but
if I define this other info in another thread, I can't see it.
I also have a third thread, which uses the same data, but I use
vtkImageViewer to render, and this thread it always works.

for the render thread that doesn't work, the code goes something like
this:

	vtkImageData *USImage = vtkImageData::New();
		USImage->SetDimensions(640,480,1);
		USImage->SetSpacing(1.0,1.0,1.0);
		USImage->SetOrigin(0.0,0.0,0.0);
		USImage->SetScalarTypeToUnsignedChar();
		USImage->SetNumberOfScalarComponents(1);
		USImage->AllocateScalars();
/*
	// THIS WORKS, IF THIS IS SET AS THE TEXTURE
	// Read image using VTK object
	vtkPNMReader* pgmImage = vtkPNMReader::New();
	// fname = vtkTestUtilities::ExpandDataFileName(argc, argv,
"Data/earth.ppm");
	pgmImage->SetFileName("Image5.pgm");
	pgmImage->Update();
*/

	vtkPlaneSource *plane = vtkPlaneSource::New();
	vtkPolyDataMapper *planeMapper = vtkPolyDataMapper::New();
		planeMapper->SetInput(plane->GetOutput());
	vtkActor *planeActor = vtkActor::New();
		planeActor->SetMapper(planeMapper);
		planeActor->SetTexture(atext);

   	// Create renderer for vtk
	vtkRenderer *ren1 = vtkRenderer::New();
	vtkRenderWindow *renWin = vtkRenderWindow::New();
		renWin->AddRenderer(ren1);
		ren1->AddActor(planeActor);

	// Set the texture
	vtkTexture *atext = vtkTexture::New();
//	atext->SetInput(pgmImage->GetOutput());
	atext->SetInput(USImage);


	while(1) {
                // My Shared data.. copies (memcpy) the data to Pointer
		pObjectSend->GetImage(Pointer);		//	Pointer =
new unsigned char[640*480];

		for (i = 0; i < 480; i++) {
			for (j = 0; j < 640; j++) {
				// Assign data to volume
				ptr = (unsigned char
*)USImage->GetScalarPointer(j,479-i,0); //ptr = new unsigned char;
				*ptr = Pointer[ i*640 + j];
			}
		}
		renWin->Render();
	}

And the thread that does work goes something like this:

	vtkImageData *USImage = vtkImageData::New();
		USImage->SetDimensions(640,480,1);
		USImage->SetSpacing(1.0,1.0,1.0);
		USImage->SetOrigin(0.0,0.0,0.0);
		USImage->SetScalarTypeToUnsignedChar();
		USImage->SetNumberOfScalarComponents(1);
		USImage->AllocateScalars();

	vtkImageViewer *SeedImage = vtkImageViewer::New();
		SeedImage->SetInput(USImage);
		SeedImage->SetZSlice(0);
		SeedImage->SetColorWindow(256);
		SeedImage->SetColorLevel(127.5);

	while(1) {
		pObjectSend->GetImage(Pointer); // My Shared data.. copies
(memcpy) the data to Pointer
		for (i = 0; i < 480; i++) {
			for (j = 0; j < 640; j++) {
				// Assign data to volume
				ptr = (unsigned char
*)USImage->GetScalarPointer(j,479-i,0);
				*ptr = Pointer[i*640 + j];
			}
		}
		SeedImage->Render();
	}



Does anyone have any idea what is going on???

Thanks very much for your help,

Julian Guerrero





More information about the vtkusers mailing list