GLUTApplication.cpp

00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
00002 /* 
00003  * Copyright (C) 2008 Micha Hersch, EPFL
00004  * RobotCub Consortium, European Commission FP6 Project IST-004370
00005  * email:   micha.hersch@robotcub.org
00006  * website: www.robotcub.org
00007  * Permission is granted to copy, distribute, and/or modify this program
00008  * under the terms of the GNU General Public License, version 2 or any
00009  * later version published by the Free Software Foundation.
00010  *
00011  * A copy of the license can be found at
00012  * http://www.robotcub.org/icub/license/gpl.txt
00013  *
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00017  * Public License for more details
00018  */
00019 #include "GLUTApplication.h"
00020 
00021 
00022 GLUTBaseApplication::GLUTBaseApplication(){}
00023 
00024 GLUTBaseApplication::GLUTBaseApplication(int argc, char **argv){
00025   glutInit(&argc, argv);
00026         glutInitWindowPosition(0,0);
00027         glutInitWindowSize(600,500);
00028         glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
00029         m_WindowID = glutCreateWindow("Simulator");
00030 
00031   SetCurrent();
00032 
00033   glutDisplayFunc(GLUTBaseApplication::GLUTApp_Render);
00034         glutReshapeFunc(GLUTBaseApplication::GLUTApp_Resize);
00035         glutIdleFunc(GLUTBaseApplication::GLUTApp_OnIdle);
00036         glutIgnoreKeyRepeat(1);
00037         glutKeyboardFunc(GLUTBaseApplication::GLUTApp_InputNormalKey);
00038         glutSpecialFunc(GLUTBaseApplication::GLUTApp_InputSpecialKey);
00039         glutMouseFunc(GLUTBaseApplication::GLUTApp_InputMouseButton);
00040         glutMotionFunc(GLUTBaseApplication::GLUTApp_InputMouseMotion);
00041         //      glutFullScreen();
00042 
00043   m_Snap                = false;
00044   m_CurrentCamera       = 0;
00045   m_CurrentMouseButton  = -1;
00046   m_CurrentMouseX       = 0;
00047   m_CurrentMouseY       = 0;
00048 
00049   m_Camera.push_back(new GLCamera());
00050   
00051 
00052 
00053   m_LastPressedButton = -1;
00054 }
00055 
00056 //GLUTBaseApplication::GLUTBaseApplication(int argc, char **argv, pTree config){}
00057 GLUTBaseApplication::~GLUTBaseApplication(){
00058   BaseFree();
00059 }
00060 
00061 void  GLUTBaseApplication::BaseFree(){
00062   unsigned int i;
00063   
00064   // Freeing Cameras
00065   for(i=0;i<m_Camera.size();i++)
00066     delete m_Camera[i];
00067   m_Camera.clear();
00068 
00069   for(i=0;i<m_SubWindows.size();i++){
00070     delete m_SubWindows[i];
00071   }
00072   m_SubWindows.clear();
00073 
00074   Free();
00075 }
00076 
00077 void  GLUTBaseApplication::BaseRender(){
00078         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00079 
00080   if(m_Snap) m_Snapshot.Begin();
00081   
00082 
00083   m_Camera[m_CurrentCamera]->Apply();
00084   Render();
00085 
00086   //m_Console.Render();
00087 
00088   int i;
00089   for(i=0;i<m_SubWindows.size();i++)
00090     m_SubWindows[i]->RenderWindow();
00091 
00092   if(m_Snap) {
00093     m_Snapshot.Finish();
00094     m_Snap = false;
00095   }
00096 
00097   glFlush();
00098         glutSwapBuffers();
00099 }
00100 
00101 void  GLUTBaseApplication::BaseResize(int width, int height){
00102   int i;
00103   
00104   // Updating Camera(s)
00105   for(i=0;i<m_Camera.size();i++)
00106     m_Camera[i]->Init(width,height);
00107   m_Camera[m_CurrentCamera]->Apply();
00108 
00109   // Updating Snapshot
00110   m_Snapshot.SetViewport(width,height);
00111 
00112   // Updating Console
00113   //m_Console.Resize(width,height);
00114 
00115   Resize(width,height);
00116 }
00117 
00118 void  GLUTBaseApplication::BaseInputNormalKey(unsigned char key, int x, int y){
00119   int i;
00120   for(i=0;i<m_SubWindows.size();i++)
00121     m_SubWindows[i]->OnWindowNormalKey(key);
00122 
00123 
00124   InputNormalKey(key,x,y);
00125 }
00126 
00127 void  GLUTBaseApplication::BaseInputSpecialKey(int key, int x, int y){
00128   int i;
00129   for(i=0;i<m_SubWindows.size();i++)
00130     m_SubWindows[i]->OnWindowSpecialKey(key);
00131   
00132   InputSpecialKey(key,x,y);
00133 }
00134 void  GLUTBaseApplication::BaseInputMouseButton(int button, int state, int x, int y){
00135   int i;  
00136   if(state == GLUT_DOWN){
00137     if(m_DblClickTimer.IsDone())
00138       m_LastPressedButton = -1;
00139     if(m_LastPressedButton!=button){
00140       m_LastPressedButton = button;
00141       m_DblClickTimer.Start(500);
00142     }else{
00143       if(!m_DblClickTimer.IsDone()){
00144         for(i=0;i<m_SubWindows.size();i++)
00145           m_SubWindows[i]->OnWindowDoubleClick(button,x,y);
00146 
00147         m_LastPressedButton = -1;
00148       }
00149     }
00150   }
00151 
00152   int cFlag = 0;
00153   for(i=0;i<m_SubWindows.size();i++)
00154     cFlag |= m_SubWindows[i]->OnWindowClick(button,state,x,y);  
00155 
00156   if(cFlag)
00157     return;
00158   
00159   // Camera motion
00160   if((state ==  GLUT_UP) && (m_CurrentMouseButton == button)){
00161     m_CurrentMouseButton = -1;
00162     m_Camera[m_CurrentCamera]->Accept();
00163   }
00164   else if((state == GLUT_DOWN)&&(m_CurrentMouseButton == -1)){
00165     m_Camera[m_CurrentCamera]->Hold();
00166     m_CurrentMouseButton = button;
00167     m_CurrentMouseX      = x;
00168     m_CurrentMouseY      = y;
00169   }
00170 
00171   InputMouseButton(button,state,x,y);
00172 }
00173 
00174 void  GLUTBaseApplication::BaseInputMouseMotion(int x, int y){
00175   int i;
00176   for(i=0;i<m_SubWindows.size();i++)
00177     m_SubWindows[i]->OnWindowMove(x,y);
00178 
00179 
00180   if(m_CurrentMouseButton == GLUT_RIGHT_BUTTON)
00181     m_Camera[m_CurrentCamera]->Move(
00182               ((float)(x-m_CurrentMouseX)),
00183              -((float)(y-m_CurrentMouseY)),
00184               0.0,
00185               0.0,
00186               0.0);
00187   if(m_CurrentMouseButton == GLUT_LEFT_BUTTON){
00188     m_Camera[m_CurrentCamera]->Move(
00189               0.0,
00190               0.0,
00191               ((float)(y-m_CurrentMouseY)),
00192              -((float)(x-m_CurrentMouseX))/2.0,
00193               0.0);
00194   }
00195   if(m_CurrentMouseButton == GLUT_MIDDLE_BUTTON){
00196     m_Camera[m_CurrentCamera]->Move(
00197               0.0,
00198               0.0,
00199               0.0,
00200               -((float)(x-m_CurrentMouseX))/2.0,
00201              -((float)(y-m_CurrentMouseY))/2.0);
00202   }
00203 
00204   InputMouseMotion(x,y);
00205 }
00206 void  GLUTBaseApplication::NextCamera(){
00207   m_CurrentCamera++;
00208   if(m_CurrentCamera >= m_Camera.size())
00209     m_CurrentCamera = 0;
00210   m_Camera[m_CurrentCamera]->Apply();
00211 }
00212 void  GLUTBaseApplication::SetCamera(int num){
00213   if((num>=0) && (num < m_Camera.size())){
00214     m_CurrentCamera = num;
00215     m_Camera[m_CurrentCamera]->Apply();
00216   }
00217 }
00218 GLCamera *GLUTBaseApplication::GetCamera(){
00219   return  (m_Camera[m_CurrentCamera]);
00220 }
00221 
00222 void  GLUTBaseApplication::Run(){
00223         glutMainLoop();
00224 }
00225 void  GLUTBaseApplication::SetCurrent(){
00226   m_GLUTApp = this;
00227 }
00228 void  GLUTBaseApplication::Exit(){
00229         BaseFree();
00230   exit(0);
00231 }
00232 
00233 
00234 /**************************************************
00235  *
00236  * method StartSnapshot
00237  *
00238  *
00239  ****************************************************/
00240 
00241 void GLUTBaseApplication::StartSnapshot(const char *filename, GLSnapshotMode mode=GLSnap_RGB){
00242   GLCamera *ccam = GetCamera();
00243   m_Snapshot.SetViewport(ccam->m_Width,ccam->m_Height);
00244   //m_Snapshot.SetViewport(200,100);
00245   m_Snapshot.SetMode(mode);
00246   m_Snapshot.SetFilename(filename);
00247   if(!m_Snapshot.Begin()){
00248     cout << "cannot save image"<<endl;
00249   }
00250   m_Snap=1;
00251 }
00252 
00253 
00254 // Virtual Callback functions
00255 void  GLUTBaseApplication::Init(){}
00256 void  GLUTBaseApplication::Render(){}
00257 void  GLUTBaseApplication::Free(){}
00258 void  GLUTBaseApplication::OnIdle(){}
00259 void  GLUTBaseApplication::Resize(int width, int height){}
00260 void  GLUTBaseApplication::InputMouseButton(int button, int state, int x, int y){}
00261 void  GLUTBaseApplication::InputMouseMotion(int x, int y){}
00262 void  GLUTBaseApplication::InputNormalKey(unsigned char key, int x, int y){}
00263 void  GLUTBaseApplication::InputSpecialKey(int key, int x, int y){}
00264 
00265 
00266 // Static Callback functions
00267 void  GLUTBaseApplication::GLUTApp_Render(){
00268   if(m_GLUTApp!=NULL)
00269     m_GLUTApp->BaseRender();
00270 }
00271 void  GLUTBaseApplication::GLUTApp_OnIdle(){
00272   if(m_GLUTApp!=NULL)
00273     m_GLUTApp->OnIdle();
00274 }
00275 void  GLUTBaseApplication::GLUTApp_Resize(int width, int height){
00276   if(m_GLUTApp!=NULL)
00277     m_GLUTApp->BaseResize(width,height);
00278 }
00279 void  GLUTBaseApplication::GLUTApp_InputMouseButton(int button, int state, int x, int y){
00280   if(m_GLUTApp!=NULL)
00281     m_GLUTApp->BaseInputMouseButton(button,state,x,y);
00282 }
00283 void  GLUTBaseApplication::GLUTApp_InputMouseMotion(int x, int y){
00284   if(m_GLUTApp!=NULL)
00285     m_GLUTApp->BaseInputMouseMotion(x,y);
00286 }
00287 void  GLUTBaseApplication::GLUTApp_InputNormalKey(unsigned char key, int x, int y){
00288   if(m_GLUTApp!=NULL)
00289     m_GLUTApp->BaseInputNormalKey(key,x,y);
00290 }
00291 void  GLUTBaseApplication::GLUTApp_InputSpecialKey(int key, int x, int y){
00292   if(m_GLUTApp!=NULL)
00293     m_GLUTApp->BaseInputSpecialKey(key,x,y);
00294 }
00295 pGLUTBaseApplication    GLUTBaseApplication::m_GLUTApp = NULL;
00296 
00297 
00298 
00299 /*--------------------------------------------------------------*/
00300 /*--------------------------------------------------------------*/
00301 /*--------------------------------------------------------------*/
00302 /*--------------------------------------------------------------*/
00303 /*--------------------------------------------------------------*/
00304 /*--------------------------------------------------------------*/
00305 
00306 /*
00307 
00308 GLUTApplication::GLUTApplication(int argc, char **argv)
00309 :GLUTBaseApplication(argc, argv){}
00310 
00311 //GLUTApplication::GLUTApplication(int argc, char **argv, pTree config)
00312 //:GLUTBaseApplication(argc, argv, config){}
00313 
00314 GLUTApplication::~GLUTApplication(){}
00315 
00316 
00317 void  GLUTApplication::Init(){
00318   glEnable(GL_DEPTH_TEST);
00319   GLfloat LightAmbient[]= { 0.0f, 0.0f, 0.0f, 0.0f };   
00320   GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };   
00321   GLfloat LightPosition[]= { 1.0f, 0.1f, 1.0f, 0.0f };  
00322   glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
00323   glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);       
00324   glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
00325   glEnable(GL_LIGHT1);
00326 
00327   glEnable(GL_COLOR_MATERIAL);
00328   glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
00329   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
00330 
00331 //  glCullFace(GL_BACK);
00332   //glEnable(GL_CULL_FACE);
00333   //glEnable(GL_LIGHTING);
00334 
00335   
00336   glEnable(GL_BLEND);
00337   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00338 
00339   glEnable(GL_LINE_SMOOTH);
00340   glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
00341   glShadeModel (GL_SMOOTH);
00342 
00343   glClearColor(0.2,0.4,0.0,1.0);
00344 
00345 }
00346 void  GLUTApplication::Render(){
00347         glPushMatrix();
00348         glScalef(2.0,1.0,2.0);
00349 
00350         // Draw ground
00351         glColor3f(0.2f, 0.7f, 0.0f);
00352         glBegin(GL_QUADS);
00353                 glVertex3f(-1.0f, 0.0f, -1.0f);
00354                 glVertex3f(-1.0f, 0.0f,  1.0f);
00355                 glVertex3f( 1.0f, 0.0f,  1.0f);
00356                 glVertex3f( 1.0f, 0.0f, -1.0f);
00357         glEnd();
00358         glScalef(1.001,1.0,1.001);
00359         glColor3f(0.0f, 0.0f, 0.0f);
00360         glBegin(GL_LINE_STRIP);
00361                 glVertex3f(-1.0f, 0.0f, -1.0f);
00362                 glVertex3f(-1.0f, 0.0f,  1.0f);
00363                 glVertex3f( 1.0f, 0.0f,  1.0f);
00364                 glVertex3f( 1.0f, 0.0f, -1.0f);
00365                 glVertex3f(-1.0f, 0.0f, -1.0f);
00366         glEnd();
00367 
00368         glPopMatrix();
00369 }
00370 void  GLUTApplication::Free(){
00371 }
00372 void  GLUTApplication::OnIdle(){
00373   glutPostRedisplay();
00374 }
00375 void  GLUTApplication::Resize(int width, int height){
00376 }
00377 void  GLUTApplication::InputMouseButton(int button, int state, int x, int y){
00378 
00379 }
00380 void  GLUTApplication::InputMouseMotion(int x, int y){
00381 }
00382 void  GLUTApplication::InputNormalKey(unsigned char key, int x, int y){
00383   if (key == 27) 
00384     Exit();
00385 }
00386 void  GLUTApplication::InputSpecialKey(int key, int x, int y){
00387 }
00388 
00389 */
00390 
00391 
00392 
00393 
00394 /*
00395  ofstream outFile("some_file_name.dat");//create output file object
00396 streambuf * cout_backup=cout.rdbuf();//create backup of standard out
00397 cout.rdbuf(outFile.rdbuf());//assign cout stream to outFile stream
00398 cout<<"hello!"<<endl;//call any function that calls cout
00399 cout.rdbuf(cout_backup);//restore the standard stream
00400 outFile.close();//best to be neat and tidy about these things
00401 */
 All Data Structures Functions Variables

Generated on Wed Sep 22 16:51:26 2010 for Body_Schema_Learning by  doxygen 1.6.1