00001 #ifndef SHOW_TRIS_H_IS_INCLUDED
00002 #define SHOW_TRIS_H_IS_INCLUDED
00003
00004 #include "disp/colors.H"
00005 #include "geom/gl_view.H"
00006 #include "geom/gl_util.H"
00007
00008
00009
00010
00011
00012
00013 MAKE_PTR_SUBC(SHOW_TRIS,GEL);
00014 class SHOW_TRIS : public GEL {
00015 public:
00016
00017
00018
00019 struct Triangle {
00020 mlib::Wpt _a, _b, _c;
00021
00022 Triangle() {}
00023 Triangle(mlib::CWpt& a, mlib::CWpt& b, mlib::CWpt& c) : _a(a), _b(b), _c(c) {}
00024
00025 bool operator==(const Triangle& tri) const {
00026 return _a == tri._a && _b == tri._b && _c == tri._c;
00027 }
00028 mlib::Wvec norm() const { return cross(_b - _a, _c - _a).normalized(); }
00029 };
00030 typedef const Triangle CTriangle;
00031 typedef ARRAY<Triangle> Triangle_list;
00032 typedef const Triangle_list CTriangle_list;
00033
00034
00035
00036 SHOW_TRIS(const Triangle_list& tris = Triangle_list(),
00037 const COLOR& fill_color = Color::yellow,
00038 const COLOR& line_color = Color::black,
00039 double alpha=1) :
00040 _tris(tris),
00041 _fill_color(fill_color),
00042 _line_color(line_color),
00043 _alpha(alpha),
00044 _line_width(1),
00045 _do_fill(true),
00046 _do_lines(true) {}
00047
00048
00049
00050 CTriangle_list& tris() const { return _tris; }
00051 void set_tris(CTriangle_list& tris) { _tris = tris; }
00052
00053 CCOLOR& fill_color() const { return _fill_color; }
00054 CCOLOR& line_color() const { return _line_color; }
00055 double alpha() const { return _alpha; }
00056
00057 void set_fill_color(CCOLOR& color) { _fill_color = color; }
00058 void set_line_color(CCOLOR& color) { _line_color = color; }
00059 void set_alpha(double alpha) { _alpha = alpha; }
00060
00061 void set_line_width(double w) { _line_width = (GLfloat)w; }
00062
00063 void set_do_fill(bool b=true) { _do_fill = b; }
00064 void set_do_lines(bool b=true) { _do_fill = b; }
00065
00066 void add(CTriangle& tri) { _tris += tri; }
00067 void clear() { _tris.clear(); }
00068 int num() const { return _tris.num(); }
00069
00070
00071
00072 virtual int draw(CVIEWptr &v) {
00073 if (_tris.empty()) return 0;
00074 if (_do_fill)
00075 draw_filled();
00076 if (_do_lines)
00077 draw_lines();
00078 return num();
00079 }
00080
00081
00082
00083 virtual DATA_ITEM* dup() const { return new SHOW_TRIS(); }
00084
00085 protected:
00086 Triangle_list _tris;
00087 COLOR _fill_color;
00088 COLOR _line_color;
00089 double _alpha;
00090 GLfloat _line_width;
00091 bool _do_fill;
00092 bool _do_lines;
00093
00094 void draw_filled() const {
00095
00096 glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_CURRENT_BIT);
00097 GL_VIEW::init_polygon_offset();
00098 glEnable(GL_LIGHTING);
00099 glShadeModel(GL_FLAT);
00100 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
00101 GL_COL(_fill_color, _alpha);
00102 glBegin(GL_TRIANGLES);
00103 for (int i=0; i<_tris.num(); i++) {
00104 draw_flat_tri(_tris[i]);
00105 }
00106 glEnd();
00107 GL_VIEW::end_polygon_offset();
00108 glPopAttrib();
00109 }
00110 void draw_flat_tri(const Triangle& tri) const {
00111 glNormal3dv(tri.norm().data());
00112 glVertex3dv(tri._a.data());
00113 glVertex3dv(tri._b.data());
00114 glVertex3dv(tri._c.data());
00115 }
00116 void draw_lines() const {
00117
00118 GL_VIEW::init_line_smooth(_line_width, GL_CURRENT_BIT);
00119 glDisable(GL_LIGHTING);
00120 GL_COL(_line_color, _alpha);
00121 glBegin(GL_LINES);
00122 for (int i=0; i<_tris.num(); i++) {
00123 draw_wire_tri(_tris[i]);
00124 }
00125 glEnd();
00126 GL_VIEW::end_line_smooth();
00127 }
00128 void draw_wire_tri(const Triangle& tri) const {
00129 glVertex3dv(tri._a.data());
00130 glVertex3dv(tri._b.data());
00131 glVertex3dv(tri._b.data());
00132 glVertex3dv(tri._c.data());
00133 glVertex3dv(tri._c.data());
00134 glVertex3dv(tri._a.data());
00135 }
00136 };
00137
00138 #endif // SHOW_TRIS_H_IS_INCLUDED
00139
00140