fshake3d  0.0.1
FreeformDensity3DSurfaceEditor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
scene_Slider.cpp
Go to the documentation of this file.
00001 #include "scene_Slider.hpp"
00002 #include "opengl.hpp"
00003 #include <iostream>
00004 
00005 Slider::Slider()
00006 : mEnabled(true)
00007 , mDrag(false)
00008 , mAxis(AXIS_X)
00009 , mUpAxis(AXIS_Z)
00010 , mAbsMode(ABSMODE_POSITIVE)
00011 , mMin(0)
00012 , mMax(1.0)
00013 , mColor(1.0,1.0,1.0,0.8)
00014 , mBGColor(0.0,0.0,0.0,0.5)
00015 , mKnobType(KNOB_STRETCH)
00016 {
00017   setBorder(0.02);
00018   setPosition( Point3d(0,0,0) );
00019 }
00020 
00021 bool Slider::hit(const Rayd& ray, double& t)
00022 {
00023   mDrag = false;
00024   if (mEnabled)
00025   {
00026     unsigned int numHits;
00027     double t0,t1;
00028 
00029     if ( intersect( mAABoxControl, ray, numHits, t0, t1) )
00030     {
00031       if (numHits == 2)
00032       {
00033         t = (t0 < t1) ? t0 : t1;
00034         
00035         // t = (t0+t1)*0.5;
00036         return true;
00037       }
00038     }
00039   }
00040   return false;
00041 }
00042 
00043 bool Slider::select(const Rayd& ray, SceneView& view)
00044 {
00045   mDrag = false;
00046   if (mEnabled)
00047   {
00048     unsigned int numHits;
00049     double t0,t1;
00050     if ( intersect( mAABoxControl, ray, numHits, t0, t1) )
00051     {
00052       if (numHits == 2)
00053       {
00054         mDrag = true;
00055 
00056         Point3d hitp = ray.getOrigin() + (t0+t1)*0.5*ray.getDir();
00057         double value = hitp[mAxis] - mPosition[mAxis];
00058 
00059         setValue(value);
00060 
00061         // initialize drag plane
00062 
00063         
00064         Point3d p0   = mPosition;
00065         p0[mUpAxis]  = 0;
00066         Point3d p1   = p0;
00067         p1[mAxis]   += 1;
00068         Point3d p2   = p1;
00069         p2[mUpAxis]  = 1;
00070         
00071         /*
00072 
00073         Vec3d d = ray.getDir();
00074         Vec3d up;
00075         up[mUpAxis] = 1;
00076 
00077         Vec3d side = makeCross(d,up);
00078 
00079         Point3d p0 = mPosition;
00080         p0[mUpAxis] = 0;
00081         Point3d p1 = p0 + up;
00082         Point3d p2 = p0 + side;
00083         */
00084 
00085         mDragPlane = Planed(p0,p1,p2);
00086         return true;
00087       }
00088       else
00089       {
00090         std::cout << "numHits " << numHits << "\n";
00091       }
00092     }
00093   }
00094   return false;
00095 }
00096 
00097 bool Slider::drag(const Rayd& ray, SceneView& view)
00098 {
00099   if (mEnabled)
00100   {
00101     if (mDrag)
00102     {
00103       double t;
00104       if ( intersect(mDragPlane, ray, t) )
00105       {
00106         Point3d hitp = ray.getOrigin() + t*ray.getDir();
00107         double value = hitp[mAxis] - mPosition[mAxis];
00108         setValue(value);
00109       }
00110       return true;
00111     }
00112   }
00113   return false;
00114 }
00115 
00116 void Slider::setValue(double v)
00117 {
00118   if (mAbsMode == ABSMODE_MIRROR)
00119   {
00120     v = abs(v);
00121   }
00122   mValue = clamp(v, mMin,mMax);
00123 
00124   if (mSlotValueChange)
00125     mSlotValueChange(mValue);
00126 }
00127 
00128 void Slider::updateSensorVolume()
00129 {
00130   Point3d pMin = mPosition;
00131   pMin[(mAxis+1)%3] -= mBorder;
00132   pMin[(mAxis+2)%3] -= mBorder;
00133   Point3d pMax = mPosition;
00134   pMax[(mAxis+1)%3] += mBorder;
00135   pMax[(mAxis+2)%3] += mBorder;
00136 
00137   pMax[mAxis] += mMax;  
00138   
00139   if (mAbsMode == ABSMODE_MIRROR)
00140   {
00141     pMin[mAxis] -= mMax;
00142   }
00143   else
00144   {
00145     pMin[mAxis] =  mMin;
00146   }
00147 
00148   mAABoxControl.setMin( pMin );
00149   mAABoxControl.setMax( pMax );
00150 }
00151 
00152 void Slider::setBorder(double border)
00153 {
00154   mBorder = border;
00155   updateSensorVolume();
00156 }
00157 
00158 void Slider::setPosition(const Point3d& p)
00159 {
00160   mPosition = p;
00161   updateSensorVolume();
00162 }
00163 
00164 void Slider::display()
00165 {
00166   if (mEnabled)
00167   {
00168     // display layer
00169 
00170     glPushAttrib(GL_ENABLE_BIT);
00171     glDisable(GL_DEPTH_TEST);
00172     glDisable(GL_ALPHA_TEST);
00173     glEnable(GL_BLEND);
00174     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00175 
00176 
00177     // draw sensor volume
00178 
00179     gl_set_color(mBGColor);
00180 
00181     glMatrixMode(GL_MODELVIEW);
00182     glPushMatrix();
00183     glTranslated(mPosition[0],mPosition[1],mPosition[2]);
00184 
00185     Point3d s(mBorder,mBorder,mBorder);
00186     // s[mAxis] = (mMax-mMin)*2;
00187     s[mAxis] = (mMax-mMin);
00188 
00189     glPushMatrix();
00190     glScaled(s[0],s[1],s[2]);
00191     glutSolidCube(1);
00192     glPopMatrix();
00193 
00194     // draw knob
00195 
00196     // glEnable(GL_LIGHTING);
00197     // glEnable(GL_DEPTH_TEST);
00198 
00199     GLfloat pointDiffuse[4] = { mColor[0], mColor[1], mColor[2], mColor[3] };
00200     // GLfloat pointAmbient[4] = { 0.2, 0.2, 0.2, 1.0 };
00201     GLfloat pointAmbient[4] = { 0.0, 0.0, 0.0, 1.0 };
00202     glMaterialfv(GL_FRONT, GL_DIFFUSE, pointDiffuse);
00203     glMaterialfv(GL_FRONT, GL_AMBIENT, pointAmbient);
00204 
00205 
00206     gl_set_color(mColor);
00207 
00208     Point3d t;
00209     t[mAxis] = mValue;
00210 
00211     switch(mKnobType)
00212     {
00213     case KNOB_TRANSLATE:
00214       {
00215         Point3d t;
00216         t[mAxis] = mValue;
00217         glTranslated(t[0],t[1],t[2]);
00218         glutSolidSphere(mBorder, 32, 16);
00219       }
00220       break;
00221     case KNOB_STRETCH:
00222       {
00223         switch(mAbsMode)
00224         {
00225         case ABSMODE_POSITIVE:
00226           {
00227             Point3d s(mBorder,mBorder,mBorder);
00228             s[mAxis] = mValue*0.5;
00229             Point3d t;
00230             t[mAxis] = mValue*0.5;
00231             glScaled(s[0],s[1],s[2]);
00232             glTranslated(t[0],t[1],t[2]);
00233           }
00234           break;
00235         case ABSMODE_MIRROR:
00236           {
00237             Point3d s(mBorder,mBorder,mBorder);
00238             s[mAxis] = mValue*2;
00239             glScaled(s[0],s[1],s[2]);
00240           }
00241           break;
00242         }
00243         glutSolidCube(1.0);
00244     
00245       }
00246       break;
00247     }
00248 
00249     glPopMatrix();
00250   
00251     glPopAttrib();
00252   }
00253 }