3D histogram with GL plot cannot be saved to disk

Hi,
I tried to run the sample code /tutorials/gl/glvox1.C to create a 3D histogram plot with GL drawing option and save the plot into a file. The code works and the plot is saved to the disk if I run the code inside a root interpreter window. However, if I compile the code in visual studio 2010 as a function and run it, even though the plot was saved to the disk, when I open the plot, the plot is blank. Please find following the code. If I change the drawing option from hist->Draw(“glcol”); to hist->Draw(“bar”); The plot will be saved, but now the root is not using openGL and the plot is not colored. Could anyone help me out? Thanks.
void glvox1()
{
gStyle->SetCanvasPreferGL(true);
gStyle->SetPalette(1);
TCanvas *c1= new TCanvas(“c1”, “A Simple Graph Example”, 10, 10, 1000, 500);
const UInt_t nX = 30;
const Double_t xMin = -1., xMax = 1., xStep = (xMax - xMin) / (nX - 1);

       const UInt_t nY = 30;
       const Double_t yMin = -1., yMax = 1., yStep = (yMax - yMin) / (nY - 1);

      const UInt_t nZ = 30;
       const Double_t zMin = -1., zMax = 1., zStep = (zMax - zMin) / (nZ - 1);

      TH3F *hist = new TH3F("glvoxel", "glvoxel", 30, -1., 1., 30, -1., 1., 30, -1., 1.);

//Fill the histogram to create a “sphere”.
for (UInt_t i = 0; i < nZ; ++i) {
const Double_t z = zMin + i * zStep;

  for (UInt_t j = 0; j < nY; ++j) {
     const Double_t y = yMin + j * yStep;

     for (UInt_t k = 0; k < nX; ++k) {
        const Double_t x = xMin + k * xStep;

        const Double_t val = 1. - (x * x + y * y + z * z);
        hist->SetBinContent(k + 1, j + 1, i + 1, val);
     }
  }

}

gStyle->SetCanvasPreferGL(1);
gStyle->SetPalette(1);

hist->Draw(“glcol”);
c1->SaveAs(“abcd.png”);

Try to add:
c1->Modified(); c1->Update();
right before:
c1->SaveAs(“abcd.png”);

note also that GL picture need to be displayed on screen to be saved. Ie: no batch …

Thanks Wile and Couet for your help.

I tried to add c1->Modified(); c1->Update(); right before c1-SaveAs(“abcd.png”). It did not work. As before, if I run the modified code inside the root window itself, the plot was correctly saved. Is it possible that I linked wrong library or something? Also Couet mentioned that: “GL picture need to be displayed on screen to be saved”. Since when I run the code in visual studio, the plot is not displayed, does it mean I cannot save the plot to a file? Or is there a way to display the ROOT plot when compiled and run in visual studio? Thanks a lot for your help.

Yes that’s what I meant, to save the “GLxxx” plots into png files they need to be displayed.

Thanks Couet for your help. One of my colleagues showed me a sample code that is based on the root\tutorials\gl\glbox.cxx and it worked as I wanted. Please find attached the files.
gl.zip (462 KB)

Ok nice :slight_smile: