00001
00002
00003
00004
00005
00006
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
00025
00026 void
00027 FlatShadeStripCB::faceCB(CBvert* v, CBface* f)
00028 {
00029
00030 glNormal3dv(f->norm().data());
00031
00032 if (v->has_color())
00033 {
00034 GL_COL(v->color(), alpha*v->alpha());
00035 }
00036
00037
00038 if (do_texcoords)
00039 if (use_auto)
00040 {
00041
00042
00043 glTexCoord2dv(auto_UV->uv_from_vert(v,f).data());
00044
00045 }
00046 else
00047
00048 {
00049
00050
00051
00052
00053
00054
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
00063 glVertex3dv(v->loc().data());
00064 }
00065
00066
00067
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
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
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
00104
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
00112 glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_CURRENT_BIT);
00113 glEnable(GL_LIGHTING);
00114 glShadeModel(GL_FLAT);
00115 GL_COL(_patch->color(), alpha());
00116
00117 if (debug_uv()) {
00118
00119
00120 if (!_patch->tex_coord_gen() )
00121 {
00122 _patch->set_tex_coord_gen(new GLSphirTexCoordGen);
00123 _patch->mesh()->changed();
00124 }
00125
00126
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
00133
00134
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
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
00161 if (_debug_uv_tex )
00162 _debug_uv_tex->apply_texture();
00163
00164 } else {
00165
00166
00167 check_patch_texture_map();
00168
00169
00170
00171
00172
00173
00174 _patch->apply_texture();
00175 }
00176
00177
00178 GtexUtil::setup_material(_patch);
00179
00180
00181 if (!(_debug_uv == _debug_uv_in_dl && BasicTexture::draw(v))) {
00182
00183
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
00191 if (!set_face_culling())
00192 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
00193
00194 FlatShadeStripCB *flat_cb = dynamic_cast<FlatShadeStripCB*>(_cb);
00195
00196
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
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
00214 _patch->draw_tri_strips(_cb);
00215
00216
00217 if (_dl.dl(v)) {
00218 _dl.close_dl(v);
00219
00220
00221 BasicTexture::draw(v);
00222 }
00223 }
00224
00225
00226 glPopAttrib();
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 if (_debug_uv) {
00241
00242
00243 Bedge_list edges = _patch->cur_edges();
00244 edges.clear_flags();
00245
00246
00247
00248 if (!BMESH::show_secondary_faces())
00249 edges.secondary_edges().set_flags(1);
00250
00251
00252
00253
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