Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

util.H

Go to the documentation of this file.
00001 #ifndef GTEX_UTIL_H_IS_INCLUDED
00002 #define GTEX_UTIL_H_IS_INCLUDED
00003 
00004 #include "geom/gl_view.H"
00005 #include "geom/world.H"
00006 #include "geom/show_tris.H"
00007 #include "mesh/edge_strip.H"
00008 #include "mesh/stripcb.H"
00009 #include "mesh/lmesh.H"
00010 
00011 /**********************************************************************
00012  * namespace TexUnit:
00013  *
00014  *   Used to coordinate what texture stages are used for
00015  *   what purposes. Texture units 1 - 3 are reserved for
00016  *   paper texture, perlin noise, and reference images in
00017  *   texture memory (when those effects are active).
00018  **********************************************************************/
00019 namespace TexUnit {
00020    enum tex_stage_t { APP=0, PAPER, PERLIN, REF_IMG };
00021 };
00022 
00023 class TriStrip;
00024 /**********************************************************************
00025  * GLStripCB:
00026  *
00027  *      provides default callbacks to use for drawing
00028  *      triangle, line or point strips with OpenGL.
00029  **********************************************************************/
00030 class GLStripCB : public StripCB {
00031  public:
00032 
00033    // most derived classes will not need to override these:
00034    virtual void begin_faces(TriStrip*)  { glBegin(GL_TRIANGLE_STRIP); }
00035    virtual void end_faces  (TriStrip*)  { glEnd(); }
00036 
00037    virtual void begin_edges(EdgeStrip*) { glBegin(GL_LINE_STRIP); }
00038    virtual void end_edges  (EdgeStrip*) { glEnd(); }
00039 
00040    virtual void begin_verts(VertStrip*) { glBegin(GL_POINTS); }
00041    virtual void end_verts  (VertStrip*) { glEnd(); }
00042 
00043    // most derived classes will override these to specify vertex
00044    // normals, colors, and/or texture coordinates in addition to
00045    // vertex coordinates:
00046    virtual void faceCB(CBvert* v, CBface*) { glVertex3dv(v->loc().data()); }
00047    virtual void edgeCB(CBvert* v, CBedge*) { glVertex3dv(v->loc().data()); }
00048    virtual void vertCB(CBvert* v)          { glVertex3dv(v->loc().data()); }
00049 };
00050 
00051 /*****************************************************************
00052  * VertNormStripCB:
00053  *
00054  *   Convenience: sends both vertex normal and position to OpenGL.
00055  *   Used for basic rendering styles that use lighting but don't
00056  *   send texture coordinates or colors (etc.) to OpenGL.
00057  *****************************************************************/
00058 class VertNormStripCB : public GLStripCB {
00059  public:
00060    virtual void faceCB(CBvert* v, CBface* f) {
00061       glNormal3dv(f->vert_normal(v).data());
00062       glVertex3dv(v->loc().data());
00063    }
00064 };
00065 
00066 /*****************************************************************
00067  * utilities
00068  *****************************************************************/
00069 namespace GtexUtil {
00070 
00071    inline str_ptr toon_path() {
00072       return Config::JOT_ROOT() + "nprdata/toon_textures/"; 
00073    }
00074    inline str_ptr toon_name(Cstr_ptr& name) { return toon_path() + name; }
00075 
00076    // sent material properties to OpenGL
00077    inline void 
00078    setup_material(APPEAR* a)
00079    {
00080       // XXX - not handling emmissive color
00081       assert(a);
00082       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,
00083                    float4(a->ambient_color(),a->transp()));
00084       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,
00085                    float4(a->color(),a->transp()));
00086       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
00087                    float4(a->specular_color(),a->transp()));
00088       glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, (GLfloat)a->shininess());
00089    }
00090 
00091    // Draw an EdgeStrip
00092    inline void
00093    draw_strip(
00094       EdgeStrip& strip, // edge strip to render
00095       double width,     // width for gl lines
00096       CCOLOR& color,    // color 
00097       double alpha=1,   // alpha
00098       StripCB* cb=0     // callback for edge strip (uses GLStripCB by default)
00099       )
00100    {
00101       VIEWptr v = VIEW::peek();
00102       GL_VIEW::init_line_smooth(GLfloat(v->line_scale()*width), GL_CURRENT_BIT);
00103  
00104       glDisable(GL_LIGHTING);      // GL_ENABLE_BIT
00105       GL_COL(color, alpha);        // GL_CURRENT_BIT
00106  
00107       GLStripCB glcb;
00108       strip.draw(cb ? cb : &glcb);
00109 
00110       GL_VIEW::end_line_smooth();
00111    }
00112    inline void draw_strip(EdgeStrip& strip, double width=1, StripCB* cb=0) {
00113       return draw_strip(strip, width, Color::black, 1, cb);
00114    }
00115 
00116    // Helper used in show_tris() below:
00117    inline SHOW_TRIS::Triangle tri(Bface* f) {
00118       return f ?
00119          SHOW_TRIS::Triangle(f->v1()->wloc(),f->v2()->wloc(),f->v3()->wloc()) :
00120          SHOW_TRIS::Triangle();
00121    }
00122    // Draw a set of Bfaces, for debugging:
00123    inline GELptr show_tris(CBface_list& faces, CCOLOR& col=Color::yellow) {
00124       SHOW_TRISptr tris = new SHOW_TRIS();
00125       for (int i=0; i<faces.num(); i++) {
00126          tris->add(tri(faces[i]));
00127       }
00128       tris->set_fill_color(col);
00129       WORLD::create(tris,false); // false means not undoable
00130       return tris;
00131    }
00132 
00133    // show an edge:
00134    inline GELptr show(
00135       Bedge*            e,
00136       double            width=2,
00137       CCOLOR&           col=Color::blue,
00138       double            alpha=1,
00139       bool              depth_test=true
00140       ) {
00141       if (!e) return 0;
00142       return WORLD::show(
00143          e->v1()->wloc(), e->v2()->wloc(), width, col, alpha, depth_test
00144          );
00145    }
00146 
00147    // show a bunch of edges
00148    inline GELlist show(
00149       CBedge_list&      edges,
00150       double            width=2,
00151       CCOLOR&           col=Color::blue,
00152       double            alpha=1,
00153       bool              depth_test=true
00154       ) {
00155       GELlist ret(edges.num());
00156       for (int i=0; i<edges.num(); i++)
00157          ret += show(edges[i], width, col, alpha, depth_test);
00158       return ret;
00159    }
00160 };
00161 
00162 #endif // GTEX_UTIL_H_IS_INCLUDED
00163 
00164 // end of file util.H

Generated on Mon Sep 18 11:39:34 2006 for jot by  doxygen 1.4.4