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

icon2d.C

Go to the documentation of this file.
00001 /*************************************************************************
00002  *    FILE: ICON2d.C
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    // If this ICON is attached to a 3D point, project it to screen space
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    //load "normal" state texture  
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    //load "active" state texture
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    //icon location
00075    _name = n;
00076    _toggle = tog;
00077    _pix = p;
00078    _currentTex = 0;
00079    _skins.push(_filename); //add the first skin to the array
00080 }
00081 
00082 /*************************************************************************
00083  * Function Name: ICON2D::draw
00084  * Parameters: 
00085  * Returns: int
00086  * Effects: 
00087  *************************************************************************/
00088 int
00089 ICON2D::draw(
00090    CVIEWptr &view
00091    )
00092 {
00093    // leave the ID reference image alone,
00094    // and don't draw ICON into screen grabs:
00095    if (_suppress_draw || view->grabbing_screen() || _hide)
00096       return 0;
00097     
00098    assert(_texture[_currentTex]);
00099       
00100    //load identity for model view
00101    glMatrixMode(GL_MODELVIEW);
00102    glPushMatrix();
00103    glLoadIdentity();
00104 
00105    // Setup projection for drawing in PIXEL coords
00106    glMatrixMode(GL_PROJECTION);
00107    glPushMatrix();
00108    glLoadMatrixd(view->pix_proj().transpose().matrix());
00109    
00110    //set up needed attributes
00111    glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT);
00112    glDisable(GL_LIGHTING);      // GL_ENABLE_BIT
00113    
00114    // activate texture
00115    if(_toggle && _active)
00116       _act_tex->apply_texture();   // apply "active" state texture
00117    else
00118       _texture[_currentTex]->apply_texture();   // apply "normal" state texture
00119 
00120    // draw a textured quad in pixel space for the icon:
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    //restore attributes
00129    glPopAttrib();
00130 
00131    glMatrixMode(GL_PROJECTION);
00132    glPopMatrix(); // unloads our projection matrix
00133 
00134    glMatrixMode(GL_MODELVIEW);
00135    glPopMatrix(); // unloads model matrix
00136    
00137    return 0; 
00138 }
00139 
00140 RAYhit &
00141 ICON2D::intersect(
00142    RAYhit   &r,
00143    CWtransf &m,
00144    int       //uv
00145    ) const
00146 {
00147         
00148    // Only pick ICON when it is associated w/ a 3D point and we know
00149    // the window size
00150    if (_can_intersect && !_suppress_draw) {
00151            
00152       //((ICON2D *)this)->recompute_xform(); // Update ICON location
00153       BBOX2D bbox(bbox2d(0,0));            // Get bounding box 
00154 
00155 
00156       //CXYpt pick_pt(r.point() + r.vec());
00157       PIXEL pick_pt(r.point() + r.vec());
00158       if (bbox.contains(pick_pt)) {  // picked!
00159          Wvec ray(r.point() - xform().origin());
00160          // assume ICON is always close to the camera
00161          // for now, that means 0.1 distance along viewing vector
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); // Check if children were intersected
00172    return r;
00173 }
00174 
00175 
00176 /*************************************************************************
00177  * Function Name: icon2d::bbox
00178  * Effects: 
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    //_skins.push(n);
00214    }
00215 
00216    void 
00217    ICON2D::update_skin()
00218    {
00219    _currentTex++;
00220    if(_currentTex >= _texture.num())
00221       _currentTex = 0;
00222 /*
00223    _texture = new TEXTUREgl(_skins[_current_skin] + ".png"); 
00224    assert(_texture);
00225    _texture->set_tex_unit(GL_TEXTURE0);
00226    _texture->set_mipmap(false);
00227    _texture->set_tex_fn(GL_REPLACE);
00228 
00229    if (_texture->load_image()) {
00230       _suppress_draw = false;
00231    } else {
00232       cerr << "ICON2D error : texture not loaded" << endl;
00233       _suppress_draw = true;
00234    }
00235    */
00236    }
00237 

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