00001 #ifndef PATCH_H_IS_INCLUDED
00002 #define PATCH_H_IS_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "geom/appear.H"
00013
00014 #include "bmesh.H"
00015 #include "tex_coord_gen.H"
00016 #include <vector>
00017
00018 class GTexture;
00019 typedef const GTexture CGTexture;
00020
00021
00022
00023
00024 class GTexture_list : public BMD_array<GTexture> {
00025
00026 public:
00027
00028
00029 GTexture_list(int n=0) : BMD_array<GTexture>(n) {}
00030 GTexture_list(const GTexture_list& list) : BMD_array<GTexture>(list) {}
00031 GTexture_list(GTexture* g) : BMD_array<GTexture>(1) {
00032 add(g);
00033 }
00034 GTexture_list(GTexture* g1, GTexture* g2) : BMD_array<GTexture>(2) {
00035 add(g1);
00036 add(g2);
00037 }
00038 GTexture_list(GTexture* g1, GTexture* g2, GTexture* g3) :
00039 BMD_array<GTexture>(3) {
00040 add(g1);
00041 add(g2);
00042 add(g3);
00043 }
00044 GTexture_list(GTexture* g1, GTexture* g2, GTexture* g3, GTexture* g4) :
00045 BMD_array<GTexture>(4) {
00046 add(g1);
00047 add(g2);
00048 add(g3);
00049 add(g4);
00050 }
00051 GTexture_list(GTexture* g1, GTexture* g2, GTexture* g3, GTexture* g4,
00052 GTexture* g5) :
00053 BMD_array<GTexture>(5) {
00054 add(g1);
00055 add(g2);
00056 add(g3);
00057 add(g4);
00058 add(g5);
00059 }
00060 GTexture_list(GTexture* g1, GTexture* g2, GTexture* g3, GTexture* g4,
00061 GTexture* g5, GTexture* g6) :
00062 BMD_array<GTexture>(6) {
00063 add(g1);
00064 add(g2);
00065 add(g3);
00066 add(g4);
00067 add(g5);
00068 add(g6);
00069 }
00070 GTexture_list(GTexture* g1, GTexture* g2, GTexture* g3, GTexture* g4,
00071 GTexture* g5, GTexture* g6, GTexture* g7) :
00072 BMD_array<GTexture>(6) {
00073 add(g1);
00074 add(g2);
00075 add(g3);
00076 add(g4);
00077 add(g5);
00078 add(g6);
00079 add(g7);
00080 }
00081 GTexture_list(GTexture* g1, GTexture* g2, GTexture* g3, GTexture* g4,
00082 GTexture* g5, GTexture* g6, GTexture* g7, GTexture* g8) :
00083 BMD_array<GTexture>(6) {
00084 add(g1);
00085 add(g2);
00086 add(g3);
00087 add(g4);
00088 add(g5);
00089 add(g6);
00090 add(g7);
00091 add(g8);
00092 }
00093
00094
00095
00096 void write_stream(ostream& os) const;
00097 void delete_all();
00098
00099 void set_patch(Patch* p) const;
00100
00101 void push_alpha(double a) const;
00102 void pop_alpha() const;
00103 };
00104 typedef const GTexture_list CGTexture_list;
00105
00106 typedef void (*init_fade_func_t)(GTexture*, GTexture*, double, double);
00107
00108 class PatchData;
00109 typedef const PatchData CPatchData;
00110
00111
00112
00113
00114
00115
00116 class PatchData {
00117
00118 public:
00119
00120 virtual ~PatchData() {}
00121
00122
00123
00124
00125 DEFINE_RTTI_METHODS_BASE("PatchData", CPatchData*);
00126
00127
00128
00129 };
00130
00131
00132
00133
00134
00135
00136 class DynamicSample;
00137 typedef const DynamicSample CDynamicSample;
00138
00139 class DynamicSample {
00140
00141 public:
00142 DynamicSample() {}
00143 DynamicSample(CBsimplex* s, Wvec bc, PIXEL pix, double weight)
00144 : _s(s),
00145 _bc(bc),
00146 _pix(pix),
00147 _weight(weight)
00148 {}
00149 virtual ~DynamicSample() {}
00150 CBsimplex* get_simplex() const { return _s; }
00151 Wvec get_bc() const { return _bc;}
00152 double get_weight() const { return _weight; }
00153 PIXEL get_pix() const { return _pix; }
00154 Wpt get_pos(CWtransf& tr) const { return tr*_s->bc2pos(_bc); }
00155
00156
00157 DEFINE_RTTI_METHODS_BASE("DynamicSample", CDynamicSample*);
00158
00159 public:
00160 CBsimplex* _s;
00161 Wvec _bc;
00162 PIXEL _pix;
00163 double _weight;
00164
00165 };
00166
00167 class Patch;
00168 typedef const Patch CPatch;
00169
00170
00171
00172
00173
00174 class Patch : public DATA_ITEM, public APPEAR, public BMESHdrawable {
00175
00176 protected:
00177
00178
00179 Patch(BMESH* = 0);
00180 friend class BMESH;
00181
00182 public:
00183
00184 virtual ~Patch();
00185
00186
00187
00188
00189 BMESH* mesh () const { return _mesh; }
00190 void set_mesh(BMESH* m) { _mesh = m; changed(); }
00191 Cstr_ptr& name () const { return _name;}
00192 void set_name(Cstr_ptr &str) { _name = str;}
00193 Cstr_ptr& texture_file () const { return _texture_file;}
00194 void set_texture_file(Cstr_ptr &str) { _texture_file = str;}
00195 TexCoordGen* tex_coord_gen() const { return _tex_coord_gen; }
00196 void set_tex_coord_gen(TexCoordGen* tg) { _tex_coord_gen = tg; }
00197
00198 CARRAY<uint>& pixels() const { return _pixels; }
00199 void add_pixel(uint p) { _pixels += p; }
00200
00201 int num_vert_strips() const { return _vert_strips.num(); }
00202 int num_edge_strips() const { return _edge_strips.num(); }
00203 int num_tri_strips() const { return _tri_strips.num(); }
00204
00205 virtual void set_data(PatchData *d) { _data = d; }
00206 virtual PatchData* get_data() { return _data; }
00207
00208 int stencil_id() {
00209 if (!_stencil_id)
00210 _stencil_id = ++_next_stencil_id;
00211 return _stencil_id;
00212 }
00213
00214
00215 const GTexture_list& gtextures() const { return _textures; }
00216
00217
00218
00219
00220
00221
00222
00223 CBface_list& faces() const { return _faces; }
00224
00225
00226
00227 Bvert_list verts() const;
00228
00229 Bedge_list edges() const;
00230
00231
00232
00233
00234
00235
00236
00237
00238 EdgeStrip& cur_sils()
00239 { return cur_patch()->build_sils(); }
00240
00241 ZcrossPath& cur_zx_sils()
00242 { return cur_patch()->build_zx_sils(); }
00243
00244 EdgeStrip* cur_creases()
00245 { return cur_patch()->build_creases(); }
00246
00247 EdgeStrip* cur_borders()
00248 { return cur_patch()->build_borders(); }
00249
00250 protected:
00251
00252
00253
00254
00255 EdgeStrip& build_sils();
00256
00257 ZcrossPath& build_zx_sils();
00258
00259 EdgeStrip* build_creases();
00260
00261 EdgeStrip* build_borders();
00262
00263 public:
00264
00265 protected:
00266
00267
00268
00269 EdgeStrip& sils() { return _sils; }
00270
00271 ZcrossPath& zx_sils() { return _zx_sils; }
00272
00273 EdgeStrip* creases() { return _creases; }
00274
00275 EdgeStrip* borders() { return _borders; }
00276
00277
00278
00279 public:
00280
00281
00282
00283
00284
00285
00286
00287 virtual Patch* cur_patch() { return this; }
00288
00289
00290 BMESH* cur_mesh() { return cur_patch()->mesh(); }
00291
00292
00293
00294 int subdiv_level() const { return _mesh ? _mesh->subdiv_level() : 0; }
00295
00296
00297
00298 virtual int rel_subdiv_level() { return 0; }
00299
00300
00301
00302 virtual int rel_edit_level()
00303 { return _mesh ? (_mesh->edit_level() - subdiv_level()) : 0; }
00304
00305
00306 virtual CBface_list& cur_faces() const { return _faces; }
00307
00308 virtual Bvert_list cur_verts() const { return verts(); }
00309
00310 virtual Bedge_list cur_edges() const { return edges(); }
00311
00312
00313
00314
00315 virtual int num_faces() const { return _faces.num();}
00316
00317
00318
00319 virtual Patch* get_child() { return 0; }
00320
00321
00322
00323
00324
00325 virtual Patch* ctrl_patch() const { return (Patch*)this; }
00326
00327
00328
00329
00330
00331
00332 CWtransf& xform() const { return _mesh->xform(); }
00333 CWtransf& inv_xform() const { return _mesh->inv_xform(); }
00334 CWtransf& obj_to_ndc() const { return _mesh->obj_to_ndc(); }
00335 CWpt& eye_local() const { return _mesh->eye_local(); }
00336 double pix_size() const { return _mesh->pix_size(); }
00337
00338
00339
00340
00341
00342
00343 void add(Bface* f);
00344 void remove(Bface* f);
00345
00346 void add(VertStrip*);
00347 void remove(VertStrip*);
00348
00349 void add(EdgeStrip*);
00350 void remove(EdgeStrip*);
00351
00352 void build_tri_strips();
00353 virtual double tris_per_strip() const {
00354 return _tri_strips.empty() ? 0.0 :
00355 double(_faces.num())/_tri_strips.num();
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365 virtual void changed() { _stamp++; }
00366
00367 virtual uint stamp() {
00368 if (_mesh && _mesh->version() != _mesh_version) {
00369 _mesh_version = _mesh->version();
00370 changed();
00371 }
00372 return _stamp;
00373 }
00374
00375
00376
00377
00378
00379
00380 virtual void triangulation_changed();
00381
00382 void creases_changed();
00383 void borders_changed();
00384
00385
00386
00387
00388
00389
00390 virtual int draw(CVIEWptr&);
00391
00392
00393
00394
00395
00396
00397
00398 virtual int draw_tri_strips(StripCB *);
00399 virtual int draw_sil_strips(StripCB *);
00400
00401 int draw_crease_strips(StripCB *);
00402 int draw_border_strips(StripCB *);
00403 int draw_edge_strips(StripCB *);
00404 int draw_vert_strips(StripCB *);
00405
00406
00407
00408
00409
00410
00411 void set_texture(GTexture* gtex);
00412 void set_texture(Cstr_ptr& style);
00413 void next_texture();
00414
00415
00416 GTexture* find_tex(Cstr_ptr& tex_name) const;
00417
00418
00419 GTexture* get_tex(Cstr_ptr& tex_name);
00420
00421
00422 int get_tex_index(Cstr_ptr& tex_name) {
00423 return _textures.get_index(get_tex(tex_name));
00424 }
00425
00426
00427
00428 virtual GTexture* cur_tex(CVIEWptr& v);
00429
00430
00431 GTexture* cur_tex() const {
00432 return _textures.valid_index(_cur_tex_i) ? _textures[_cur_tex_i] : 0;
00433 }
00434
00435
00436
00437
00438
00439
00440 virtual int write_stream (ostream &os);
00441 virtual int read_stream (istream &is, str_list &leftover);
00442 virtual int read_texture (istream &is, str_list &leftover);
00443 virtual int read_color (istream &is, str_list &leftover);
00444 virtual int read_texture_map (istream &is, str_list &leftover);
00445 virtual int read_patchname (istream &is, str_list &leftover);
00446
00447
00448
00449
00450
00451
00452 virtual ref_img_t use_ref_image();
00453
00454 virtual int draw_vis_ref();
00455 virtual int draw_ref_img(ref_img_t);
00456 virtual int draw_final(CVIEWptr &v);
00457
00458
00459
00460
00461
00462
00463 DEFINE_RTTI_METHODS3("Patch", Patch*, DATA_ITEM, Patch *);
00464
00465
00466
00467
00468
00469
00470 virtual CTAGlist &tags() const;
00471 virtual DATA_ITEM *dup () const;
00472
00473
00474
00475
00476
00477
00478 virtual CCOLOR& color () const;
00479 virtual bool has_color () const;
00480 virtual void set_color (CCOLOR& c);
00481 virtual void unset_color ();
00482
00483 virtual double transp () const;
00484 virtual bool has_transp () const;
00485 virtual void set_transp(double t);
00486 virtual void unset_transp();
00487
00488
00489 virtual void set_texture (CTEXTUREptr& t);
00490
00491 virtual void unset_texture ();
00492
00493
00494 void apply_texture() {
00495 if (has_texture()) {
00496 _texture->apply_texture(&_tex_xform);
00497 }
00498 }
00499
00500
00501
00502 static init_fade_func_t _init_fade;
00503
00504
00505
00506
00507 PIXEL get_old_sample_center() { return _old_center; }
00508
00509 PIXEL get_sample_center() { return _new_center; }
00510
00511 VEXEL get_z() { return _z; }
00512
00513 PIXEL get_patch_center() { return _patch_center; }
00514
00515
00516 bool get_do_dynamic_stuff() const { return _do_dynamic_stuff; }
00517 void do_dynamic_stuff(bool d) { _do_dynamic_stuff = d; }
00518 void set_do_lod(bool d) { _do_lod = d; }
00519 bool get_do_lod() { return _do_lod; }
00520
00521 PIXEL sample_origin() const { return _o; }
00522 VEXEL sample_u_vec() const { return (_u_o - _o); }
00523 VEXEL sample_v_vec() const { return (_v_o - _o); }
00524 double sample_scale() const { return _scale; }
00525 const vector<DynamicSample>& get_samples() const{ return _old_samples; }
00526
00527 double lod_t() const { return _lod_t; }
00528 VEXEL lod_u() const { return _lod_u; }
00529 VEXEL lod_v() const { return _lod_v; }
00530
00531 void update_dynamic_samples();
00532 void reset_dynamic_samples();
00533
00534 private:
00535 void create_dynamic_samples();
00536
00537
00538 protected:
00539
00540 BMESH* _mesh;
00541
00542 Bface_list _faces;
00543 ARRAY<TriStrip*> _tri_strips;
00544
00545 EdgeStrip _sils;
00546 ZcrossPath _zx_sils;
00547 EdgeStrip* _creases;
00548 EdgeStrip* _borders;
00549 ARRAY<EdgeStrip*> _edge_strips;
00550 ARRAY<VertStrip*> _vert_strips;
00551
00552 GTexture_list _textures;
00553 int _cur_tex_i;
00554 int _non_tex_i;
00555
00556 GTexture* _prev_tex;
00557
00558 ARRAY<uint> _pixels;
00559 uint _stamp;
00560 uint _mesh_version;
00561 bool _tri_strips_dirty;
00562
00563 str_ptr _name;
00564 str_ptr _texture_file;
00565
00566 TexCoordGen* _tex_coord_gen;
00567 PatchData* _data;
00568
00569 int _stencil_id;
00570 static int _next_stencil_id;
00571
00572
00573
00574 vector<DynamicSample> _old_samples;
00575
00576 PIXEL _old_center;
00577 PIXEL _new_center;
00578 VEXEL _z;
00579 double _scale;
00580 PIXEL _patch_center;
00581
00582
00583
00584 PIXEL _o;
00585 PIXEL _u_o;
00586 PIXEL _v_o;
00587 uint _dynamic_stamp;
00588 bool _do_dynamic_stuff;
00589 bool _do_lod;
00590 double _lod_t;
00591 VEXEL _lod_u;
00592 VEXEL _lod_v;
00593
00594
00595
00596
00597
00598 static TAGlist *_patch_tags;
00599 COLOR &color_ () { return _color;}
00600 str_ptr &name_ () { return _name;}
00601 int &cur_tex_i_() { return _cur_tex_i;}
00602 virtual void get_faces(TAGformat &d);
00603 virtual void put_faces(TAGformat &d) const;
00604 virtual void get_color (TAGformat &d) { COLOR c; *d >> c; set_color(c); }
00605 virtual void put_color (TAGformat &d) const {
00606 if (has_color())
00607 d.id() << color();
00608 }
00609 virtual void get_texture (TAGformat &d);
00610 virtual void put_textures(TAGformat &d) const;
00611 virtual void recompute();
00612
00613
00614
00615
00616
00617 EdgeStrip* build_edge_strip(EdgeStrip* strip, CSimplexFilter& filter);
00618
00619 };
00620
00621
00622
00623
00624
00625
00626
00627
00628 inline Patch*
00629 get_patch(CBface* f)
00630 {
00631 return f ? f->patch() : 0;
00632 }
00633
00634
00635
00636
00637
00638 inline Patch*
00639 get_patch(CBsimplex* s)
00640 {
00641
00642
00643
00644
00645 return is_edge(s) ? ((CBedge*)s)->patch() : get_patch(get_bface(s));
00646 }
00647
00648
00649
00650
00651 inline Patch*
00652 get_ctrl_patch(CBface* f)
00653 {
00654 Patch* p = get_patch(f);
00655 return p ? p->ctrl_patch() : 0;
00656 }
00657
00658
00659
00660
00661 inline Patch*
00662 get_ctrl_patch(CBsimplex* s)
00663 {
00664 Patch* p = get_patch(s);
00665 return p ? p->ctrl_patch() : 0;
00666 }
00667
00668
00669
00670
00671 class PatchEdgeFilter : public SimplexFilter {
00672 public:
00673 PatchEdgeFilter(Patch* p) : _patch(p) {}
00674
00675 virtual bool accept(CBsimplex* s) const {
00676 if (!is_edge(s))
00677 return false;
00678 Bedge* e = (Bedge*)s;
00679 return (
00680 !_patch ||
00681 get_patch(e->f1()) == _patch ||
00682 get_patch(e->f2()) == _patch
00683 );
00684 }
00685
00686 protected:
00687 Patch* _patch;
00688 };
00689
00690
00691
00692
00693 class PatchFaceFilter : public SimplexFilter {
00694 public:
00695 PatchFaceFilter(Patch* p) : _patch(p) {}
00696
00697 virtual bool accept(CBsimplex* s) const {
00698 if (!is_face(s))
00699 return false;
00700 return ((Bface*)s)->patch() == _patch;
00701 }
00702
00703 protected:
00704 Patch* _patch;
00705 };
00706
00707
00708
00709
00710
00711
00712 inline void
00713 Patch_list::creases_changed() const
00714 {
00715 for (int k=0; k<_num; k++)
00716 _array[k]->creases_changed();
00717 }
00718
00719 inline void
00720 Patch_list::triangulation_changed() const
00721 {
00722 for (int k=0; k<_num; k++)
00723 _array[k]->triangulation_changed();
00724 }
00725
00726 inline void
00727 Patch_list::write_stream(ostream& os) const
00728 {
00729 for (int k=0; k<_num; k++)
00730 _array[k]->write_stream(os);
00731 }
00732
00733 inline void
00734 Patch_list::delete_all()
00735 {
00736 while (!empty())
00737 delete pop();
00738 }
00739
00740 inline double
00741 Patch_list::min_alpha() const
00742 {
00743 double ret=1;
00744 for (int k=0; k<_num; k++)
00745 ret = min(ret, _array[k]->transp());
00746 return ret;
00747 }
00748
00749 #endif // PATCH_H_IS_INCLUDED
00750
00751