Question on lighting coefficients

Paul Hsieh pahsieh at usgs.gov
Mon Jul 5 16:03:54 EDT 1999


Why do lighting coefficients (e.g., ambient and diffuse) work differently on 
1. an actor whose color is defined by scalar values via the mapper, versus
2. an actor whose color is defined by the SetColor method of its property?

This is illustrated by the attached sample code. In this code I create
a structured points data set containing a single cubic cell. The scalar
values at the eight points are all zero. This cell is rendered in 
two ways. The first way uses a mapper with the default
scalar range (0 to 1) and color mapping (red to blue), so the cell 
is rendered red. In the second way, the ScalarVisibilityOff() method
is invoked on the mapper, and the color of the actor is set to red.
When rendered with default lighting coefficients (diffuse = 1, ambient = 0)
both renderings look the same.

By pressing the 'u' key (actives user function), the lighting coefficients
are altered (See code below). Now the renderings look different.

What do I have to do to make the actor in case 1 above look the same
the the actor in case 2 above (for the same lighting coefficients)? 
Thanks for any help. Paul

//===== sample code ====

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkStructuredPoints.h"
#include "vtkDataSetMapper.h"
#include "vtkActor.h"

// These variables are declared global so they can
// be accessed by the user method
vtkRenderWindow *renWindow;
vtkActor *actor1;
vtkActor *actor2;
int flag;

// User method invoked by pressing the 'u' key
void ChangeLighting(void *arg)
{
	switch (flag)
	{
	case 0:
		actor1->GetProperty()->SetAmbient(0.5);
		actor2->GetProperty()->SetAmbient(0.5);
		flag = 1;
		break;
	case 1:
		actor1->GetProperty()->SetDiffuse(0.5);
		actor2->GetProperty()->SetDiffuse(0.5);
		flag = 2;
		break;
	case 2:
		// set back to default lighting coefficients
		actor1->GetProperty()->SetDiffuse(1);
		actor1->GetProperty()->SetAmbient(0);
		actor2->GetProperty()->SetDiffuse(1);
		actor2->GetProperty()->SetAmbient(0);
		flag = 0;
		break;
	}
	renWindow->Render();
}

void main()
{
	// Create the rendering stuff
	vtkRenderer *ren = vtkRenderer::New();
	ren->SetBackground(1, 1, 1);
	renWindow = vtkRenderWindow::New();
	renWindow->AddRenderer(ren);
	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renWindow);
	iren->SetUserMethod(ChangeLighting, 0);
	flag = 0;

	// Create a structured points data set
	vtkStructuredPoints *sp = vtkStructuredPoints::New();
	sp->SetDimensions(2, 2, 2);
	sp->SetOrigin(0, 0, 0);
	sp->SetSpacing(1, 1, 1);

	// Create point data
	vtkScalars *scalars = vtkScalars::New();
	for (int i=0; i< 8; i++)
	{
		scalars->InsertNextScalar(0);
	}
	sp->GetPointData()->SetScalars(scalars);
	scalars->Delete();

	// Create two mappers, one with scalar visibility on and
	// the other with scalar invisiblility off
	vtkDataSetMapper *mapper1 = vtkDataSetMapper::New();
	mapper1->SetInput(sp);
	mapper1->ScalarVisibilityOn();

	vtkDataSetMapper *mapper2 = vtkDataSetMapper::New();
	mapper2->SetInput(sp);
	mapper2->ScalarVisibilityOff();

	// Create two actors, the first one is colored according
	// to scalar values and the second one is set to a
	// specific color
	actor1 = vtkActor::New();
	actor1->SetMapper(mapper1);

	actor2 = vtkActor::New();
	actor2->SetMapper(mapper2);
	actor2->GetProperty()->SetColor(1, 0, 0);
	actor2->AddPosition(2, 0, 0);

	// Render
	ren->AddActor(actor1);
	ren->AddActor(actor2);
	renWindow->Render();
	iren->Start();

	// Clean up
	ren->Delete();
	renWindow->Delete();
	iren->Delete();
	sp->Delete();
	mapper1->Delete();
	mapper2->Delete();
	actor1->Delete();
	actor2->Delete();
}


-----------------------------------------------------------------------------
This is the private VTK discussion list.  Please keep messages on-topic.
Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
<majordomo at gsao.med.ge.com>.  For help, send message body containing
"info vtkusers" to the same address.     Live long and prosper.
-----------------------------------------------------------------------------





More information about the vtkusers mailing list