3.5.6 Lighting sample

In contrast to the most of other programs, MathGL supports several (up to 10) light sources. Moreover, the color each of them can be different: white (this is usual), yellow, red, cyan, green and so on. The use of several light sources may be interesting for the highlighting of some peculiarities of the plot or just to make an amusing picture. Note, each light source can be switched on/off individually. The sample code is:

int sample(mglGraph *gr)
{
  mglData a;  mgls_prepare2d(&a);
  gr->Title("Several light sources");
  gr->Rotate(50,60);  gr->Light(true);
  gr->AddLight(1,mglPoint(0,1,0),'c');
  gr->AddLight(2,mglPoint(1,0,0),'y');
  gr->AddLight(3,mglPoint(0,-1,0),'m');
  gr->Box();  gr->Surf(a,"h");
  return 0;
}
Example of several light sources.

Additionally, you can use local light sources and set to use diffuse reflection instead of specular one (by default) or both kinds. Note, I use attachlight command to keep light settings relative to subplot.

int sample(mglGraph *gr)
{
  gr->Light(true);  gr->AttachLight(true);
  gr->SubPlot(2,2,0); gr->Title("Default"); gr->Rotate(50,60);
  gr->Line(mglPoint(-1,-0.7,1.7),mglPoint(-1,-0.7,0.7),"BA"); gr->Box();  gr->Surf(a);

  gr->SubPlot(2,2,1); gr->Title("Local");   gr->Rotate(50,60);
  gr->AddLight(0,mglPoint(1,0,1),mglPoint(-2,-1,-1));
  gr->Line(mglPoint(1,0,1),mglPoint(-1,-1,0),"BAO");  gr->Box();  gr->Surf(a);

  gr->SubPlot(2,2,2); gr->Title("no diffuse"); gr->Rotate(50,60);
  gr->SetDiffuse(0);
  gr->Line(mglPoint(1,0,1),mglPoint(-1,-1,0),"BAO");  gr->Box();  gr->Surf(a);

  gr->SubPlot(2,2,3); gr->Title("diffusive only");  gr->Rotate(50,60);
  gr->SetDiffuse(0.5);
  gr->AddLight(0,mglPoint(1,0,1),mglPoint(-2,-1,-1),'w',0);
  gr->Line(mglPoint(1,0,1),mglPoint(-1,-1,0),"BAO");  gr->Box();  gr->Surf(a);
}
Example of different types of lighting.