TGLIsoMesh.cxx

Go to the documentation of this file.
00001 // @(#)root/gl:$Id: TGLIsoMesh.cxx 33600 2010-05-21 09:24:32Z rdm $
00002 // Author:  Timur Pocheptsov  28/07/2009
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TGLIncludes.h"
00013 
00014 #include "TGLPlotPainter.h"
00015 #include "TGLIsoMesh.h"
00016 
00017 namespace Rgl {
00018 
00019 //Functions for TGLTF3/TGLIso/TGL5DPainter.
00020 //______________________________________________________________________________
00021 template<class V>
00022 void DrawMesh(GLenum type, const std::vector<V> &vs, const std::vector<V> &ns, 
00023               const std::vector<UInt_t> &fTS)
00024 {
00025    //Surface with material and lighting.
00026    glEnableClientState(GL_VERTEX_ARRAY);
00027    glEnableClientState(GL_NORMAL_ARRAY);
00028    glVertexPointer(3, type, 0, &vs[0]);
00029    glNormalPointer(type, 0, &ns[0]);
00030    glDrawElements(GL_TRIANGLES, fTS.size(), GL_UNSIGNED_INT, &fTS[0]);
00031    glDisableClientState(GL_NORMAL_ARRAY);
00032    glDisableClientState(GL_VERTEX_ARRAY);
00033 }
00034 
00035 //______________________________________________________________________________
00036 void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns, 
00037               const std::vector<UInt_t> &ts)
00038 {
00039    //Call function-template.
00040    DrawMesh(GL_FLOAT, vs, ns, ts);
00041 }
00042 
00043 //______________________________________________________________________________
00044 void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns, 
00045               const std::vector<UInt_t> &ts)
00046 {
00047    //Call function-template.
00048    DrawMesh(GL_DOUBLE, vs, ns, ts);
00049 }
00050 
00051 //______________________________________________________________________________
00052 template<class V>
00053 void DrawMesh(GLenum type, const std::vector<V> &vs, const std::vector<UInt_t> &fTS)
00054 {
00055    //Only vertices, no normal (no lighting and material).
00056    glEnableClientState(GL_VERTEX_ARRAY);
00057    glVertexPointer(3, type, 0, &vs[0]);
00058    glDrawElements(GL_TRIANGLES, fTS.size(), GL_UNSIGNED_INT, &fTS[0]);
00059    glDisableClientState(GL_VERTEX_ARRAY);
00060 }
00061 
00062 //______________________________________________________________________________
00063 void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts)
00064 {
00065    //Call function-template.
00066    DrawMesh(GL_FLOAT, vs, ts);
00067 }
00068 
00069 //______________________________________________________________________________
00070 void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts)
00071 {
00072    //Call function-template.
00073    DrawMesh(GL_DOUBLE, vs, ts);
00074 }
00075 
00076 //______________________________________________________________________________
00077 template<class V, class GLN, class GLV>
00078 void DrawMesh(GLN normal3, GLV vertex3, const std::vector<V> &vs, 
00079               const std::vector<V> &ns, const std::vector<UInt_t> &fTS, 
00080               const TGLBoxCut &box)
00081 {
00082    //Mesh with cut.
00083    //Material and lighting are enabled.
00084    glBegin(GL_TRIANGLES);
00085 
00086    for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
00087       const UInt_t * t = &fTS[i * 3];
00088       if (box.IsInCut(&vs[t[0] * 3]))
00089          continue;
00090       if (box.IsInCut(&vs[t[1] * 3]))
00091          continue;
00092       if (box.IsInCut(&vs[t[2] * 3]))
00093          continue;
00094 
00095       normal3(&ns[t[0] * 3]);
00096       vertex3(&vs[t[0] * 3]);
00097       
00098       normal3(&ns[t[1] * 3]);
00099       vertex3(&vs[t[1] * 3]);
00100       
00101       normal3(&ns[t[2] * 3]);
00102       vertex3(&vs[t[2] * 3]);
00103    }
00104 
00105    glEnd();
00106 }
00107 
00108 //______________________________________________________________________________
00109 void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns, 
00110               const std::vector<UInt_t> &ts, const TGLBoxCut &box)
00111 {
00112    //Call function-template.
00113    DrawMesh(&glNormal3fv, &glVertex3fv, vs,  ns, ts, box);
00114 }
00115 
00116 //______________________________________________________________________________
00117 void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns, 
00118               const std::vector<UInt_t> &ts, const TGLBoxCut &box)
00119 {
00120    //Call function-template.
00121    DrawMesh(&glNormal3dv, &glVertex3dv, vs, ns, ts, box);
00122 }
00123 
00124 //______________________________________________________________________________
00125 template<class V, class GLV>
00126 void DrawMesh(GLV vertex3, const std::vector<V> &vs, const std::vector<UInt_t> &fTS, 
00127               const TGLBoxCut &box)
00128 {
00129    //Mesh with cut.
00130    //No material and lighting.
00131    glBegin(GL_TRIANGLES);
00132 
00133    for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
00134       const UInt_t * t = &fTS[i * 3];
00135       if (box.IsInCut(&vs[t[0] * 3]))
00136          continue;
00137       if (box.IsInCut(&vs[t[1] * 3]))
00138          continue;
00139       if (box.IsInCut(&vs[t[2] * 3]))
00140          continue;
00141 
00142       vertex3(&vs[t[0] * 3]);
00143       vertex3(&vs[t[1] * 3]);
00144       vertex3(&vs[t[2] * 3]);
00145    }
00146 
00147    glEnd();
00148 }
00149 
00150 //______________________________________________________________________________
00151 void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts, const TGLBoxCut &box)
00152 {
00153    //Call function-template.
00154    DrawMesh(&glVertex3fv, vs, ts, box);
00155 }
00156 
00157 //______________________________________________________________________________
00158 void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts, const TGLBoxCut &box)
00159 {
00160    //Call function-template.
00161    DrawMesh(&glVertex3dv, vs, ts, box);
00162 }
00163 
00164 //______________________________________________________________________________
00165 void NormalToColor(Double_t *rfColor, const Double_t *n)
00166 {
00167    //NormalToColor generates a color from a given normal
00168    const Double_t x = n[0];
00169    const Double_t y = n[1];
00170    const Double_t z = n[2];
00171    rfColor[0] = (x > 0. ? x : 0.) + (y < 0. ? -0.5 * y : 0.) + (z < 0. ? -0.5 * z : 0.);
00172    rfColor[1] = (y > 0. ? y : 0.) + (z < 0. ? -0.5 * z : 0.) + (x < 0. ? -0.5 * x : 0.);
00173    rfColor[2] = (z > 0. ? z : 0.) + (x < 0. ? -0.5 * x : 0.) + (y < 0. ? -0.5 * y : 0.);
00174 }
00175 
00176 //______________________________________________________________________________
00177 void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
00178                    const std::vector<UInt_t> &fTS)
00179 {
00180    //Colored mesh with lighting disabled.
00181    Double_t color[] = {0., 0., 0., 0.15};
00182 
00183    glBegin(GL_TRIANGLES);
00184 
00185    for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
00186       const UInt_t *t = &fTS[i * 3];
00187       const Double_t * n = &ns[t[0] * 3];
00188       //
00189       NormalToColor(color, n);
00190       glColor4dv(color);
00191       glVertex3dv(&vs[t[0] * 3]);
00192       //
00193       n = &ns[t[1] * 3];
00194       NormalToColor(color, n);
00195       glColor4dv(color);
00196       glVertex3dv(&vs[t[1] * 3]);
00197       //
00198       n = &ns[t[2] * 3];
00199       NormalToColor(color, n);
00200       glColor4dv(color);
00201       glVertex3dv(&vs[t[2] * 3]);
00202    }
00203 
00204    glEnd();
00205 }
00206 
00207 //______________________________________________________________________________
00208 void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
00209                    const std::vector<UInt_t> &fTS, const TGLBoxCut & box)
00210 {
00211    //Colored mesh with cut and disabled lighting.
00212    Double_t color[] = {0., 0., 0., 0.15};
00213 
00214    glBegin(GL_TRIANGLES);
00215 
00216    for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
00217       const UInt_t *t = &fTS[i * 3];
00218       if (box.IsInCut(&vs[t[0] * 3]))
00219          continue;
00220       if (box.IsInCut(&vs[t[1] * 3]))
00221          continue;
00222       if (box.IsInCut(&vs[t[2] * 3]))
00223          continue;
00224       const Double_t * n = &ns[t[0] * 3];
00225       //
00226       NormalToColor(color, n);
00227       glColor4dv(color);
00228       glVertex3dv(&vs[t[0] * 3]);
00229       //
00230       n = &ns[t[1] * 3];
00231       NormalToColor(color, n);
00232       glColor4dv(color);
00233       glVertex3dv(&vs[t[1] * 3]);
00234       //
00235       n = &ns[t[2] * 3];
00236       NormalToColor(color, n);
00237       glColor4dv(color);
00238       glVertex3dv(&vs[t[2] * 3]);
00239    }
00240 
00241    glEnd();
00242 }
00243 
00244 }

Generated on Tue Jul 5 14:18:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1