Biomechanical Joint Model
 Author: Anderson Maciel

comeqglwidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Anderson Maciel                                 *
00003  *   andi.maciel@gmail.com                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <comeqglwidget.h>
00021 #include <general/comescenario.h>
00022 #include <algebra/comemesh.h>
00023 #include <algebra/comeface.h>
00024 #include <bio/comebiostructure.h>
00025 #include <bio/comemoleculescartilage.h>
00026 //#include <comediscretizer.h>
00027 #include <qcheckbox.h>
00028 #include <qtimer.h>
00029 #include <qcursor.h>
00030 #include <qslider.h>
00031 #include <qpopupmenu.h>
00032 #include <qspinbox.h>
00033 #include <qstatusbar.h>
00034 #include <GL/glut.h>
00035 
00036 
00037 
00038 #ifdef GPU_FORCES
00039         
00040         #include <Cg/cg.h>
00041         #include <Cg/cgGL.h>
00042 
00043         // Global variables: hold the Cg context that we're storing our programs
00044         // in as well as handles to the vertex and fragment program used in this
00045         // demo.
00046         
00047         CGcontext context;
00048         CGprogram vertexProgram, fragmentProgram;
00049         
00050         // Choose the vertex and fragment profiles to use.  Try to use
00051         // CG_PROFILE_ARBVFP1 and CG_PROFILE_ARBFP1, depending on hardware support.
00052         // If those aren't available, fall back to CG_PROFILE_VP30 and
00053         // CG_PROFILE_FP30, respectively.
00054         
00055         CGprofile vertexProfile, fragmentProfile;
00056                 
00057 #endif
00058 
00059 
00060 COME_QGLWidget::COME_QGLWidget( QWidget *parent, const char *name ):QGLViewer(parent, name){
00061         
00062         Timer = new QTimer( this, "Timer" );
00063         scene = NULL;
00064         wireframe_ = false;
00065         flatShading_ = false;
00066         connect( Timer, SIGNAL( timeout() ), SLOT( updateGL() ) );
00067 }
00068    
00069 void
00070 COME_QGLWidget::startTimer( int msec ){
00071     
00072     Timer->start( msec, false );
00073 }
00074 
00075 void
00076 COME_QGLWidget::setScene( COME_Scenario *sc ){
00077     
00078     scene = sc;
00079 }
00080 
00081 void
00082 COME_QGLWidget::setGlobal( COME *global1 ){
00083     
00084         global = global1;
00085 }
00086 
00087 COME*
00088 COME_QGLWidget::getGlobal(){
00089 
00090         return global;
00091 }
00092 
00093 void
00094 COME_QGLWidget::draw(){
00095 
00096         //CGparameter 
00097         //cgGLSetParameter3fv(cgGetNamedParameter(fragmentProgram, "lightColor"), lightColor);
00098         
00099         
00100         if( scene ){
00101                 glPushMatrix();
00102                         glTranslatef( 10, 0, 0  );
00103                         glutSolidSphere(1, 15, 15);
00104                 glPopMatrix();
00105                 glPushMatrix();
00106                         glTranslatef( 20, 0, 0  );
00107                         glutSolidSphere(1, 15, 15);
00108                 glPopMatrix();
00109 
00110         }
00111 }
00112         
00113 
00114 void
00115 COME_QGLWidget::init(){
00116 
00117         // Restore previous viewer state.
00118         restoreStateFromFile();
00119         
00120         setSceneRadius(1.0);  // for instance 
00121         //setSceneCenter(0,0,0); // for instance
00122         camera()->showEntireScene();
00123 
00124         // Set 'Control' as the FPS toggle state key (default was 'F' and becomes 'CTRL+F')
00125         //setShortcutStateKey(QGLViewer::DISPLAY_FPS, Qt::ControlButton);
00126         
00127         #ifdef GPU_FORCES
00128                 // Basic Cg setup; create an initial context
00129                 context = cgCreateContext();
00130                 
00131                 // setup profile
00132                 if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1))
00133                 fragmentProfile = CG_PROFILE_ARBFP1;
00134                 else {
00135                         // try FP30
00136                         if (cgGLIsProfileSupported(CG_PROFILE_FP30))
00137                         fragmentProfile = CG_PROFILE_FP30;
00138                         else {
00139                         fprintf(stderr, "Neither arbfp1 or fp30 fragment profiles supported on this system.\n");
00140                         exit(1);
00141                         }
00142                 }
00143                 // Load CG program
00144                 LoadCgPrograms();
00145         #endif
00146         
00147         // Display the help window. The help window tabs are automatically updated when you define new
00148         // standard key or mouse bindings (as is done above). Use shortcutBindingsString() and
00149         // mouseBindingsString() to add the description of your own new actions.
00150         //help();
00151 }
00152 
00154 //      Define new key bindings : F & W      //
00156 
00157 void
00158 COME_QGLWidget::keyPressEvent(QKeyEvent *e){
00159 
00160   // Get event state key
00161   const Qt::ButtonState state = (Qt::ButtonState)(e->state() & Qt::KeyButtonMask);
00162   // Bug in Qt : use 0x0f00 instead of Qt::KeyButtonMask with Qt versions < 3.1
00163 
00164   // A simple switch on e->key() is not sufficient if we want to take state key into account.
00165   // With a switch, it would have been impossible to separate 'F' from 'CTRL+F'.
00166   // That's why we use imbricated if...else and a "handled" boolean.
00167   bool handled = false;
00168   if ((e->key()==Qt::Key_W) && (state==Qt::NoButton))
00169     {
00170       wireframe_ = !wireframe_;
00171       if (wireframe_){
00172         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00173       } else {
00174         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00175         }
00176       handled = true;
00177       updateGL();
00178     }
00179   else
00180     if ((e->key()==Qt::Key_Q) && (state==Qt::NoButton))
00181       {
00182         flatShading_ = !flatShading_;
00183         if (flatShading_)
00184           glShadeModel(GL_FLAT);
00185         else
00186           glShadeModel(GL_SMOOTH);
00187         handled = true;
00188         updateGL();
00189       }
00190   // and so on with other else if.
00191 
00192   if (!handled){
00193     QGLViewer::keyPressEvent(e);
00194    }
00195 }
00196 
00197 
00198 QString
00199 COME_QGLWidget::helpString() const
00200 {
00201   QString text("<h2>VRlab - COME 3D Viewer</h2>");
00202   text += "Copyright (C) 2004 by Anderson Maciel<br>";
00203   text += "andi.maciel@gmail.com <br><br>";
00204   text += "This program is free software; you can redistribute it and/or modify ";
00205   text += "it under the terms of the GNU General Public License as published by ";
00206   text += "the Free Software Foundation; either version 2 of the License, or ";
00207   text += "(at your option) any later version. <br><br>";
00208   text += "This program is distributed in the hope that it will be useful, ";
00209   text += "but WITHOUT ANY WARRANTY; without even the implied warranty of ";
00210   text += "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the ";
00211   text += "GNU General Public License for more details. <br><br>";
00212   text += "You should have received a copy of the GNU General Public License ";
00213   text += "along with this program; if not, write to the ";
00214   text += "Free Software Foundation, Inc., ";
00215   text += "59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.";
00216 
00217   text += "<h3>Basic instructions:</h3> ";
00218   text += "Use the mouse to move the camera around the object. ";
00219   text += "You can revolve around, zoom and translate with the three buttons. ";
00220   text += "Left and middle buttons pressed together rotate around the camera z axis<br><br>";
00221   text += "Pressing <b>Alt</b> and one of the function key (<b>F1</b>..<b>F12</b>) defines a camera keyFrame. ";
00222   text += "Simply press the function key again to restore it. Several keyFrames define a ";
00223   text += "camera path. Paths are saved when you quit the application.<br><br>";
00224   text += "Press <b>W</b> to switch between wireframe and filled polygons, <b>Q</b> to switch between flat and smooth shading, <b>A</b> for the world axis, ";
00225   text += "<b>Alt+Return</b> for full screen mode and <b>Control+S</b> to save a snapshot.<br><br>";
00226   text += "A double click aligns the closer axis with the camera (left, if close enough), fits ";
00227   text += "the zoom of the camera (middle) or re-center the scene (right).<br>";
00228   text += "A left double click while right button is pressed defines the <i>revolveAroundPoint</i>.";
00229   text += "With middle button pressed instead, it zooms on the pixel.<br><br>";
00230   text += "See also the <b>Shortcuts</b> and <b>Mouse</b> tabs and the documentation web pages.<br>";
00231   text += "Press <b>Escape</b> to exit the viewer.";
00232   return text;
00233 }
00234 
00235 void
00236 COME_QGLWidget::drawCylinder(){
00237 
00238         GLUquadricObj *qobj;
00239         qobj = gluNewQuadric();
00240         glPushMatrix();
00241                 glRotatef( -90, 1, 0, 0 ); 
00242                 gluCylinder( qobj, 1, 1, 1, 8, 4 );
00243         glPopMatrix();
00244         gluDeleteQuadric(qobj);
00245 }
00246 
00247 static void
00248 LoadCgPrograms() {
00249 
00250         #ifdef GPU_FORCES    
00251                 //assert(cgIsContext(context));
00252         
00253                 // And similarly set things up for the fragment program.
00254                 fragmentProgram = cgCreateProgramFromFile(context, CG_SOURCE, CWD "calculate_forces.cg",
00255                                                 fragmentProfile, NULL, NULL);
00256                 if (!cgIsProgramCompiled(fragmentProgram))
00257                         cgCompileProgram(fragmentProgram);
00258         
00259                 cgGLEnableProfile(fragmentProfile);
00260                 cgGLLoadProgram(fragmentProgram);
00261         #endif
00262 }

Generated on Thu Dec 1 10:13:35 2005 for COME - Biomechanical Joint Model by  doxygen 1.4.5