00001
00002
00003
00004
00005 #include "std/support.H"
00006 #include "glew/glew.H"
00007
00008 #include "disp/colors.H"
00009 #include "geom/icon2d.H"
00010
00011 using namespace mlib;
00012
00013 static int tm=DECODER_ADD(ICON2D);
00014
00015 static bool debug = Config::get_var_bool("DEBUG_ICON2D",false);
00016
00017 DLhandler ICON2D::_dl;
00018
00019 void
00020 ICON2D::recompute_xform()
00021 {
00022
00023 if (!_is2d)
00024 _pt2d = xform().origin();
00025 else _pt2d = XYpt(xform().origin()[0], xform().origin()[1]);
00026 }
00027
00028 ICON2D::ICON2D(Cstr_ptr &n, Cstr_ptr &filename, int num, bool tog, const mlib::PIXEL &p) :
00029 GEOM(n),
00030 _is2d(0),
00031 _center(0),
00032 _can_intersect(1),
00033 _show_boxes(0),
00034 _filename(Config::JOT_ROOT() + filename),
00035 _cam(num),
00036 _suppress_draw(false),
00037 _active(false),
00038 _toggle(false),
00039 _hide(false)
00040 {
00041
00042 if (debug)
00043 cerr << "ICON2D::ICON2D: loading file: " << _filename << endl;
00044
00045
00046 _texture.push(new TEXTUREgl(_filename + ".png"));
00047 assert(_texture[0]);
00048 _texture[0]->set_tex_unit(GL_TEXTURE0);
00049 _texture[0]->set_mipmap(false);
00050 _texture[0]->set_tex_fn(GL_REPLACE);
00051
00052 if (_texture[0]->load_image()) {
00053 _suppress_draw = false;
00054 } else {
00055 cerr << "ICON2D error : texture not loaded" << endl;
00056 _suppress_draw = true;
00057 }
00058
00059
00060 if(tog)
00061 {
00062 _act_tex = new TEXTUREgl(_filename + "_active.png");
00063 assert(_act_tex);
00064 _act_tex->set_mipmap(false);
00065 _act_tex->set_tex_fn(GL_REPLACE);
00066
00067 if (_act_tex->load_image()) {
00068 _suppress_draw = false;
00069 } else {
00070 cerr << "ICON2D error : active texture not loaded" << endl;
00071 _suppress_draw = true;
00072 }
00073 }
00074
00075 _name = n;
00076 _toggle = tog;
00077 _pix = p;
00078 _currentTex = 0;
00079 _skins.push(_filename);
00080 }
00081
00082
00083
00084
00085
00086
00087
00088 int
00089 ICON2D::draw(
00090 CVIEWptr &view
00091 )
00092 {
00093
00094
00095 if (_suppress_draw || view->grabbing_screen() || _hide)
00096 return 0;
00097
00098 assert(_texture[_currentTex]);
00099
00100
00101 glMatrixMode(GL_MODELVIEW);
00102 glPushMatrix();
00103 glLoadIdentity();
00104
00105
00106 glMatrixMode(GL_PROJECTION);
00107 glPushMatrix();
00108 glLoadMatrixd(view->pix_proj().transpose().matrix());
00109
00110
00111 glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT);
00112 glDisable(GL_LIGHTING);
00113
00114
00115 if(_toggle && _active)
00116 _act_tex->apply_texture();
00117 else
00118 _texture[_currentTex]->apply_texture();
00119
00120
00121 glBegin(GL_QUADS);
00122 glTexCoord2f(0.0, 0.0); glVertex2d(_pix[0],_pix[1]);
00123 glTexCoord2f(1.0, 0.0); glVertex2d(_pix[0] + 32.0,_pix[1]);
00124 glTexCoord2f(1.0, 1.0); glVertex2d(_pix[0] + 32.0,_pix[1] + 32.0);
00125 glTexCoord2f(0.0, 1.0); glVertex2d(_pix[0],_pix[1] + 32.0);
00126 glEnd();
00127
00128
00129 glPopAttrib();
00130
00131 glMatrixMode(GL_PROJECTION);
00132 glPopMatrix();
00133
00134 glMatrixMode(GL_MODELVIEW);
00135 glPopMatrix();
00136
00137 return 0;
00138 }
00139
00140 RAYhit &
00141 ICON2D::intersect(
00142 RAYhit &r,
00143 CWtransf &m,
00144 int
00145 ) const
00146 {
00147
00148
00149
00150 if (_can_intersect && !_suppress_draw) {
00151
00152
00153 BBOX2D bbox(bbox2d(0,0));
00154
00155
00156
00157 PIXEL pick_pt(r.point() + r.vec());
00158 if (bbox.contains(pick_pt)) {
00159 Wvec ray(r.point() - xform().origin());
00160
00161
00162 r.check(0, 0, 0, (ICON2D *)this, ray.normalized(),
00163 r.point() + r.vec()*0.1, Wpt::Origin(),
00164 (APPEAR *) this, PIXEL());
00165
00166 return r;
00167 }
00168 }
00169
00170
00171 GEOM::intersect(r,m);
00172 return r;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 BBOX2D
00182 ICON2D::bbox2d(
00183 int border,
00184 char *s,
00185 int force
00186 ) const
00187 {
00188 PIXEL start, endpt;
00189
00190 start = _pix;
00191 endpt = PIXEL(_pix[0] + 32.0,_pix[1] + 32.0);
00192
00193 return BBOX2D(start, endpt);
00194 }
00195
00196
00197 void
00198 ICON2D::add_skin(Cstr_ptr &n)
00199 {
00200 _texture.push(new TEXTUREgl(_filename + ".png"));
00201 assert(_texture[0]);
00202 _texture[0]->set_tex_unit(GL_TEXTURE0);
00203 _texture[0]->set_mipmap(false);
00204 _texture[0]->set_tex_fn(GL_REPLACE);
00205
00206 if (_texture[0]->load_image()) {
00207 _suppress_draw = false;
00208 } else {
00209 cerr << "ICON2D error : texture not loaded" << endl;
00210 _suppress_draw = true;
00211 }
00212 _currentTex++;
00213
00214 }
00215
00216 void
00217 ICON2D::update_skin()
00218 {
00219 _currentTex++;
00220 if(_currentTex >= _texture.num())
00221 _currentTex = 0;
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 }
00237