How to draw solid sphere

HLR draws in the code below, but can I draw it as solid?

#include <TApplication.h>
#include <TCanvas.h>
#include <TGeoManager.h>
#include <TGeoVolume.h>
#include <TGeoMaterial.h>
#include <TGeoMedium.h>
#include <TSystem.h>
#include <TRandom.h>

Int_t randomColor()
{

TRandom random;
Double_t color = 7. * random.Rndm();
return (1 + Int_t(color));

}

int main(int argc, char** argv) {
// ROOT uygulamasını başlat
TApplication app(“app”, &argc, argv);

// Yeni bir canvas oluştur
TCanvas* c1 = new TCanvas("c1", "Geometry Shapes", 200, 10, 700, 500);

// Geometri yöneticisini oluştur
TGeoManager* geoManager = new TGeoManager("sphere", "Küre Örneği");

// Malzeme ve ortam tanımla
TGeoMaterial* mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
TGeoMedium* med = new TGeoMedium("MED", 1, mat);

// Üst hacmi oluştur
TGeoVolume* top = geoManager->MakeBox("TOP", med, 100, 100, 100);
geoManager->SetTopVolume(top);

// Küreyi oluştur
TGeoVolume* sphere = geoManager->MakeSphere("SPHERE", med, 0, 50, 0, 180, 0, 360);
sphere->SetLineColor(randomColor());
sphere->SetLineWidth(2);
top->AddNode(sphere, 1);

// Geometriyi kapat
geoManager->CloseGeometry();
geoManager->SetNsegments(80);

// Geometriyi çiz
top->Draw("gl");

// Canvas'ı güncelle
c1->Update();

// ROOT uygulamasını çalıştır
app.Run();

return 0;

}

@agheata should know.

Try: top->Draw("ogl"); You need ROOT configured with OpenGL support.

Thank you for the answer. It works with ogl, but two screens appear, one of these screens is gl viewer, the other is geometry shape, I want to draw a solid sphere in a single screen because I want to draw solid and isosurface together in the future, so I need a single screen, same, I want to draw solid spheres in TH3 like in the glparametric.C example in the tutorials because I want to use the root library in chemical molecule drawings. Is it possible to draw a solid sphere in TH3, TH3F etc.?


Is it possible to draw the molecule on the geom shape screen like in the picture (both isosurface and solid sphere will be drawn)?

I guess there is no such thing as Ttube?
#include <TApplication.h>
#include <TCanvas.h>
#include <TH3F.h>
#include <TStyle.h>
#include <TPaveLabel.h>
#include <TGLViewer.h>
#include <TGLTH3Composition.h>
#include <TPolyLine3D.h>

void DrawBenzene() {
gStyle->SetCanvasPreferGL(kTRUE);
TCanvas* c1 = new TCanvas(“c1”, “Benzene Molecule”, 800, 600);
TGLTH3Composition* comp = new TGLTH3Composition;

// Create histograms for carbon and hydrogen atoms
TH3F* hCarbon = new TH3F("hCarbon", "Carbon Atoms", 20, -2, 2, 20, -2, 2, 20, 0, 4);
TH3F* hHydrogen = new TH3F("hHydrogen", "Hydrogen Atoms", 20, -2, 2, 20, -2, 2, 20, 0, 4);

// Atom coordinates and types: 0 = black (carbon), 1 = pink (hydrogen)
struct Atom {
    Double_t x, y, z;
    int type; // 0 for carbon, 1 for hydrogen
};

Atom atoms[] = {
    {1.0,  0.0,  0.0, 0},   // Carbon
    {-1.0,  0.0,  0.0, 0},  // Carbon
    {0.5,  0.866, 0.0, 0},  // Carbon
    {-0.5,  0.866, 0.0, 0}, // Carbon
    {-0.5, -0.866, 0.0, 0}, // Carbon
    {0.5, -0.866, 0.0, 0},  // Carbon
    {1.5,  0.0,  0.0, 1},   // Hydrogen
    {-1.5, 0.0,  0.0, 1},   // Hydrogen
    {0.5,  1.732, 0.0, 1},  // Hydrogen
    {-0.5, 1.732, 0.0, 1},  // Hydrogen
    {-0.5, -1.732, 0.0, 1}, // Hydrogen
    {0.5, -1.732, 0.0, 1}   // Hydrogen
};

// Fill histograms with atom coordinates
for (const auto& atom : atoms) {
    if (atom.type == 0) {
        hCarbon->Fill(atom.x, atom.y, atom.z);
    }
    else {
        hHydrogen->Fill(atom.x, atom.y, atom.z);
    }
}

// Set colors for atoms
hCarbon->SetFillColor(kBlack);
hHydrogen->SetFillColor(kPink);

// Add atoms to the composition
comp->AddTH3(hCarbon, TGLTH3Composition::kSphere);
comp->AddTH3(hHydrogen, TGLTH3Composition::kSphere);

// Draw the composition and add title
comp->Draw();
TPaveLabel* title = new TPaveLabel(0.04, 0.86, 0.96, 0.98, "Benzene Molecule Visualization with Bonds");
title->SetFillColor(32);
title->Draw();

// Define bonds between atoms using TPolyLine3D
int bonds[][2] = {
    {0, 1}, {0, 2}, {1, 3}, {2, 4}, {3, 5}, {4, 0}, // Carbon bonds
    {0, 6}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {5, 11} // Hydrogen bonds
};

// Draw each bond using TPolyLine3D
for (const auto& bond : bonds) {
    Atom atom1 = atoms[bond[0]];
    Atom atom2 = atoms[bond[1]];

    // Create a polyline with two points
    TPolyLine3D* bondLine = new TPolyLine3D(2);
    bondLine->SetPoint(0, atom1.x, atom1.y, atom1.z);
    bondLine->SetPoint(1, atom2.x, atom2.y, atom2.z);
    bondLine->SetLineColor(kGray + 2);
    bondLine->SetLineWidth(2);
    bondLine->Draw();
}

}

int main(int argc, char** argv) {
TApplication app(“app”, &argc, argv);
DrawBenzene();
app.Run();
return 0;
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.