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

flat_shade.C

Go to the documentation of this file.
00001 /*!
00002  *  \file flat_shade.C
00003  *  \brief Contains the implementation fo the FlatShadeTexture GTexture and
00004  *  related classes.
00005  *
00006  *  \sa flat_shade.H
00007  *
00008  */
00009 
00010 #include "disp/colors.H"
00011 #include "geom/texturegl.H"
00012 #include "geom/gl_view.H"
00013 #include "gtex/util.H"
00014 #include "mesh/uv_data.H"
00015 #include "mesh/lmesh.H"
00016 #include "mesh/ledge_strip.H"
00017 
00018 #include "flat_shade.H"
00019 
00020 bool FlatShadeTexture::_debug_uv = false;
00021 
00022 static bool debug = Config::get_var_bool("DEBUG_DEBUG_UV",false);
00023 /**********************************************************************
00024  * FlatShadeStripCB:
00025  **********************************************************************/
00026 void 
00027 FlatShadeStripCB::faceCB(CBvert* v, CBface* f) 
00028 {
00029    // normal
00030    glNormal3dv(f->norm().data());
00031 
00032    if (v->has_color()) 
00033    {
00034       GL_COL(v->color(), alpha*v->alpha());
00035    }
00036 
00037    // texture coords
00038    if (do_texcoords) 
00039      if (use_auto) //force automatic 
00040      {   
00041 
00042          //use spherical text coord gen
00043         glTexCoord2dv(auto_UV->uv_from_vert(v,f).data());
00044 
00045      }
00046      else
00047 
00048       {
00049          // the patch has a texture... try to find
00050          // appropriate texture coordinates...
00051 
00052          // use patch's TexCoordGen if possible,
00053          // otherwise use the texture coordinates stored
00054          // on the face (if any):
00055          TexCoordGen* tg = f->patch()->tex_coord_gen();
00056          if (tg)
00057             glTexCoord2dv(tg->uv_from_vert(v,f).data());
00058          else if (UVdata::lookup(f))
00059             glTexCoord2dv(UVdata::get_uv(v,f).data());
00060       }
00061   
00062    // vertex coords
00063    glVertex3dv(v->loc().data());
00064 }
00065 
00066 /**********************************************************************
00067  * UVDiscontinuousEdgeFilter:
00068  **********************************************************************/
00069 class UVDiscontinuousEdgeFilter : public SimplexFilter {
00070  public:
00071    virtual bool accept(CBsimplex* s) const {
00072       return is_edge(s) && !UVdata::is_continuous((Bedge*)s);
00073    }
00074 };
00075 
00076 /**********************************************************************
00077  * HasUVFaceFilter:
00078  **********************************************************************/
00079 class HasUVFaceFilter : public SimplexFilter {
00080  public:
00081    virtual bool accept(CBsimplex* s) const {
00082       return is_face(s) && UVdata::has_uv((Bface*)s);
00083    }
00084 };
00085 
00086 /**********************************************************************
00087  * FlatShadeTexture:
00088  **********************************************************************/
00089 FlatShadeTexture::~FlatShadeTexture()
00090 {
00091 }
00092 
00093 int
00094 FlatShadeTexture::draw(CVIEWptr& v)
00095 {
00096    if (_ctrl)
00097       return _ctrl->draw(v);
00098    _cb->alpha = alpha();
00099 
00100    if (!_patch)
00101       return 0;
00102 
00103    // Don't draw w/ texture maps if there are no uv coords at all.
00104    // but don't check every frame whether there are uv coords:
00105    
00106    if (_check_uv_coords_stamp != _patch->stamp()) {
00107       _has_uv_coords = _patch->cur_faces().any_satisfy(HasUVFaceFilter());
00108       _check_uv_coords_stamp = _patch->stamp();
00109    }
00110 
00111    // Set gl state (lighting, shade model)
00112    glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_CURRENT_BIT);
00113    glEnable(GL_LIGHTING);               // GL_ENABLE_BIT
00114    glShadeModel(GL_FLAT);               // GL_LIGHTING_BIT
00115    GL_COL(_patch->color(), alpha());    // GL_CURRENT_BIT
00116    
00117    if (debug_uv()) {
00118 
00119       //assign some texture coords if no tex_coord_gen is assigned
00120        if (!_patch->tex_coord_gen() )
00121          {
00122             _patch->set_tex_coord_gen(new GLSphirTexCoordGen); 
00123             _patch->mesh()->changed();
00124          }
00125 
00126       // decide what image file to use to show the uv-coords:
00127       str_ptr debug_uv_tex_name =
00128          Config::get_var_str("DEBUG_UV_TEX_MAP", "checkerboard.png",true);
00129       static str_ptr pre_path = Config::JOT_ROOT() + "nprdata/other_textures/";
00130       str_ptr path = pre_path + debug_uv_tex_name;
00131 
00132       // we only try a given pathname once. but if it fails
00133       // and they reset DEBUG_UV_TEX_MAP while running the
00134       // program we can pick up the change and try again.
00135 
00136       if (path != _debug_tex_path) {
00137 
00138          _debug_tex_path = path;
00139          _debug_uv_tex = new TEXTUREgl(_debug_tex_path);
00140          _debug_uv_in_dl = false;
00141 
00142          if (debug) {
00143             // Don't print extraneous messages unless they care:
00144             cerr << "Loading debug uv texture " << **path << " ..." << endl;
00145          }
00146 
00147          if (!_debug_uv_tex->load_texture()) {
00148             cerr << "Can't load debug uv texture: "
00149                  << **_debug_tex_path
00150                  << endl
00151                  << "Set environment variable DEBUG_UV_TEX_MAP "
00152                  << "to an image file in "
00153                  << **pre_path
00154                  << " and try again."
00155                  << endl;
00156             _debug_uv_tex = 0;
00157          }
00158       }
00159 
00160       // Apply the texture if any faces have uv coordinates:
00161       if (_debug_uv_tex /*&& _has_uv_coords*/) //<- using auto UV
00162          _debug_uv_tex->apply_texture(); // GL_ENABLE_BIT
00163 
00164    } else {
00165       
00166       // XXX - dumb hack
00167       check_patch_texture_map();
00168 
00169       // this is a no-op unless needed:
00170       // (NB: don't put it inside display list creation):
00171      
00172      
00173      // if (_has_uv_coords)    <- using auto UV
00174          _patch->apply_texture(); // GL_ENABLE_BIT
00175    }
00176 
00177    // Set material parameters for OGL:
00178    GtexUtil::setup_material(_patch);
00179 
00180    // Try for the display list if it's valid
00181    if (!(_debug_uv == _debug_uv_in_dl && BasicTexture::draw(v))) {
00182 
00183       // Try to generate a display list
00184       int dl = _dl.get_dl(v, 1, _patch->stamp());
00185       if (dl) {
00186          glNewList(dl, GL_COMPILE);
00187          _debug_uv_in_dl = _debug_uv;
00188       }
00189       
00190       // Set up face culling for closed surfaces
00191       if (!set_face_culling()) 
00192          glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);  // GL_LIGHTING_BIT
00193       
00194       FlatShadeStripCB *flat_cb = dynamic_cast<FlatShadeStripCB*>(_cb);
00195 
00196       // Turn on texturing if any
00197       if (_has_uv_coords && debug_uv() && _debug_uv_tex) {
00198          if (flat_cb) flat_cb->enable_texcoords();
00199      } else if (debug_uv() && _debug_uv_tex && !_has_uv_coords) {
00200         if (flat_cb)
00201         {
00202            flat_cb->enable_texcoords();
00203            //flat_cb->enable_autoUV();
00204         }
00205      } else if (_has_uv_coords && _patch->has_texture()) {
00206          if (flat_cb) flat_cb->enable_texcoords();
00207       } else {
00208          if (flat_cb) flat_cb->disable_texcoords();
00209       }
00210 
00211       err_adv(debug && _debug_uv, "  drawing uvs in flat shade");
00212       
00213       // draw the triangle strips
00214       _patch->draw_tri_strips(_cb);
00215 
00216       // end the display list here
00217       if (_dl.dl(v)) {
00218          _dl.close_dl(v);
00219 
00220          // the display list is built; now execute it
00221          BasicTexture::draw(v);
00222       }
00223    }
00224 
00225    // restore GL state
00226    glPopAttrib();
00227 
00228    // Have to move this outside the display list and gl push/pop attrib
00229    // or else it's all:
00230 /*
00231 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00232 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00233 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00234 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00235 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00236 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00237 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00238 FlatShadeTexture::draw - End ***NOTE*** OpenGL Error: [500] 'Invalid Enumerator'
00239 */
00240    if (_debug_uv) {
00241 
00242       // Build the edge strip at the current subdivision level
00243       Bedge_list edges = _patch->cur_edges();
00244       edges.clear_flags();
00245 
00246       // If secondary edges shouldn't be drawn, set their flags
00247       // so they won't be drawn:
00248       if (!BMESH::show_secondary_faces())
00249          edges.secondary_edges().set_flags(1);
00250 
00251       // Draw uv-discontinuity boundaries as yellow lines.
00252       // Construct filter that accepts unreached uv-discontinuous
00253       // edges of this patch:
00254       UnreachedSimplexFilter    unreached;
00255       UVDiscontinuousEdgeFilter uvdisc;
00256       PatchEdgeFilter           mine(_patch->cur_patch());
00257       EdgeStrip disc_edges(edges, unreached + uvdisc + mine);
00258       if (!disc_edges.empty()) {
00259          GtexUtil::draw_strip(disc_edges, 3, Color::yellow, 0.8);
00260       }
00261    }
00262 
00263    GL_VIEW::print_gl_errors("FlatShadeTexture::draw - End");
00264 
00265    return _patch->num_faces();
00266 }
00267 
00268 /* end of file flat_shade.C */

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